jueves, 7 de marzo de 2013

Plugin nagios para enviar notificaciones html por correo


Modificaciones del plugin nagios "notify-html-email" para enviar notificaciones html por correo.

He modificado el plugin para que aparezcan mis gráficas pnpnagios dentro del correo, castellanizarlo, etc.

Un ejemplo de como queda:


Hay que recordar que en ubuntu no viene instalado por defecto la libreria MIME lite Perl pero se puede instalar facilmente desde:

apt-get install libmime-lite-perl

El comando que hay que definir en nagios debería ser algo asi:
/usr/bin/perl $USER1$/notify-html-email $USER6$

$USER1$ es el directorio de plugins
$USER6$ es la url de acceso a nagios
estas variables se definen en el archivo /etc/nagios3/resources.cfg

Aquí dejo el script modificado

#!/usr/bin/perl

use strict;
use warnings;

use MIME::Lite;
use URI::Escape;

my $nagios_url = $ARGV[0] || die('You must pass the url to your nagios installation as the first argument'); 

my $color_red    = '#FF8080';
my $color_green  = '#80FF80';
my $color_yellow = '#FFFF80';
my $color_orange = '#FF9900';
my $color_even   = '#C9C9C9';
my $color_odd    = '#EAEAEA';

my %colors=(
    'PROBLEM'         => $color_red,
    'RECOVERY'        => $color_green,
    'ACKNOWLEDGEMENT' => $color_yellow,
    'CRITICAL'        => $color_red,
    'WARNING'         => $color_yellow,
    'OK'              => $color_green,
    'UNKNOWN'         => $color_orange,
    'UP'              => $color_green,
    'DOWN'            => $color_red,
    'UNREACHABLE'     => $color_orange,
);

my %allowed_types = (
    'PROBLEM'         => 1,
    'RECOVERY'        => 1,
    'ACKNOWLEDGEMENT' => 1
);

exit if !defined($allowed_types{$ENV{NAGIOS_NOTIFICATIONTYPE}});
my $type = $ENV{NAGIOS_SERVICEATTEMPT} ? 'service' : 'host';

my $font = 'helvetica, arial';
my $style_th_even = '"background-color: #c9c9c9; font-weight: bold; width: 130px;"';
my $style_th_odd  = '"background-color: #eaeaea; font-weight: bold; width: 130px;"';
my $style_td_even = '"background-color: #c9c9c9;"';
my $style_td_odd  = '"background-color: #eaeaea;"';
my $style_table   = "\"background-color: #000000; font-family: consolas, verdana, arial; font-size: 12px;\"";
my $style_body   = "\"background-color: #ffffff;\"";

my $style_state = sprintf('"background-color: %s"', 
    $type eq 'service'
    ? $colors{$ENV{NAGIOS_SERVICESTATE}}
    : $colors{$ENV{NAGIOS_HOSTSTATE}}
);

my $subject = sprintf('[EMPRESA] %s [%s] ',
    $type eq 'service' ? $ENV{NAGIOS_HOSTNAME} . ' - ' . $ENV{NAGIOS_SERVICEDESC} : $ENV{NAGIOS_HOSTNAME},
    $type eq 'service' ? $ENV{NAGIOS_SERVICESTATE} : $ENV{NAGIOS_HOSTSTATE}
);

my $host_url = sprintf('%s/cgi-bin/status.cgi?navbarsearch=1&host=%s',
    $nagios_url,
    $ENV{NAGIOS_HOSTNAME}
);
my @rows;
my $notification_title;

if($type eq 'service') {
    my $service_url = sprintf('%s/cgi-bin/extinfo.cgi?type=2&host=%s&service=%s',
        $nagios_url,
        $ENV{NAGIOS_HOSTNAME},
        uri_escape($ENV{NAGIOS_SERVICEDESC})
    );
    my $service_perf_image_url = sprintf('%s/../../pnp4nagios/graph?host=%s&srv=%s',
        $nagios_url,
        $ENV{NAGIOS_HOSTNAME},
        uri_escape($ENV{NAGIOS_SERVICEDESC})
    );
    $notification_title = sprintf('Notificacion de Servicio - %s (%s)',
$ENV{NAGIOS_SERVICESTATE},
$ENV{NAGIOS_SERVICESTATETYPE}
    );

    @rows = (
        {
            'title' => 'Estado:',
            'data'  => "$ENV{NAGIOS_SERVICESTATE} ($ENV{NAGIOS_SERVICESTATETYPE})",
            'style' => $style_state,
        },
        { 
            'title' => 'Equipo:',
            'data'  => "<a href=\"$host_url\">$ENV{NAGIOS_HOSTNAME}</a> ($ENV{NAGIOS_HOSTALIAS}) $ENV{NAGIOS_HOSTGROUPNAME}",
        },
        { 
            'title' => 'Direccion IP:',
            'data'  => $ENV{NAGIOS_HOSTADDRESS},
        },
        { 
            'title' => 'Servicio:',
            'data'  => "<a href=\"$service_url\">$ENV{NAGIOS_SERVICEDESC}</a> ($ENV{NAGIOS_SERVICEDISPLAYNAME})",
        },
        {
            'title' => 'Chequeo del Servicio',
            'data'  => "$ENV{NAGIOS_SERVICEOUTPUT}",
        },
        {
            'title' => 'Momento del evento:',
            'data'  => $ENV{NAGIOS_SHORTDATETIME},
        },
        {
            'title' => 'PNP4Nagios:',
            'data'  => "<a href=\"$service_perf_image_url\">Visualizar graficos</a>",
        },
    );
} else {
    my $host_perf_image_url = sprintf('%s/../../pnp4nagios/graph?host=%s&srv=_HOST_',
        $nagios_url,
        $ENV{NAGIOS_HOSTNAME},
        uri_escape($ENV{NAGIOS_SERVICEDESC})
    );
    $notification_title = sprintf('Notificacion de Equipo - %s (%s)',
$ENV{NAGIOS_HOSTSTATE},
$ENV{NAGIOS_HOSTSTATETYPE}
    );

    @rows = (
        {
            'title' => 'Estado:',
            'data'  => "$ENV{NAGIOS_HOSTSTATE} ($ENV{NAGIOS_HOSTSTATETYPE})",
            'style' => $style_state,
        },

            'title' => 'Equipo:',
            'data'  => "<a href=\"$host_url\">$ENV{NAGIOS_HOSTNAME}</a> ($ENV{NAGIOS_HOSTALIAS}) $ENV{NAGIOS_HOSTGROUPNAME}",
        },
        { 
            'title' => 'Direccion IP:',
            'data'  => $ENV{NAGIOS_HOSTADDRESS},
        },
        {
            'title' => 'Chequeo del equipo:',
            'data'  => $ENV{NAGIOS_HOSTOUTPUT},
        },
        {
            'title' => 'PNP4Nagios:',
            'data'  => "<a href=\"$host_perf_image_url\">Visualizar graficos</a>",
        },
    );
}

if($ENV{NAGIOS_NOTIFICATIONTYPE} eq 'ACKNOWLEDGEMENT') {
    push(@rows,
        {
            'title' => 'Reconocido por:',
            'data'  => $ENV{NAGIOS_NOTIFICATIONAUTHORALIAS},
        }
    );
    push(@rows,
        {
            'title' => 'Comentario:',
            'data'  => $ENV{NAGIOS_NOTIFICATIONCOMMENT},
        }
    );
}

# Begin the email
my $body = "
<body style=$style_body>
<h2 style=\"text-align:center; font-family: verdana, arial; color: #203040\">Monitorizacion Nagios</h2>
<p style=\"text-align:center; font-family: verdana, arial; color: #404040\">$notification_title</p>
<table align=\"center\" border=\"0\" cellpadding=\"4\" cellspacing=\"2\" style=$style_table>";

# Add the rows to the email
for(my $i=0; $i<@rows; $i++) {
    $body .= sprintf('<tr><th style=%s>%s</th><td style=%s>%s</td></tr>',
        $i % 2 == 0 ? $style_th_even : $style_th_odd,
        $rows[$i]->{'title'},
        defined($rows[$i]->{'style'}) ? $rows[$i]->{'style'} : $i % 2 == 0 ? $style_td_even : $style_td_odd,
        $rows[$i]->{'data'}
    );
}
$body .= "</table><p style=\"text-align:center\"><img src=\"cid:lgNextel.png\" height=\"30px\"></body>";

my $msg = MIME::Lite->new(
    To      => $ENV{NAGIOS_CONTACTEMAIL},
    Subject => $subject,
    Type    =>'multipart/related'
);

$msg->attach(Type => 'text/html', Data => $body);
$msg->attach(Type => 'image/png', Id   => 'lgNextel.png', Path => '/var/www/img/lgNextel.png');


$msg->send();

No hay comentarios: