diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2015-09-26 20:28:52 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2015-09-26 20:28:52 +0300 |
commit | c62da081f3dca12030a32fc10eb240c45d08f8f3 (patch) | |
tree | c30ce230e8806a8cebc0fe245ddbe39065ca7e9c /sendmail.pl | |
parent | 40468ec97480e350dc47e6db44b87ccce35ac67b (diff) | |
download | netsnmp-sendmail-c62da081f3dca12030a32fc10eb240c45d08f8f3.tar.gz netsnmp-sendmail-c62da081f3dca12030a32fc10eb240c45d08f8f3.tar.bz2 |
Rewrite using mib2c
* .gitignore: Update.
* MANIFEST: Update.
* Makefile.PL: Build Sendmail.pm
* NetSNMP/Sendmail.pm: Remove.
* Sendmail.mib2c: New file.
* sendmail.pl: New file.
Diffstat (limited to 'sendmail.pl')
-rw-r--r-- | sendmail.pl | 239 |
1 files changed, 239 insertions, 0 deletions
diff --git a/sendmail.pl b/sendmail.pl new file mode 100644 index 0000000..8cc6c65 --- /dev/null +++ b/sendmail.pl @@ -0,0 +1,239 @@ +# SNMP Sendmail Statistics Module +# Copyright (C) 2015 Sergey Poznyakoff <gray@gnu.org> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +package NetSNMP::Sendmail; +require 5.10.0; +use strict; +use feature 'state'; +use NetSNMP::agent::Support; +use NetSNMP::agent (':all'); +use NetSNMP::ASN (':all'); +use Carp; + +use Data::Dumper; + +require Exporter; +our @ISA = qw(Exporter); + +our $VERSION = "0.91"; + +our @EXPORT_OK = qw(&Configure); + +my $mailq_bin = 'mailq'; +my $mailstats_bin = 'mailstats'; + +my %ttl = ( + mailq => 10, + mailstats => 10 +); + +my %config = ( + mailq_ttl => \$ttl{mailq}, + mailstats_ttl => \$ttl{mailstats}, + mailq => \$mailq_bin, + mailstats => \$mailstats_bin, + bindir => sub { + $mailq_bin = "$_[1]/$mailq_bin" if $mailq_bin !~ m#^/#; + $mailstats_bin = "$_[1]/$mailstats_bin" if $mailstats_bin !~ m#^/#; + } +); + +sub debug { +} + +sub Configure { +# print Dumper ( \@_ ); + local %_ = @_; + while (my ($k,$v) = each %_) { + if (exists($config{$k})) { + if (ref($config{$k}) eq 'CODE') { + &{$config{$k}}($k, $v); + } else { + ${$config{$k}} = $v; + } + } else { + confess "unknown keyword: $k"; + } + } +} + +sub import { + my $pkg = shift; # package + my @syms = (); # symbols to import + my @config = (); # configuration + my $dest = \@syms; # symbols first + for (@_) { + if ($_ eq ':config') { + $dest = \@config; + next; + } + push @$dest, $_; + } + local $Exporter::ExportLevel = 1; + $pkg->SUPER::import(@syms); + Configure(@config) if @config; +} + +my %timestamp; + +sub queue_stats { + state %tmp; + + if ($ttl{mailq}) { + my $now = time(); + return %tmp if $now - $timestamp{mailq} < $ttl{mailq}; + $timestamp{mailq} = $now; + %tmp = (); + } + open(my $fd, '-|', $mailq_bin) + or die "can't run $mailq_bin: $!"; + while (<$fd>) { + chomp; + if (/^(^\S+) is empty/) { + push @{$tmp{q}}, { queueName => $1, queueMessages => 0 }; + } elsif (/^\s*(\S+) \((\d+) requests\)/) { + push @{$tmp{q}}, { queueName => $1, queueMessages => $2 }; + } elsif (/Total requests:\s+(\d+)\s*$/) { + $tmp{total} = $1 + } + } + close($fd); + return %tmp; +} + +sub mailer_stats { + state $timestamp; + state %mstats; + + my $now = time(); + return %mstats if $now - $timestamp{mailstats} < $ttl{mailstats}; + $timestamp{mailstats} = $now; + %mstats = (); + + debug("calling $mailstats_bin"); + open(my $fd, '-|', "$mailstats_bin -P") + or die "can't run $mailstats_bin: $!"; + my $line = 0; + + while (<$fd>) { + ++$line; + + next if $line == 1; + + s/^\s+//; +# msgsfr bytes_from msgsto bytes_to msgsrej msgsdis msgsqur Mailer + my @a = split /\s+/; + if ($a[0] eq 'T') { + @{$mstats{totals}}{('totalMessagesFrom', + 'totalKBytesFrom', + 'totalMessagesTo', + 'totalKBytesTo', + 'totalMessagesRejected', + 'totalMessagesDiscarded', + 'totalMessagesQuarantined')} = @a[1..7]; + } elsif ($a[0] eq 'C') { + @{$mstats{conn}}{('connectionMessagesFrom', + 'connectionMessagesTo', + 'connectionMessagesRejected')} = @a[1..3]; + } else { + my %h; + @h{('mailerMessagesFrom', + 'mailerKBytesFrom', + 'mailerMessagesTo', + 'mailerKBytesTo', + 'mailerMessagesRejected', + 'mailerMessagesDiscarded', + 'mailerMessagesQuarantined', + 'mailerName')} = @a[1..8]; + push @{$mstats{mailer}}, \%h; + } + } + close($fd); + return %mstats; +} + +# ############# + +sub get_queueTotal { + my %qstats = queue_stats(); + return $qstats{total}; +} + +sub get_queueTable { + my ($name, $off, $oid) = @_; + my $idx = getOidElement($oid, $off); + my %qstats = queue_stats(); + return $qstats{q}->[$idx-1]{$name}; +} + +sub check_queueTable { + my ($off, $oid) = @_; + my $idx = getOidElement($oid, $off); + my %qstats = queue_stats(); + return $idx-1 <= $#{$qstats{q}}; +} + +sub next_queueTable { + my ($len, $oid) = @_; + + my $idx = getOidElement($oid, $len); + my %qstats = queue_stats(); + ++$idx; + if ($idx-1 <= $#{$qstats{q}}) { + return setOidElement($oid, $len, $idx); + } + return 0; +} + +sub get_mailerTable { + my ($name, $off, $oid) = @_; + my $idx = getOidElement($oid, $off); + my %mstats = mailer_stats(); + return $mstats{mailer}->[$idx-1]{$name}; +} + +sub check_mailerTable { + my ($off, $oid) = @_; + my $idx = getOidElement($oid, $off); + my %mstats = mailer_stats(); + return $idx-1 <= $#{$mstats{mailer}}; +} + +sub next_mailerTable { + my ($len, $oid) = @_; + + my $idx = getOidElement($oid, $len); + my %mstats = mailer_stats(); + + ++$idx; + if ($idx-1 <= $#{$mstats{mailer}}) { + return setOidElement($oid, $len, $idx); + } + return 0; +} + +sub total_table_get { + my $name = shift; + my %mstats = mailer_stats(); + return $mstats{totals}->{$name}; +} + +sub connection_table_get { + my $name = shift; + my %mstats = mailer_stats(); + return $mstats{conn}->{$name}; +} + |