BASH – Script that distinguishes between a Database Server and a Web Server and does service checks.

The script can be used in a for loop. It generates an Error Alarm only when a critical condition is at hand. These specific conditions are as follows. The script can detect if the server is running Apache or PostgreSQL. So if for example it sees that Apache is not installed it will not do a check for the Apache process. It will move on to do a check for PostgreSQL and perform the same check as mentioned above. In other words the script is not blindly checking for processes.

If Apache is down.
If PostgreSQL is down.
If CPU load exceeds normal levels
If the root partition has reached 90% usage
If /storage (other specific directory of your choosing) has reached 90% usage.

#The three variables below are used to check CPU idle and filesystem usage under / and /storage
#cpu_idle=`mpstat | awk '{print $11}' | tail -1 | bc -l`;
cpu_cores=`cat /proc/cpuinfo | grep 'processor' | wc -l`
cpu_load=`uptime | awk '{print $12}'`
root_space=`df -h / | awk '{print $5}' | tail -1 | sed 's/%//'`
storage_space=`df -h /storage | awk '{print $5}' | tail -1 | sed 's/%//'`
#---------------------------------------------------------------------
#THIS BLOCK OF CODE CHECKS TO SEE WHAT TYPE OF SERVER IT IS (web or DB)
#AND IF THE SERVICE IS RUNNING
#The two variables below do the check to find out what type of server it is.
httpd_check=`ls /usr/local/apache/bin/httpd`
postgres_check=`ls /etc/init.d/postgresql`
#The two variables will check to see if the service is running
httpd_status=`ps -ef | grep httpd | grep -v grep`
postgres_status=`ps -ef | grep pgsql | grep -v pgsql`
#Here if the variable returns a value of zero it will look for the HTTPD status
echo $httpd_check >> /dev/null
if [[ $? -eq 0 ]]
then
echo $httpd_status >> /dev/null
if [[ $? -gt 0 ]];
then
echo -e "CRITICAL HTTPD SERVICE on host "$HOSTNAME" - IS NO LONGER RUNNING"
fi
fi
#Here if the variable returns a value of zero it will look for the POSTGRESQL status
echo $postgres_check >> /dev/null
if [[ $? -eq 0 ]]
then
echo $postgres_status >> /dev/null
if [[ $? -gt 0 ]];
then
echo -e "CRITICAL POSTGRESQL SERVICE on host "$HOSTNAME" - IS NO LONGER RUNNING\n"
fi
fi
#-------------
#The commands below are generic in nature and they will check for
#CPU IDLE and for / and /storage usage

if [[ "$cpu_load" > "$cpu_cores" ]]
then
echo -e "CRITICAL CPU USAGE on host $HOSTNAME CPU is at $cpu_load percent"
fi

if [[ "$root_space" > 90 ]]
then
echo "CRITICAL FILESYSTEM ROOT on host "$HOSTNAME" FSystem is at $root_space percent"
fi

if [[ "$storage_space" > 90 ]]
then
echo -e "CRITICAL FILESYSTEM STORAGE on host "$HOSTNAME" FSystem is at $storage_space percent"
fi
Share Button

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload the CAPTCHA.