#!/bin/bash
##############################################################################
# Bash script written by Vahid Hedayati April 2013
##############################################################################
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
##############################################################################
# This script takes:
#
# Once given a hostname and community string with or without specific load limit
# with no load limit, using snmp it will look at amount of cpu's and calculate load average / cpu count -
# cpu count hacked to be more than 2 than actually on host to alert when it looks really serious
# This script requires SNMP to be configured and installed on both local / remote servers
# -- UPDATES - debian - ubuntu systems run:
# sudo apt-get install snmp-mibs-downloader
snmpwalk="/usr/bin/snmpwalk"
snmpget="/usr/bin/snmpget"
# Set to 0 if no load limit set then it will get cpu count and increment this
get_cpu=0;
function usage() {
echo "$0 -H localhost -l 1 -p public"
echo "$0 -H localhost -p public"
echo "-----------------------------------"
echo "-H is hostname set to localhost"
echo "-l is load limit set to 1 or not set at all"
echo "-p is snmp passsword"
echo "-----------------------------------"
echo "You can leave the load blank , this then defines the load according to CPU's"
echo "so a 4 cpu server will have a load set to 6 if it goes above 4 then alerts are sent"
}
function return_cpu() {
# Below had been working on work machines
#cpu_count=$($snmpwalk -v2c -c $pass -Oq $server hrProcessorLoad|wc -l)
# This is a generic view that should hopefully work on all Linux Machines
cpu_count=$($snmpwalk -v2c -c $pass -Oq $server HOST-RESOURCES-MIB::hrProcessorLoad|wc -l)
if [ "$cpu_count" == "" ]; then
cpu_count=1;
fi
actual_cpus=$cpu_count
# VH: To stop default cpu count from alerting we will add an extra cpu to ensure load is sufficient for an alert
((cpu_count++))
((cpu_count++))
}
function calculate_load() {
return_cpu;
good_res="";
bad_res="";
graphtest="|";
if [ "$limit" == "" ]; then
limit=$cpu_count;
fi
loads="1 2 3"
bad=0;
loadrun="";
cpu_info="Actual_cpus:$actual_cpus - Alert_Limit:$limit";
for ids in $loads
do
if [[ $ids == 1 ]]; then
loadrun="1_minute";
elif [[ $ids == 2 ]]; then
loadrun="5_minutes";
elif [[ $ids == 3 ]]; then
loadrun="15_minutes";
fi
run=$($snmpget -v2c -c $pass $server .1.3.6.1.4.1.2021.10.1.3.$ids)
if [[ $run =~ "STRING" ]]; then
actual_load=$(echo $run|awk -F"STRING: " '{ print $2; }'|tr -d '"')
current_load=$(echo $run|awk -v limit=$limit -v cpus=$cpu_count -F"STRING: " '{ if ( ($2 > 0)) { smb=($2 / cpus); print smb;} else { print $2; } }'|tr -d '"')
calc=$(echo $limit|awk -v cload=$current_load '{$3 = cload / $1; printf "%f", $3}')
if [[ $calc < 1 ]]; then
good_res=$good_res" ("$loadrun"_avg:"$current_load", "$loadrun"_actual="$actual_load" ) - "
graphtest=$graphtest" $loadrun"_avg"="$current_load"m;;;0 "$loadrun"_actual="$actual_load"m;;;0 "
else
bad_res=$bad_res" ("$loadrun"_avg:"$current_load", "$loadrun"_actual="$actual_load" ) - "
graphtest=$graphtest" $loadrun"_avg"="$current_load"m;;;0 "$loadrun"_actual="$actual_load"m;;;0 "
((bad++))
fi
else
echo -n "$loadrun does not exist ";
((bad++))
fi
done
if [[ $bad > 0 ]]; then
echo "CRITICAL: $cpu_info $bad_res $graphtest"
exit 2;
else
echo "OK: $cpu_info $good_res" $graphtest;
exit 0;
fi
}
while test -n "$1"; do
case "$1" in
--help|-h)
usage
exit 0
;;
--host|-H)
server=$2;
shift
;;
--limit|-l)
limit=$2;
if [ "$limit" == "-p" ]; then
limit="";
pass=$3;
calculate_load;
exit
fi
shift
;;
--snmpass|-p)
pass=$2;
calculate_load;
exit
;;
*)
echo "Unknown argument: $1"
echo "-h for help";
exit 1
;;
esac
shift
done
if [ $# -eq 0 ]; then
echo "-h for help";
exit 1;
fi