#!/usr/bin/perl

# +----------------------------------------------------------------------+
# |              A Munin Graph Plugin for ASSP                           |
# |            [ assp-envelope-recipient-statistics ]                    |
# +----------------------------------------------------------------------+
# |        Author: Enrico Labedzki                                       |
# |        Email: enzo@guel.info                                         |
# |        Last Modified: 2010-02-22                                     |
# |        Licence: GPLv3 http://www.gnu.org/licenses/gpl-3.0.txt        |
# +----------------------------------------------------------------------+

use strict;
use warnings;
use File::Basename;
use LWP;
use Mail::Sendmail;

# -------------------------- DEBUG VARS ---------------------------------
my $DEBUG = 0;														# for debugging purpose
my $EMAILDEBUG = 0;													# for email debugging
my $pluginname = &basename( "$0" );									# get the basename of the plugin
my @to = qw( webmaster@guel.info );									# the list of admins receivced messages on an error
my $from = "$pluginname-at-host\@guel.info";						# the host where the plugin is installed
my $muninnodename = "mail.guel.info";		        				# the real Node from where it comes
my $smtp = "mail.guel.info";										# the smtp relay to send the mail

# ------------------------- GLOBAL VARS ---------------------------------
my $version = "1.0";												# UA Version
my $agentname = "$pluginname Munin Plugin V$version";				# UA String
my $url = "http://localhost:55553/";								# (defaults to localhost)
my $response = 0;													# the server output
my @content = ();													# the content we're retrive from $response
my %index = (														# for Version 2
		'from'	=> 40,												# <-- index frame from ( a tweak for other ASSP Versions )
		'to'	=> 63												# <-- index frame to ( "" )
);

# -----------------------------------------------------------------------
my @muninlabel = (
	# Envelope Recipient Statistics --> index 40..63
	"Local Recipients Accepted",
	"Validated Recipients",
	"Unchecked Recipients",
	"Spam-Lover Recipients",
	"Remote Recipients Accepted",
	"Whitelisted Recipients",
	"Not Whitelisted Recipients",
	"Noprocessed Recipients",
	"Email Reports",
	"Spam Reports",
	"Ham Reports",
	"Whitelist Additions",
	"Whitelist Deletions",
	"Redlist Additions",
	"Redlist Deletions",
	"Local Recipients Rejected",
	"Nonexistent Recipients",
	"Delayed Recipients",
	"Delayed (Late) Recipients",
	"Delayed (Expired) Recipients",
	"Embargoed Recipients",
	"Spam Bucketed Recipients",
	"Remote Recipients Rejected",
	"Relay Attempts Rejected"
);

# ============= SANITY CHECKS ================
unless( defined(@ARGV) ){
	$ARGV[0] = "";
}

# =============== THE GET ====================
if( $ARGV[0] eq "" ){
	my $agent = LWP::UserAgent->new();
	$agent->agent("$agentname");	
	$response = $agent->get( $url );
	&response_error() unless $response->is_success;
	@content = split( /\n/, $response->content );

	my $line = "";
	my $count = $index{from};	
	my $label;
	my( $key, $value, $last );
	while( 1 ){
		&finish() if( $count > $index{to} );
		$line = $content[$count];
		$count++;
		chomp( $line );
		next if $line eq "";										# no empty lines
		next if $line =~ /^\n$/;									# no newlines
		next if $line =~ /^\r$/;									# or else
		next if $line =~ /^\r\n$/;									# http specific
		( $key, $value, $last) = split( /\|/, $line );				# split up to three values
		$key =~ s/^\s//g; 	$key =~ s/\s$//g;						# no spaces at the end and the beginning
		$value =~ s/^\s//g;	$value =~ s/\s$//g;
		$value =~ s/(\d*)\s*.*/$1/g;								# remove more than one values from splited data
		$value =~ s/[a-zA-Z\(\)\%]//g;								# and not alphanummeric glyphs
		$last =~ s/^\s//g;	$last =~ s/\s$//g;

		$label = $key;
		$label =~ s/\s/\_/g;										# generate a label
		$label =~ s/\-/\_/g;										# the subs glyph to underline
		$label =~ s/[\(\)]//g;										# no special glyphs feel free to add more
		print "$label.value $value\n";								# print the result to the label
	}
}

# =============== FUNCTIONS ==================
sub finish{
	exit 0;
}
sub response_error{
	if( $DEBUG ){
		foreach my $admin ( @to ){
			my %mail = ( smtp => "$smtp", To => "$admin", From => "$from", Subject => "Munin Plugin $pluginname", Message => "ERROR: $agentname at $muninnodename\n" );
			&sendmail(%mail) or die "ERROR: $Mail::Sendmail::error\n";

			if( $EMAILDEBUG ){
				print "OK. Log says:\n$Mail::Sendmail::log\n";
			}
		}
	}
	exit 1;	# if no admin exists
}

# ============== MUNIN GRAPHER COINFIG =======

if( $ARGV[0] eq "config" ){
	print "graph_title ASSP - Envelope Recipient Statistics\n";
	print "graph_vlabel Counter in k,m\n";
	print "graph_category spamfilter\n";

	my $label;
	foreach my $key ( @muninlabel ){
		$label = $key;
		$label =~ s/\s/\_/g;
		$label =~ s/\-/\_/g;
		$label =~ s/[\(\)]//g;
		print "$label.label $key\n";
	}
	exit 0;
}

