#!/usr/bin/perl
#------------------------------------------------------------------------------
#    sysusage - Full system monitoring with RRDTOOL
#    Copyright (C) 2003-2009 Gilles Darold
#
#    This program is provided WITHOUT WARRANTY of any kind, either
#    expressed or implied. It is free software, and you are welcome
#    to modify or re-distribute it under same terms of Perl itself.
#
# Author: Gilles Darold <gilles@darold.net>
#
#------------------------------------------------------------------------------
#
# This script is used to send thresold alarmes received from
# the sysusage report tool.
#
#------------------------------------------------------------------------------
use strict;

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

my $VERSION = '2.10';

my $SMTP     = '';
my $FROM     = '';
my $TO       = '';
my $HOSTPROG = '/bin/hostname';
my $TITLE    = '';
my $CURVAL   = '';
my $THRESVAL = '';
my $HELP     = '';
my $TIMEOUT  = 30;
my $NAGIOS   = '';
my $WARN_LEVEL  = 0;
my $SERVICE  = '';

# Nagios alarm level
my %LEVEL = (
	3 => 'urgent', 
	2 => 'critical',
	1 => 'warning',
	0 => 'information'
);
# Get information received from commande line
&check_args();


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

# Send email if there's a SMTP server to join
if ($SMTP && $FROM && $TO) {
	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: sysusagewarn\n");
	$smtp->datasend("X-Alarm-Context: system\n");
	$smtp->datasend("X-Alarm-Priority: $LEVEL{$WARN_LEVEL}\n");
	$smtp->datasend("X-Alarm-Source: $hostname\n");
	$smtp->datasend("X-Alarm-Protocol: posix\n");
	$smtp->datasend("X-Alarm-Application: sysusage\n");
	$smtp->datasend("X-Alarm-Version: $VERSION\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;
}

if ($NAGIOS) {
	my $txt = "$TITLE (currrent: $CURVAL, threshold: $THRESVAL)";
	$txt = $TITLE if ($WARN_LEVEL == 0);
	system("$NAGIOS $hostname \"$SERVICE\" $WARN_LEVEL \"[SysUsage] $txt\" > /dev/null 2>&1");
}

exit 0;


####
# Function used to return command line usage 
####
sub usage
{
	print qq{
usage: $0 -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 value : Current value monitored by sysusage.pl
 -v 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 : Destination email address the alarm message. Default root\@localhost
 -b path : Path to program hostname. Default is /bin/hostname
 -n path  : Path to Nagios program submit_check_result. Default is none. 
 -l value : Alarm level type (0=OK,1=WARNING,2=CRITICAL,3=UNKNOWN). Default: 1. 
 -r service : Nagios service name to used. Must be any sysusage target type of
              monitoring defined in the configuration file.
 -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,
		"n=s"   => \$NAGIOS,
		"l=s"   => \$WARN_LEVEL,
		"r=s"   => \$SERVICE,
		"help!" => \$HELP,
        ) || &usage();
        &usage() if ($HELP);

	# Check configuration
	if (! -f $HOSTPROG) {
		print STDERR "ERROR: Can't find hostname program in $HOSTPROG.\n";
		&usage();
	} elsif (!-x $HOSTPROG) {
		print STDERR "ERROR: Can't execute $HOSTPROG.\n";
		&usage();
	}
	if ($NAGIOS && ! -x $NAGIOS) {
		print STDERR "ERROR: Can't execute $NAGIOS.\n";
		&usage();
	}
	if ($NAGIOS && !$SERVICE) {
		print STDERR "WARNING: No Nagios service given. Set it to sysusage\n";
		$SERVICE = 'sysusage';
	}
	if (!$TITLE) {
		print STDERR "ERROR: Can't compose message. Title is required.\n";
		&usage();
	}
	if (($CURVAL eq '') || ($THRESVAL eq '')) {
		if (!$NAGIOS || $WARN_LEVEL) {
			print STDERR "ERROR: Can't compose message. Current value and thresold value are required.\n";
			&usage();
		}
	}
	if (!$NAGIOS && (!$SMTP || !$FROM || !$TO)) {
		print STDERR "ERROR: Can't send message. SMTP server, sender and destination address are required.\n";
		&usage();
	}
	if ($SMTP && (!$FROM || !$TO)) {
		print STDERR "ERROR: Can't send message. sender and destination SMTP address are required.\n";
		&usage();
	}
	$WARN_LEVEL = 1 if ($WARN_LEVEL eq '');
}
