#!/bin/bash

### This is the contact name of who errors with this script should get sent to.
contact="mjones@liquidweb.com"

function exists {
	if command -v $1 &>/dev/null
	then
		return 1
        else
                echo "$1 was not found, please ensure $1 is installed and in the PATH. $contact might want to know about this."
                exit 255
        fi
}

function is_integer {
	challenge="$1"
	checked=`echo $challenge | egrep '([A-Z]|[a-z])'`
	if [[ -z $checked ]] ;then 
		return 1 
	else 
		return 0 
	fi
}

exists sar
exists awk
exists grep
exists tail
exists head
exists bc
exists cut
exists egrep

## 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=`grep "processor" /proc/cpuinfo | wc -l`;
is_integer $cpuNum
ret="$?"
if [[ "$ret" -eq '0' ]] ; then
	echo "ERROR: Unable to determine cpu count, got: $cpuNum. Please report this problem to $contact"
	exit $ret
fi

## grab system load currently
cLoad=`sar -q|tail -n 2|head -n 1|awk '{printf $5}'`
is_integer $cLoad
ret="$?"
if [[ "$ret" -eq '0' ]] ; then
        echo "ERROR: Unable to determine 1 minute load average, got: $cLoad. Please report this problem to $contact"
        exit $ret
fi

## grab system load average for last few hours
aLoad=`sar | tail -n 1 |awk '{print $3}'`;
is_integer $aLoad
ret="$?"
if [[ "$ret" -eq '0' ]] ; then
	echo "ERROR: Unable to determine system load average over the last few hours; got: $aLoad. Please report this problem to $contact"
	exit $ret
fi

## grab the io wait
iow=`sar | tail -n 1 | awk '{print $6}'`;
is_integer $iow
ret="$?"
if [[ "$ret" -eq '0' ]] ; then
        echo "ERROR: Unable to determine system I/O wait; got: $iow. Please report this problem to $contact"
        exit $ret
fi

## grab the mem info
tMem=`free -m | grep Mem | awk '{print $2}'`;
is_integer $tMem
ret="$?"
if [[ "$ret" -eq '0' ]] ; then
        echo "ERROR: Unable to determine memory statistics; got: $tMem. Please report this problem to $contact"
        exit $ret
fi

uMem=`free -m | grep "/" | awk '{print $3}'`;
is_integer $uMem
ret="$?"
if [[ "$ret" -eq '0' ]] ; then
        echo "ERROR: Unable to determine memory cache statistics; got: $uMem. Please report this problem to $contact"
        exit $ret
fi

sUsed=`sar -r | tail -n 1 | awk '{print $8}'`;
is_integer $sUsed
ret="$?"
if [[ "$ret" -eq '0' ]] ; then
        echo "ERROR: Unable to determine I/O statistics; got: $sUsed. Please report this problem to $contact"
        exit $ret
fi

## math time
biow=$(echo "scale = 2; $iow * 1000"|bc -l|cut -d. -f1);
is_integer $biow
ret="$?"
if [[ "$ret" -eq '0' ]] ; then
        echo "ERROR: Unable to compute I/O statistics; got: $biow. Please report this problem to $contact"
        exit $ret
fi

bcLoadAvg=$(echo "scale = 2;$cLoad / $cpuNum * 1000" |bc -l|cut -d. -f1);
is_integer $bcLoadAvg
ret="$?"
if [[ "$ret" -eq '0' ]] ; then
        echo "ERROR: Unable to compute load average statistics; got: $bcLoadAvg. Please report this problem to $contact"
        exit $ret
fi

aLoadAvg=$(echo "scale = 2;$aLoad / $cpuNum * 1000" | bc -l| cut -d. -f1);
is_integer $aLoadAvg
ret="$?"
if [[ "$ret" -eq '0' ]] ; then
        echo "ERROR: Unable to compute load average statistics; got: $aLoadAvg. Please report this problem to $contact"
        exit $ret
fi

cLoadAvg=$(echo "scale = 2;$bcLoadAvg / 1000" | bc -l);
is_integer $cLoadAvg
ret="$?"
if [[ "$ret" -eq '0' ]] ; then
        echo "ERROR: Unable to compute load average statistics; got: $cLoadAvg. Please report this problem to $contact"
        exit $ret
fi

pMem=$(echo "scale = 2; $uMem / $tMem * 100" | bc -l|cut -d. -f1);
is_integer $pMem
ret="$?"
if [[ "$ret" -eq '0' ]] ; then
        echo "ERROR: Unable to compute load average statistics; got: $pMem. Please report this problem to $contact"
        exit $ret
fi

## 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 I/O
if [[ $biow -lt 15000 ]]; then
	echo -e "$txtgrn $iow average I/O wait for the last few hours, looks good $txtrst\n";
elif [[ $biow -lt 50000 ]]; then
	echo -e "$txtylw $iow average I/O is kinda high, check into this $txtrst\n";
else
	echo -e "$txtred ZOMG I/O 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

