-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcheck_traffic.sh
1956 lines (1654 loc) · 56.2 KB
/
check_traffic.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#########################################################################
#
# File: check_traffic.sh
# Description: Nagios check plugins to check network interface traffic with SNMP run in *nix.
# Language: GNU Bourne-Again SHell
# Version: 1.4.0
# Date: 2013-11-05
# Corp.: Chenlei
# Author: cloved@gmail.com, chnl@163.com (U can msn me with this), QQ 31017671
# WWW: http://www.itnms.info
# Perl Version: U Can find the perl/Net::SNMP Version in the same site.
#########################################################################
# Bugs:
# The Latest Version will be released in http://bbs.itnms.info.
# You can send bugs to http://bbs.itnms.info,
# or email to me directly: chnl@163.com or cloved@gmail.com
#########################################################################
# Todo:
# Do not use unit at performance data, for pnp/rrd graphing better.
# Add the History performance data file support.(maybe)
# Multi hosts and mult interfaces check function is a quick release, \
# need to review and process the code.
#########################################################################
# ChangeLog:
#
# Version 1.4.0
# 2013-11-05
# Fix bug for Conter64 check.
#
# Version 1.3.11
# 2013-10-09
# Add -Oa option to all snmplwak/snmpget command.
#
# Version 1.3.10
# 2013-01-29
# Set the Default TIMEOUT. Thanks to martin.leeyd.
#
# Version 1.3.9
# 2012-12-05
# Use 'id --user --name' instead of '$USER'
#
# Version 1.3.8
# 2012-09-28
# Add -Oa option to snmplwak command with List Interface function.
#
# Version 1.3.7
# 2012-09-25
# Fix spelling mkstakes of help msgs.
#
# Version 1.3.6
# 2012-09-25
# Fix bug of Conversion K to M. With bps use 1000, with B/s use 1024;
#
# Version 1.3.5
# 2012-09-04
# Give out more suitable help messages when can not get the interface index. Thanks to Jack.
#
# Version 1.3.4
# 2012-08-17
# Fix some spelling mistake. Thanks, Jack.
#
# Version 1.3.3
# 2012-08-16
# 1)Fix some spelling mistake;
# 2)Add support with multi hosts and multi interfaces checks.
#
# Version 1.3.2
# 2012-08-15
# Add -N args support with multi interface checks
#
# Version 1.3.1
# 2012-08-13
# Fix history data time update bug.
#
# Version 1.3.0
# 2012-08-10
# Add support with the -N args, with interface name when to check the single interface.
#
# Version 1.2.12
# 2012-05-20
# Fix bugs for message output.
#
# Version 1.2.11
# 2012-05-20
# Use snmpget instead of snmpwalk for reducing CPU footprint.
#
# Version 1.2.10
# 2012-05-12
# 1)bug fix for debug log
# 2)add "J" to suffix when Jitter option was set
# 3)add "R" to suffix when Range option was set
# 4)some tips in help messages fix
#
# Version 1.2.9
# 2012-05-10
# Fix bugs of the Same host Multi interfaces traffic aggregating and jitter calculating.
#
# Version 1.2.8
# 2012-04-26
# 1) Add the support for multi interfaces checking (in the same host/device) and traffic aggregation.
# Example: -I 2,3 or -I 10,12,16,18
# 2) Add the default suffix "itnms"
# 3) Check bc command
#
# Version 1.2.7
# 2012-04-05
# 1) Add "exit $Severity" at line 927. fix bug for Jitter exit value. Thanks gouldchu.
# 2) U can use "-F s/S" to control the format and get less output.
#
# Version 1.2.6
# 2011-12-13
# Spelling fix, from itnms.net to itnms.info.
#
# Version 1.2.5
# 2011-10-18
# Spelling fix.
#
# Version 1.2.4
# 2011-10-12
# Add snmp v3 support.
#
# Version 1.2.3
# 2011-09-05
# Fix bugs when high traffic occurs -- ifSpeed check error
#
# Version 1.2.2
# 2011-03-11
# Fix bugs with overflow detection. Thanks for jans1086.
#
# Version 1.2.1
# 2010-11-19
# Fix some spelling mistake;
#
# Version 1.2.0
# 2010-04-26
# Change default Scale value from 4 to 2, for more friendly output. If you want more excat value, U can change it for youself.
# Fix the Min Interval check bugs, move the check before write the current data to data hist file.
# Add the Max Interval var, default value is 1800, if the hist data file is too old, drop the result.
# Fix the for output "Maybe 32 bit counter overflow, because we got a negative value here." when check too frequent. And set Min_Interval to 30 as default.
# Add the -F option for simple or more simple(s/S) output format.
# Add the -i option for the individual suffix with the CF/STAT_HIST_DATA if necessary.
# Use the 64bit counter as default, when snmp version is v2c. If the system not support it, use 32bit counter instead.
# Get the interface's IF-MIB::ifSpeed, if the traffic value is bigger than it, drop it and output with OOPS and exit with Unkown.
# Modify the CF_HIST_DATA file name from "/var/tmp/check_traffic_${Host}_${Interface}.hist_dat" to
# "/var/tmp/check_traffic_${Host}_${Interface}.hist_dat_${USER}_64|32" for resolving
# 1)user and
# 2)64/32bit
# transfer problem which make
# 1)the hist data file read/write error and
# 2)with huge error traffic value.
# Add the function for testing Traffic Jitter(The orginal idea come from msn chat wiht wjks@hotmail.com).
# Add a option -p N, N(suggest values is from 4 to 12) is a number that we comare this time value with the average value of previos N times
# we had been checked.
# If the value we checked this time is not in our defined scope(a % value) with -w/-c option specified(such as -w 20,20 -c30,30 ),
# we think that it is a traffic jitter.
# For this option, add a file for storing the hist data to stat: /var/tmp/check_traffic_${Host}_${Interface}.hist_dat_${USER}_64|32_ctj_$Num"
#
# Version 1.1.6
# 2009-02-20
# Fix some mistake at perfdata output format with Warning and Critical Value. (thanks for Jiang Shan)
# Remove the redundant code, merge code for --range option.
# Write data before the exit(for the reason of IsFirst), for the next time use.
# When write or read file error, use Unknow instead of Warning severity.
# If get a negative netflow or time interval value here, exit with unknow.
#
# Version 1.1.5
# 2008-09-28
# Fix bug on perfdata output format;
# Fix some spelling mistake;
# Add the Default Value for UseRange as "False";
#
# Version 1.1.4
# 2008-09-18
# Add -r options, Use Range instead of single value in warning and critical Threshold;
# This option suggestion by zhgypg@hotmail.com at http://www.itnms.info/thread-1220-1-1.html
#
# Version 1.1.3
# 2008-09-17
# Set the default Interval as 12 seconds;
#
# Version 1.1.2
# 2008-08-19
# Check the snmp agent support the 64 bit counter or not;
# Check the interface status, if not OK, exit with Critical status;
# Get the interface name with Interface Index Value;
#
# Version 1.1.1
# 2008-08-06
# Fix some bugs in version compare.
# Use -6 option, use 64 bit counter.
#
# Version 1.1.0
# 2008-06-11
# In snmp v2c, use counters ifHC* instead of if*;
#
# Version 1.0.9
# 2008-04-22
# More friendly output when getting snmp info error.
#
# Version 1.0.8
# 2008-03-31
# Correct some spelling mistake
#
# Version 1.0.7
# 2008-03-28
# If it's the first time to touch hist_dat, echo OK and Tips out;
# Test the hist_dat can be read and write;
# Use the Vars for $OutPut and $PerfData
# Fix some output format.
#
# Version 1.0.6
# 2008-03-25
# Correct Performance data output with Warning and Critical Value of Total and Interval for pnp graphing.
#
# Version 1.0.5
# 2008-03-24
# Correct Performance data output for pnp graphing.
#
# Version 1.0.4
# 2008-03-21
# Correct Performance data output to "Nagios plug-in development guidelines",
# for Graphing the performance data in the web with PNP.
# The standard is: 'label'=value[UOM];[warn];[crit];[min];[max]
#
# Version 1.0.3
# 2008-03-20
# More friendly output with function list_interface().
#
# Version 1.0.2
# 2008-03-06
# Fix some coding bugs;
# Add the -L support;
# -K/-M to speicify in K or M (bps,B/s);
# -B/-b switch to B/s or bps;
# Add Total traffic value in output
#
# Version 1.0.1
# 2008-02-28
# Fix two cacl bugs at line 212 and 244.
# In print_full_help_msg(), '$$' instead $$.
#
# Version 1.0
# 2008-02-27
# Original Version.
#########################################################################
# Heh, just a ad here :), for my honey.
# http://shop35165045.taobao.com/
############################
#
# Exit values:
# ------------
# 0 OK
# 1 Warning
# 2 Cirital
# 3 Unknown
# Others Unknown
#
# ----------------------------------------------------------------------
# These are Parameters from external
# -h
# Get the help message
#
# -v
# Verbose mode, to debug some messages out to the /tmp directory with log file name check_traffic.$$.
#
# -V 1|2c|3
# Specify the version of snmp
#
# -C Community
# Specify the Community
#
# -H host
# Specify the host
#
# -6 Use 64 bit counter, ifHC* instead of if*.
#
# -r Use Range instead of single value in warning and critical Threshold;
#
# -I interface
# Specify the interface
#
# -N interface name
# Specify the interface name
#
# -L List all Interfaces on specify host
#
# -B/b Switch to B/s or bps, default is -b, bps
#
# -K/M Switch to K or M (bsp,B/s), default is -K
#
# -w Warning value Kbps, in and out
# -c Critical value Kbps, in and out
# Set Warning and Critical Traffic value
# -F s/S
# Simple or more simple output format
#
# -p number
# It is a number that we comare this time value with the average value of previos N times we had been checked.
# Suggestion values is from 4 to 12.
#
# -i suffix
# It's the individual suffix with the CF/STAT_HIST_DATA if necessary.
# -F s/S
# Get less output with s or S option.
unset LANG
Scale=2
Unit_1="K"
Unit_2="bps"
UseRange="False"
ifIn32="ifInOctets"
ifOut32="ifOutOctets"
ifIn64="ifHCInOctets"
ifOut64="ifHCOutOctets"
# Set the Min Interval of Check.
Min_Interval=30
Max_Interval=1800
# Set the Default TIMEOUT.
Timeout=15
print_help_msg(){
print_version
$Echo "Usage: $0 -h to get help."
$Echo
$Echo 'Report bugs to: cloved@gmail.com'
$Echo 'Home page: <http://bbs.itnms.info/forum.php?mod=viewthread&tid=767&extra=page%3D1>'
$Echo 'Geting help: <http://bbs.itnms.info/forum.php?mod=forumdisplay&fid=10&page=1> or Email to: cloved@gmail.com'
}
print_full_help_msg(){
print_version
$Echo "Usage:"
$Echo "$0 [ -v ] [ -6 ] [ -i Suffix ] [ -F s|S ] [-p N] [ -r ] -V 1|2c|3 ( -C snmp-community | -A \"AuthString\" (when use snmp v3, U must give the AuthString)) -H host [ -L ] (-I interface|-N interface name) -w in,out-warning-value -c in,out-critical-value -K/M -B/b "
$Echo "Example:"
$Echo "${0} -V 2c -C public -H 127.0.0.1 -I 4 -w 200,100 -c 300,200 -K -B"
$Echo "Or"
$Echo "${0} -V 2c -C public -H 127.0.0.1 -N FastEthernet0/1 -w 200,100 -c 300,200 -K -B"
$Echo "Or -r to use Range Value Options:"
$Echo "${0} -V 2c -C public -H 127.0.0.1 -I 4 -r -w 200-300,100-200 -c 100-400,50-250 -K -B"
$Echo "Or"
$Echo "${0} -V 2c -C public -H 127.0.0.1 -N eth0 -r -w 200-300,100-200 -c 100-400,50-250 -K -B"
$Echo "Or -p N to use Traffic Jitter Options:"
$Echo "${0} -V 2c -C public -H 127.0.0.1 -I 4 -p 8 -w 45,45 -c 55,55"
$Echo "Or"
$Echo "${0} -V 2c -C public -H 127.0.0.1 -N eth0 -p 8 -w 45,45 -c 55,55"
$Echo
$Echo "Or for single host and multi interfaces checking (in the same host/device) and traffic aggregation:"
$Echo "${0} -V 2c -C public -H 127.0.0.1 -I 2,3,8,9 -w 200,100 -c 300,200 -K -B"
$Echo "${0} -V 2c -C public -H 127.0.0.1 -N FastEthernet0/1,FastEthernet0/2 -w 200,100 -c 300,200 -K -B"
$Echo "Or -r to use Range Value Options:"
$Echo "${0} -V 2c -C public -H 127.0.0.1 -I 2,3,8,9 -r -w 200-300,100-200 -c 100-400,50-250 -K -B"
$Echo "${0} -V 2c -C public -H 127.0.0.1 -I -N FastEthernet0/1,FastEthernet0/2 -r -w 200-300,100-200 -c 100-400,50-250 -K -B"
$Echo "Or -p N to use Traffic Jitter Options:"
$Echo "${0} -V 2c -C public -H 127.0.0.1 -I 2,3,8,9 -p 8 -w 45,45 -c 55,55"
$Echo "${0} -V 2c -C public -H 127.0.0.1 -N FastEthernet0/1,FastEthernet0/2 -p 8 -w 45,45 -c 55,55"
$Echo
$Echo "Or for multi hosts and mult interfaces checking (in the multi hosts/devices) and traffic aggregation:"
$Echo "${0} -V 2c,1 -C public,private -H 127.0.0.1,10.76.2.15,10.7.4.18 -I 2,2,1 -w 200,100 -c 300,200 -K -B"
$Echo "${0} -V 2c,1 -C public,private -H 127.0.0.1,10.76.2.15,10.7.4.18 -N FastEthernet0/20,FastEthernet0/2,eth0 -w200,100 -c300,200 -KB"
$Echo "Or -r to use Range Value Options:"
$Echo "${0} -V 2c,1 -C public,private -H 127.0.0.1,192.168.1.1 -I 2,3 -w 200-300,100-200 -c 100-400,50-250 -K -B"
$Echo "${0} -V 2c,1 -C public,private -H 127.0.0.1,192.168.1.1 -N FastEthernet0/8,FastEthernet0/2 -r -w 200-300,100-200 -c 100-400,50-250 -K -B"
$Echo "Or -p N to use Traffic Jitter Options:"
$Echo "${0} -V 2c,1 -C public,private -H 127.0.0.1,192.168.1.1 -I 2,3 -p 8 -w 45,45 -c 55,55"
$Echo "${0} -V 2c,1 -C public,private -H 127.0.0.1,192.168.1.1 -N FastEthernet0/21,FastEthernet0/24 -p 8 -w 45,45 -c 55,55"
$Echo
$Echo "If you do not use -K/M -B/b options, default -K -b, corresponding to Kbps."
$Echo "Make sure that the check interval greater than 30 Seconds."
$Echo "Or modify the Min_Interval default value as you need "
$Echo 'And, if you want in Verbose mode, use -v, to check the debug messages in the file /tmp/check_traffic.$$.'
$Echo
$Echo "Or use $0 [ -v ] -V 1|2c|3 -C snmp-community -H host -L "
$Echo "To list all interfaces on the host."
$Echo
$Echo "Or check for snmp v3 device:"
$Echo "${0} -V 3 -A \"-u kschmidt -l authPriv -a MD5 -A mysecretpass -x DES -X mypassphrase\" -H 127.0.0.1 -I 4 -w 200,100 -c 300,200 -K -B"
$Echo "Or"
$Echo "${0} -V 3 -A \"-u kschmidt -l authPriv -a MD5 -A mysecretpass -x DES -X mypassphrase\" -H 127.0.0.1 -N eth0 -w 200,100 -c 300,200 -K -B"
$Echo
$Echo
$Echo 'Report bugs to: cloved@gmail.com'
$Echo 'Home page: <http://bbs.itnms.info/forum.php?mod=viewthread&tid=767&extra=page%3D1>'
$Echo 'Geting help: <http://bbs.itnms.info/forum.php?mod=forumdisplay&fid=10&page=1> or Email to: cloved@gmail.com'
}
print_version(){
$Echo $(cat $0 | head -n 7 | tail -n 1|sed 's/\# //')
}
print_err_msg(){
$Echo "Error."
print_full_help_msg
}
check_record_cnt(){
echo $2 | awk -F "$1" '{print NF}'
}
list_interface(){
$SNMPWALK -v $Version $Community $Host "IF-MIB::ifDescr" |sed 's/IF-MIB::ifDescr./Interface index /g' | sed 's/= STRING:/orresponding to /g'
#exit 3
}
get_interface_index(){
intNames="$1"
intIndex=""
intNameList=$(echo $intNames|sed 's/,/ /g')
for intName in $intNameList
do
intIndex="$intIndex "$($SNMPWALK -v $Version $Community $Host "IF-MIB::ifDescr" |awk -F 'STRING: ' '{if ($2 == "'$intName'")print $0}' | awk -F '=' '{print $1}' | sed 's/IF-MIB::ifDescr.//')
done
}
gen_string(){
string_num=$1
string_i=1
string_t=""
string_p="NA "
while [ $string_i -le $string_num ]
do
string_t="$string_t$string_p"
string_i=`expr $string_i + 1`
done
}
#adjust_value(){
# if [ `echo "$1 < 1" | bc` -eq 1 ]; then
# return "0"${$1} # if -lt 1, will error at: return: 0.1: numeric argument required
# else
# return $1
# fi
#}
to_debug(){
if [ "$Debug" = "true" ]; then
$Echo "$*" >> /tmp/check_traffic.log.$$ 2>&1
#$Echo "$*" >> /tmp/check_traffic.log 2>&1
if [ "$zDebug" = "true" ]; then
$Echo "$*"
fi
fi
}
case "$(uname -s)"
in
SunOS)
Echo="echo"
;;
Linux)
Echo="echo -e"
;;
*)
Echo="echo"
;;
esac
if [ $# -lt 1 ]; then
print_help_msg
exit 3
else
while getopts :vz6rhi:p:F:V:C:A:H:I:N:LKMBbw:c: OPTION
do
case $OPTION
in
v)
#$Echo "Verbose mode."
Debug=true
;;
z)
zDebug=true
;;
V)
Version=$OPTARG
if [ $Version == "3" ]; then
SnmpVersion=3
fi
;;
C)
Community=$OPTARG
;;
A)
AuthString=$OPTARG
;;
i)
Suffix="$OPTARG"
;;
F)
Format="$OPTARG"
;;
6)
Bit64="True"
;;
p)
Num="$OPTARG"
TrafficJitter="True"
;;
r)
UseRange="True"
;;
H)
Host=$OPTARG
;;
L)
ListInt="True"
;;
I)
Interface=$OPTARG
;;
N)
UseIntName="True"
InterfaceName=$OPTARG
;;
w)
WarningV=$OPTARG
;;
c)
CriticalV=$OPTARG
;;
M)
isM="True"
Unit_1="M"
;;
K)
;;
B)
isB="True"
Unit_2="B"
;;
b)
;;
h)
print_full_help_msg
exit 3
;;
?)
$Echo "Error: Illegal Option."
print_help_msg
exit 3
;;
esac
done
fi
SNMPWALK=`which snmpwalk 2>&1`
if [ $? -ne 0 ];then
$Echo $SNMPWALK
$Echo "Can not found command snmpwalk in you system PATH: $PATH, pleas check it"
exit 3
fi
SNMPWALK="$SNMPWALK -t $Timeout -Oa"
to_debug Use $SNMPWALK to check traffic
SNMPGET=`which snmpget 2>&1`
if [ $? -ne 0 ];then
$Echo $SNMPGET
$Echo "Can not found command snmpget in you system PATH: $PATH, pleas check it"
exit 3
fi
SNMPGET="$SNMPGET -t $Timeout -Oa"
to_debug Use $SNMPGET to check traffic
BC=`which bc 2>&1`
if [ $? -ne 0 ];then
$Echo $BC
$Echo "Can not found command bc in you system PATH: $PATH, pleas check it"
exit 3
fi
to_debug Use $BC to calculate
if [ ! -z "$Interface" -a ! -z "$InterfaceName" ] ; then
$Echo "Please Use -N or -I only"
print_help_msg
exit 3
fi
mmHostCnt=`check_record_cnt "," "$Host"`
mmCommunityCnt=`check_record_cnt "," "$Community"`
mmVersionCnt=`check_record_cnt "," "$Version"`
if [ "$UseIntName""ZZ" == "TrueZZ" ]; then
Interface="$InterfaceName"
fi
mmIntCnt=`check_record_cnt "," "$Interface"`
if [ $mmHostCnt -gt 1 ]; then
#MM
if [ $mmHostCnt -lt 1 -o $mmCommunityCnt -lt 1 -o $mmVersionCnt -lt 1 -o $mmIntCnt -lt 1 ]; then
$Echo "Args Error."
print_full_help_msg
exit 3
fi
if [ $mmHostCnt -ne $mmCommunityCnt -o $mmCommunityCnt -ne $mmVersionCnt -o $mmVersionCnt -ne $mmIntCnt ]; then
$Echo "Args Error."
print_full_help_msg
exit 3
fi
mmHostList=$(echo $Host|sed 's/,/ /g')
mmCommunityList=$(echo $Community|sed 's/,/ /g')
mmVersionList=$(echo $Version|sed 's/,/ /g')
mmIntList=$(echo $Interface|sed 's/,/ /g')
mmIntNameList=$(echo $InterfaceName|sed 's/,/ /g')
mmHostArray=($mmHostList)
mmCommunityArray=($mmCommunityList)
mmVersionArray=($mmVersionList)
mmIntArray=($mmIntList)
mmIntNameArray=($mmIntNameList)
##MMArray Data Struct
#MMArray[0](Host Community Version Interface InterfaceName)
# 0 1 2 3 4
#MMArray[...]
##
declare -a MMArray
Columns=5
Rows=$mmHostCnt
#init ctbspIn/ctbspOut
ctbpsIn=0
ctbpsOut=0
#init ctDiffRIAVG/ctDiffROAVG
ctDiffRIAVG=0
ctDiffROAVG=0
for mmII in `seq 1 $mmHostCnt`
do
let "aIndex = $mmII - 1"
let "mmIndex = $aIndex * $Columns + 0"
MMArray[$mmIndex]=${mmHostArray[$aIndex]}
Host=${MMArray[$mmIndex]}
to_debug MMArray host "${MMArray[$mmIndex]}"
let "mmIndex = $aIndex * $Columns + 1"
MMArray[$mmIndex]=${mmCommunityArray[$aIndex]}
Community=${MMArray[$mmIndex]}
to_debug MMArray community "${MMArray[$mmIndex]}"
let "mmIndex = $aIndex * $Columns + 2"
MMArray[$mmIndex]=${mmVersionArray[$aIndex]}
Version=${MMArray[$mmIndex]}
to_debug MMArray version "${MMArray[$mmIndex]}"
let "mmIndex = $aIndex * $Columns + 3"
MMArray[$mmIndex]=${mmIntArray[$aIndex]}
Interface=${MMArray[$mmIndex]}
to_debug MMArray int "${MMArray[$mmIndex]}"
let "mmIndex = $aIndex * $Columns + 4"
MMArray[$mmIndex]=${mmIntNameArray[$aIndex]}
InterfaceName=${MMArray[$mmIndex]}
to_debug MMArray int "${MMArray[$mmIndex]}"
if [ -z "$Version" -o -z "$Host" ] ; then
$Echo "Args Error."
print_full_help_msg
exit 3
fi
if [ "$SnmpVersion" = "3" ]; then
if [ -z "$AuthString" ]; then
$Echo "Args Error."
print_full_help_msg
exit 3
else
Community="$AuthString"
fi
else
if [ -z "$Community" ]; then
$Echo "Args Error."
print_full_help_msg
exit 3
else
Community=" -c $Community"
fi
fi
if [ "$UseIntName""ZZ" == "TrueZZ" ]; then
get_interface_index $InterfaceName
Interface=$intIndex
if [ -z "$Interface" -o "$Interface" == " " ] ; then
$Echo Can not get the interface index with "$InterfaceName" at "$Host".
exit 3
fi
Interface=$(echo $Interface|sed 's/ /,/g')
fi
if [ "$ListInt" = "True" ]; then
$Echo "List Interface for host $Host."
list_interface
exit 3
fi
if [ -z "$Interface" -o -z "$WarningV" -o -z "$CriticalV" ] ; then
$Echo "Args Error."
print_full_help_msg
exit 3
fi
to_debug All Values are \" Warning: "$WarningV" and Critical: "$CriticalV" \".
WVC=`check_record_cnt "," "$WarningV"`
CVC=`check_record_cnt "," "$CriticalV"`
to_debug WVC is $WVC and CVC is $CVC
if [ $UseRange = "True" ] ;then
to_debug UseRange is True
#####################
if [ $WVC -ne 2 -o $CVC -ne 2 ] ; then
$Echo "Warning and Critical Value error."
print_full_help_msg
exit 3
else
W1=`echo $WarningV| awk -F "," '{print $1}'`
W1b=`echo $W1| awk -F "-" '{print $1}'`
W1e=`echo $W1| awk -F "-" '{print $2}'`
W2=`echo $WarningV| awk -F "," '{print $2}'`
W2b=`echo $W2| awk -F "-" '{print $1}'`
W2e=`echo $W2| awk -F "-" '{print $2}'`
Wtb=`echo "$W1b + $W2b"|bc`
Wte=`echo "$W1e + $W2e"|bc`
to_debug Warning Value is $W1 $W2 $Wtb $Wte
C1=`echo $CriticalV| awk -F "," '{print $1}'`
C1b=`echo $C1| awk -F "-" '{print $1}'`
C1e=`echo $C1| awk -F "-" '{print $2}'`
C2=`echo $CriticalV| awk -F "," '{print $2}'`
C2b=`echo $C2| awk -F "-" '{print $1}'`
C2e=`echo $C2| awk -F "-" '{print $2}'`
Ctb=`echo "$C1b + $C2b"|bc`
Cte=`echo "$C1e + $C2e"|bc`
to_debug Critical Value is $C1 $C2 $Ctb $Cte
check_1b=`echo "$C1b < $W1b" | bc`
check_1e=`echo "$C1e > $W1e" | bc`
check_2b=`echo "$C2b < $W2b" | bc`
check_2e=`echo "$C2e > $W2e" | bc`
to_debug check_1 is $check_1b , $check_1e check_2 is $check_2b $check_2e
if [ $check_1b -ne 1 -o $check_1e -ne 1 -o $check_2b -ne 1 -o $check_2e -ne 1 ] ; then
$Echo "Error, the corresponding Critical End value must greater than Warning End value, And Critical Begin value must less than Warning End value"
print_full_help_msg
exit 3
fi
fi
#####################
else
to_debug Use Range is False
if [ $WVC -ne 2 -o $CVC -ne 2 ] ; then
$Echo "Warning and Critical Value error."
print_full_help_msg
exit 3
else
W1=`echo $WarningV| awk -F "," '{print $1}'`
W2=`echo $WarningV| awk -F "," '{print $2}'`
Wt=`echo "$W1 + $W2"|bc`
to_debug Warning Value is $W1 $W2 $Wt
C1=`echo $CriticalV| awk -F "," '{print $1}'`
C2=`echo $CriticalV| awk -F "," '{print $2}'`
Ct=`echo "$C1 + $C2"|bc`
to_debug Critical Value is $C1 $C2 $Ct
check_1=`echo "$C1 > $W1" | bc`
check_2=`echo "$C2 > $W2" | bc`
to_debug check_1 is $check_1 , check_2 is $check_2
if [ $check_1 -ne 1 -o $check_2 -ne 1 ] ; then
$Echo "Error, the corresponding Critical value must greater than Warning value."
print_full_help_msg
exit 3
fi
fi
fi
if [ -z $Suffix ]; then
Suffix=itnms
fi
if [ $TrafficJitter"AA" = "TrueAA" ]; then
Suffix=${Suffix}J
fi
if [ $UseRange = "True" ] ;then
Suffix=${Suffix}R
fi
Username=`id --user --name`
# This file will save the traffic data from previos check.
# Make sure it will never be deleted.
CF_HIST_DATA="/var/tmp/check_traffic_${Host}_${Interface}_MM_${Username}_${Suffix}.hist_dat"
to_debug CF_HIST_DATA "$CF_HIST_DATA"
Time=`date +%s`
ifName="`$SNMPGET -v $Version $Community $Host IF-MIB::ifDescr.${Interface}| awk -F ":" '{print $4}'`"
ifSpeed="`$SNMPGET -v $Version $Community $Host IF-MIB::ifSpeed.${Interface}| awk -F ":" '{print $4}'`"
Flag64Content=`$SNMPGET -v $Version $Community $Host IF-MIB::ifHCOutOctets.${Interface} 2>&1`
if [ $? -eq 0 ]; then
echo $Flag64Content |grep Counter64 >/dev/null 2>&1
Flag64=$?
if [ $Flag64 -eq 0 -a "$Version" = "2c" ];then
ifIn=$ifIn64
ifOut=$ifOut64
BitSuffix=64
fi
else
ifIn=$ifIn32
ifOut=$ifOut32
BitSuffix=32
if [ "$Bit64" = "True" ] ;then
$Echo "Maybe your Host(device) not support 64 bit counter. Please confirm your ARGS and re-check it with Verbose mode, then to check the log.(If you snmp not support 64 bit counter, do not use -6 option)"
exit 3
fi
fi
#set CF_HIST_DATA File Name
CF_HIST_DATA=${CF_HIST_DATA}_${BitSuffix}
#set STAT_HIST_DATA File Name
STAT_HIST_DATA=${CF_HIST_DATA}_ctj_$Num
if [ ! -f $CF_HIST_DATA ]; then
IsFirst="True"
touch $CF_HIST_DATA
if [ $? -ne 0 ];then
Severity=3
Msg="Unknown"
OutPut="Create File $CF_HIST_DATA Error with user `id`."
$Echo "$Msg" "-" $OutPut
exit $Severity
fi
fi
if [ ! -r $CF_HIST_DATA -o ! -w $CF_HIST_DATA ]; then
Severity=3
Msg="Unknown"
OutPut="Read or Write File $CF_HIST_DATA Error with user `id`."
$Echo "$Msg" "-" $OutPut
exit $Severity
fi
if [ $TrafficJitter"AA" = "TrueAA" ]; then
if [ ! -f $STAT_HIST_DATA ]; then
touch $STAT_HIST_DATA
IsStatFirst="True"
if [ $? -ne 0 ];then
Severity=3
Msg="Unknown"
OutPut="Create File $STAT_HIST_DATA Error with user `id`."
$Echo "$Msg" "-" $OutPut
exit $Severity
fi
if [ ! -r $STAT_HIST_DATA -o ! -w $STAT_HIST_DATA ]; then
Severity=3
Msg="Unknown"
OutPut="Read or Write File $STAT_HIST_DATA Error with user `id`."
$Echo "$Msg" "-" $OutPut
exit $Severity
fi
gen_string $Num
to_debug string_t $string_t
TC=0
TRI=($string_t)
TRO=($string_t)
echo $TC >$STAT_HIST_DATA
echo ${TRI[@]} >>$STAT_HIST_DATA
echo ${TRO[@]} >>$STAT_HIST_DATA
fi
C=(`head -n 1 $STAT_HIST_DATA`)
RI=(`head -n 2 $STAT_HIST_DATA|tail -n 1 `)
RO=(`tail -n 1 $STAT_HIST_DATA`)
to_debug C RI RO $C $RI $RO
to_debug C RI RO $C ${RI[@]} ${RO[@]}
to_debug C N $C $Num
fi
_result_status=`$SNMPGET -v $Version $Community $Host "IF-MIB::ifOperStatus.${Interface}"| awk '{print $4}' | awk -F '(' '{print $1}'`
if [ "$_result_status" != "up" ]; then
$Echo "The Interface name:${ifName} -- index:${Interface} you checked seems not up."
exit 3
fi
_result_in=`$SNMPGET -v $Version $Community $Host "IF-MIB::${ifIn}"."$Interface"`
_result_out=`$SNMPGET -v $Version $Community $Host "IF-MIB::${ifOut}"."$Interface" `
to_debug time is $Time, $SNMPGET check result in is $_result_in, out is $_result_out
_result_in=`echo $_result_in |awk '{print $4}'`
_result_out=`echo $_result_out|awk '{print $4}'`
to_debug time is $Time, $SNMPGET check result in is $_result_in, out is $_result_out
if [ -z "$_result_in" -o -z "$_result_out" ] ; then
$Echo "No Data been get here. Please confirm your ARGS and re-check it with Verbose mode, then to check the log.(If you snmp not support 64 bit counter, do not use -6 option)"
exit 3
fi
In=`echo "$_result_in * 8 " |bc`
Out=`echo "$_result_out * 8 " |bc`
to_debug Index is $index Time is $Time, In is $In, Out is $Out
HistData=`cat $CF_HIST_DATA| head -n 1`
HistTime=`echo $HistData| awk -F "|" '{print $1}'|sed 's/ //g'`
HistIn=`echo $HistData| awk -F "|" '{print $2}'|sed 's/ //g'`
HistOut=`echo $HistData| awk -F "|" '{print $3}'|sed 's/ //g'`
to_debug HistData is $HistData HistTime is $HistTime, HistIn is $HistIn, HistOut is $HistOut
if [ -z "$HistTime" -o -z "$HistIn" -o -z "$HistOut" ] ; then
if [ "$IsFirst" = "True" ]; then
#If there is a empty hist file, can write the data before exit(for the reason of IsFirst),
#the data can be used for next time.
echo "$Time|$In|$Out" > $CF_HIST_DATA
to_debug "$Time|$In|$Out" $CF_HIST_DATA
continue
else
Severity="3"
Msg="Unknown"
OutPut="Can not found data in the history data file. \
Please to check the file $CF_HIST_DATA, or use use verbose mode and check the debug file"
$Echo "$Msg" "-" $OutPut
exit $Severity
fi
else
Interval=`echo "$Time - $HistTime" | bc`
if [ $Interval -lt $Min_Interval ] ; then
$Echo "The check interval must greater than $Min_Interval Seconds. But now it is $Interval. Please retry it later."
exit 3
fi
#echo DEBUG here: data of $Host $Interface $ifName "$Time|$In|$Out"
to_debug DEBUG here: data of $Host $Interface $ifName "$Time|$In|$Out"
echo "$Time|$In|$Out" > $CF_HIST_DATA
if [ $? -ne 0 ];then
Severity=3
Msg="Unknown"
OutPut="Write File $CF_HIST_DATA Error with user `id`."
$Echo "$Msg" "-" $OutPut
exit $Severity
fi
if [ $Interval -gt $Max_Interval ] ; then
$Echo "The check interval is too large. It is greater than $Max_Interval. The result is dropped. We will use fresh data at the next check."
exit 3
fi
fi
DiffIn=`echo "$In - $HistIn" | bc`
DiffOut=`echo "$Out - $HistOut" | bc`
to_debug Interval/DiffIn/DiffOut $Interval $DiffIn $DiffOut
if [ ` echo " $Interval > 0 " |bc ` -eq 0 ] ; then
$Echo "we got a negative time interval value here."
exit 3