-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtnmSnmpMonitor.sh
executable file
·328 lines (305 loc) · 11.4 KB
/
tnmSnmpMonitor.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#! /bin/bash
#TNM Assignment "GROUP CA"
#Exercise #3: Development of a simple network management application
#start
finish() {
# Your cleanup code here
showTitle
echo
COLUMNS=$(tput cols)
credits="*************************************"
printf "%*s\n" $(((${#credits}+$COLUMNS)/2)) "$credits"
credits="* Thanks for using TNM SNMP MONITOR *"
printf "%*s\n" $(((${#credits}+$COLUMNS)/2)) "$credits"
credits="*************************************"
printf "%*s\n" $(((${#credits}+$COLUMNS)/2)) "$credits"
credits="* Developed By *"
printf "%*s\n" $(((${#credits}+$COLUMNS)/2)) "$credits"
credits="* Abhijeet Viswam - 2019HT12228 *"
printf "%*s\n" $(((${#credits}+$COLUMNS)/2)) "$credits"
credits="* Vella Venkatesh - 2019HT12186 *"
printf "%*s\n" $(((${#credits}+$COLUMNS)/2)) "$credits"
credits="* Rahul Kushwaha - 2019HT66508 *"
printf "%*s\n" $(((${#credits}+$COLUMNS)/2)) "$credits"
credits="*************************************"
printf "%*s\n" $(((${#credits}+$COLUMNS)/2)) "$credits"
sleep 5
pkill -s 0
}
#Cleanup on exit
trap finish EXIT
showTitle(){
#clear screen and prevent scrollback
clear && printf '\e[3J'
echo; echo
COLUMNS=$(tput cols)
title="TNM SNMP MONITOR"
printf "%*s\n" $(((${#title}+$COLUMNS)/2)) "$title"
title="================"
printf "%*s\n" $(((${#title}+$COLUMNS)/2)) "$title"
}
selectOption(){
case $option in
1)
Q_D_SystemDetails
;;
2)
QP_D_statistics
;;
4)
setThresholdForInterfaceStatisticalParam
;;
5)
setThresholdAnyStatisticalParam
;;
*)
echo "!!invalid option!!"
;;
esac
}
Q_D_SystemDetails(){
showTitle
echo -e "Querying and displaying systems details.\n"
sysDescr=`snmpget -v 1 -Oqv -c public localhost 1.3.6.1.2.1.1.1.0 2>/dev/null`
sysObjectId=`snmpget -v 1 -Oqvn -c public localhost 1.3.6.1.2.1.1.2.0 2>/dev/null`
sysUptime=`snmpget -v 1 -Oqv -c public localhost 1.3.6.1.2.1.1.3.0 2>/dev/null`
sysContact=`snmpget -v 1 -Oqv -c public localhost 1.3.6.1.2.1.1.4.0 2>/dev/null`
sysName=`snmpget -v 1 -Oqv -c public localhost 1.3.6.1.2.1.1.5.0 2>/dev/null`
sysLocation=`snmpget -v 1 -Oqv -c public localhost 1.3.6.1.2.1.1.6.0 2>/dev/null`
sysServices=`snmpget -v 1 -Oqv -c public localhost 1.3.6.1.2.1.1.7.0 2>/dev/null`
echo -e "System Description\tOID 1.3.6.1.2.1.1.1.0\t: $sysDescr"
echo -e "sysObjectId\t\tOID 1.3.6.1.2.1.1.2.0\t: $sysObjectId"
echo -e "sysUptime\t\tOID 1.3.6.1.2.1.1.3.0\t: $sysUptime"
echo -e "sysContact\t\tOID 1.3.6.1.2.1.1.4.0\t: $sysContact"
echo -e "sysName\t\t\tOID 1.3.6.1.2.1.1.5.0\t: $sysName"
echo -e "sysLocation\t\tOID 1.3.6.1.2.1.1.6.0\t: $sysLocation"
echo -e "sysServices\t\tOID 1.3.6.1.2.1.1.7.0\t: $sysServices"
echo
echo "Press any key to go back to menu"
read -n1
}
QP_D_statistics(){
showTitle
echo "Querying periodically and displaying the statistics of the interfaces on your system."
ifTotalNum=`snmpget -v 2c -Oqv -c public localhost 1.3.6.1.2.1.2.1.0`
echo -e "\nAvailable interfaces :"
for (( i=1; i<=$ifTotalNum; i++ ))
do
ifDescr=`snmpget -v 2c -Oqv -c public localhost 1.3.6.1.2.1.2.2.1.2.$i`
echo "$i. $ifDescr"
done
echo -en "\nSelect interface to query : "
read ifIndex
re='^[0-9]+$'
if [[ $ifIndex == "" ]] ; then
echo -e "No input provided. Selecting interface numbered 1 by default\n"
ifIndex=1
elif ! [[ $ifIndex =~ $re ]] ; then
echo "error: Not a number";
read -n1
return
elif [[ $ifIndex -gt $ifTotalNum ]] ; then
echo selected index out of range
read -n1
return
fi
echo -n "Enter refresh interval in seconds (default 10 seconds) : "
read period
if [[ $period == "" ]] ; then
period=10
fi
toggleStats=0
while [ 1 ]
do
showTitle
echo -e "Values are refreshed every $period seconds\n"
if [[ $toggleStats == 1 ]] ; then
echo "Full Interface Statistics"
echo "-------------------------"
snmpwalk -v 2c -c public localhost 1.3.6.1.2.1.2
else
echo "Essential Interface Statistics"
echo "------------------------------"
ifDescr=`snmpget -v 2c -Oqv -c public localhost 1.3.6.1.2.1.2.2.1.2.$ifIndex`
echo -e "Description (OID : 1.3.6.1.2.1.2.2.1.2.$ifIndex)\t: $ifDescr"
ifType=`snmpget -v 2c -Oqv -c public localhost 1.3.6.1.2.1.2.2.1.3.$ifIndex`
echo -e "Type (OID : 1.3.6.1.2.1.2.2.1.3.$ifIndex)\t: $ifType"
ifOperStatus=`snmpget -v 2c -Ovq -c public localhost 1.3.6.1.2.1.2.2.1.8.$ifIndex`
echo -e "Operation status (OID : 1.3.6.1.2.1.2.2.1.8.$ifIndex)\t: $ifOperStatus"
echo
ifInUcastPkts=`snmpget -v 2c -Ovq -c public localhost 1.3.6.1.2.1.2.2.1.11.$ifIndex`
echo -e "Unicast packets - IN (OID : 1.3.6.1.2.1.2.2.1.11.$ifIndex)\t: $ifInUcastPkts"
ifInNUcastPkts=`snmpget -v 2c -Ovq -c public localhost 1.3.6.1.2.1.2.2.1.12.$ifIndex`
echo -e "NonUnicast packets - IN (OID : 1.3.6.1.2.1.2.2.1.12.$ifIndex)\t: $ifInNUcastPkts"
ifInDiscards=`snmpget -v 2c -Ovq -c public localhost 1.3.6.1.2.1.2.2.1.13.$ifIndex`
echo -e "Discarded packets - IN (OID : 1.3.6.1.2.1.2.2.1.13.$ifIndex)\t: $ifInDiscards"
ifInErrors=`snmpget -v 2c -Ovq -c public localhost 1.3.6.1.2.1.2.2.1.14.$ifIndex`
echo -e "Error packets - IN (OID : 1.3.6.1.2.1.2.2.1.14.$ifIndex)\t: $ifInErrors"
echo
ifOutUcastPkts=`snmpget -v 2c -Ovq -c public localhost 1.3.6.1.2.1.2.2.1.17.$ifIndex`
echo -e "Unicast packets - OUT (OID : 1.3.6.1.2.1.2.2.1.17.$ifIndex)\t: $ifOutUcastPkts"
ifOutNUcastPkts=`snmpget -v 2c -Ovq -c public localhost 1.3.6.1.2.1.2.2.1.18.$ifIndex`
echo -e "NonUnicast packets - OUT (OID : 1.3.6.1.2.1.2.2.1.18.$ifIndex)\t: $ifOutNUcastPkts"
ifOutDiscards=`snmpget -v 2c -Ovq -c public localhost 1.3.6.1.2.1.2.2.1.19.$ifIndex`
echo -e "Discarded packets - OUT (OID : 1.3.6.1.2.1.2.2.1.19.$ifIndex)\t: $ifOutDiscards"
ifOutErrors=`snmpget -v 2c -Ovq -c public localhost 1.3.6.1.2.1.2.2.1.20.$ifIndex`
echo -e "Error packets - OUT (OID : 1.3.6.1.2.1.2.2.1.20.$ifIndex)\t: $ifOutErrors"
fi
echo
echo "Press 'T' to toggle between essential interface statistics and full interface statistics"
echo Press any other key to go back to menu
read -t $period -n1 isTpressed
if [[ $? == 0 ]] ; then
if [[ $isTpressed == 't' || $isTpressed == 'T' ]] ; then
if [[ $toggleStats == '0' ]] ; then
toggleStats=1
else
toggleStats=0
fi
else
break
fi
fi
done
}
monitorOID(){
parameter=$1
threshold=$2
while [ 1 ]
do
if [[ $parameter != "" ]] ; then
paramVal=`snmpget -v 1 -Oqv -c public localhost $parameter`
if [[ $paramVal -ge $threshold ]] ; then
command -v whiptail >/dev/null
whipAvailable=$?
if [[ !whipAvailable ]] ; then
TERM=vt220 whiptail --title "ALERT!!!" --infobox "Value of $parameter exceeded the threshold $threshold\nCurrentValue = $paramVal\nPress any key to continue" 10 0
else
echo
echo ALERT!!
echo "Value of $parameter exceeded the threshold $threshold:CurrentValue = $paramVal"
fi
exit
fi
else
echo Please enter OID to monitor
break
fi
sleep 3
done
}
setThresholdForInterfaceStatisticalParam(){
showTitle
echo -n "Set thresholds for Interfaces statistical parameter."
ifTotalNum=`snmpget -v 2c -Oqv -c public localhost 1.3.6.1.2.1.2.1.0`
echo -e "\nAvailable interfaces :"
for (( i=1; i<=$ifTotalNum; i++ ))
do
ifDescr=`snmpget -v 2c -Oqv -c public localhost 1.3.6.1.2.1.2.2.1.2.$i`
echo "$i. $ifDescr"
done
echo -en "\nSelect interface to set threshold : "
read ifIndex
re='^[0-9]+$'
if [[ $ifIndex == "" ]] ; then
echo -e "No input provided. Selecting interface numbered 1 by default\n"
ifIndex=1
elif ! [[ $ifIndex =~ $re ]] ; then
echo "error: Not a number";
read -n1
return
elif [[ $ifIndex -gt $ifTotalNum ]] ; then
echo selected index out of range
read -n1
return
fi
showTitle
echo "Essential Interface Statistics"
echo "------------------------------"
ifDescr=`snmpget -v 2c -Oqv -c public localhost 1.3.6.1.2.1.2.2.1.2.$ifIndex`
echo -e "Description (OID : 1.3.6.1.2.1.2.2.1.2.$ifIndex)\t: $ifDescr"
ifType=`snmpget -v 2c -Oqv -c public localhost 1.3.6.1.2.1.2.2.1.3.$ifIndex`
echo -e "Type (OID : 1.3.6.1.2.1.2.2.1.3.$ifIndex)\t: $ifType"
ifOperStatus=`snmpget -v 2c -Ovq -c public localhost 1.3.6.1.2.1.2.2.1.8.$ifIndex`
echo -e "Operation status (OID : 1.3.6.1.2.1.2.2.1.8.$ifIndex)\t: $ifOperStatus"
echo
ifInUcastPkts=`snmpget -v 2c -Ovq -c public localhost 1.3.6.1.2.1.2.2.1.11.$ifIndex`
echo -e "Unicast packets - IN (OID : 1.3.6.1.2.1.2.2.1.11.$ifIndex)\t: $ifInUcastPkts"
ifInNUcastPkts=`snmpget -v 2c -Ovq -c public localhost 1.3.6.1.2.1.2.2.1.12.$ifIndex`
echo -e "NonUnicast packets - IN (OID : 1.3.6.1.2.1.2.2.1.12.$ifIndex)\t: $ifInNUcastPkts"
ifInDiscards=`snmpget -v 2c -Ovq -c public localhost 1.3.6.1.2.1.2.2.1.13.$ifIndex`
echo -e "Discarded packets - IN (OID : 1.3.6.1.2.1.2.2.1.13.$ifIndex)\t: $ifInDiscards"
ifInErrors=`snmpget -v 2c -Ovq -c public localhost 1.3.6.1.2.1.2.2.1.14.$ifIndex`
echo -e "Error packets - IN (OID : 1.3.6.1.2.1.2.2.1.14.$ifIndex)\t: $ifInErrors"
echo
ifOutUcastPkts=`snmpget -v 2c -Ovq -c public localhost 1.3.6.1.2.1.2.2.1.17.$ifIndex`
echo -e "Unicast packets - OUT (OID : 1.3.6.1.2.1.2.2.1.17.$ifIndex)\t: $ifOutUcastPkts"
ifOutNUcastPkts=`snmpget -v 2c -Ovq -c public localhost 1.3.6.1.2.1.2.2.1.18.$ifIndex`
echo -e "NonUnicast packets - OUT (OID : 1.3.6.1.2.1.2.2.1.18.$ifIndex)\t: $ifOutNUcastPkts"
ifOutDiscards=`snmpget -v 2c -Ovq -c public localhost 1.3.6.1.2.1.2.2.1.19.$ifIndex`
echo -e "Discarded packets - OUT (OID : 1.3.6.1.2.1.2.2.1.19.$ifIndex)\t: $ifOutDiscards"
ifOutErrors=`snmpget -v 2c -Ovq -c public localhost 1.3.6.1.2.1.2.2.1.20.$ifIndex`
echo -e "Error packets - OUT (OID : 1.3.6.1.2.1.2.2.1.20.$ifIndex)\t: $ifOutErrors"
setThresholdAnyStatisticalParam
}
setThresholdAnyStatisticalParam(){
echo
echo -n "Set thresholds for statistical parameter.
Enter the statistical parameter : "
read parameter
output=`snmpget -v 1 -Oqv -c public localhost $parameter 2>/dev/null`
if [[ $? != 0 ]] ; then
echo "Invalid OID.
1. Please enter Integer statistical OID correctly (e.g.: 1.3.6.1.2.1.1.1.0).
or press any other key to go to Main Menu"
read -n1 option
if [[ $option == 1 ]] ; then
setThresholdAnyStatisticalParam
return
else
GetUserInput
return
fi
fi
echo -n "Enter threshold for Alert : "
read threshold
monitorOID $parameter $threshold &
echo "Parameter $parameter is being monitored. Press any key to return to main menu"
read -n1
}
GetUserInput(){
while [ 1 ]
do
showTitle
echo "
Choose the options from the list and enter the respective number:
1. Show system details
2. Monitor interface statistics
3. Setup threshold alert
Press 'Q' to quit application"
read -n1 option
echo
if [[ $option == 1 || $option == 2 || $option == 3 ]] ; then
if [[ $option == 3 ]] ; then
echo "Setting threshold for statistical parameter
Choose the options from the list and enter the respective number to :
1. Setting threshold for interface statistical parameter
2. Setting threshold for any integer oriented statistical parameter
3. GoTo Main Menu"
read -n1 subOption
if [[ $subOption == 1 || $subOption == 2 ]] ; then
option=$((option+subOption))
else
echo "!!Redirecting to Main Menu .. Choose the right option:"
continue
fi
fi
selectOption
elif [[ $option == 'q' || $option == 'Q' ]] ; then
break
else
echo "!!Invalid Option!!..Choose the right option:"
fi
done
}
GetUserInput