#!/usr/bin/perl

# +----------------------------------------------------------------------+
# |              A Munin Graph Plugin for ASSP                           |
# |         [ assp-smtp-handler-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@guel.info );									# the list of admins receivced messages on an error
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'	=> 4,												# <-- index frame from ( a tweak for other ASSP Versions )
		'to'	=> 5												# <-- index frame to ( "" )
);

#------------------------------------------------------------------------
my @muninlabel = (													# General Runtime Information
	#"ASSP Proxy Uptime",
	#"Messages Processed",
	#"Non-Local Mail Blocked",
	#"CPU Usage",
	"Concurrent SMTP Sessions"
);

# ============= 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} );								# EXIT IF GENERAL SECTION FINISH
		$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" ){
	my $count = 0;
	my $label;
	foreach my $key ( @muninlabel ){
		if( $count == 0 ){	# General Runtime Information
			print "graph_title ASSP - General Runtime Information\n";
			print "graph_vlabel Counter in Percent\n";
			print "graph_category spamfilter\n";
		}
		$label = $key;
		$label =~ s/\s/\_/g;
		$label =~ s/\-/\_/g;
		$label =~ s/[\(\)]//g;
		print "$label.label $key\n";
		$count++;
	}
	exit 0;
}

