#!/bin/bash
## presets
txtred=$(tput setaf 1)    # Red
txtgrn=$(tput setaf 2)    # Green
txtylw=$(tput setaf 3)    # Yellow
txtrst=$(tput sgr0)       # Text reset
## grab number of cpus
cpuNum=`cat /proc/cpuinfo | grep "processor" | wc -l`;

## grab system load currently
cLoad=`sar | tail -n 2 | head -n 1 | awk '{print $3}'`;

## grab system load average for last few hours
aLoad=`sar | tail -n 1 |awk '{print $3}'`;


## grab the io wait
iow=`sar | tail -n 1 | awk '{print $6}'`;

## grab the mem info

tMem=`free -m | grep Mem | awk '{print $2}'`;
uMem=`free -m | grep "/" | awk '{print $3}'`;
sUsed=`sar -r | tail -n 1 | awk '{print $8}'`;

## math time
biow=$(echo "scale = 2; $iow * 1000"|bc -l|cut -d. -f1);
bcLoadAvg=$(echo "scale = 2;$cLoad / $cpuNum * 1000" |bc -l|cut -d. -f1);
aLoadAvg=$(echo "scale = 2;$aLoad / $cpuNum * 1000" | bc -l| cut -d. -f1);
cLoadAvg=$(echo "scale = 2;$bcLoadAvg / 1000" | bc -l);
pMem=$(echo "scale = 2; $uMem / $tMem * 100" | bc -l|cut -d. -f1);

echo "pmem = $pMem";

## Output diskclaimer

echo -e "$(tput bold) This script uses sar data, averages are based from 00:00 system time of the day you run it $txtrst";

## output load
if  [ $bcLoadAvg -lt  1000 ];  then 
echo -e "$txtgrn The load is $cLoad or $cLoadAvg per core.  This is fine \n $txtrst";
elif [ $bcLoadAvg < 2000 ]; then 
echo -e "$txtylw The load is $cLoad or $cLoadAvg per core.  This is a moderate load \n $txtrst";
else 
echo -e "$txtred The load is $cLoad or $cLoadAvg per core.  This should be investigated \n $txtrst";
fi;

## output IO
if [ $biow -lt 15000 ]; then
echo -e "$txtgrn $iow average IO wait for the last few hours, looks good $txtrst\n";
elif [ $biow -lt 50000 ]; then
echo -e "$txtylw $iow average IO is kinda high, check into this $txtrst\n";
else
echo -e "$txtred ZOMG IO wait is $iow , is this drive dying? $txtrst\n";
fi;

## output Mem info
if [ $pMem -lt 50 ];then
echo -e "$txtgrn $pMem % memory usage is just fine $txtrst \n";
elif [ $pMem -lt 80 ]; then
echo -e "$txtylw $pMem % memory usage is getting up there, what's the cause? \n $txtrst";
else
echo -e "$txtred $pMem % memory usage is way up there, add ram or lower configs \n $txtrst";
fi


## output Swap
if [ $sUsed -lt 50000 ]; then 
echo -e "$txtgrn $sUsed k average swap used today, not to bad $txtrst \n";
elif [ $sUsed -lt 100000 ]; then
echo -e "$txtylw $sUsed k of swap on average today, could be an issue $txtrst \n";
else
echo -e "$txtred $sUsed k of swap is quite a bit, what's going on here \n $txtrst ";
fi

