First of all I use the snmp 'pass' method to execute a shellscript in snmpd.conf:
Code: Select all
pass .1.3.6.1.4.1.2021.83 /bin/bash /path/to/scripts/haproxy.sh
Code: Select all
#!/bin/bash
###########
#
function getData() {
let OBJTYPE=$1 ; # OID.1.1.1.y
let OBJTYPE--
let OBJID=$2 ; # OID.1.1.X.y
OLDIFS=$IFS
IFS=","
OBJECT=( $( echo "${HAINFO[${OBJID}]}" ) )
IFS=$OLDIFS
RAWVALUE="`echo ${OBJECT[$OBJTYPE]}`"
case "$OBJTYPE" in
33)
RETVAL="integer\n"
RETVAL="$RETVAL${OBJID}\n"
EXITVAL=0
;;
17)
RETVAL="string\n"
RETVAL="$RETVAL${RAWVALUE:-0}\n"
EXITVAL=0
;;
9)
RETVAL="counter\n"
RETVAL="$RETVAL${RAWVALUE:-0}\n"
EXITVAL=0
;;
8)
RETVAL="counter\n"
RETVAL="$RETVAL${RAWVALUE:-0}\n"
EXITVAL=0
;;
1)
RETVAL="string\n"
RETVAL="$RETVAL${RAWVALUE:-0}\n"
EXITVAL=0
;;
0)
RETVAL="string\n"
RETVAL="$RETVAL${RAWVALUE:-0}\n"
EXITVAL=0
;;
*)
RETVAL="integer\n"
RETVAL="$RETVAL${RAWVALUE:-0}\n"
EXITVAL=0
;;
esac
}
function getNext() {
REQUEST=$1
# Always start at .1.1.1.1
if [ "$REQUEST" == "" ]; then
REQUEST=".1.1.1.0"
fi
OBJECTID=`echo $REQUEST | awk 'BEGIN { FS="." } ; { print $5 }'`
OBJECTTYPE=`echo $REQUEST | awk 'BEGIN { FS="." } ; { print $4 }'`
if [ "$OBJECTID" == "" ]; then
let OBJECTID=0
fi
let OBJECTID=$OBJECTID
let OBJECTTYPE=$OBJECTTYPE
# Get next entry
if [ $OBJECTID -lt ${#HAINFO[@]} ]; then
let OBJECTID=${OBJECTID}+1
fi
if [ $OBJECTID -ge ${#HAINFO[@]} ]; then
let OBJECTTYPE=${OBJECTTYPE}+1
let OBJECTID=1
fi
if [ $OBJECTTYPE -gt ${MAXITEMS} ]; then
exit 0
fi
getData ${OBJECTTYPE} ${OBJECTID}
RETOID="$MY_OID.1.1.${OBJECTTYPE}.${OBJECTID}\n"
}
###### Settings ######
MY_OID=".1.3.6.1.4.1.2021.83" ;# Set in /etc/snmp/snmpd.conf
# Arguments
REQ_OID="${2#$MY_OID}" ;# Strip MY_OID from requested OID
REQ_TYPE="$1" ;# n,g(GET),s(SET)
###### MAIN ######
### Log
#logger -t snmp_vsinfo -p debug "SNMP request attempted: $0 $*"
# Obtain haproxy-stats info in HAINFO array and ItemNames in HAITEMS array
OLDIFS=$IFS
IFS=$'\n'
HAINFO=( $( echo "show stat" | socat stdio /var/run/haproxy.stat ) )
IFS=","
HAITEMS=( $( echo "${HAINFO[0]}" | grep \# | cut -d \ -f 2- ) )
IFS=$OLDIFS
let MAXITEMS=${#HAITEMS[@]}+1
# Check request
case "${REQ_TYPE}" in
-n)
getNext $REQ_OID
;;
-s)
### Someone tried to set a value... log for analysis
logger -t snmp_haproxy -p crit "SET attempted $0 $*"
exit 0
;;
-g)
### GetValue
REQUEST=$REQ_OID
OBJECTID=`echo $REQUEST | awk 'BEGIN { FS="." } ; { print $5 }'`
OBJECTTYPE=`echo $REQUEST | awk 'BEGIN { FS="." } ; { print $4 }'`
getData ${OBJECTTYPE} ${OBJECTID}
RETOID="$MY_OID.1.1.${OBJECTTYPE}.${OBJECTID}\n"
;;
*)
exit 0
;;
esac
printf "${RETOID}${RETVAL}"
exit ${EXITVAL}