Hello Monks,
I wrote a small SNMP based monitor that hooks onto an OID and responds to various SNMP commands.
(I use my Organizations OID, but for Internet purposes 9999 works just fine.)
I would like to modify this so I have a root monitor "driver" which loads the monitors from individual files placed in a ./plugins/ directory. In other words how do I design this in pluggable type architecture? Can you help me out with some reading material or basic examples?
Also, I am always looking for ways to improve my code so, if you have any constructive criticism or ideas I would like to hear them.
For bonus points and extremely low on the priority scale: how to I get Perl to tell snmpwalk the OID names?
As always, thanks for your help.
There is a colorized version here: http://pastebin.com/QKH4H9ta
#!/usr/bin/perl
#
# for this to work place this file in: /usr/share/snmp/exim_queue_mo
+nitor.pl
# insert the following line in /usr/share/snmp/snmpd.conf
# perl do "/usr/share/snmp/exim_queue_monitor.pl
#
use strict;
use warnings;
# Global Vars:
our $agent;
# Private vars
my $root_oid = '.1.3.6.1.4.1.9999';
my $running;
# set to 1 to get debugging information
my $debugging = 0;
# since we're embedded, set this to 0
my $subagent = 0;
BEGIN {
print STDERR "starting exim_queue_monitor.pl\n";
}
use NetSNMP::OID (':all');
use NetSNMP::agent (':all');
use NetSNMP::ASN (':all');
print STDERR "exim_queue_monitor.pl loaded ok\n";
# where we are going to hook onto
my $reg_oid = new NetSNMP::OID($root_oid);
print STDERR "registering at ",$reg_oid,"\n" if $debugging;
# we register ourselves with the master agent we're embedded in.
$agent->register('exim_queue_monitor', $reg_oid, \&exim_queue_monitor,
+ );
# processes system command and updates request obj.
sub process_exim_mail_queue {
my $request = shift;
my $queue_len = `exim -bpc`;
chomp $queue_len;
print STDERR " -> queue length = $queue_len\n" if $debugging;
$request->setValue(ASN_INTEGER, $queue_len);
}
sub process_postfix_mail_queue {
my $request = shift;
my $queue_len = `find /var/spool/postfix/deferred/ /var/spool/post
+fix/active/ /var/spool/postfix/maildrop/ | wc -l`;
chomp $queue_len;
print STDERR " -> queue length = $queue_len\n" if $debugging;
$request->setValue(ASN_INTEGER, $queue_len);
}
# define a handler for incoming SNMP requests for our part of the OID
+tree.
sub exim_queue_monitor {
my ($handler, $registration_info, $request_info, $requests) = @_;
my $request;
#debug messages
do {
print STDERR "refs: ",join(", ", ref($handler), ref($registrat
+ion_info),
ref($request_info), ref($requests)),"\n";
print STDERR "processing a request of type " . $request_info->
+getMode() . "\n";
}
if $debugging;
for($request = $requests; $request; $request = $request->next()) {
my $oid = $request->getOID();
print STDERR " processing request of $oid\n" if $debugging;
# if SNMP monitor request mode is GET
if ($request_info->getMode() == MODE_GET) {
# queue request for exim
if ($oid == new NetSNMP::OID($root_oid . ".1.2.1")) {
process_exim_mail_queue $request;
}
# queue request for postfix
elsif ($oid == new NetSNMP::OID($root_oid . ".1.2.2")) {
process_postfix_mail_queue $request;
}
}
# if SNMP monitor request mode is GETNEXT
elsif ($request_info->getMode() == MODE_GETNEXT) {
# queue request for exim
if ($oid == new NetSNMP::OID($root_oid . ".1.2.1")) {
process_exim_mail_queue $request;
}
# queue request for postfix
elsif ($oid == new NetSNMP::OID($root_oid . ".1.2.2")) {
process_postfix_mail_queue $request;
}
}
}# end for loop
print STDERR " finished processing\n" if $debugging;
}
EDIT:Updated to more recent version, fixed minor naming.