In the last chapter we looked at a simple PERL script that would get an OID and then nicely format the output. We can use this method to string several gets together and build more useful monitoring applications.
Lets look at a "full featured" application that uses a simple polling method using the CLI interface to snmpget:
#!/usr/local/bin/perl $SNMP_GET_CMD = "snmpget -v1 -c public -Ovq -m PowerNet-MIB"; $SNMP_TARGET = "10.10.1.224"; chomp($model = `${SNMP_GET_CMD} ${SNMP_TARGET} PowerNet-MIB::upsBasicIdentModel.0`); chomp($serial = `${SNMP_GET_CMD} ${SNMP_TARGET} PowerNet-MIB::upsAdvIdentSerialNumber.0`); chomp($cap = `${SNMP_GET_CMD} ${SNMP_TARGET} PowerNet-MIB::upsAdvBatteryCapacity.0`); chomp($temp = `${SNMP_GET_CMD} ${SNMP_TARGET} PowerNet-MIB::upsAdvBatteryTemperature.0`); chomp($out_load = `${SNMP_GET_CMD} ${SNMP_TARGET} PowerNet-MIB::upsAdvOutputLoad.0`); chomp($out_v = `${SNMP_GET_CMD} ${SNMP_TARGET} PowerNet-MIB::upsAdvOutputVoltage.0`); chomp($out_f = `${SNMP_GET_CMD} ${SNMP_TARGET} PowerNet-MIB::upsAdvOutputFrequency.0`); chomp($out_status = `${SNMP_GET_CMD} ${SNMP_TARGET} PowerNet-MIB::upsBasicOutputStatus.0`); $model =~ s/\"//g; # Ditch the quotes. $serial =~ s/\"//g; print <<END; APC UPS ${SNMP_TARGET} Model: ${model} Serial No: ${serial} Battery Capacity: ${cap} Battery Temp(F): ${temp} Output Status: ${out_status} Output Load: ${out_load} Output: ${out_v}VAC @ ${out_f}Hz END
When we run that, we get a nice and pretty output with the data we want:
$ ./apc_status.pl APC UPS 10.10.1.224 Model: Silcon DP340E Serial No: SE(removed) Battery Capacity: 100 Battery Temp(F): 32 Output Status: onLine Output Load: 53 Output: 118VAC @ 60Hz
This method, while not particularly pretty of efficient, is very quick and easy to both write and debug making this approach useful in a pinch.