#!/usr/bin/perl -w # # Nagios reporter # Modified by Felipe Ferreira 23/06/09 # # Fetches Nagios report from web, processes HTML/CSS and emails to someone # Written by Rob Moss, 2005-07-26, coding@mossko.com # # # Version 1.4 # use strict; use Getopt::Long; use Net::SMTP; use LWP::UserAgent; use Date::Manip; my $mailhost = 'localhost'; # Fill these in! my $maildomain = 'email.com'; # Fill these in! my $mailfrom = 'nagios@temail.com'; # Fill these in! my $mailto = 'fferreira@email.com'; # Fill these in! my $timeout = 30; my $mailsubject = 'Informe Nagios'; my $mailbody = ''; my $logfile ='/usr/local/nagios/var/mail.log'; #Where would you like your logfile to live? my $debug = 0; #Set the debug level to 1 or higher for information my $type = 'weekly'; my $repdateprev; my $reporturl; my $nagssbody; my $nagsssummary; my $webuser = 'nagiosadmin'; # Set this to a read-only nagios user (not nagiosadmin!) my $webpass = 'monitor03'; # Set this to a read-only nagios user (not nagiosadmin!) my $webbase = 'http://XXXX/nagios'; # Set this to the base of Nagios web page my $webcssembed = 1; GetOptions ( "debug=s" => \$debug, "help" => \&help, "type=s" => \$type, "email=s" => \$mailto, "embedcss" => \$webcssembed, ); if (not defined $type or $type eq "") { help(); exit; } elsif ($type eq "overnight") { report_overnight(); } elsif ($type eq "daily") { report_daily(); } elsif ($type eq "weekly") { report_weekly(); } elsif ($type eq "monthly") { report_monthly(); } else { die("Unknown report type $type\n"); } debug(1,"reporturl: [$reporturl]"); $mailbody = http_request($reporturl); if ($webcssembed) { # Stupid hacks for dodgy notes $nagssbody = http_request("$webbase/stylesheets/summary.css"); $nagsssummary = "\n"; $nagsssummary .= "\n"; $mailbody =~ s@@@; $mailbody =~ s@@$nagsssummary@; } open(FILE, "> /tmp/nagios-report-htmlout.html") or warn "can't open file /tmp/nagios-report-htmlout.html: $!\n"; print FILE $mailbody; close FILE; sendmail(); ############################################################################### sub help { print <<_END_; Nagios web->email reporter program. $0 --help This screen --email= Send to this address instead of the default address "$mailto" --type=overnight Overnight report, from 17h last working day to Today (9am) --type=daily Daily report, 08:00-16:00 yestarday (custom) --type=weekly Weekly report, last week (custom) --type=monthly Monthly report, 1st of prev month at 9am to last day of month, 9am --embedcss Downloads the CSS file and embeds it into the main HTML to enable Lotus Notes to work (yet another reason to hate Notes) _END_ exit 1; } ############################################################################### sub report_monthly { # This should be run on the 1st of every month $repdateprev = DateCalc("yesterday",1); debug(1,"repdateprev = $repdateprev"); # #2006072116:48:37 my ($repsday, $repsmonth, $repsyear, $repshour ) = 0; $repdateprev =~ /(\d\d\d\d)(\d\d)(\d\d)(.*)/; $repsday = 01; $repsmonth = $2; $repsyear = $1; $repshour = 0; my ($repeday, $repemonth, $repeyear, $repehour ) = 0; my $repdatenow = ParseDate("today"); debug(1,"repdatenow = $repdatenow"); $repdatenow =~ /(\d\d\d\d)(\d\d)(\d\d)(.*)/; $repeday = $3; $repemonth = $2; $repeyear = $1; $repehour = 0; $reporturl = "$webbase/cgi-bin/summary.cgi?report=1&displaytype=1&timeperiod=custom" . "&smon=$repsmonth&sday=$repsday&syear=$repsyear&shour=$repshour&smin=0&ssec=0" . "&emon=$repemonth&eday=$repeday&eyear=$repeyear&ehour=$repehour&emin=0&esec=0" . '&hostgroup=all&servicegroup=all&host=all&alerttypes=3&statetypes=2&hoststates=3&servicestates=56&limit=500'; $mailsubject = "Nagios alerts for month $repsmonth/$repsyear"; } ############################################################################### sub report_weekly { #Use TimePeriod LastWeek, #EDIT THIS LINE WITH CUSTOM URL! $reporturl = "$webbase/cgi-bin/summary.cgi?report=1&displaytype=3&timeperiod=lastweek&smon=6&sday=1&syear=2009&shour=0&smin=0&ssec=0&emon=6&eday=23&eyear=2009&ehour=24&emin=0&esec=0&hostgroup=DIBA_Win2k3_Exchange&servicegroup=Criticos&host=all&alerttypes=2&statetypes=2&hoststates=7&servicestates=32&limit=10"; $mailsubject = "Nagios Top alertas de la semana pasada"; } ############################################################################### sub report_daily { #Report for the day of yestarday, from 8h to 16h $repdateprev = Date_PrevWorkDay("today",1); debug(1,"repdateprev = $repdateprev"); #2006072116:48:37 my ($repsday, $repsmonth, $repsyear, $repshour ) = 0; $repdateprev =~ /(\d\d\d\d)(\d\d)(\d\d)(.*)/; $repsday = $3; $repsmonth = $2; $repsyear = $1; $repshour = 8; my ($repeday, $repemonth, $repeyear, $repehour ) = 0; # my $repdatenow = ParseDate("today"); # debug(1,"repdatenow = $repdatenow"); # $repdatenow =~ /(\d\d\d\d)(\d\d)(\d\d)(.*)/; $repeday = $3; $repemonth = $2; $repeyear = $1; $repehour = 16; $reporturl = "$webbase/cgi-bin/summary.cgi?report=1&displaytype=3&timeperiod=custom" . "&smon=$repsmonth&sday=$repsday&syear=$repsyear&shour=$repshour&smin=0&ssec=0" . "&emon=$repemonth&eday=$repeday&eyear=$repeyear&ehour=$repehour&emin=0&esec=0" . '&hostgroup=DIBA_Win2k3_Exchange&servicegroup=Criticos&host=all&alerttypes=2&statetypes=2&hoststates=7&servicestates=32&limit=10'; $mailsubject = "SLA Nagios alerts de ayer de las 8h a las 16h"; } ############################################################################### sub report_overnight { $repdateprev = Date_PrevWorkDay("today",1); debug(1,"repdateprev = $repdateprev"); #2006072116:48:37 my ($repsday, $repsmonth, $repsyear, $repshour ) = 0; $repdateprev =~ /(\d\d\d\d)(\d\d)(\d\d)(.*)/; $repsday = $3; $repsmonth = $2; $repsyear = $1; $repshour = 17; my ($repeday, $repemonth, $repeyear, $repehour ) = 0; my $repdatenow = ParseDate("today"); debug(1,"repdatenow = $repdatenow"); $repdatenow =~ /(\d\d\d\d)(\d\d)(\d\d)(.*)/; $repeday = $3; $repemonth = $2; $repeyear = $1; $repehour = 9; $reporturl = "$webbase/cgi-bin/summary.cgi?report=1&displaytype=1&timeperiod=custom" . "&smon=$repsmonth&sday=$repsday&syear=$repsyear&shour=$repshour&smin=0&ssec=0" . "&emon=$repemonth&eday=$repeday&eyear=$repeyear&ehour=$repehour&emin=0&esec=0" . '&hostgroup=all&servicegroup=all&host=all&alerttypes=3&statetypes=2&hoststates=3&servicestates=56&limit=500'; $mailsubject = "Nagios overnight alerts from $repsday/$repsmonth/$repsyear ${repshour}h to present"; } ############################################################################### 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', 'Content_Base' => $webbase, ); # 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 $smtp = Net::SMTP->new( $mailhost, Hello => $maildomain, Timeout => $timeout, Debug => $debug, ); $smtp->mail($mailfrom); $smtp->to($mailto); $smtp->data(); $smtp->datasend("To: $mailto\n"); $smtp->datasend("From: $mailfrom\n"); $smtp->datasend("Subject: $mailsubject\n"); $smtp->datasend("MIME-Version: 1.0\n"); $smtp->datasend("Content-type: multipart/mixed; boundary=\"boundary\"\n"); $smtp->datasend("\n"); $smtp->datasend("This is a multi-part message in MIME format.\n"); $smtp->datasend("--boundary\n"); $smtp->datasend("Content-type: text/html\n"); $smtp->datasend("Content-Disposition: inline\n"); $smtp->datasend("Content-Description: Nagios report\n"); $smtp->datasend("$mailbody\n"); $smtp->datasend("--boundary\n"); $smtp->datasend("Content-type: text/plain\n"); $smtp->datasend("Please read the attatchment\n"); $smtp->datasend("--boundary--\n"); $smtp->dataend(); $smtp->quit; }