X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=snmp-port-status.sh;h=050dd3c6694a41d63ab8776ab17576bdf9fb8a6b;hb=982652b5bb4c17fd00293c14d435deb76cd3b286;hp=43702d701932f5bcfde21a23964b0483351178a2;hpb=eeee8e2cee657964cd86affdd3344c458ecf19c5;p=dell-switch diff --git a/snmp-port-status.sh b/snmp-port-status.sh index 43702d7..050dd3c 100755 --- a/snmp-port-status.sh +++ b/snmp-port-status.sh @@ -1,51 +1,57 @@ -#!/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 ifInErrors ifOutErrors ifInDiscards + echo Usage: "$0" hostname ifAdminStatus BRIDGE-MIB::dot1dStpPortState ifInErrors ifOutErrors ifInDiscards exit 4 fi -log=/dev/shm/port-status/ -test -d $log || mkdir $log +dir=/dev/shm/port-status/ +if [ ! -d $dir ] ; then + mkdir $dir + ln -sv `pwd`/port-status/.git $dir/ +fi 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` -:> $log/$sw +extra=$* +snmpwalk="snmpwalk -Oqs -v2c -Cc -c $COMMUNITY $sw" -for i in `seq 1 $numports`; do - name=`$snmp IF-MIB::ifName.$i` - alias=`$snmp IF-MIB::ifAlias.$i` - if [ "$name" = "No Such Instance currently exists at this OID" ]; then - continue - fi +fping $sw 2>>/dev/shm/dead - iftype=`$snmp IF-MIB::ifType.$i` +:> $dir/$sw -# if [ "$iftype" = "other" ] || [ "$iftype" = propVirtual ] || [ "$iftype" = softwareLoopback ]; then -# continue -# fi - - status=`$snmp IF-MIB::ifOperStatus.$i` - if [ "$status" = "notPresent" ] ; then - continue; - fi +for oid in ifName ifHighSpeed ifOperStatus $extra ifType ifAlias +do + echo -n "# snmpwalk $sw [$oid] = " >/dev/stderr - #descr=`$snmp IF-MIB::ifDescr.$i` - speed=`$snmp IF-MIB::ifSpeed.$i | sed 's/000000//'` + $snmpwalk $oid | cut -d. -f2- | tee $dir/$sw-$oid | wc -l >/dev/stderr - extra="" - for add in "$@"; do - extra="$extra "`$snmp IF-MIB::$add.$i` - done + # put [] around alias and remove empty ones + if [ "$oid" = 'ifAlias' ] ; then + cat $dir/$sw-$oid | sed -e 's/ / [/' -e 's/$/]/' -e 's/ \[\]$//' > $dir/$sw-$oid.new && mv $dir/$sw-$oid.new $dir/$sw-$oid + fi -# echo "## $sw [$name] $iftype $status $descr $speed" - echo "$sw $i $name $speed $status $iftype$extra [$alias]" | tee -a $log/$sw + if [ ! -s $dir/$sw ] ; then + mv $dir/$sw-$oid $dir/$sw + else + join -a 1 $dir/$sw $dir/$sw-$oid > $dir/$sw.new && mv $dir/$sw.new $dir/$sw + rm $dir/$sw-$oid + fi done +# add switch name prefix +cat $dir/$sw | sed "s/^/$sw /" > $dir/$sw.new && mv $dir/$sw.new $dir/$sw + +cat $dir/$sw + +test ! -z "$extra" && exit 0 # don't commit if we have custom oids + +ports_changed=`cd $dir && git diff $sw | grep '^-' | awk '{ print $3 }' | tr '\n' ' '` +if [ ! -z "$ports_changed" ] ; then + cd $dir && git commit -m "$sw : $ports_changed" $dir/$sw +fi