#!/usr/bin/perl -w

use strict;

###
## Define some relationships with the Cyrus SNMP OIDs.
## While we are at it, initialize some variables too.
###

my $string_title   = "enterprises.3.6.1.1.1.0";
my $string_version = "enterprises.3.6.1.1.2.0";
my $string_uptime  = "enterprises.3.6.1.1.3.0";
my $string_forks   = "enterprises.3.6.1.2.1.1";
my $string_running = "enterprises.3.6.1.2.1.2";
my $string_service = "enterprises.3.6.1.2.1.3";
my $string_maxrun  = "enterprises.3.6.1.2.1.5";

my $title = "";
my $version = "";
my $uptime = "";

my @forks = ();
my @running = ();
my @service = ();
my @maxrun = ();

my $forks_total = 0;
my $running_total = 0;
my $service_total = 0;
my $maxrun_total = 0;


###
## Make the query and store away the data.  It would be better
## to actually use the perl SNMP module to make the query.  But
## since we don't want to depend on the module being installed
## on the system, we will use the snmpwalk program directly.
###

my $snmpwalk = "/usr/local/net-snmp-4.2.1/bin/snmpwalk";
open SNMP, "$snmpwalk localhost public enterprises.3.6.1|" or die "SNMP Failed: $!";

while (<SNMP>) {
    chomp;

    $title = $1       if /$string_title = \"([^\"]*)\"/;
    $version = $1     if /$string_version = \"([^\"]*)\"/;
    $uptime = $1      if /$string_uptime = Timeticks: \([0-9]*\) (.*)\./;

    $forks[$1] = $2   if /$string_forks\.([0-9]*) = Counter32: ([0-9]*)/;
    $running[$1] = $2 if /$string_running\.([0-9]*) = Gauge32: ([0-9]*)/;
    $service[$1] = $2 if /$string_service\.([0-9]*) = \"([^\"]*)\"/;
    $maxrun[$1] = $2  if /$string_maxrun\.([0-9]*) = Gauge32: ([0-9]*)/;
}

close SNMP;


###
## Determine the maximum length of service name
###

my $service_length = 8;  # for length of "Services" header
for (my $i = 1; $i < @service; $i++) {
    my $length = length($service[$i]);
    $service_length = $length if $length > $service_length;
}


###
## Now, pretty print the data
###

print "Cyrus IMAP Server $version\n";
print `/usr/bin/date`;
print "Up $uptime\n\n\n";

my @header = ("Services", "Forks", "Running", "Maximum");
my @length = ($service_length, 10, 10, 10);

printf("%-$length[0]s  %-$length[1]s  %-$length[2]s  %s\n", @header);

for (my $i = 0; $i < @length; $i++) {
    for (my $j = 0; $j < $length[$i]; $j++) {
	print "-";
    }
    print "  ";
}

print "\n";

for (my $i = 1; $i < @service; $i++) {
    $forks_total += $forks[$i];
    $running_total += $running[$i];
    $maxrun_total += $maxrun[$i];

    printf("%-$length[0]s  %-$length[1]s  %-$length[2]s  %s\n", 
	$service[$i], $forks[$i], $running[$i], $maxrun[$i]);
}

for (my $i = 0; $i < @length; $i++) {
    for (my $j = 0; $j < $length[$i]; $j++) {
	print "=";
    }
    print "  ";
}

printf("\n%-$length[0]s  %-$length[1]s  %-$length[2]s  %s\n", 
    "Total", $forks_total, $running_total, $maxrun_total);

print "\n\nNOTES\n";
print "-----------------------------------------------------------\n";
print "Forks   = Total number of forks since server was started.\n";
print "Running = Total number of processes currently running.\n";
print "Maximum = Maximum number of processes running concurrently.\n";
print "\n";

