Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Do you know if is there any easy way to send output of every check run by nagios to syslog?

share|improve this question

2 Answers 2

up vote 0 down vote accepted

I wrote a small script which can be used as a notification and / or event handler. This was beneficial at the time since I did not want to log soft error states and the build I was working with could not separate the two. It also provides some formatting options.

This is easy to use by adding a user syslog with the notify-by-syslog options defined. Simply add this user as a contact whereever you want a specific service to log to syslog.

The Nagios command in the config file:

define command{
        command_name    notify-service-by-syslog
        command_line    /usr/bin/perl $USER1$/send_syslog.pl \
                 --state $SERVICESTATE$ --host $HOSTADDRESS$ \
                --msg "$HOSTADDRESS$ $SERVICEDESC$ $SERVICESTATE$ $SERVICEOUTPUT$ $LONGSERVICEOUTPUT$" --hard $SERVICESTATETYPE$
        }
define command{
        command_name    notify-host-by-syslog
        command_line    /usr/bin/perl $USER1$/send_syslog.pl \
                --hard $HOSTSTATETYPE$ --state $HOSTSTATE$ --host $HOSTADDRESS$ \
                --msg "$HOSTADDRESS$: $HOSTSTATE$ $HOSTOUTPUT$ $LONGHOSTOUTPUT$"
         }

The send_syslog.pl script

#!/usr/bin/perl -w

use Sys::Syslog qw(:standard :macros);
use strict;
use Getopt::Long;
&Getopt::Long::config('bundling');

my $help;
my $hard;
my $state;
my $host;
my $msg;

get_options();
run_process();

sub run_process
{
    if( $hard eq "SOFT" )
    {
        return 0;
    }
    my $alert=LOG_DEBUG;
    $alert=LOG_EMERG    if $state eq  "DOWN";
    $alert=LOG_INFO     if $state eq  "UP";
    $alert=LOG_CRIT     if $state eq  "CRITICAL";
    $alert=LOG_WARNING  if $state eq  "WARNING";
    $alert=LOG_INFO     if $state eq  "OK";

    openlog('nagios','','daemon');
    syslog($alert,"$host     $msg");
}

sub get_options
{
    GetOptions
        ("help|h"        => \$help,
         "hard:s"        => \$hard,
         "state:s"       => \$state,
         "host:s"        => \$host,
         "msg:s"         => \$msg
         );
    if( defined($help) )
    {
        print "--help called\n\n";
        print_usage();
    }
}
sub print_usage
{
    print "--help | -H: Print this screen\n";
    print "--hard <HOSTSTATETYPE|SERVICESTATETYPE>\n";
    print "  with a SOFT or HARD state; only alerts on the HARD states.\n";
    print "--state <HOSTSTATE|SERVICESTATE>";
    print "--host <HOSTADDRESS>\n";
    print "--msg <Message Body>: Defines the message body to render\n";
    exit 1;
}
share|improve this answer
    
Thanks! this was what I finally did too, although my scripts is in bash to reduce overhead (we have LOTS of services monitored). –  Matt Dec 20 '11 at 11:45

Did you set up the use_syslog option in Nagios configuration?

http://nagios.sourceforge.net/docs/nagioscore/3/en/configmain.html#use_syslog

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.