#!/usr/bin/perl

# +----------------------------------------------------------------------+
# |              A Munin Graph Plugin for ASSP                           |
# |               [ assp-message-statistics ]                            |
# +----------------------------------------------------------------------+
# |        Author: Enrico Labedzki                                       |
# |        Email: enrico.labdzki@brodos.de                               |
# |        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@bguel.info );								# the list of admins receivced messages on an 
my $from = "$pluginname-at-host\@guel.info";						# the host from where it comes
my $muninnodename = "mail.guel.info";		        				# the 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'	=> 66,												# <-- index frame from ( a tweak for other ASSP Versions )
		'to'	=> 100												# <-- index frame to ( "" )
);


# -----------------------------------------------------------------------
my @muninlabel = (
	# Message Statistics --> index 66..100
	"Bayesian Hams",
	"Whitelisted",
	"Local",
	"Noprocessing",
	"Spamlover Spams Passed",
	"Bayesian Spams",
	"Domains Blacklisted",
	"HELO Blacklisted",
	"HELO Invalid",
	"HELO Forged",
	"Missing MX",
	"Missing PTR",
	"Invalid PTR",
	"Spam Collected Messages",
	"Penalty Trap Messages",
	"Bad Attachments",
	"Viruses Detected",
	"Sender Regex",
	"Bomb Regex",
	"Penalty Box",
	"Message Scoring",
	"Invalid Local Sender",
	"Invalid Internal Mail",
	"Scripts",
	"SPF Failures",
	"RBL Failures",
	"URIBL Failures",
	"Max Errors Exceeded",
	"Delayed",
	"Empty Recipient",
	"Not SRS Signed Bounces",
	"MSGID Signature",
	"DKIM",
	"DKIM pre Check",
	"Pre Header"
);

# ============= 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 - Message 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;
}

