Emails récapitulatif des alertes Nagios en cours

From Deimos.fr / Bloc Notes Informatique
Jump to: navigation, search
Nagios

Software version Core 3.2.1
Operating System Debian 6
Website Nagios Website
Last Update 02/08/2013
Others

1 Introduction

J'ai longtemps utilisé cet outil trouvé sur le net, puis repris conjointement avec un ex collègue qui permet de recevoir un email en fonction d'alertes de Nagios. Nous l'utilisions pour recevoir, avant d'arriver au taf ces emails, pour savoir quels sont les problèmes avant d'arriver. Cela permet d'arriver soit en courant, soit serein :-)

C'est un script qui va parser l'interface web graphique de Nagios, récupérer le minimum d'informations pour un bon affichage sur smartphone et envoyer un mail. Il suffit de mettre ceci en crontab pour recevoir ça le matin ou quant vous le souhaitez.

2 Installation

Il va falloir avoir Perl et certaines dépendances :

Command aptitude
aptitude install liblwp-useragent-determined-perl libemail-sender-perl

3 Configuration

Configuration File /usr/local/bin/morningchecks.pl
#!/usr/bin/perl -w
# Inspired by Rob Moss, 2005-07-26, coding@mossko.com
# Created by Charles-Henri TURPIN
# Modified by Pierre Mavro
 
###########################################################################################
 
my $mailsmtp	=	'smtp.deimos.fr';    #	Fill these in!my $mailfrom    =       'deimos@deimos.fr';  #   Coming from abovemy $mailto	=	'deimos@deimos.fr';  # Set defautls receiversmy $mailsubject	=	'Morning Checks'; 
###########################################################################################
 
package Service;
sub const
{
	my ($classe, $hostname, $name, $duration, $message, $color) = @_; #la fonction reçoit comme premier paramètre le nom de la classe
   	my $this = {"hostname" => "hostname",
		   "name" => "name",
		   "duration" => "duration",
		   "message" => "message",
		   "color" => "color",};
 
	$this->{"hostname"} = $hostname if defined $hostname;
	$this->{"name"} = $name if defined $name;
	$this->{"duration"} = $duration if defined $duration;
	$this->{"message"} = $message if defined $message;
	$this->{"color"} = $color if defined $color;
 
 
   	bless ($this,$classe); #lie la référence à la classe
   	return $this; #on retourne la référence consacrée
}
 
sub DESTROY
{
	return 0;
} 
1;
 
use strict;
use Getopt::Long;
use LWP::UserAgent;
use Mail::Sender;
$Mail::Sender::NO_X_MAILER = 1;
 
 
my @services = ();
my $mailbody	=	'';
my @finalmailbody = (
	'<html><head><style type="text/css">
    .titre {
	    color: #FFFFFF;
	    background-color: #357AB7;
	    font-size: 12;
    }
    .titre td{
	border: 2px solid #000000;
    }
    .el{
	font-size: 12;
    }
    .confluence{
	font-size: 10;
    }
    </style></head><body>'
);
 
my $debug		=	0;									#	Set the debug level to 1 or higher for informationmy $webuser		=	'';							#	Set this to a read-only nagios user (not nagiosadmin!)my $webpass		=	'';							#	Set this to a read-only nagios user (not nagiosadmin!)my $full;
my $reporturl;
my @nagiosnames;
 
 
&GetOptions (
	"debug=s"	=>	\$debug,
	"help"		=>	\&help,
	"email=s"	=>	\$mailto,
	"names=s"	=>	\@nagiosnames,
	"full"		=>	\$full
);
 
if(!defined($nagiosnames[0]))
{
    @nagiosnames = ('nagios-prod');}
program(@nagiosnames);
################################ FUNCTIONS #######################################
sub program
{
    my @nagiosnames = @_;
 
    foreach my $nagiosname (@nagiosnames)
    {
	launch("$nagiosname");
	main();
	check();
    }
    system("touch /tmp/nagios-report-htmlout.html; chmod 666 /tmp/nagios-report-htmlout.html");    
 
    push @finalmailbody, '</body></html>';
    sendmail();
    system("rm /tmp/nagios-report-htmlout.html");
}
sub launch
{
    my $nagiosname = shift;
    my $title = "<p align=\"center\"><b>".uc($nagiosname)."</b></p><hr color=\"black\" align=\"center\" width=\"80%\"/>";
    push @finalmailbody, $title;
    #-- 20110926 - Match the monitoring screen's request
    #$reporturl = "http://$nagiosname/cgi-bin/nagios3/status.cgi?host=all&servicestatustypes=28&hoststatustypes=3&serviceprops=42&sorttype=1&sortoption=6&noheader";
    if($nagiosname =~ /internal/)
    {
        $reporturl = "http://$nagiosname/cgi-bin/nagios3/status.cgi?hostgroup=prod-srv&style=detail&servicestatustypes=28&serviceprops=8&sorttype=1&sortoption=6&noheader";
    }
    else
    {
        $reporturl = "http://$nagiosname/cgi-bin/nagios3/status.cgi?style=detail&servicestatustypes=28&serviceprops=8&sorttype=1&sortoption=6&noheader";
    }
	# Get all status
	if ($full)
	{
		$reporturl="http://$nagiosname/cgi-bin/nagios3/status.cgi?host=all&sorttype=2&sortoption=3";
	}
}
###############################################################################
sub main
{
    debug(1,"reporturl: [$reporturl]");
 
    $mailbody = http_request($reporturl);
 
    open(FILE, "> /tmp/nagios-report-htmlout.html") or warn "can't open file /tmp/nagios-report-htmlout.html: $!\n";
 
    #print "DEBUG $mailbody\n\n\n\n";
    print FILE $mailbody;
    close FILE;
}
###############################################################################
sub check
{
    open(FILE, "</tmp/nagios-report-htmlout.html");
    my $gotit = 0; # = 1 while having a service
    my $service;
    while(<FILE>)
    {
	chomp $_;
	my $line = $_;
 
	if($line =~ /\&host=(.*?)&service=(.*?)\'/)
	{
	    #print "DEBUG ENTERS !!!\n";
	    $service = Service->const();
	    $service->{hostname} = $1;
	    $service->{name} = $2;
	    $service->{name} =~ s/\+/ /g;
	    $service->{name} =~ s/\%2F/\//g;
	    $service->{name} =~ s/\%3A/:/g;
	}
	elsif($line =~ /CLASS=\'statusUNKNOWN/)
	{
	    #print "DEBUG UNKNOWN !!\n";
		$service->{color} = "FFDA9F";
		$gotit =1;
	}
	elsif($line =~ /CLASS=\'statusWARNING/)
	{
	    #print "DEBUG WARNING !!\n";
		$service->{color} = "FEFFC1";
		$gotit =1;
	}
	elsif($line =~ /CLASS=\'statusOK/)
	{
		if ($full)
		{
	    	#print "DEBUG WARNING !!\n";
			$service->{color} = "99FF99";
			$gotit =1;
		}
	}
	elsif($line =~ /CLASS=\'statusCRITICAL/)
	{
	    #print "DEBUG CRITICAL !!\n";
		$service->{color} = "FFBBBB";
		$gotit =1;
	}
	elsif($line =~ /\'center\'\>(.*?)\</)
	{
	    #print "DEBUG MESSAGE !!\n";
		$service->{message} = $1;
	}
	elsif($line =~ /\<\/TR\>/)
	{
	    if($gotit == 1)
	    {
		#print "DEBUG PUSH ! $service->{color}, $service->{name}, $service->{message}, $service->{hostname}, $service->{duration}\n";
		push(@services, $service);
		$gotit = 0;
	    }
	}
	elsif($line =~ /\>\s*(\d+d\s*\d+h\s*\d+m\s*\d+s)/)
	{
	    #print "DEBUG DURATION !!\n";
		$service->{duration} = $1;
	}
    }
    close(FILE);
 
    #### mailbody creation ####
 
    push @finalmailbody, '<table width="100%"><tr class="titre"><td >Hostname</td><td>Service</td><td>Duration</td><td>Status</td></tr>';
    foreach my $el (@services)
    {
	#print "DEBUG  HAD A SERVICE !\n";
	push @finalmailbody, "<tr class=\"el\" bgcolor=\"#$el->{color}\"><td>$el->{hostname}</td><td bgcolor=\"#$el->{color}\">$el->{name}</td><td>$el->{duration}</td><td>$el->{message}</td></tr>\n";
    }
    push @finalmailbody, '</table>';
 
    $mailbody = ''; #Resetting values for next times
    @services = (); #Resetting values for next times
}
 
###############################################################################
sub help {
print <<_END_;
 
Nagios web->email reporter program.
 
$0 <args>
 
--help
	This screen
 
--email=<email>
	Send to this address instead of the default address
	"$mailto"
 
-names
    Names of the nagios servers you want to check (multiple names is ok)
 
-full
	Do not send only unhandled alerts but all (even ok)
 
_END_
 
exit 1;
 
}
 
###############################################################################
sub http_request {
	my $ua;
	my $req;
	my $res;
 
	my $geturl = shift;
	if (not defined $geturl or $geturl eq "") {
		warn "No URL defined for http_request\n";
		return 0;
	}
	$ua = LWP::UserAgent->new;
	$ua->agent("Nagios Report Generator " . $ua->agent);
	$req = HTTP::Request->new(GET => $geturl);
	$req->authorization_basic($webuser, $webpass);
	$req->header(	'Accept'			=>	'text/html',
				);
 
	# send request
	$res = $ua->request($req);
 
	# check the outcome
	if ($res->is_success) {
		debug(1,"Retreived URL successfully");
		return $res->decoded_content;
	}
	else {
		print "Error: " . $res->status_line . "\n";
		return 0;
	}
}
 
###############################################################################
sub debug {
	my ($lvl,$msg) = @_;
	if ( defined $debug and $lvl <= $debug ) {
		chomp($msg);
		print localtime(time) .": $msg\n";
	}
	return 1;
}
 
###############################################################################
sub sendmail {
	my $sender = new Mail::Sender {
		smtp => "$mailsmtp",
		from => "$mailfrom",
		on_errors => 'die',
	};
	$sender->Open({
		to => "$mailto",
	    subject => $mailsubject,
	    ctype => "text/html",
	    encoding => "quoted-printable"
	}) or die $Mail::Sender::Error,"\n";
 
	my $body_line;	
	foreach $body_line (@finalmailbody)
	{
		$sender->SendEnc($body_line);
	};
	$sender->Close();
}
 
###############################################################################

4 References

Ajoutez ensuite les droits d'exécution.

5 Utilisation

Pour l'utilisation, c'est relativement simple, il faut renseigner les informations présentes dans le script si vous souhaitez avoir des valeurs par défaut. Ensuite il est possible de spécifier en paramètre ces mêmes options ou non :

Command morningchecks.pl
/usr/local/bin/morningchecks.pl -names nagios-prod
/usr/local/bin/morningchecks.pl -full -names nagios-prod --email=deimos@deimos.fr

Consultez également l'aide pour plus d'informations :

Command morningchecks.pl
> /usr/local/bin/morningchecks.pl --help
 
Nagios web->email reporter program.
 
/usr/local/bin/morningchecks.pl <args>
 
--help
	This screen
 
--email=<email>
	Send to this address instead of the default address
	"deimos@deimos.fr"
 
-names
    Names of the nagios servers you want to check (multiple names is ok)
 
-full
	Do not send only unhandled alerts but all (even ok)