Nagios Core 4.08 don't send traps in order.

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Nagios_Admin_M
Posts: 77
Joined: Fri Jul 22, 2016 4:39 am

Nagios Core 4.08 don't send traps in order.

Post by Nagios_Admin_M »

Hey,

I send traps from Nagios Core 4.08. I use values and variables in order as follow, but they are send in different order. The values need to be send exactly as they are specified.

Code: Select all

command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$ | var2=$HOSTNAME$ var3=$HOSTSTATEID$ var4=$HOSTSTATETYPE$ var5=$LASTHOSTCHECK$ var6=$HOSTDURATIONSEC$ var7=$LASTHOSTSTATECHANGE$ var8=$LASTHOSTSTATECHANGE$ var9=$HOSTATTEMPT$ var10=$HOSTDOWNTIME$ var11=$HOSTSTATE$ /usr/bin/test_trap

Code: Select all

#test_trap
./send_trap

# send_trap

#!/bin/bash
/usr/bin/snmptrap -v 2c -c $1 $ncprobe '' NAGIOS-NOTIFY-MIB::nSvcEvent nSvcHostname s "$2" nHostStateID i $3 nSvcDesc s "$4" nSvcStateID i $5 nSvcAttempt i $6 nSvcDurationSec i $7 nSvcGroupName s "$8" nSvcLastCheck i $9 nSvcLastChange i ${10}  nSvcOutput s "${11}"
Last edited by tgriep on Wed Sep 07, 2016 11:45 am, edited 2 times in total.
Reason: Please use [code][/code] tags around code output
User avatar
tgriep
Madmin
Posts: 9190
Joined: Thu Oct 30, 2014 9:02 am

Re: Nagios Core 4.08 don't send traps in order.

Post by tgriep »

I found this URL with instructions on how to setup Nagios to send SNMP Traps. Take a look at it and see if it helps out in the issue you are having.
http://askaralikhan.blogspot.com/2010/0 ... agios.html
Be sure to check out our Knowledgebase for helpful articles and solutions!
Nagios_Admin_M
Posts: 77
Joined: Fri Jul 22, 2016 4:39 am

Re: Nagios Core 4.08 don't send traps in order.

Post by Nagios_Admin_M »

The url doesn't correlate current projects scripts and it was updated.

I try to do it such a way:

STEP I - I modify command file:
define command{
command_name notify-host-by-email
command_line var2=$HOSTNAME$ var3=$HOSTSTATEID$ var4=$SERVICEDESC$ var5=$SERVICESTATEID$ var6=$SERVICEATTEMPT$ var7=$SERVICEDURATIONSEC$ var8=$SERVICEGROUPNAME$ var9=$LASTSERVICECHECK$ var10=$LASTSERVICESTATECHANGE$ var11=$SERVICEOUTPUT$ /usr/bin/my_script
}

STEP II this is snmptrap file configuration:
/usr/bin/snmptrap -v 2c -c $1 $ncprobe '' NAGIOS-NOTIFY-MIB::nSvcEvent nSvcHostname s "$2" nHostStateID i $3 nSvcDesc i "$4" nSvcStateID i $5 nSvcAttempt i $6 nSvcDurationSec i $7 nSvcGroupName s "$8" nSvcLastCheck i $9 nSvcLastChange i ${10} nSvcOutput s "${11}"

STEP III - log file out put:
ServerName 0 $ $ $ $ $ $ $ $

The SERVICEDESC - is not assign by Nagios and returns a dolar in the log file. I have also used SERVICEDESC in different location and moved it into var 4,6,7 etc. with no result. In my opinion Nagios doesn't posses this value and not pass it to the snmptrap cript.
User avatar
tgriep
Madmin
Posts: 9190
Joined: Thu Oct 30, 2014 9:02 am

Re: Nagios Core 4.08 don't send traps in order.

Post by tgriep »

In step 1, the commands should be setup like this. One command to send the Host trap and one to send the service trap. Your example will not work as the variables are not after the script's command.
The keyword called manager in the examples below, would be the hostname or IP address of the system you want to send the SNMP Traps to.

Code: Select all

# 'send-service-trap' command definition
define command{
      command_name send-service-trap
      command_line /usr/local/bin/send-service-trap manager public "$HOSTNAME$" "$SERVICEDESC$" $SERVICESTATEID$ "$SERVICEOUTPUT$"
}

Code: Select all

# 'send-host-trap' command definition
define command{
                command_name send-host-trap
                command_line /usr/local/bin/send-host-trap manager public "$HOSTNAME$" $HOSTSTATEID$ "$HOSTOUTPUT$"
}
Then the scripts have to be defined like the following

Code: Select all

===/usr/local/bin/send-service-trap ====
# Arguments:
# $1 = Management Station
# $2 = Community String
# $3 = host_name
# $4 = service_description (Description of the service)
# $5 = return_code (An integer that determines the state
#       of the service check, 0=OK, 1=WARNING, 2=CRITICAL,
#       3=UNKNOWN).
# $6 = plugin_output (A text string that should be used
#       as the plugin output for the service check)
#
#
/usr/bin/snmptrap -v 2c -c $2 $1 '' NAGIOS-NOTIFY-MIB::nSvcEvent nSvcHostname s "$3" nSvcDesc s "$4" nSvcStateID i $5 nSvcOutput s "$6"
===/usr/local/bin/send-host-trap=======

Code: Select all

# Arguments:
# $1 = Management Station
# $2 = Community String
# $3 = host_name
# $4 = HostStatID A number that corresponds to the current state of the host: 0=UP, 1=DOWN, 2=UNREACHABLE.
# $5 = HOSTOUTPUT The first line of text output from the last host check (i.e. "Ping OK").
#
#
/usr/bin/snmptrap -v 2c -c $2 $1 '' NAGIOS-NOTIFY-MIB::nHostEvent nHostname s "$3" nHostStateID i $4 nHostOutput s "$5"
If you want to send more variables than what the snmptrap application can do, you will have to concatenate them and send it as one argument is that is possible.
Be sure to check out our Knowledgebase for helpful articles and solutions!
Nagios_Admin_M
Posts: 77
Joined: Fri Jul 22, 2016 4:39 am

Re: Nagios Core 4.08 don't send traps in order.

Post by Nagios_Admin_M »

Hey,
I add an entry to the commands.cfg file as follow:
# 'send-host-trap' command definition
define command{
command_name send-host-trap
command_line /usr/local/bin/send-host-trap manager public "$HOSTNAME$" $HOSTSTATEID$ "$HOSTOUTPUT$"
}

to the contacts.cfg

3 define contact {
74 contact_name test_contact
75 use generic-contact
76 alias test_alias
77 email @email
78 can_submit_commands 1
79 service_notification_options w,u,c,r
80 host_notification_options d,u,r
81 host_notification_commands send-host-trap
82 }

and further more I did created the send-host-trap

1 #!/bin/sh
2 /usr/bin/snmptrap -v 2c -c $2 $1 '' NAGIOS-NOTIFY-MIB::nHostEvent nHostname s "$3" nHostStateID i $4 nHostOutput s "$5"
3 cat << EOF >> /logfile
4 Hello World
5 EOF

The result is that, the command is not executed by Nagios, the log file is not altered and remains empty also the Nagios'es log file do not include any input.
Nagios_Admin_M
Posts: 77
Joined: Fri Jul 22, 2016 4:39 am

Re: Nagios Core 4.08 don't send traps in order.

Post by Nagios_Admin_M »

I tried to send the $SERVICEDESC$ value through notify-host-by-email, but I can only do it through the notify-service-by-email. Are values of the Nagios'es oits are assigned to an appropriate notify method ?
User avatar
tgriep
Madmin
Posts: 9190
Joined: Thu Oct 30, 2014 9:02 am

Re: Nagios Core 4.08 don't send traps in order.

Post by tgriep »

Verify that the script you created called

Code: Select all

/usr/local/bin/send-host-trap
Is executable by the nagios user and that the nagios user account can read it.
Also, the folder where the log file is and the log file, needs to be writable by the nagios user.

The $SERVICEDESC$ macro is only valid for service checks so it will not work for host checks.
Be sure to check out our Knowledgebase for helpful articles and solutions!
Nagios_Admin_M
Posts: 77
Joined: Fri Jul 22, 2016 4:39 am

Re: Nagios Core 4.08 don't send traps in order.

Post by Nagios_Admin_M »

I typed into command line following commands:

1 sudo /usr/bin/snmptrap -v 2c -c 1 MY_HOST '' NAGIOS-NOTIFY-MIB::nSvcEvent nSvcHostname s "$4"

Result: The trap is sent

when I type this command:

2. usr/bin/snmptrap -v 2c -c 1 MY_HOST '' NAGIOS-NOTIFY-MIB::nSvcEvent nSvcHostname s "$2" nSvcDesc s "$4" nSvcStateID i $5 nSvcAttempt i $6 nSvcDurationSec i $7 nSvcGroupName s "$8" nSvcLastCheck i $9 nSvcLastChange i ${10} nSvcOutput s "${11}"

Result:
No log handling enabled - turning on stderr logging nSvcStateID: Value out of range (nSvcAttempt)
Nagios_Admin_M
Posts: 77
Joined: Fri Jul 22, 2016 4:39 am

Re: Nagios Core 4.08 don't send traps in order.

Post by Nagios_Admin_M »

My current commands.cfg
/usr/local/bin/send-service-trap $HOSTNAME$ $SERVICEDESC$ $SERVICEDESC$ $SERVICEATTEMPT$ $SERVICEDURATIONSEC$ $SERVICEGROUPNAME$ $LASTSERVICE CHECK$ $LASTSERVICECHECK$ $SERVICEOUTPUT$

send-service-trap
/usr/bin/snmptrap -v 2c -c 1 My_Host '' NAGIOS-NOTIFY-MIB::nSvcEvent nSvcHostname s "$2" nSvcDesc s "$4" nSvcStateID i $5 nSvcAttempt i $6 nSvcDurationSec i $7 nSvcGroupName s "$8" nSvcLastCheck i $9 nSvcLastChange i ${10} nSvcOutput s "${11}"

After I execute this command in the command line:

No log handling enabled - turning on stderr logging
nSvcStateID: Value out of range (nSvcAttempt)
Nagios_Admin_M
Posts: 77
Joined: Fri Jul 22, 2016 4:39 am

Re: Nagios Core 4.08 don't send traps in order.

Post by Nagios_Admin_M »

Ok, all I need to do is to send trap in this order.

nHostname nHostStateID nSvcDesc nSvcStateID nSvcAttempt nSvcDurationSec nSvcGroupName nSvcLastCheck nSvcLastChange nSvcOutput

How can I do it ?
Locked