Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Perl SNMP Monitor

by PyrexKidd (Monk)
on Sep 05, 2011 at 22:58 UTC ( [id://924306]=perlquestion: print w/replies, xml ) Need Help??

PyrexKidd has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Perl SNMP Monitor
by Mr. Muskrat (Canon) on Sep 07, 2011 at 17:13 UTC

    "There is a colorized version here: http://pastebin.com/QKH4H9ta"

    I prefer the PerlMonks' colorized version provided by Corion.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://924306]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-19 10:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found