X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=snmp-port-status.sh;h=4ebea7465f7990b98c6bd76334eed20a90e350fc;hb=dd6776bd8fdbe5e15f2c8928b397512ef15c8f61;hp=158917b8c7599c704344af5bcc59b3a2e411e484;hpb=cf2a51aa94bf9ecba2fdba6e30dfbc74ef6f0bad;p=dell-switch diff --git a/snmp-port-status.sh b/snmp-port-status.sh index 158917b..4ebea74 100755 --- a/snmp-port-status.sh +++ b/snmp-port-status.sh @@ -1,10 +1,10 @@ -#!/bin/bash +#!/bin/bash -e # modified from https://gist.github.com/krokodilerian/a88b4ae992706c22e0b0 # see /var/lib/snmp/mibs/ietf/IF-MIB if [ -z "$1" ]; then - echo Usage: "$0" hostname + echo Usage: "$0" hostname ifAdminStatus BRIDGE-MIB::dot1dStpPortState ifInErrors ifOutErrors ifInDiscards exit 4 fi @@ -12,33 +12,43 @@ log=/dev/shm/port-status/ test -d $log || mkdir $log sw="$1" +shift # rest of arguments are IfEntry SEQUENCE . ./snmp.conf -snmp="snmpget -v 2c -c $COMMUNITY -Cf -Ov -OQ $sw" -numports=`$snmp IF-MIB::ifNumber.0` +extra=$* +snmpwalk="snmpwalk -Oqs -v2c -Cc -c $COMMUNITY $sw" + +fping $sw 2>>/dev/shm/dead + :> $log/$sw -for i in `seq 1 $numports`; do - name=`$snmp IF-MIB::ifAlias.$i` - if [ "$name" = "No Such Instance currently exists at this OID" ]; then - continue - fi +for oid in ifName ifHighSpeed ifOperStatus $extra ifType ifAlias +do + echo -n "# snmpwalk $sw [$oid] = " >/dev/stderr - iftype=`$snmp IF-MIB::ifType.$i` + $snmpwalk $oid | cut -d. -f2- | tee $log/$sw-$oid | wc -l >/dev/stderr -# if [ "$iftype" = "other" ] || [ "$iftype" = propVirtual ] || [ "$iftype" = softwareLoopback ]; then -# continue -# fi - - status=`$snmp IF-MIB::ifOperStatus.$i` - if [ "$status" = "notPresent" ] ; then - continue; + # put [] around alias and remove empty ones + if [ "$oid" = 'ifAlias' ] ; then + cat $log/$sw-$oid | sed -e 's/ / [/' -e 's/$/]/' -e 's/ \[\]$//' > $log/$sw-$oid.new && mv $log/$sw-$oid.new $log/$sw-$oid fi - #descr=`$snmp IF-MIB::ifDescr.$i` - speed=`$snmp IF-MIB::ifSpeed.$i | sed 's/000000//'` - -# echo "## $sw [$name] $iftype $status $descr $speed" - echo "$sw $i $speed $status [$name]" | tee -a $log/$sw + if [ ! -s $log/$sw ] ; then + mv $log/$sw-$oid $log/$sw + else + join -a 1 $log/$sw $log/$sw-$oid > $log/$sw.new && mv $log/$sw.new $log/$sw + rm $log/$sw-$oid + fi done +# add switch name prefix +cat $log/$sw | sed "s/^/$sw /" > $log/$sw.new && mv $log/$sw.new $log/$sw + +cat $log/$sw + +test ! -z "$extra" && exit 0 # don't commit if we have custom oids + +ports_changed=`cd $log && git diff $sw | grep '^-' | awk '{ print $3 }' | tr '\n' ' '` +if [ ! -z "$ports_changed" ] ; then + cd $log && git commit -m "$sw : $ports_changed" $log/$sw +fi