#!/bin/bash
# System status script
# Feel free to copypaste, modify, and share; that's what we did.

# Text colors
RED='\e[1;31m'
WHT='\e[1;37m'
GRN='\e[1;32m'
BLU='\e[0;34m'
CLR='\e[0m'

INACTIVE="$RED[INACTIVE]$CLR"
ACTIVE="$GRN[ACTIVE]$CLR"

function printline {
	pad=$(printf '%0.1s' "."{1..60})
	padlength=50
	string1=$1
	string2=$2
	color=$3
	clear='\e[0m'
	printf '┃'
	printf '%s ' "$string1"
	printf '%*.*s' 0 $((padlength - ${#string1} - ${#string2} - 2 )) "$pad"
	printf " $color%s$clear" "$string2"
	printf '┃\n'
}

function printtextline {
	pad=$(printf '%0.1s' " "{1..60})
	padlength=50
	string1=$1
	string2=""
	color=$3
	clear='\e[0m'
	printf '┃'
	printf '%s ' "$string1"
	printf '%*.*s' 0 $((padlength - ${#string1} - ${#string2} - 2 )) "$pad"
	printf " $color%s$clear" "$string2"
	printf '┃\n'
}

function printblank {
	echo -e "┃                                                  ┃"
}

function printdivider {
	echo -e "┠──────────────────────────────────────────────────┨"
}

function printblankheader {
	echo -e "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓"
}

function printfooter {
	echo -e '┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛'
}

echo -e "$BLU"
toilet -f smblock.tlf "Netsyms Business"
toilet -f smblock.tlf "Accelerator"
echo -e "$CLR"
printblankheader
printtextline "Engineered by Netsyms Technologies"
printblank
printtextline "Home: https://netsyms.com"
printtextline "Support: https://support.netsyms.com"
printtextline "Source: https://source.netsyms.com"
printtextline "Business: https://netsyms.biz"
printdivider
printtextline "The programs included with this system are free"
printtextline "software; the exact distribution terms for each"
printtextline "program are described in the individual files in"
printtextline "/usr/share/doc/*/copyright."
printfooter

upSeconds="$(/usr/bin/cut -d. -f1 /proc/uptime)"
secs=$((${upSeconds}%60))
mins=$((${upSeconds}/60%60))
hours=$((${upSeconds}/3600%24))
days=$((${upSeconds}/86400))
UPTIME=`printf "%01d days, %02dh %02dm" "$days" "$hours" "$mins"`

freekb=$(cat /proc/meminfo | grep MemFree | awk {'print $2'})
totalkb=$(cat /proc/meminfo | grep MemTotal | awk {'print $2'})

freemb=$(($freekb/1000))
totalmb=$(($totalkb/1000))

percentfree=$(echo "scale=0; $freekb*100/$totalkb" | bc)

echo -e "┏━━━━━━━━━━━━━━━━━┥ System  Info ┝━━━━━━━━━━━━━━━━━┓"
printline "Date" "$(date +%b\ %e\ %Y)" $WHT
printline "Time" "$(date +%H:%M:%S)" $WHT
printline "Uptime" "$UPTIME" $WHT
printline "Processes" "$(ps ax | wc -l | tr -d " ")" $WHT
printline "Load averages" "$(uptime | awk -F'[a-z]:' '{ print $2}')" $WHT
memcolor=$GRN
if (( $percentfree < 30 )); then
	memcolor=$RED
fi
printline "Memory used" "$(($totalmb-$freemb)) MB ($((100-$percentfree))%)" $memcolor
printline "Memory free" "$freemb MB ($percentfree%)" $memcolor
cpu=$(</sys/class/thermal/thermal_zone0/temp)
cpu=$((cpu/1000))
cpucolor=$GRN
if (( $cpu > 45 )); then
	cpucolor=$RED
fi
printline "CPU temperature" "$cpu°C" $cpucolor

printfooter

#
# Service Status
#

echo -e "┏━━━━━━━━━━━━━━━━━━━┥ Services ┝━━━━━━━━━━━━━━━━━━━┓"

if [ $(systemctl status cjdns.service | grep 'Active: ' | awk '{ print $2 }') = 'active' ]; then
    printline "CJDNS Service" "[RUNNING]" $GRN
else
    printline "CJDNS Service" "[STOPPED]" $RED
fi
if [ $(systemctl status nginx.service | grep 'Active: ' | awk '{ print $2 }') = 'active' ]; then
    printline "NGINX Service" "[RUNNING]" $GRN
else
    printline "NGINX Service" "[STOPPED]" $RED
fi
PHP_VERSION=$(php -v | head -n 1 | cut -c5-7)
if [ $(systemctl status php$PHP_VERSION-fpm.service | grep 'Active: ' | awk '{ print $2 }') = 'active' ]; then
    printline "PHP $PHP_VERSION Service" "[RUNNING]" $GRN
else
    printline "PHP $PHP_VERSION Service" "[STOPPED]" $RED
fi

printfooter

#
# IP/Network Info
#
LOCAL_IP=$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1')
PUBLIC_IP=$(dig +short myip.opendns.com @resolver1.opendns.com)
CJDNS_IP=$(sudo grep -m 1 '"ipv6"' /etc/cjdroute.conf | awk '{ print $2 }' | sed 's/[",]//g')
echo -e "┏━━━━━━━━━━━━━━━━━┥ Network Info ┝━━━━━━━━━━━━━━━━━┓"
printline "Local" "$LOCAL_IP" $WHT
printline "Public" "$PUBLIC_IP" $WHT
printline "CJDNS" "$CJDNS_IP" $WHT
printdivider
printtextline "For network bandwidth usage data, run vnstat"
printfooter
#vnstat -i eth0+tun0+wlan0 --style 0
