174259f71e4402caf29ded2cc6d35bbfb7c73c28
[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
18 snmpwalk="snmpwalk -Oqs -v2c -Cc -c $COMMUNITY $sw"
19
20
21 :> $log/$sw
22
23 for oid in ifName ifHighSpeed ifOperStatus ifType $* ifAlias
24 do
25         echo -n "# snmpwalk $sw [$oid] = "
26
27         $snmpwalk $oid | cut -d. -f2- | tee $log/$sw-$oid | wc -l
28
29         # put [] around alias and remove empty ones
30         if [ "$oid" = 'ifAlias' ] ; then
31                 cat $log/$sw-$oid | sed -e 's/ / [/' -e 's/$/]/' -e 's/ \[\]$//' > $log/$sw-$oid.new && mv $log/$sw-$oid.new $log/$sw-$oid
32         fi
33
34         if [ ! -s $log/$sw ] ; then
35                 mv $log/$sw-$oid $log/$sw
36         else
37                 join -a 1 $log/$sw $log/$sw-$oid > $log/$sw.new && mv $log/$sw.new $log/$sw
38                 rm $log/$sw-$oid
39         fi
40 done
41
42 # add switch name prefix
43 cat $log/$sw | sed "s/^/$sw /" > $log/$sw.new && mv $log/$sw.new $log/$sw
44
45 cat $log/$sw
46
47 exit 1
48
49 snmp="snmpget -v 2c -c $COMMUNITY -Cf -Ov -OQ $sw"
50
51 numports=`$snmp IF-MIB::ifNumber.0`
52 :> $log/$sw
53
54 for PORT in `seq 1 $numports`; do
55
56 ##      name=`$snmp IF-MIB::ifName.$i`
57 ##      alias=`$snmp IF-MIB::ifAlias.$i`
58 ##      if [ "$name" = "No Such Instance currently exists at this OID" ]; then
59 ##              continue
60 ##      fi
61 ##
62 ##      iftype=`$snmp IF-MIB::ifType.$i`
63 ##
64 ###     if [ "$iftype" = "other" ] || [ "$iftype" = propVirtual ] || [ "$iftype" = softwareLoopback ]; then
65 ###             continue
66 ###     fi 
67 ##      
68 ##      status=`$snmp IF-MIB::ifOperStatus.$i`
69 ##      if [ "$status" = "notPresent" ] ; then
70 ##              continue;
71 ##      fi
72 ##
73 ##      #descr=`$snmp IF-MIB::ifDescr.$i`
74 ##      #speed=`$snmp IF-MIB::ifSpeed.$i | sed 's/000000//'`
75 ##      speed=`$snmp IF-MIB::ifHighSpeed.$i`
76 ##
77 ##      extra=""
78 ##      for add in "$@"; do
79 ##              extra="$extra "`$snmp IF-MIB::$add.$i`
80 ##      done
81 ##
82 ###     echo "## $sw [$name] $iftype $status $descr $speed"
83 ##      #echo "$sw $i $name $speed $status $iftype$extra [$alias]" | tee -a $log/$sw
84
85         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
86 done
87
88 exit 1
89
90 ports_changed=`cd $log && git diff $sw | grep '^-' | awk '{ print $3 }' | tr '\n' ' '`
91 if [ ! -z "$ports_changed" ] ; then
92         cd $log && git commit -m "$sw : $ports_changed" $log/$sw
93 fi