-
Notifications
You must be signed in to change notification settings - Fork 2
/
snmp-port-status.sh
executable file
·57 lines (42 loc) · 1.5 KB
/
snmp-port-status.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/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 ifAdminStatus BRIDGE-MIB::dot1dStpPortState ifInErrors ifOutErrors ifInDiscards
exit 4
fi
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
extra=$*
snmpwalk="snmpwalk -Oqs -v2c -Cc -c $COMMUNITY $sw"
fping $sw 2>>/dev/shm/dead
:> $dir/$sw
for oid in ifName ifHighSpeed ifAdminStatus ifOperStatus $extra ifType ifAlias
do
echo -n "# snmpwalk $sw [$oid] = " >/dev/stderr
$snmpwalk $oid | cut -d. -f2- | tee $dir/$sw-$oid | wc -l >/dev/stderr
# 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
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 | grep -v ' down ' | grep -v notPresent
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