View Full Version : Gestione notifiche via mail con Nagios
Ho già configurato Nagios in modo che mi arrivino notifiche via mail quando un servizio va in stato critico.
Ora mi servirebbe questo:
mandare una mail ad un orario determinato del giorno con la situazione istantanea di tutti i host/servizi sia in stato Critical, Warning e Ok.
E' possibile?
So che basterebbe aprire l'interfaccia web del programma per avere subito la situazione sotto gli occhi ma a me servirebbe mandare una mail riassuntiva di tutti i servizi.
Grazie.
Gimli[2BV!2B]
18-05-2011, 20:26
Hai visto la sezione exchange del sito ufficiale (http://exchange.nagios.org/)?
In particolare direi che la categoria Reporting (http://exchange.nagios.org/directory/Addons/Reporting) dovrebbe fare al caso tuo.
Di questi quello che, da non utilizzatore Nagios, mi sembra più specializzato al solo invio di brevi report via mail a tempi prefissati è Nagios Email Reporter (http://exchange.nagios.org/directory/Addons/Reporting/Nagios-Email-Reporter/details).
Ha anche un'opzione per integrare il css nel corpo della mail per soddisfare client email un po' particolari come Lotus Notes.
Certo, come precisato, è un script privo di installazione da editare per impostare i propri parametri, ma il codice mi sembra ordinato e ben scritto.
Grazie mille!
Mi ero calato in vari tentativi e non avevo pensato di guardare lì.
Ora modifico lo script secondo le mie esigenze dato che non soddisfa al 100% le richieste a cui devo rispondere.
Dopo un opportuno settaggio da linea di comando faccio:
./nagios-reporter.v131.pl --email=nome@xxx.it --type=daily
e mi arriva puntuale la mail.
Ora non mi resta che ordinare l'esecuzione script ad un determinato orario del giorno.
(cosa che devo ancora capire...)
Grazie mille ancora!
Gimli[2BV!2B]
19-05-2011, 20:00
Perfetto, vengono bene le mail? Potrei anche suggerirlo all'ufficio IT, mi pare di ricordare che da qualche tempo stanno utilizzando Nagios e ne sembrano soddisfatti.
Per pianificare l'esecuzione, solitamente, si utilizza il demone "del tempo" cron, impostando opportunamente crontab (http://www.manpagez.com/man/5/crontab/).
Scusa il ritardo.
Le notifiche che arrivano via mail risultano incolonnate bene ma sono assenti gli effetti grafici in alcune. Che sia un problema di CSS?
Ecco le foto così puoi farti un'idea.
http://img269.imageshack.us/img269/267/nagios1.jpg (http://imageshack.us/photo/my-images/269/nagios1.jpg/)
http://img855.imageshack.us/img855/6166/nagios2.jpg (http://imageshack.us/photo/my-images/855/nagios2.jpg/)
http://img195.imageshack.us/img195/6506/nagios3.jpg (http://imageshack.us/photo/my-images/195/nagios3.jpg/)
Gimli[2BV!2B]
23-05-2011, 19:07
Prova ad utilizzare l'opzione --embedcss dello script, anche GMail scavalca i css non in linea come Lotus Notes.
Avvevo provato anche con quello ma stesso risultato... :cry:
Gimli[2BV!2B]
24-05-2011, 22:31
Nel mio Nagios ho notato che l'indirizzo relativo dei css è leggermente diverso rispetto a quello presente nelle espressioni regolari dello script.
Prova a modificare le righe in grassetto:if ($webcssembed) {
# Stupid hacks for dodgy notes
$nagssbody = http_request("$webbase/stylesheets/summary.css");
$nagsssummary = "<style type=\"text\/css\">\n";
foreach ( split(/\n/,$nagssbody) ) {
chomp;
if (not defined $_ or $_ eq "" ) {
next;
}
$nagsssummary .= "<!-- $_ -->\n";
}
$nagsssummary .= "</style>\n";
$nagsssummary .= "<base href=\"$webbase/cgi-bin/\">\n";
$mailbody =~ s@<LINK REL=\'stylesheet\' TYPE=\'text/css\' HREF=\'/stylesheets/common.css\'>@@;
$mailbody =~ s@<LINK REL=\'stylesheet\' TYPE=\'text/css\' HREF=\'/stylesheets/summary.css\'>@$nagsssummary@;
}In questo modo:if ($webcssembed) {
# Stupid hacks for dodgy notes
$nagssbody = http_request("$webbase/stylesheets/summary.css");
$nagsssummary = "<style type=\"text\/css\">\n";
foreach ( split(/\n/,$nagssbody) ) {
chomp;
if (not defined $_ or $_ eq "" ) {
next;
}
$nagsssummary .= "<!-- $_ -->\n";
}
$nagsssummary .= "</style>\n";
$nagsssummary .= "<base href=\"$webbase/cgi-bin/\">\n";
$mailbody =~ s@<LINK REL=\'stylesheet\' TYPE=\'text/css\' HREF=\'/nagios/stylesheets/common.css\'>@@;
$mailbody =~ s@<LINK REL=\'stylesheet\' TYPE=\'text/css\' HREF=\'/nagios/stylesheets/summary.css\'>@$nagsssummary@;
}Se non dovesse funzionare controlla cosa c'è scritto nei LINK REL='stylesheet' della mail che ricevi e/o il contenuto del blocco <style type="text/css"></style>, ovvero gli stili embedded generati.
Non funziona lo stesso.
In una mail non c'è il LINK REL='stylesheet' ed il contenuto del blocco <style type="text/css"></style> è:
<style type="text/css">
<!-- .summary { font-family: arial,serif; background-color: white; color: black; font-size: 10pt; } -->
<!-- .errorMessage { font-family: arial,serif; text-align: center; color: red; font-weight: bold; font-size: 12pt; } -->
<!-- .errorDescription { font-family: arial,serif; text-align: center; font-weight: bold; font-size: 12pt; } -->
<!-- .warningMessage { font-family: arial,serif; text-align: center; color: red; font-weight: bold; font-size: 10pt; } -->
<!-- .infoBox { font-family: arial,serif; font-size: 8pt; background-color: #C4C2C2; padding: 2; } -->
<!-- .infoBoxTitle { font-family: arial,serif; font-size: 10pt; font-weight: bold; } -->
<!-- .infoBoxBadProcStatus { font-family: arial,serif; color: red; } -->
<!-- A.homepageURL:Hover { font-family: arial,serif; color: red; } -->
<!-- .linkBox { font-family: arial,serif; font-size: 8pt; background-color: #DBDBDB; padding: 1; } -->
<!-- .reportRange { font-family: arial,serif; text-align: center; font-weight: bold; font-size: 10pt; } -->
<!-- .reportDuration { font-family: arial,serif; text-align: center; font-size: 8pt; } -->
<!-- .reportTime { font-family: arial,serif; text-align: center; font-size: 8pt; } -->
<!-- .reportDataEven { font-family: arial,serif; font-size: 8pt; background-color: #B4B5CC; padding: 2; } -->
<!-- .reportDataOdd { font-family: arial,serif; font-size: 8pt; background-color: #CDCEE9; padding: 2; } -->
<!-- .dataTitle { font-family: arial,serif; text-align: center; font-weight: bold; font-size: 12pt; } -->
<!-- .dataSubTitle { font-family: arial,serif; text-align: center; font-weight: bold; font-size: 10pt; } -->
<!-- TABLE.data { font-family: arial,serif; font-size: 10pt; background-color: white; padding: 2; } -->
<!-- TH.data { font-family: arial,serif; font-size: 10pt; background-color: white; text-align: left; background-color: #999797; color: #DCE5C1; } -->
<!-- .dataOdd { font-family: arial,serif; font-size: 9pt; background-color: #DBDBDB; } -->
<!-- .dataEven { font-family: arial,serif; font-size: 9pt; background-color: #C4C2C2; } -->
<!-- .hostUP { font-family: arial,serif; font-size: 9pt; text-align: left; background-color: #33FF00; } -->
<!-- .hostDOWN { font-family: arial,serif; font-size: 9pt; text-align: left; background-color: #F83838; } -->
<!-- .hostUNREACHABLE { font-family: arial,serif; font-size: 9pt; text-align: left; background-color: #F83838; } -->
<!-- .serviceOK { font-family: arial,serif; font-size: 9pt; text-align: left; background-color: #33FF00; } -->
<!-- .serviceWARNING { font-family: arial,serif; font-size: 9pt; text-align: left; background-color: #FFFF00; } -->
<!-- .serviceUNKNOWN { font-family: arial,serif; font-size: 9pt; text-align: left; background-color: #FF9900; } -->
<!-- .serviceCRITICAL { font-family: arial,serif; font-size: 9pt; text-align: left; background-color: #F83838; } -->
<!-- .optBox { font-family: arial narrow,serif; font-size: 10pt; font-weight: bold; } -->
<!-- .optBoxTitle { font-family: arial,serif; font-weight: bold; font-size: 10pt; } -->
<!-- .optBoxItem { font-family: arial,serif; font-size: 8pt; font-weight: bold; } -->
<!-- .optBoxValue { font-family: arial,serif; font-size: 8pt; font-style: italic; } -->
<!-- .helpfulHint { font-family: arial,serif; text-align: left; font-size: 8pt; font-style: italic; text-align: center; } -->
<!-- .dateSelectTitle { font-family: arial,serif; text-align: center; font-weight: bold; font-size: 12pt; } -->
<!-- .dateSelectSubTitle { font-family: arial,serif; text-align: left; font-weight: bold; font-size: 10pt; } -->
<!-- .dateSelectItem { font-family: arial,serif; text-align: left; font-size: 8pt; } -->
<!-- .reportSelectTip { font-family: arial,serif; text-align: left; font-size: 8pt; font-style: italic; } -->
<!-- .reportSelectTitle { font-family: arial,serif; text-align: center; font-weight: bold; font-size: 12pt; } -->
<!-- .reportSelectSubTitle { font-family: arial,serif; text-align: right; font-size: 10pt; } -->
<!-- .reportSelectItem { font-family: arial,serif; text-align: left; font-size: 8pt; } -->
</style>
Mentre in un'altra mail è presente soltanto il LINK REL='stylesheet'
<LINK REL='stylesheet' TYPE='text/css' HREF='/nagios/stylesheets/status.css'>
Mentre nel cestino avevo ancora una mail con 2 LINK REL (precedente alla modifica)
<LINK REL='stylesheet' TYPE='text/css' HREF='/nagios/stylesheets/common.css'>
<LINK REL='stylesheet' TYPE='text/css' HREF='/nagios/stylesheets/summary.css'>
Gimli[2BV!2B]
25-05-2011, 22:27
Il risultato desiderato è il primo, ma, a quanto pare, non è sufficiente.
Lo temevo, gli stili devono essere tutti in linea negli elementi a cui sono relativi per scavalcare quelli delle interfacce webmail più diffuse.
Ho aggiunto l'opzione --inlinecss che, usando il modulo Python pynliner (http://pypi.python.org/pypi/pynliner): applica gli stili del CSS a tutti gli elementi inserendoli nell'attributo style (posizione con massima priorità).
Il fatto di aver dovuto utilizzare Python non mi esalta molto, ma quel modulo è quello che mi ha convinto maggiormente tre le opzioni che ho rintracciato in rete.
Se pynliner non risultasse disponibile nei repository è possibile installarlo con il comando easy_install pynliner (con diritti root, preferibilmente).#!/usr/local/bin/perl -w
#
# Nagios overnight/daily/weekly/monthly reporter
#
# Fetches Nagios report from web, processes HTML/CSS and emails to someone
# Written by Rob Moss, 2005-07-26, coding@mossko.com
# Little extension by Gimli, 2011-05-25
#
# Use at your own risk, knoweledge of perl required.
#
# Version 1.3.1
# - Overnight, Daily, Weekly, Monthly reports
# Version 1.3.1B
# - New option --inlinecss
#
use strict;
use Getopt::Long;
use Net::SMTP;
use LWP::UserAgent;
use Date::Manip;
my $mailhost = 'maiserver.domain.com'; # Fill these in!
my $maildomain = 'domain.com'; # Fill these in!
my $mailfrom = 'me@mycompany.com'; # Fill these in!
my $mailto = 'me@mycompany.com'; # Fill these in!
my $timeout = 30;
my $mailsubject = '';
my $mailbody = '';
my $logfile = '/usr/local/nagios/var/mail.log'; # Where would you like your logfile to live?
my $debug = 1; # Set the debug level to 1 or higher for information
my $type = '';
my $repdateprev;
my $reporturl;
my $nagssbody;
my $nagsssummary;
my $webuser = 'nagios'; # Set this to a read-only nagios user (not nagiosadmin!)
my $webpass = 'password'; # Set this to a read-only nagios user (not nagiosadmin!)
my $webbase = 'http://nagios-server.blah/nagios'; # Set this to the base of Nagios web page
my $webcssembed = 0;
my $webcssinline = 0;
GetOptions (
"debug=s" => \$debug,
"help" => \&help,
"type=s" => \$type,
"email=s" => \$mailto,
"embedcss" => \$webcssembed,
"inlinecss" => \$webcssinline,
);
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( $mailbody eq 0 ) {
exit 1;
}
# Remove HTML comments: avoid problems with Pynliner if $webcssinline == 1
$mailbody =~ s/<!--(.|\s)*?-->//g;
if ($webcssembed || $webcssinline) {
# --inlinecss depends on --embedcss
debug(1,"Embed CSS!");
# Stupid hacks for dodgy notes
$nagssbody = http_request("$webbase/stylesheets/summary.css");
if( $nagssbody eq 0 ) {
exit 1;
}
$nagsssummary = "<style type=\"text\/css\">\n";
foreach ( split(/\n/,$nagssbody) ) {
chomp;
if (not defined $_ or $_ eq "" ) {
next;
}
$nagsssummary .= "<!-- ";
$nagsssummary .= lc($_);
$nagsssummary .= " -->\n";
}
$nagsssummary .= "</style>\n";
$nagsssummary .= "<base href=\"$webbase/cgi-bin/\">\n";
$mailbody =~ s@<LINK REL=\'stylesheet\' TYPE=\'text/css\' HREF=\'/nagios/stylesheets/common.css\'>@@;
$mailbody =~ s@<LINK REL=\'stylesheet\' TYPE=\'text/css\' HREF=\'/nagios/stylesheets/summary.css\'>@$nagsssummary@;
}
my $output = "/tmp/nagios-report-htmlout.html";
open(FILE, "> $output") or warn "can't open file $output: $!\n";
if ($webcssinline) {
debug(1,"Inline CSS!");
close FILE;
chomp(my $Python = `which python`);
debug(1,"Inline CSS: python executable -> $Python");
if ( $Python eq "" ) {
debug(0,"Inline CSS: python executable not found!");
exit 1;
}
else {
# Escape some problematic characters
$mailbody =~ s/\n//g;
$mailbody =~ s/\r//g;
$mailbody =~ s/'/\\'/g;
$mailbody =~ s/"/\\"/g;
my $Script = "import pynliner; output = pynliner.fromString('$mailbody'); print(output);";
my $status = system "$Python -c \"$Script\" >$output";
if( $status != 0 ) {
debug(0,"Inline CSS: Python error.");
debug(0," Is Python module \"Pynliner\" installed?");
debug(0," http://pypi.python.org/pypi/pynliner");
}
}
}
else {
print FILE $mailbody;
close FILE;
}
sendmail();
###############################################################################
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"
--type=overnight
Overnight report, from 17h last working day to Today (9am)
--type=daily
Daily report, 09:00 last working day to Today (9am)
--type=weekly
Weekly report, 9am 7 days ago, until 9am today (run at 9am friday!)
--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)
--inlinecss
Downloads the CSS file and insert all the properties into inline
styles of the relative elements. This force all the webmails to not
override any CSS attribute.
-> Selects --embedcss.
-> Depends on Python and Python module Pynliner:
http://pypi.python.org/pypi/pynliner
_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 {
# This should be run on Friday, 9am
$repdateprev = Date_PrevWorkDay("today",5);
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 = 9;
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 alerts for week ending $repsday/$repsmonth/$repsyear";
}
###############################################################################
sub report_daily {
$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 = 7;
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 = 7;
$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 24 hours $repsday/$repsmonth/$repsyear ${repshour}h to present";
}
###############################################################################
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;
}
Nel codice per generare la mail mi dice questo:
Thu May 26 15:24:55 2011: Retreived URL successfully
Thu May 26 15:24:55 2011: Embed CSS!
Thu May 26 15:24:55 2011: Retreived URL successfully
Thu May 26 15:24:55 2011: Inline CSS!
Thu May 26 15:24:55 2011: Inline CSS: python executable -> /usr/bin/python
Traceback (most recent call last):
File "<string>", line 1, in ?
File "/usr/lib/python2.4/site-packages/pynliner/__init__.py", line 187, in fromString
return Pynliner().from_string(string).run()
File "/usr/lib/python2.4/site-packages/pynliner/__init__.py", line 85, in run
self._get_styles()
File "/usr/lib/python2.4/site-packages/pynliner/__init__.py", line 105, in _get_styles
self._get_external_styles()
File "/usr/lib/python2.4/site-packages/pynliner/__init__.py", line 123, in _get_external_styles
url = self.root_url + url
AttributeError: 'Pynliner' object has no attribute 'root_url'
Thu May 26 15:24:55 2011: Inline CSS: Python error.
Thu May 26 15:24:55 2011: Is Python module "Pynliner" installed?
Thu May 26 15:24:55 2011: http://pypi.python.org/pypi/pynliner
Net::SMTP>>> Net::SMTP(2.29)
Net::SMTP>>> Net::Cmd(2.26)
Net::SMTP>>> Exporter(5.58)
Net::SMTP>>> IO::Socket::INET(1.29)
Net::SMTP>>> IO::Socket(1.29)
Net::SMTP>>> IO::Handle(1.25)
Spero che non sia dovuto al fatto che ho la versione 2.4
Inoltre da yum non mi vede la 2.6
Gimli[2BV!2B]
26-05-2011, 20:29
Ho visto quell'errore se nel file HTML erano presenti LINK REL='stylesheet'.
In quel caso il modulo cerca di scaricarli, ma non sa dove andare a trovarli, visto che gli passo una stringa: root_url è nullo.
Visto che l'errore lo riconosco escluderei un problema di versione.
Ho reso più aggressiva la rimozione dei LINK REL:#!/usr/local/bin/perl -w
#
# Nagios overnight/daily/weekly/monthly reporter
#
# Fetches Nagios report from web, processes HTML/CSS and emails to someone
# Written by Rob Moss, 2005-07-26, coding@mossko.com
# Little extension by Gimli, 2011-05-26
#
# Use at your own risk, knoweledge of perl required.
#
# Version 1.3.1
# - Overnight, Daily, Weekly, Monthly reports
# Version 1.3.1B
# - New option --inlinecss
# Version 1.3.1C
# - Modified regular expressions for --embedcss
#
use strict;
use Getopt::Long;
use Net::SMTP;
use LWP::UserAgent;
use Date::Manip;
my $mailhost = 'maiserver.domain.com'; # Fill these in!
my $maildomain = 'domain.com'; # Fill these in!
my $mailfrom = 'me@mycompany.com'; # Fill these in!
my $mailto = 'me@mycompany.com'; # Fill these in!
my $timeout = 30;
my $mailsubject = '';
my $mailbody = '';
my $logfile = '/usr/local/nagios/var/mail.log'; # Where would you like your logfile to live?
my $debug = 1; # Set the debug level to 1 or higher for information
my $type = '';
my $repdateprev;
my $reporturl;
my $nagssbody;
my $nagsssummary;
my $webuser = 'nagios'; # Set this to a read-only nagios user (not nagiosadmin!)
my $webpass = 'password'; # Set this to a read-only nagios user (not nagiosadmin!)
my $webbase = 'http://nagios-server.blah/nagios'; # Set this to the base of Nagios web page
my $webcssembed = 0;
my $webcssinline = 0;
GetOptions (
"debug=s" => \$debug,
"help" => \&help,
"type=s" => \$type,
"email=s" => \$mailto,
"embedcss" => \$webcssembed,
"inlinecss" => \$webcssinline,
);
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( $mailbody eq 0 ) {
exit 1;
}
# Remove HTML comments: avoid problems with Pynliner if $webcssinline == 1
$mailbody =~ s/<!--(.|\s)*?-->//g;
if ($webcssembed || $webcssinline) {
# --inlinecss depends on --embedcss
debug(1,"Embed CSS!");
# Stupid hacks for dodgy notes
$nagssbody = http_request("$webbase/stylesheets/summary.css");
if( $nagssbody eq 0 ) {
exit 1;
}
$nagsssummary = "<style type=\"text\/css\">\n";
foreach ( split(/\n/,$nagssbody) ) {
chomp;
if (not defined $_ or $_ eq "" ) {
next;
}
$nagsssummary .= "<!-- ";
$nagsssummary .= lc($_);
$nagsssummary .= " -->\n";
}
$nagsssummary .= "</style>\n";
$nagsssummary .= "<base href=\"$webbase/cgi-bin/\">\n";
$mailbody =~ s@<LINK REL=\'stylesheet\' TYPE=\'text/css\' HREF=\'/(?:nagios/)stylesheets/summary.css\'>@$nagsssummary@;
$mailbody =~ s@<LINK REL=\'stylesheet\' TYPE=\'text/css\'(.|\s)*.css\'>@@g;
}
my $output = "/tmp/nagios-report-htmlout.html";
open(FILE, "> $output") or warn "can't open file $output: $!\n";
if ($webcssinline) {
debug(1,"Inline CSS!");
close FILE;
chomp(my $Python = `which python`);
debug(1,"Inline CSS: python executable -> $Python");
if ( $Python eq "" ) {
debug(0,"Inline CSS: python executable not found!");
exit 1;
}
else {
# Escape some problematic characters
$mailbody =~ s/\n//g;
$mailbody =~ s/\r//g;
$mailbody =~ s/'/\\'/g;
$mailbody =~ s/"/\\"/g;
my $Script = "import pynliner; output = pynliner.fromString('$mailbody'); print(output);";
my $status = system "$Python -c \"$Script\" >$output";
if( $status != 0 ) {
debug(0,"Inline CSS: Python error.");
debug(0," Is Python module \"Pynliner\" installed?");
debug(0," http://pypi.python.org/pypi/pynliner");
}
}
}
sendmail();
###############################################################################
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"
--type=overnight
Overnight report, from 17h last working day to Today (9am)
--type=daily
Daily report, 09:00 last working day to Today (9am)
--type=weekly
Weekly report, 9am 7 days ago, until 9am today (run at 9am friday!)
--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)
--inlinecss
Downloads the CSS file and insert all the properties into inline
styles of the relative elements. This force all the webmails to not
override any CSS attribute.
-> Selects --embedcss.
-> Depends on Python and Python module Pynliner:
http://pypi.python.org/pypi/pynliner
_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 {
# This should be run on Friday, 9am
$repdateprev = Date_PrevWorkDay("today",5);
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 = 9;
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 alerts for week ending $repsday/$repsmonth/$repsyear";
}
###############################################################################
sub report_daily {
$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 = 7;
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 = 7;
$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 24 hours $repsday/$repsmonth/$repsyear ${repshour}h to present";
}
###############################################################################
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,"Retrieved 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;
}
Se l'errore dovesse rimanere, prova ad eseguirlo con solo --embedcss e riporta quali stili sono presenti tra LINK REL='stylesheet' e <style type="text/css"></style> (basta qualche riga).
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.