EventHandler fires too often on SNMP-TrapDirector

I am running SNMP-TrapDirector to handle Interface Up/Down traps.
The handler sets the state of a switch’s service definition to OK (Up) or WARNING (Down).

Whenever a port comes up, we run an automation comparing the MAC on the switch port with the one in our documentation (Netbox). We created a simple EventCommand for this which calls a Webhook in an Ansible automation.

The Request should only fire if:

  • service_state = OK
  • service_state_type = HARD
  • service_last_state != OK
  • service_long_output ends in " Up"

Unfortunatly, our Backend always receives two identical automation requests :frowning:

grafik

I guess what I am looking for is a state-machine for the service state - ideally I want to filter in the first “Up” event.

Thanks already!
Alex

It’s a while since I did anything with the event handler.

If I remember correctly, I also passed the previous state and it worked.
Can you post the config of your EventCommand and the filter code at the top in the script you call?

Thank you for you quick reply.

This is what I use:

object EventCommand "network_port_event" {
  import "plugin-event-command"
  command = [ PluginDir + "/network-port-up-down-handler.sh" ]
  arguments = {
        "--state" = "$service.state$"
        "--last-state" = "$service.last_state$"
        "--state-type" = "$service.state_type$"
        "--last-state-type" = "$service.last_state_type$"
        "--output" = "$service.output$"
        "--host" = "$host.name$"
        "--ip" = "$host.address$"
        "--service" = "$service.name$"
  }
}

network-port-up-down-handler.sh

#!/bin/bash

STATE=“”
STATE_TYPE=“”
LAST_STATE=“”
LAST_STATE_TYPE=“”
OUT=“”
IP=“”
SERVICE=“”

while [[ “$#” -gt 0 ]] ; do
case $1 in
–state)
STATE=$2 ; shift ;;
–state-type)
STATE_TYPE=$2 ; shift ;;
–last-state)
LAST_STATE=$2 ; shift ;;
–last-state-type)
LAST_STATE_TYPE=$2 ; shift ;;
–output)
OUT=$2 ; shift ;;
–host)
HOST=$2 ; shift ;;
–ip)
IP=$2 ; shift ;;
–service)
SERVICE=$2 ; shift ;;
*)
echo “Unknown parameter: $1” ; exit 1 ;;
esac
shift
done

if [[ “$STATE” = “OK” && “$STATE_TYPE” = “HARD” && “$LAST_STATE” != “OK” ]]; then
T=${OUT## Interface }
INTERFACE=${T%% Up}
if [ “${OUT##* }” = “Up” ] ; then
/usr/bin/curl -s https://ansible/api/integrations/test123 -X POST --data-raw “{“device_name”:”${HOST}“,“interface_name”:”${INTERFACE}“}” -H “Content-Type: application/javascript”
fi
fi

exit 0

LGTM… next log into a file something like datetime var1 var2 … to track down, what happens.

I tired to find my old config and code but it was in the last instance I can’t find right now.

Great idea about writing a debug log :slight_smile:

It’s indeed a problem with the switch sending unsolicited traps. I should have checked before…

Sorry for the noise!