/
check_cisco_crc.sh
204 lines (175 loc) · 4.65 KB
/
check_cisco_crc.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/bash
#
# check_cisco_crc.sh - Version 1.0
# check_cisco_crc.sh by @Twibow
# Fork of check_hp_crc.sh by Michael St - 23.10.2014
#
# This Script checks Cisco Switches on every Interface for CRC Errors
#
# MIBS
# get CRC ERRORS on INTERFACES
CRC_MIB="1.3.6.1.4.1.9.2.2.1.1.12"
# RETURN VALUES:
# OK = 0
# WARNING = 1
# CRITICAL = 2
# UNKNOWN = 3
#
LIBEXEC="/usr/bin/snmpwalk"
SNMPC="public"
SNMPV="2c"
HOST=""
WARN=0
CRIT=1
DEBUG=0
CNTIF=0
SETWARN=0
SETCRIT=0
SETUNKNOWN=0
SNMPOUTPUT=""
CRCPORTSWARN=""
CRCPORTSCRIT=""
CRCOKPERF=""
RETURN=3
PERFDATA=0
MODE_SELECTION="crc"
# here is the most important function in this script - the help function
function print_help
{
echo "
./check_hp_crc.sh -H HOSTADRESS -C <SNMP COMMUNITY> -v <SNMP VERSION> -w <NUMBER of CRC ERRORS> -c <NUMBER of CRC ERRORS>
-H = Hostaddress
-C = SNMP Community (optional, if not set we use public)
-v = SNMP Version (optional, if not set we use 2c)
-w = Warning Threshold
-c = Critical Threshold
-p = Enable Performance Data for all Ports
-h = Help - Print this Help!
"
}
#check command line arguments
# Reset in case getopts has been used previously in the shell.
OPTIND=1
while getopts "H:C:v:w:c:hdpm:" opt; do
case "$opt" in
h)
print_help
exit 0
;;
H) HOST=$OPTARG
;;
C) SNMPC=$OPTARG
;;
v) SNMPV=$OPTARG
;;
w) WARN=$OPTARG
;;
c) CRIT=$OPTARG
;;
d) DEBUG=1
;;
p) PERFDATA=1
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
# check if warn is smaller than CRIT &
if [ $WARN -ge $CRIT ]; then
echo "ERROR - If a WARNING-Threshhold is set, you have to set a greater CRITICAL-Threshold too!"
exit 3;
fi
# some debugging: activate with -d in cli
if [ "$DEBUG" -eq "1" ]; then
# debug out
echo [Debug] = "${DEBUG}"
echo [Host] = "${HOST}"
echo [Snmp Community] = "${SNMPC}"
echo [Snmp Version] = "${SNMPV}"
echo [Warn] = "${WARN}"
echo [Crit] = "${CRIT}"
echo [Perfdata] = "$PERFDATA"
echo [Mode slected] = "$MODE_SELECTION"
fi
function getInterfacesCRCErrors {
# some local variables - clear so far
ifarray=""
crcarray=""
IFS=$'\n' #set separator to newline only
# do the snmpwalk and fetch data for all ports on switch
#SNMPOUT=( $(snmpwalk -v 2c -c public 10.255.30.99 RMON-MIB::etherStatsCRCAlignErrors) )
if ! SNMPOUT=( $($LIBEXEC -v $SNMPV -c $SNMPC $HOST $CRC_MIB) ); then
SETUNKNOWN=1
fi
# now lets cut the snmpwalk output to portnumbers and crc-errors
for i in "${SNMPOUT[@]}"
do
ifarray+=( $(echo "$i" | awk -F. '{print $8}' | awk -F " " '{ print $1 }' | awk '/101./' | grep -o '.\{2\}$' | sed 's/^0//' ) )
crcarray+=( $(echo "$i" | awk '/101./' | awk '{print $NF}') )
done
#now go over the array - here happens the magic
for i in ${ifarray[@]}
do
if [ ${crcarray[i]} -ge $CRIT ]; then
if [ $DEBUG -eq 1 ]; then
echo "[DEBUG] CRIT THRESHOLD found: "${crcarray[i]} "CRC-Errors on Port: "${ifarray[i]}
fi
SETCRIT=1
CRCPORTSCRIT+="Port ${ifarray[i]} has ${crcarray[i]} CRC-ERRORS <br>"
elif [ ${crcarray[i]} -ge $WARN -a ${crcarray[i]} -lt $CRIT ]; then
if [ $DEBUG -eq 1 ]; then
echo "[DEBUG] WARN THRESHOLD found: "${crcarray[i]} "CRC-Errors on Port: "${ifarray[i]}
fi
SETWARN=1
CRCPORTSWARN+="Port ${ifarray[i]} has ${crcarray[i]} CRC-ERRORS <br>"
fi
#here we create the performance data
CRCPERF+="Port ${ifarray[i]}=${crcarray[i]};$WARN;$CRIT;; "
done
if [ $SETUNKNOWN -eq 1 ]; then
echo "UNKNOWN"
RETURN=3
elif [ $SETCRIT -eq 0 -a $SETWARN -eq 0 -a $SETUNKNOWN -eq 0 ]; then
if [ $PERFDATA -eq 1 ]; then
echo "OK - Switch has no CRC-Errors | $CRCPERF"
RETURN=0
else
echo "OK - Switch has no CRC-Errors"
RETURN=0
fi
elif [ $SETCRIT -eq 1 ]; then
if [ $PERFDATA -eq 1 ]; then
echo "CRITICAL - $CRCPORTSCRIT | $CRCPERF"
RETURN=2
else
echo "CRITICAL - $CRCPORTSCRIT"
RETURN=2
fi
elif [ $SETWARN -eq 1 ] && [ $SETCRIT -eq 0 ]; then
if [ $PERFDATA -eq 1 ]; then
echo "WARNING - $CRCPORTSWARN | $CRCPERF"
RETURN=1
else
echo "WARNING - $CRCPORTSWARN"
RETURN=1
fi
fi
unset IFS
}
main() {
if [ -z $HOST ]; then
print_help
elif [ "$MODE_SELECTION" != "crc" -a "$MODE_SELECTION" != "packetsin" -a "$MODE_SELECTION" != "packetsout" ]; then
print_help
elif [ "$MODE_SELECTION" == "crc" ]; then
getInterfacesCRCErrors
elif [ "$MODE_SELECTION" == "packetsin" -o "$MODE_SELECTION" == "packetsout" ]; then
getInterfacesPacketErrors
else
print_help
fi
if [ $DEBUG -eq 1 ]; then
echo "[Return Code] = $RETURN"
fi
exit $RETURN
}
main "$@"