This 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.
I suggest monitoring all partitions. Include the disk partition information in the snmpd.conf file and the hosts you want to monitor in the diskchecker.conf file. If you wanted to monitor the disk partitions on host 10.10.10.5 and wanted to be alerted if the available disk space on partition /var had exceeded 75%, you would follow the examples below. Remember the % listed in the snmpd.conf file is the desired minimum disk space free before the threshold is exceeded.
SNMP stands for Simple Network Monitoring Protocol. It is a management tool all systems administrators should be using. It is a must for tools like MRTG, RRDTool, and a variety of other graphing and monitoring tools. SNMP is an extensible way that systems, programs, and environments can be monitored. SNMP can not only respond to tools with statistics like "Network card octets sent" or "Linux 5 minute load average", but it can also be used to change settings when using the Read-Write mode.
Making changes using SNMP is an advanced topic and will not be covered in this article. I will look only at the Read-Only mode. This script relies on the SNMP libraries and daemon to be installed on all the servers that you choose to monitor. Most of today's Linux distributions ship with SNMP packages (as do most networking equipment) and are ready to work out of the box.
On an RPM-based system such as Red Hat or Mandrake, run the command rpm -qa | grep snmp. This will display all the RPMs installed on your system that are SNMP-related. The most common SNMP libraries are ucd-snmp and net-snmp. Ucd-snmp was renamed to net-snmp when it changed major versions from 4.x to 5.x. If SNMP isn't installed, your distribution CD will have it. Install it from the CD or find it at http://rpmfind.net. If you need to build your own from source, follow these steps:
* Obtain the source for Net-SNMP at:
Once SNMP is installed, you must configure it. A basic snmpd.conf file might look like this:
# syslocation: The [typically physical] location of the system.
syslocation TEST DATACENTER
# syscontact: The contact information for the administrator
# sysservices: The proper value for the sysServices object.
# rocommunity: a SNMPv1/SNMPv2c read-only access community name
# rwcommunity: a SNMPv1/SNMPv2c read-write access community name
disk / 25%
Next, start the daemon by running "snmpd" (if you compiled your own) or "service snmpd start" (if you installed from an RPM). Now, to get the "diskchecker.sh" script to work, make sure you define at least one partition and the disk space % minimum threshold in your snmpd.conf file. If you edit the snmpd.conf file after starting the the "snmpd" daemon, restart the daemon for the new configuration to take effect.
You can execute the "diskchecker.sh" script from the command line or from CRON. I run mine once an hour from CRON. This example CRON entry executes at three minutes past every hour:
03 * * * * /path-to-script/diskchecker.sh >> /dev/null 2>&1
First the script scans through the diskchecker.conf file (where all your hosts are listed, one per line), and then it does an SNMPWALK of the HOST MIB OID that corresponds with DISKS. An OID is an Object Identifier. SNMP identifies all the information it monitors with an OID. The two OIDs we are looking at are "188.8.131.52.4.1.2021.9.1.100 and 184.108.40.206.4.1.2021.9.1.101" from the HOST MIB.
An MIB (Management Information Base) is the database of objects that SNMP clients can access. There are two standard MIBs defined — MIB I defined in RFC 1156, and MIB II defined in RFC 1213. As I said before, SNMP is extensible; anyone can request and define an OID.
Many companies design custom SNMP objects that are private and specific to their needs. For example, a company might design an application that is monitored via SNMP. The company requests and obtains their own OID from the IANA (Internet Assigned Numbers Authority) and then defines the objects and structure to fit their specification. This is outside the scope of this column, but I thought I'd mention how SNMP can be used. SNMPWALK is a client query tool that is passed a host, SNMP community string, and, optionally, an OID. In the script, I first need to know whether any of the partitions defined in a hosts snmpd.conf file exceed the miniumum disk % threshold specified.
If a threshold is exceeded, the OID "220.127.116.11.4.1.2021.9.1.100" returns a value of 1 and the script then does an SNMPWALK of OID "18.104.22.168.4.1.2021.9.1.101". This actually returns the status of all monitored partitions with their current disk utilization and the disk threshold set in the snmpd.conf file.
To discard the data not required, the script does a grep for the keyword "less", since the partition flagged with less than the threshold is what it is concerned about. The rest of the script formats the email and sends it to the appropriate email address.
SNMP is a very powerful tool and should be part of every administrator's tool kit. This column has only scratched the surface of what SNMP can do. I hope you'll take the time to learn its ins and outs; I still am.
Marc Skinner lives in Minneapolis where he is a Linux Engineer for Agiliti, Inc. He can be reached at: email@example.com
Questions or problems regarding this web site should be directed to firstname.lastname@example.org.
Copyright © 2008 Art Beckman. All rights reserved.
Last Modified: March 9, 2008