#!/usr/bin/perl
#------------------------------------------------------------------------------
#
# SysUsage thresold alarms report
# Copyright (C) 2003-2007 Gilles Darold - Groupe SAMSE
#
# This script is used to send thresold alarmes received from
# the sysusage report tool.
#
# Author: Gilles Darold
#-------------------------------------------------------------------------------
use strict;

use Getopt::Long;
use Net::SMTP;
use POSIX qw(locale_h);
setlocale(LC_ALL, 'C');

my $VERSION = '2.6';

my $SMTP     = 'localhost';
my $FROM     = 'root@localhost';
my $TO       = 'root@localhost';
my $HOSTPROG = '/bin/hostname';
my $TITLE    = '';
my $CURVAL   = '';
my $THRESVAL = '';
my $HELP     = '';
my $TIMEOUT  = 30;


# Get information received from commande line
&check_args();


my $hostname = `$HOSTPROG`;
chomp($hostname);

# Send email
my $smtp = Net::SMTP->new($SMTP, Timeout => $TIMEOUT);
$smtp->mail($FROM);
$smtp->to($TO);
$smtp->data();
$smtp->datasend("Content-Type: text/plain;\n");
$smtp->datasend("To: $TO\n");
$smtp->datasend("From: $FROM\n");
$smtp->datasend("Subject: [Alarme] Sysusage - $TITLE\n");
$smtp->datasend("X-Alarm-Module: sysusage\n");
$smtp->datasend("X-Alarm-Context: system\n");
$smtp->datasend("X-Alarm-Priority: urgent\n");
$smtp->datasend("X-Alarm-Source: $hostname\n");
$smtp->datasend("X-Alarm-Protocol: posix\n");
$smtp->datasend("X-Alarm-Application: warn_thresold.pl\n");
$smtp->datasend("\n");
$smtp->datasend("Le systeme $hostname a reporte un depassement de limite\n");
$smtp->datasend("Information    : $TITLE\n");
$smtp->datasend("Threshold value: $THRESVAL\n");
$smtp->datasend("Current value  : $CURVAL\n");
$smtp->datasend("Les statistiques complete sont disponibles sur <a href=\"http://$hostname.samse.fr/sysusage/\" target=\"_new\">http://$hostname.samse.fr/sysusage/</a>\n");
$smtp->datasend("\n");
$smtp->datasend("\n");
$smtp->dataend();
$smtp->quit;


exit 0;


####
# Function used to return command line usage 
####
sub usage
{
	print qq{
usage: warn_thresold.pl -t subject -c current_value -v threshold_value [-s smtp_srv] [-f from] [-d to] [-b hostname_prog]


	-t subject     : Subject of the alarm
	-c curr_value  : Current value monitored by sysusage.pl
	-v thres_value : Threshold value used.
	-s smtp_srv    : SMTP server name or ip where to send email. Default localhost
	-f from        : Sender email address of the alarm message. Default root\@localhost
	-d to          : Email address of the recipient of the alarm message. Default root\@localhost
	-b host_bin    : Path to program hostname. Default is /bin/hostname
	-h             : Output this message and exit

};

	exit(1);
}

####
# Function used to check command line argument and configuration
####
sub check_args
{
        GetOptions(
                "t=s"   => \$TITLE,
		"c=s"   => \$CURVAL,
		"v=s"   => \$THRESVAL,
                "h!"    => \$HELP,
                "f=s"   => \$FROM,
                "d=s"   => \$TO,
                "s=s"   => \$SMTP,
		"b=s"   => \$HOSTPROG,
		"help!" => \$HELP,
        ) || &usage();
        &usage() if ($HELP);

	# Check configuration
	if (! -f $HOSTPROG) {
		print STDERR "ERROR: Can't find hostname program in $HOSTPROG.\n";
		&usage();
	}
	if (!$TITLE || ($CURVAL eq '') || ($THRESVAL eq '')) {
		print STDERR "ERROR: Can't compose message. Title, current value and thresold value are required.\n";
		&usage();
	}
	if (!$SMTP || !$FROM || !$TO) {
		print STDERR "ERROR: Can't send message. SMTP server, sender and destination address are required.\n";
		&usage();
	}
}
