single snmpget invocation per port, much faster
[dell-switch] / snmp-port-status.sh
1 #!/bin/bash -e
2
3 # modified from https://gist.github.com/krokodilerian/a88b4ae992706c22e0b0
4 # see /var/lib/snmp/mibs/ietf/IF-MIB
5
6 if [ -z "$1" ]; then
7         echo Usage: "$0" hostname ifInErrors ifOutErrors ifInDiscards
8         exit 4
9 fi
10
11 log=/dev/shm/port-status/
12 test -d $log || mkdir $log
13
14 sw="$1"
15 shift # rest of arguments are IfEntry SEQUENCE
16 . ./snmp.conf
17 snmp="snmpget -v 2c -c $COMMUNITY -Cf -Ov -OQ $sw"
18
19 numports=`$snmp IF-MIB::ifNumber.0`
20 :> $log/$sw
21
22 for PORT in `seq 1 $numports`; do
23
24 ##      name=`$snmp IF-MIB::ifName.$i`
25 ##      alias=`$snmp IF-MIB::ifAlias.$i`
26 ##      if [ "$name" = "No Such Instance currently exists at this OID" ]; then
27 ##              continue
28 ##      fi
29 ##
30 ##      iftype=`$snmp IF-MIB::ifType.$i`
31 ##
32 ###     if [ "$iftype" = "other" ] || [ "$iftype" = propVirtual ] || [ "$iftype" = softwareLoopback ]; then
33 ###             continue
34 ###     fi 
35 ##      
36 ##      status=`$snmp IF-MIB::ifOperStatus.$i`
37 ##      if [ "$status" = "notPresent" ] ; then
38 ##              continue;
39 ##      fi
40 ##
41 ##      #descr=`$snmp IF-MIB::ifDescr.$i`
42 ##      #speed=`$snmp IF-MIB::ifSpeed.$i | sed 's/000000//'`
43 ##      speed=`$snmp IF-MIB::ifHighSpeed.$i`
44 ##
45 ##      extra=""
46 ##      for add in "$@"; do
47 ##              extra="$extra "`$snmp IF-MIB::$add.$i`
48 ##      done
49 ##
50 ###     echo "## $sw [$name] $iftype $status $descr $speed"
51 ##      #echo "$sw $i $name $speed $status $iftype$extra [$alias]" | tee -a $log/$sw
52
53         echo "$sw $PORT "`$snmp IF-MIB::ifName.$PORT IF-MIB::ifHighSpeed.$PORT IF-MIB::ifOperStatus.$PORT IF-MIB::ifType.$PORT IF-MIB::ifAlias.$PORT` | grep -v 'No Such Instance currently exists at this OID' | sed 's/\(ethernetCsmacd\) \(..*\)$/\1 [\2]/' | tee -a $log/$sw
54 done
55