Shell Corner: Disk Space Checking with SNMP
By and Ed Schaefer, February 02, 2005
diskchecker.shby Marc SkinnerThis bash script - diskchecker.sh - is an SNMP poller that polls the SNMP OID of the disk partitions defined in the snmpd.conf file on Linux boxes. It polls the disks of hosts defined in the diskchecker.conf file. You can specify the drive and the minimum percentage you want free. Once the percentage is exceeded (e.g., a partition is set for 25% and suddenly only 20% of the partition is free), an email is generated.
pre>
#!/bin/bash
##########################################################################################
#
# SNMP Disk Usage Poller (diskchecker.sh)
# Usage: place hosts to check in /etc/diskchecker.conf
# Requirments: Must have the HOST MIB support compiled into NET-SNMP
# Must have the following per disk partion you want to monitor per host in snmpd.conf file
# /home %25 : this means - the disk thresh-hold for /home will be flagged when the minimun
# : disk space is less than %25 free.
#
#
# make sure you change EMAILADDRESS, SNMPCOMMUNITY, and SNMPPATH to fit your needs.
#
# License = Free/GPL/Use at own risk
#
###########################################################################################
#
# Script Variables
#
##########################################
EMAILADDRESS="[email protected]" #### email address to whom alerts are sent #####
EMAILSUBJECT="!DISKCHECKER!" #### subject of alert #####
SNMPCOMMUNITY="public" #### snmp read-only community string #####
SNMPPATH="/usr/local/bin" #### path where snmpwalk is located #####
##########################################
#
# DON'T CHANGE BELOW THIS LINE
#
###########################################################################################
#
generror=0
while read i; do
error=`$SNMPPATH/snmpwalk -c $SNMPCOMMUNITY $i 1.3.6.1.4.1.2021.9.1.100 | grep " 1" | awk 'BEGIN { FS =":" } ; { print $4}'`
if [ $error -eq 1 ]; then
if [ $generror -eq 0 ]; then
generror=1
if [ $generror -eq 1 ]; then
cat > /tmp/diskchecker.mail.$$ << MSG
*** DISK SPACE ERROR! ***
One or more drives is running low on disk.
------------------------------------------
MSG
generror=2
fi
fi
cat >> /tmp/diskchecker.mail.$$ << MSG
[ $i : $(host $i | awk 'BEGIN { FS =" " } ; { print $5}')]
$($SNMPPATH/snmpwalk -c $SNMPCOMMUNITY $i 1.3.6.1.4.1.2021.9.1.101 | grep less | awk 'BEGIN { FS =":" } ; { print $4}') :: $($SNMPPATH/snmpwalk -c $SNMPCOMMUNITY $i 1.3.6.1.4.1.2021.9.1.101 | grep less | awk 'BEGIN { FS =":" } ; { print $5}')
MSG
fi
done < /etc/diskchecker.conf
if [ -e /tmp/diskchecker.mail.$$ ]; then
mail -s $EMAILSUBJECT $EMAILADDRESS << MSG
$(cat /tmp/diskchecker.mail.$$)
MSG
sleep 2
rm -f /tmp/diskchecker.mail.$$
fi
# END SCRIPT