#!/usr/bin/perl

#

# for this to work place this file in: /usr/share/snmp/exim_queue_monitor.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

" ;

}

use NetSNMP :: OID ( ':all' ) ;

use NetSNMP :: agent ( ':all' ) ;

use NetSNMP :: ASN ( ':all' ) ;

print STDERR "exim_queue_monitor.pl loaded ok

" ;

# where we are going to hook onto

my $reg_oid = new NetSNMP :: OID ( $root_oid ) ;

print STDERR "registering at " , $reg_oid , "

" 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

" 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/postfix/active/ /var/spool/postfix/maildrop/ | wc -l` ;

chomp $queue_len ;

print STDERR " -> queue length = $queue_len

" 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 ( $registration_info ) ,

ref ( $request_info ) , ref ( $requests ) ) , "

" ;

print STDERR "processing a request of type " . $request_info -> getMode ( ) . "

" ;

}

if $debugging ;

for ( $request = $requests ; $request ; $request = $request -> next ( ) ) {

my $oid = $request -> getOID ( ) ;

print STDERR " processing request of $oid

" 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

" if $debugging ;