Page 1 of 1

Configuring Nagios to send SNMP traps

Posted: Wed Apr 15, 2020 1:54 am
by Gestio_LAN
Hi everyone,

We are migrating our old OpenNMS monitoring system to a Nagios 4.4.3 and I need to send SNMP traps from Nagios to a server in my parent company. We're already doing this in OpenNMS so we need to "adapt" the script to work on Nagios. Problem that I'm new on linux enviroment and I'm pretty lost. I've been searching on google but didn't find clear solutions, so I hope you can help me with this.

We're sending 2 different traps:
1- Keepalive trap every 5 minutes:

Code: Select all

#!/bin/sh

OPENNMS_HOME="/opt/opennms"
SEND_TRAP="/usr/bin/snmptrap"
LOGFILE=log-send-event-passive2.log
PASS="CORE CdControl"
DESTINATION="0.0.0.0"	#IP CdC

TIMESTAMP=`date '+%Y/%m/%d %H:%M:%S'`

COMMANDT="$SEND_TRAP -v1 -c public $DESTINATION .1.3.6.1.4.1.20006.9 1.1.1.1 6 10 ''"
COMMANDT="$COMMANDT .1.3.6.1.4.1.20006.9.1 s \"Nagios \""
COMMANDT="$COMMANDT .1.3.6.1.4.1.20006.9.2 s \"NORMAL\""
COMMANDT="$COMMANDT .1.3.6.1.4.1.20006.9.3 s \"Nagios\""
COMMANDT="$COMMANDT .1.3.6.1.4.1.20006.9.4 s \"Monitoratge\""
COMMANDT="$COMMANDT .1.3.6.1.4.1.20006.9.5 s \"$TIMESTAMP\""
COMMANDT="$COMMANDT .1.3.6.1.4.1.20006.9.6 s \"KeepAlive\""
COMMANDT="$COMMANDT .1.3.6.1.4.1.20006.9.7 s \"\""
COMMANDT="$COMMANDT .1.3.6.1.4.1.20006.9.8 s \"LAN\""
COMMANDT="$COMMANDT .1.3.6.1.4.1.20006.9.9 s \"GESTIO DE LAN\""
COMMANDT="$COMMANDT .1.3.6.1.4.1.20006.9.10 s \"OK\""
COMMANDT="$COMMANDT .1.3.6.1.4.1.20006.9.11 s \"\""

sh  -c "$COMMANDT"

echo $COMMANDT >> /opt/opennms/bin/$LOGFILE

#EOF
2- Alarm traps in case of "DOWN" state for especific critical hosts (we "mark" this critical hosts manually).

Code: Select all

#!/bin/sh

OPENNMS_HOME="/opt/opennms"
SEND_TRAP="/usr/bin/snmptrap"
EVENT_DOWN="uei.opennms.org/nodes/nodeDown"
EVENT_UP="uei.opennms.org/nodes/nodeUp"
EVENT_TRAP="uei.opennms.org/custom/event/SendTrapCdC"
LOGFILE=log-send-event-passive.log
PASS="CORE CdControl"
DESTINATION="0.0.0.0"	#IP CdC
TIMESTAMP=`date '+%Y/%m/%d %H:%M:%S'`
ASSET=$1
CATEGORY=$2
SEU=$3
UEI=$4
LOGMSG=$5
DESC=$6

#Si Falten Parametres sortim
if [ $# -ne 6 ]; then
	exit 0

fi
#Si no es CORE sortim
if [ "$CATEGORY" != "$PASS" ]; then
	exit 0
fi
CATEGORY="Core"
#Si no es un Event catalogat Sortim
if [ "$UEI" != "$EVENT_DOWN" ] && [ "$UEI" != "$EVENT_UP" ]; then
	echo "No es event down o Up"
	exit 0
fi

if [ "$UEI" == "$EVENT_DOWN" ]; then
	STATUS="Critical"
else
	STATUS="Normal"
fi

COMMANDT="$SEND_TRAP -v1 -c public $DESTINATION2 .1.3.6.1.4.1.20006.9 1.1.1.1 6 10 ''"
COMMANDT="$COMMANDT .1.3.6.1.4.1.20006.9.1 s \"$ASSET\""
COMMANDT="$COMMANDT .1.3.6.1.4.1.20006.9.2 s \"$STATUS\""
COMMANDT="$COMMANDT .1.3.6.1.4.1.20006.9.3 s \"$CATEGORY\""
COMMANDT="$COMMANDT .1.3.6.1.4.1.20006.9.4 s \"LAN: $SEU\""
COMMANDT="$COMMANDT .1.3.6.1.4.1.20006.9.5 s \"$TIMESTAMP\""
COMMANDT="$COMMANDT .1.3.6.1.4.1.20006.9.6 s \"$LOGMSG\""
COMMANDT="$COMMANDT .1.3.6.1.4.1.20006.9.7 s \"$DESC\""
COMMANDT="$COMMANDT .1.3.6.1.4.1.20006.9.8 s \"LAN\""
COMMANDT="$COMMANDT .1.3.6.1.4.1.20006.9.9 s \"GESTIO DE LAN\""
COMMANDT="$COMMANDT .1.3.6.1.4.1.20006.9.10 s \"Disponibilitat\""
COMMANDT="$COMMANDT .1.3.6.1.4.1.20006.9.11 s \"\""

# echo "$COMMANDT"

sh  -c "$COMMANDT"

echo $COMMANDT >> /opt/opennms/bin/$LOGFILE

COMMAND2="$OPENNMS_HOME/bin/send-event.pl $EVENT_TRAP  --parm 'EventUEI $UEI'"
COMMAND2="$COMMAND2 --parm 'nodename $ASSET' --parm 'DateTime $TIMESTAMP'"
sh -c "$COMMAND2"
#EOF
(NOTE we're supposing that my destination IP is 0.0.0.0 and my OpenNMS IP is 1.1.1.1, only as a example in both codes).

All that variables like "ASSET, CATEGORY, STATUS, SEU…" are neccessary (demanded by my company) and I can set by hostgroups, but I don't know how to define that on this codes. And, to execute this, I think I can do it creating a new user who it's "host notification command" are executing this script instead of default "notify-host-by-email" (something like "send-trap", and create a command to do this, obviously)

I'm sorry if I'm explaining poorly, if you need some more specific info please, tell me.

Hope you can help me guys.

Thanks!

Re: Configuring Nagios to send SNMP traps

Posted: Wed Apr 15, 2020 3:01 pm
by cdienger
The could be a couple ways of going about this depending on what you want to do exactly. Do you want to send just notifications to the parent company? If so then you are on the right track - you'll want ot create a command that calls your script and passes it the macros(variables) you'd like to send in the trap. The command is then associated with a user so that the trap is sent for a notification.

http://askaralikhan.blogspot.com/2010/0 ... agios.html steps though this and https://github.com/nagios-plugins/nagio ... NOTIFY-MIB is where you can find the MIB.

The other option would be to use an event handler. The difference with this is that the this would be called any time there is a state change and not just when a notification needs to be sent. See https://assets.nagios.com/downloads/nag ... dlers.html. Note that event handlers can be global host, global service, host specific, or service specific.

I don't some of the macros you want will be available by default - see https://assets.nagios.com/downloads/nag ... olist.html for default marcos - but you could create custom macros. See https://assets.nagios.com/downloads/nag ... acros.html for more information on macros including how to create custom ones.