Skip to content

Instantly share code, notes, and snippets.

@JohnTheNerd
Last active February 5, 2019 04:14
Show Gist options
  • Save JohnTheNerd/00c2b679d0fcc0a2a6d49a5fa56bbdbd to your computer and use it in GitHub Desktop.
Save JohnTheNerd/00c2b679d0fcc0a2a6d49a5fa56bbdbd to your computer and use it in GitHub Desktop.
run with `2> /dev/null` to hide any errors, and add `username ALL=(ALL) NOPASSWD: /usr/sbin/ufw status` to a sudoers file if you don't have passwordless sudo
#!/bin/bash
hddPath="/mnt/modemhdd"
dockerContainerList=("one" "two" "three")
date=`date`
echo
echo "System information as of: $date"
echo
function [[[ () {
local LANG=C lhs rhs
printf -v lhs '%07.3f' "$1"; lhs=${lhs/./}
printf -v rhs '%07.3f' "$3"; rhs=${rhs/./}
case "$2" in
-lt) return $(( ! ( 10#$lhs < 10#$rhs ) )) ;;
-le) return $(( ! ( 10#$lhs <= 10#$rhs ) )) ;;
-eq) return $(( ! ( 10#$lhs == 10#$rhs ) )) ;;
-ge) return $(( ! ( 10#$lhs >= 10#$rhs ) )) ;;
-gt) return $(( ! ( 10#$lhs > 10#$rhs ) )) ;;
esac
}
red=`tput setaf 1`
green=`tput setaf 2`
yellow=`tput setaf 3`
normal=`tput sgr0`
#cpu=`top -b -n2 | grep "Cpu(s)" | awk '{print $2+$4 "%"}' | tail -n1`
firewall=$(sudo ufw status | head -1)
firewallStatus="${red}OFF!${normal}"
if [ "$firewall" == "Status: active" ] ; then
firewallStatus="${green}On${normal}"
fi
systemHealth="${green}Good!${normal}"
dockerStatus=`docker ps 2> /dev/null`
for item in "${dockerContainerList[@]}"; do
echo "$dockerStatus" | grep "$item" | grep Up > /dev/null
errCode=$?
if [ $errCode -ne 0 ]; then
docker ps > /dev/null 2> /dev/null
errCode=$?
if [ "$errCode" == "1" ]; then
systemHealth="${yellow}Failed!${normal}"
else
systemHealth="${red}BAD!${normal}"
fi
fi
done
printf "Firewall:\t%s\tSystem Health:\t%s\n" $firewallStatus $systemHealth
temp=`cat /sys/class/thermal/thermal_zone0/temp`
let 'temp = temp/1000' # temps show as 1000x of their actual value
if [ $temp -gt 70 ]; then temp="${red}$temp'c${normal}"
elif [ $temp -gt 50 ]; then temp="${yellow}$temp'c${normal}"
else temp="${green}$temp'c${normal}"; fi
ip=`curl -s 'https://api.ipify.org'`
if [ -z $ip ]; then $ip="\e[1;31mNO INTERNET OUT!"
else ip="${green}$ip${normal}"; fi
printf "Temperature:\t%s\tIP Address:\t%s\n" $temp $ip
mem_info=$(LC_ALL=C free -w 2>/dev/null | grep "^Mem" || LC_ALL=C free | grep "^Mem")
memory_usage=$(awk '{printf("%.0f",(($2-($4+$6+$7))/$2) * 100)}' <<<${mem_info})
if [[[ $memory_usage -gt 70 ]]]; then memory_usage="${red}$memory_usage%${normal}"
elif [[[ $memory_usage -gt 50 ]]]; then memory_usage="${yellow}$memory_usage%${normal}"
else memory_usage="${green}$memory_usage%${normal}"; fi
time=`uptime | grep -ohe 'up .*' | sed 's/,/\ hours/g' | awk '{ printf $2" "$3 }'`
timeInt=`uptime | grep -ohe 'up .*' | sed 's/,/\ hours/g' | awk '{ printf $2 }'`
if [[[ $timeInt -gt 100 ]]]; then time="${red}$time${normal}"
elif [[[ $timeInt -gt 60 ]]]; then time="${yellow}$time${normal}"
else time="${green}$time${normal}"; fi
printf "RAM usage:\t%s\tSystem uptime:\t%s\n" $memory_usage "$time"
swap_info=$(LC_ALL=C free -m | grep "^Swap")
swap_usage=$( (awk '/Swap/ { printf("%3.0f", $3/$2*100) }' <<<${swap_info} 2>/dev/null || echo 0) | tr -c -d '[:digit:]')
let result=($divide+$by-1)/$by
if [[[ $swap_usage -gt 50 ]]]; then swap_usage="${red}$swap_usage%${normal}"
elif [[[ $swap_usage -gt 1 ]]]; then swap_usage="${yellow}$swap_usage%${normal}"
else swap_usage="${green}$swap_usage%${normal}"; fi
UptimeString=$(uptime | tr -d ',')
oldload="$(awk -F"average: " '{print $2}'<<<"${UptimeString}")"
load=""
for loadPiece in $oldload; do
if [[[ $loadPiece -gt 2 ]]]; then load="$load${red}$loadPiece ${normal}"
elif [[[ $loadPiece -gt 1 ]]]; then load="$load${yellow}$loadPiece ${normal}"
else load="$load${green}$loadPiece ${normal}"; fi
done
printf "Swap usage:\t%s\tSystem load:\t%s\n" $swap_usage "$load"
root_usage=`df -h / | awk '/\// {print $(NF-1)}' | tr -d "%"`
if [[[ $root_usage -gt 70 ]]]; then root_usage="${red}$root_usage%${normal}"
elif [[[ $root_usage -gt 50 ]]]; then root_usage="${yellow}$root_usage%${normal}"
else root_usage="${green}$root_usage%${normal}"; fi
users=`users | wc -w` # done above
if [[[ $users -gt 2 ]]]; then users="${red}$users${normal}"
elif [[[ $users -gt 1 ]]]; then users="${yellow}$users${normal}"
else users="${green}$users${normal}"; fi
printf "/ Usage:\t%s\tLogged in:\t%s\n" $root_usage $users
home_usage=`df -h "$hddPath" | awk '/\// {print $(NF-1)}' | tr -d "%"`
if [[[ $home_usage -gt 70 ]]]; then home_usage="${red}$home_usage%${normal}"
elif [[[ $home_usage -gt 50 ]]]; then home_usage="${yellow}$home_usage%${normal}"
else home_usage="${green}$home_usage%${normal}"; fi
processes=`ps aux | wc -l`
if [[[ $processes -gt 250 ]]]; then processes="${red}$processes${normal}"
elif [[[ $processes -gt 200 ]]]; then processes="${yellow}$processes${normal}"
else processes="${green}$processes${normal}"; fi
printf "HDD Usage:\t%s\tProcesses:\t%s\n" $home_usage $processes
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment