Centreon & Nagios Event Handler for APC 9210 PDU

The following script can be used as an event handler script for Centreon or Nagios. You may need to change the directory where the log files are stored depending upon your installation.
The script (and log file directory) must be owned by the same user that runs the centreon or nagios engine.
EVENT HANDLER SCRIPT (apc_pdu_9210_outlets.sh)

#!/bin/sh
#
# Event handler script for changing the state of the APC 9210 PDU Outlets
# Originally adapted from the Nagios and Centreon documentation
#
# Author: Steve Talley
# Version: 2015.08.26
#
# This script will execute when the host is in a SOFT DOWN or UNREACHABLER state on the
# third retry or when the host reaches the HARD DOWN or UNREACHABLE state.
#
# Call this with an event handler and pass the following variables in order:
# 1: $HOSTSTATE$
# 2: $HOSTSTATETYPE$
# 3: $HOSTATTEMPT$
# 4: SNMP Community
# 5: IP address of the APC PDU
# 6: Outlet number to change
#
# Make sure to give ownership to the centreon-engine user for this script.
#=================
# VARIABLES
#=================
# Keep our log file in a directory writeable by centreon-engine
logFile=/var/log/centreon-engine/apc_pdu_9210_outlets.log
#Define our incoming variables
host_state=$1
host_state_type=$2
host_state_type_num=$3
snmpcommunity=$4
pdu_ip=$5
outlet_number=$6
outlet_action=$7
#=================
# LOGGING
#=================
#Write some lines to the log
echo -n "`date`: " >> $logFile
echo $host_state $host_state_type $host_state_type_num $snmpcommunity $pdu_ip $outlet_number $outlet_action >> $logFile
#=================
# FUNCTIONS
#=================
# Define the function for our SNMP set command
function snmpsetAPC9210()
{
    echo "Changing PDU Outlet ${6} to state ${7} (Centreon Host State: ${1}/${2}/${3})..." >> $logFile
    snmpset -v1 -c ${4} ${5} 1.3.6.1.4.1.318.1.1.4.4.2.1.3.${6} integer ${7}  >> $logFile    
}
#=================
# ALIASES
#=================
# Now define an alias command that passes all of the variables to the function
alias run_snmpsetAPC9210='snmpsetAPC9210 $host_state $host_state_type $host_state_type_num $snmpcommunity $pdu_ip $outlet_number $outlet_action'
#=================
# CASE STATEMENTS
#=================
# Determine the host state
case "$host_state" in
UP)
    # The host is up; do not run any commands.
    ;;
DOWN|UNREACHABLE)
    # The host is down or unreachable.
    # Determine if this is a HARD or SOFT state
    case "$host_state_type" in
        
    # We're in a "soft" state, meaning that Nagios is in the middle of retrying the
    # check before it turns into a "hard" state and contacts get notified...
    SOFT)
            
        # Determine the host check attempt number; do not run on the first reported error
        # because it may just be a fluke!
        case "$host_state_type_num" in
                
        # Wait until the check has been tried 3 times before changing the outlet state.
        # If the check fails on the 4th time, the state type will turn to "hard" and
        # contacts will be notified of the problem.
        # Hopefully this will change the outlet statee successfully, so the 4th check will
        # result in a "soft" recovery.  If that happens no one gets notified because we
        # fixed the problem!
        3)
            run_snmpsetAPC9210
            ;;
            esac
        ;;
                
    # The host check managed to turn into a hard error without getting fixed.
    # It should have been fixed by the code above, but for some reason it didn't.
    # Let's give it one last try, shall we?  
    # Note: Contacts have already been notified of a problem with the service at this
    # point (unless you disabled notifications for this service)
    HARD)
        run_snmpsetAPC9210
        ;;
    esac
    ;;
esac
exit 0

In Centreon or Nagios, call the command with the following variables in order: $HOSTSTATE$ $HOSTSTATETYPE$ $HOSTATTEMPT$ SNMPCommunity PDU_IPAddress Outlet_Number Outlet_State
The outlet state can be 1-On / 2-Off / 3-Reboot
Screen Shot 2015-08-26 at 2.33.14 PM


 
 

Additional References:

http://thenetworksherpa.com/using-snmp-to-switch-apc-power-outlets/

Be the first to comment

Leave a Reply

Your email address will not be published.


*