Skip to content

Instantly share code, notes, and snippets.

@celber
Created June 6, 2015 15:26
Show Gist options
  • Save celber/416a83781c0f6863e9da to your computer and use it in GitHub Desktop.
Save celber/416a83781c0f6863e9da to your computer and use it in GitHub Desktop.
#!/bin/bash
# network
declare hostname
declare net_usage # todo
# hardware
declare ram_usage
declare swap_usage
declare cpu_usage
declare cpu_temp # todo
declare hdd_usage
# directories
declare home_usage
declare var_usage
# security
declare logged_users
declare last_login
# services
declare uptime
declare httpd_up
declare vnc_up
declare znc_up
declare quake_up
declare redis_up
declare kernel=`uname`
declare template=''
template+='Host:%30s\n'
template+='------------Hardware----------\n'
template+='Ram:%10s\tSwap:%10s\n'
template+='Cpu:%10s\tTemp:%10s\n'
template+='-------------System------------\n'
template+='/home:%8s /var:%10s\n'
template+='Hdd usage:%21s \n'
template+='Users logged in:%15s \n'
template+='Last login:%20s \n'
template+='Uptime:%24s \n'
template+='------------Services-----------\n'
template+='WWW:%7s ZNC:%7s VNC:%7s\n'
template+='Quake:%5s Redis:%5s\n'
declare is_linux
if [ "$uname" == "Linux" ]
then
is_linux=1
else
is_linux=0
fi
function get_stats {
hostname=`hostname`
uptime=`uptime | awk -F, '{sub(".*up ",x,$1);print $1}'`
cpu_usage=$(get_cpu_usage)
cpu_temp="N/A"
home_usage=`du -sh /home 2> /dev/null | awk '{print $1}'`
var_usage=`du -sh /var 2> /dev/null | awk '{print $1}'`
hdd_usage=`df / 2> /dev/null | awk 'END{print $5}'`
ram_usage=$(get_ram_usage)
swap_usage=$(get_swap_usage)
logged_users=`who | awk '{ print $1 }' | sort | uniq | wc -l`
last_login=$(get_last_login)
local fail_sign="⨯"
local ok_sign="✓"
[[ `ps aux | grep httpd | wc -l | awk '{print $1}'` == "1" ]] && httpd_up=$fail_sign || httpd_up=ok_sign
[[ `ps aux | grep vnc | wc -l | awk '{print $1}'` == "1" ]] && vnc_up=$fail_sign || vnc_up=ok_sign
[[ `ps aux | grep znc | wc -l | awk '{print $1}'` == "1" ]] && znc_up=$fail_sign || znc_up=ok_sign
[[ `ps aux | grep darkplaces | wc -l | awk '{print $1}'` == "1" ]] && quake_up=$fail_sign || quake_up=ok_sign
[[ `ps aux | grep redis | wc -l | awk '{print $1}'` == "1" ]] && redis_up=$fail_sign || redis_up=ok_sign
}
function render_stats {
get_stats
printf "$template" "$hostname" "$ram_usage" "$swap_usage" \
"$cpu_usage" "$cpu_temp" "$home_usage" "$var_usage" "$hdd_usage" \
"$logged_users" "$last_login" "$uptime" "$httpd_up" "$znc_up" "$vnc_up" \
"$quake_up" "$redis_up"
#echo $hostname
#echo $ram_usage
#echo $swap_usage
#echo $cpu_usage
#echo $cpu_temp
#echo $hdd_usage
#echo $home_usage
#echo $var_usage
#echo $logged_users
#echo $last_login
#echo $uptime
#echo $httpd_up
#echo $vnc_up
#echo $znc_up
#echo $quake_up
#echo $redis_up
}
function show_tick {
local sign
if [ $1 == 0 ]
then
sign="⨯"
else
sign="✓"
fi
echo "$sign"
}
function get_last_login {
local last_login
if [ $is_linux == "1" ]
then
last_login=`tail -n 1 /var/log/secure | awk '{print $1 " " $2 " " $3}'`
else
last_login=`tail -n 1 /var/log/authd.log | awk '{print $1 " " $2 " " $3}'`
fi
echo "$last_login"
}
function get_swap_usage {
local swap_usage
if [ $is_linux == "1" ]
then
swap_usage=`free -m | awk 'NR==4{print $2 "MB"}'` #todo remove float point
else
swap_usage="N/A" #todo get Swap on mac
fi
echo "$cpu_usage"
}
function get_ram_usage {
local ram_usage
if [ $is_linux == "1" ]
then
ram_usage=`free -m | awk 'NR==2{print $2 "MB"}'` #todo remove float point
else
ram_usage="N/A" #todo get RAM on mac
fi
echo "$cpu_usage"
}
function get_cpu_usage {
local cpu_usage
if [ $is_linux == "1" ]
then
cpu_usage=`grep 'cpu ' /proc/stat | awk '{usage=(($2+$4)*100/($2+$4+$5))} END {print usage "%"}'` #todo remove float point
else
cpu_usage="N/A" #todo get CPU on mac
fi
echo "$cpu_usage"
}
render_stats
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment