4

I'm trying to configure my Nagios setup to automatically log a ticket in our ticketing system when a host goes down (PING service goes into a HARD CRITICAL state). I've got a script that will create tickets that runs successfully with 'sudo -u nagios'. I've got a command and event handler set up that executes when the host state fails, or at least appears to. However, the script associate with the command doesn't seem to be executing, or the logging I've added isn't working. Any ideas where to look next? The log file isn't created when the event handler is invoked.

command:

define command {
        command_name    make-ticket
        command_line    /etc/nagios/commands/make-ticket-wrapper "$SERVICESTATE$" "$SERVICESTATETYPE$" "$HOSTNAME" "$HOSTADDRESS$ "$HOSTSTATE" "$HOSTGROUPALIAS" "$SERVICEDESC"
}

example host and service

define host {
    use             generic-switch
    host_name       test
    alias           test
    address         192.168.100.13
    }

define service {
    use                     generic-service
    host_name               test
    service_description     PING
    check_command           check_ping!200.0,20%!600.0,60%
    normal_check_interval   5
    retry_check_interval    1
    event_handler           make-ticket
    }

wrapper script:

#!/bin/bash
#
# Cut a ticket
#

set -x

LOGFILE="/tmp/tickets.log"

touch $LOGFILE
echo Running make-ticket-wrapper `date` >> $LOGFILE
echo Params: $* >> $LOGFILE

TICKET="/etc/nagios/commands/make-ticket"

SERVICESTATE="$1"
SERVICESTATETYPE="$2"
HOSTNAME="$3"
HOSTADDRESS="$4"
HOSTSTATE="$5"
HOSTGROUPALIAS="$6"
SERVICEDESC="$7"

echo "SERVICESTATE=$SERVICESTATE" >> $LOGFILE
echo "SERVICESTATETYPE=$SERVICESTATETYPE" >> $LOGFILE
echo "HOSTNAME=$HOSTNAME" >> $LOGFILE
echo "HOSTADDRESS=$HOSTADDRESS" >> $LOGFILE
echo "HOSTSTATE=$HOSTSTATE" >> $LOGFILE
echo "HOSTGROUPALIAS=$HOSTGROUPALIAS" >> $LOGFILE
echo "SERVICEDESC=$SERVICEDESC" >> $LOGFILE

$TICKET "$SERVICESTATE" "$SERVICESTATETYPE" "$HOSTNAME" "$HOSTADDRESS" "$HOSTSTATE" "$HOSTGROUPALIAS" "$SERVICEDESC" 2>&1 | tee -a $LOGFILE

log:

[1422560163] HOST ALERT: test;DOWN;SOFT;1;(Host Check Timed Out)
[1422560253] HOST ALERT: test;DOWN;SOFT;2;(Host Check Timed Out)
[1422560353] HOST ALERT: test;DOWN;SOFT;3;(Host Check Timed Out)
[1422560433] SERVICE ALERT: test;PING;CRITICAL;HARD;1;PING CRITICAL - Packet loss = 100%
[1422560433] SERVICE EVENT HANDLER: test;PING;CRITICAL;HARD;1;make-ticket
Chris Heilman
  • 43
  • 1
  • 4
  • I have to ask, the wrapper script is executable and readable by nagios, right? From the description it is not clear to me if you tested with sudo make-ticket-wrapper or just make-ticket – Dan Feb 02 '15 at 20:05
  • Yeah, both are executable and sudo works for both of them. – Chris Heilman Feb 02 '15 at 20:54

2 Answers2

4

command_line /etc/nagios/commands/make-ticket-wrapper "$SERVICESTATE$" "$SERVICESTATETYPE$" "$HOSTNAME" "$HOSTADDRESS$ "$HOSTSTATE" "$HOSTGROUPALIAS" "$SERVICEDESC"

You're missing a double quote after $HOSTADDRESS$, and missing a $ at the end of HOSTNAME, HOSTSTATE, HOSTGROUPALIAS, and SERVICEDESC.

When you miss a double quote, it screws up the rest of the line, causing the shell script to miss some arguments.

When you leave out the trailing dollar sign, Nagios doesn't substitute the macro, so it gets passed to the shell where it's resolved as an empty variable.

Keith
  • 4,637
  • 15
  • 25
0

nagios chokes on all those quotes

change:
command_line /etc/nagios/commands/make-ticket-wrapper "$SERVICESTATE$" "$SERVICESTATETYPE$" "$HOSTNAME" "$HOSTADDRESS$ "$HOSTSTATE" "$HOSTGROUPALIAS" "$SERVICEDESC"

to:
command_line /etc/nagios/commands/make-ticket-wrapper $SERVICESTATE$ $SERVICESTATETYPE$ $HOSTNAME$ $HOSTADDRESS$ $HOSTSTATE$ $HOSTGROUPALIAS$ $SERVICEDESC$

Plus you also missed some $ signs in the command line, look above. It will work as expected after that. I just tried it:

#cat /tmp/tickets.log Running make-ticket-wrapper Mon Feb 2 17:32:05 EST 2015 Params: OK HARD test 192.168.1.1 UP $ PING SERVICESTATE=OK SERVICESTATETYPE=HARD HOSTNAME=test HOSTADDRESS=192.168.1.1 HOSTSTATE=UP HOSTGROUPALIAS=$ SERVICEDESC=PING

Ricardo
  • 739
  • 5
  • 6
  • This is not good advice. The quotes are there to prevent command injection. – Keith Feb 03 '15 at 02:42
  • If you look at the nagios docs, those parameters do not have quotes. See the example half way down the page http://nagios.sourceforge.net/docs/3_0/eventhandlers.html I am sure there is an elegant way to prevent command injection but I don't think its what the OP was worried about. – Ricardo Feb 03 '15 at 03:08
  • 1
    For most of the macros, the quotes aren't strictly necessary. But for SERVICEDESC, for example... you need the quotes if your service_description has a space in it (many of mine do). It's better to have the quotes and not need them, than vice versa. – Keith Feb 03 '15 at 20:15