From c8b798f32dad6403614b5c9f67c301f305a77e42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=B3mas=20Edwardsson?= Date: Thu, 29 Apr 2010 13:08:00 +0000 Subject: [PATCH] Added perty-nagios-mailer.pl --- perty-nagios-mailer.pl/trunk/TODO | 3 + .../trunk/perty-nagios-mailer.pl | 148 ++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 perty-nagios-mailer.pl/trunk/TODO create mode 100644 perty-nagios-mailer.pl/trunk/perty-nagios-mailer.pl diff --git a/perty-nagios-mailer.pl/trunk/TODO b/perty-nagios-mailer.pl/trunk/TODO new file mode 100644 index 0000000..68b57d4 --- /dev/null +++ b/perty-nagios-mailer.pl/trunk/TODO @@ -0,0 +1,3 @@ +* Support for multiple graphs +* Possible html escape +* Write the usage function diff --git a/perty-nagios-mailer.pl/trunk/perty-nagios-mailer.pl b/perty-nagios-mailer.pl/trunk/perty-nagios-mailer.pl new file mode 100644 index 0000000..fb135a5 --- /dev/null +++ b/perty-nagios-mailer.pl/trunk/perty-nagios-mailer.pl @@ -0,0 +1,148 @@ +#!/usr/bin/perl +# +# Copyright 2010, Tomas Edwardsson +# +# This script is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This script is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +use strict; +use warnings; +use MIME::Lite; +use MIME::Entity; +use MIME::Base64; + +# Some defaults +my $pnp4nagios_perfdata = '/nagios/share/perfdata'; +my $pnp4nagios_phpdir = '/nagios/share'; +my $nagios_cgiurl = "http://slxnagios1/nagios/cgi-bin"; +my $pnp4nagios_url = "http://slxnagios1/nagios/pnp4nagios.php"; +my $from_address = 'nagios@lsh.is'; +my $logo = '/nagios/share/images/sblogo.jpg'; + +if (@ARGV != 8) { + usage(); +} + +my ($recipient, $date, $type, $host, $ip, $service, $state, $message) = @ARGV; + +my $rrd = ''; +if (-f "$pnp4nagios_perfdata/$host/$service.rrd") { + my $t = time(); + $rrd = `( cd $pnp4nagios_phpdir;php -r 'parse_str("host=$host&srv=$service&source=1&view=0&end=$t&display=image", \$_GET); include_once("pnp4nagios.php");' )`; +} + +my $head = MIME::Entity->build( + Type => 'multipart/related', + From => $from_address, + To => $recipient, + Subject => "Nagios, $state - $host - $service" +); + +my $alt = MIME::Entity->build( + Type => 'multipart/alternative' +); + +my $txt_content = MIME::Entity->build( + Type => 'text/plain', + Charset => 'UTF-8', + Encoding => 'quoted-printable', + Data => sprintf(<build( + Type => 'text/html', + Charset => 'UTF-8', + Encoding => 'quoted-printable', + Data => qq{ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +} . ($rrd ? qq{ + + + +} : "") . qq{ +
Host$host ($ip)
Service$service
State$state
Date$date
Type$type
Description
$message
+ +
+ +} +); + +my $rrdimage = MIME::Entity->build( + Data => $rrd, + Type => 'image/gif', + Id => '', + Encoding => 'base64' +) if ($rrd); +$alt->add_part($txt_content); +$alt->add_part($html_content); +$head->add_part($alt); +$head->add_part($rrdimage) if ($rrd); + +$head->attach( + Path => $logo, + Type => 'image/png', + Id => '', + Encoding => 'base64' +); + + +open SENDMAIL, '|/usr/sbin/sendmail -t'; +$head->print(\*SENDMAIL); +close SENDMAIL; + +