-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_iftraffic_nrpe.pl
executable file
·304 lines (233 loc) · 7.49 KB
/
check_iftraffic_nrpe.pl
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
#!/usr/bin/perl -w
#
# npre plugin to monitor network bandwidth
# Created by Van Dyck Sven
#
# Updated by Luke Harris
# Date: 2015-06-23
#
# v0.93 Updated regex for line var as some systems include a space (thanks to https://github.com/JOpsDev)
# v0.92 Change performance data output to work together with pnp4nagios and Splunk
# v0.91 Change performance data output to work togethet with pnp4nagios
# v0.9 Initial version
#
# Script based on check_iftraffic.pl
#
# Example Usage:
# ./check_iftraffic_nrpe.pl -i eth0 -b 1000 -u m -w 90 -c 95
use strict;
use Getopt::Long;
&Getopt::Long::config('bundling');
use Data::Dumper;
my $iface_descr;
my $iface_speed;
my $opt_h;
my $units;
my $line;
my $error;
my @splitLine;
# Path to tmp files
my $TRAFFIC_FILE = "/tmp/traffic";
# changes sos 20090717 UNKNOWN must bes 3
my %STATUS_CODE =
( 'UNKNOWN' => '3', 'OK' => '0', 'WARNING' => '1', 'CRITICAL' => '2' );
#default values;
my ( $in_bytes, $out_bytes ) = 0;
my $warn_usage = 85;
my $crit_usage = 98;
my $o_noreg = undef; # Do not use Regexp for name
#added 20050614 by mw
my $max_value;
my $max_bytes;
#cosmetic changes 20050614 by mw, see old versions for detail
my $status = GetOptions(
"h|help" => \$opt_h,
"w|warning=s" => \$warn_usage,
"c|critical=s" => \$crit_usage,
"b|bandwidth=i" => \$iface_speed,
'r' => \$o_noreg,
'noregexp' => \$o_noreg,
"u|units=s" => \$units,
"i|interface=s" => \$iface_descr,
#added 20050614 by mw
"M|max=i" => \$max_value
);
if ( $status == 0 ) {
print_help();
exit $STATUS_CODE{'OK'};
}
if ( ( !$iface_descr ) or ( !$iface_speed ) ) {
print_usage();
}
#change 20050414 by mw
$iface_speed = bits2bytes( $iface_speed, $units ) / 1024;
if ( !$max_value ) {
#if no -M Parameter was set, set it to 32Bit Overflow
$max_bytes = 4194304 ; # the value is (2^32/1024)
}
else {
$max_bytes = unit2bytes( $max_value, $units );
}
#Parse /proc/net/dev on localhost to gather network statistics instead if querying SNMP
open F, "</proc/net/dev" or die "Can't open /proc/net/dev: $!";
my @f = <F>;
close F;
foreach (@f) {
if ($_ =~ /${iface_descr}/){
$line=$_;
last;
#Interface found, exiting loop
}
}
$line =~ s/^\s+|\s+$//g;
@splitLine=split /[: ]+/, $line;
$in_bytes=$splitLine[1];
$out_bytes=$splitLine[9];
$in_bytes = $in_bytes / 1024;
$out_bytes = $out_bytes / 1024;
#end network statistics gathering
#Starting calculations
my $row;
my $last_check_time = time - 1;
my $last_in_bytes = $in_bytes;
my $last_out_bytes = $out_bytes;
if (
open( FILE,
"<" . $TRAFFIC_FILE . "_if" . $iface_descr
)
)
{
while ( $row = <FILE> ) {
#cosmetic change 20050416 by mw
#Couldn't sustain;-)
## chomp();
( $last_check_time, $last_in_bytes, $last_out_bytes ) =
split( ":", $row );
### by sos 17.07.2009 check for last_bytes
if ( ! $last_in_bytes ) { $last_in_bytes=$in_bytes; }
if ( ! $last_out_bytes ) { $last_out_bytes=$out_bytes; }
if ($last_in_bytes !~ m/\d/) { $last_in_bytes=$in_bytes; }
if ($last_out_bytes !~ m/\d/) { $last_out_bytes=$out_bytes; }
}
close(FILE);
}
my $update_time = time;
open( FILE, ">" . $TRAFFIC_FILE . "_if" . $iface_descr )
or die "Can't open $TRAFFIC_FILE for writing: $!";
printf FILE ( "%s:%.0ld:%.0ld\n", $update_time, $in_bytes, $out_bytes );
close(FILE);
my $db_file;
#added 20050614 by mw
#Check for and correct counter overflow (if possible).
#See function counter_overflow.
$in_bytes = counter_overflow( $in_bytes, $last_in_bytes, $max_bytes );
$out_bytes = counter_overflow( $out_bytes, $last_out_bytes, $max_bytes );
my $in_traffic = sprintf( "%.2lf",
( $in_bytes - $last_in_bytes ) / ( time - $last_check_time ) );
my $out_traffic = sprintf( "%.2lf",
( $out_bytes - $last_out_bytes ) / ( time - $last_check_time ) );
# sos 20090717 changed due to rrdtool needs bytes
#my $in_traffic_absolut = sprintf( "%.0d", $last_in_bytes * 1024 );
#my $out_traffic_absolut = sprintf( "%.0d", $last_out_bytes * 1024 );
my $in_traffic_absolut = $in_bytes * 1024 ;
my $out_traffic_absolut = $out_bytes * 1024;
my $in_usage = sprintf( "%.1f", ( 1.0 * $in_traffic * 100 ) / $iface_speed );
my $out_usage = sprintf( "%.1f", ( 1.0 * $out_traffic * 100 ) / $iface_speed );
my $in_prefix = "k";
my $out_prefix = "k";
if ( $in_traffic > 1024 ) {
$in_traffic = sprintf( "%.2f", $in_traffic / 1024 );
$in_prefix = "M";
}
if ( $out_traffic > 1024 ) {
$out_traffic = sprintf( "%.2f", $out_traffic / 1024 );
$out_prefix = "M";
}
$in_bytes = sprintf( "%.2f", $in_bytes / 1024 );
$out_bytes = sprintf( "%.2f", $out_bytes / 1024 );
my $exit_status = "OK";
my $output = "Total RX Bytes: $in_bytes MB, Total TX Bytes: $out_bytes MB<br>";
$output .=
"Average Traffic: $in_traffic "
. $in_prefix . "B/s ("
. $in_usage
. "%) in, $out_traffic "
. $out_prefix . "B/s ("
. $out_usage
. "%) out";
if ( ( $in_usage > $crit_usage ) or ( $out_usage > $crit_usage ) ) {
$exit_status = "CRITICAL";
}
if ( ( $in_usage > $warn_usage )
or ( $out_usage > $warn_usage ) && $exit_status eq "OK" )
{
$exit_status = "WARNING";
}
$output .= "<br>$exit_status bandwidth utilization.\n"
if ( $exit_status ne "OK" );
$output .=
#"| inUsage=$in_usage;$warn_usage;$crit_usage outUsage=$out_usage;$warn_usage;$crit_usage " . "inAbsolut=$in_traffic_absolut outAbsolut=$out_traffic_absolut\n";
+"| inUsage=$in_usage;$warn_usage;$crit_usage;; outUsage=$out_usage;$warn_usage;$crit_usage;; inBandwidth=$in_traffic;;;; outBandwidth=$out_traffic;;;; inAbsolut=$in_traffic_absolut;;;; outAbsolut=$out_traffic_absolut;;;;\n";
##$output .=
##"| inUsage=$in_usage;$warn_usage;$crit_usage;; outUsage=$out_usage;$warn_usage;$crit_usage;;\n";
print $output;
exit( $STATUS_CODE{$exit_status} );
#added 20050416 by mw
#Converts an input value to value in bits
sub bits2bytes {
return unit2bytes(@_) / 8;
}
#added 20050416 by mw
#Converts an input value to value in bytes
sub unit2bytes {
my ( $value, $unit ) = @_;
if ( $unit eq "g" ) {
return $value * 1024 * 1024 * 1024;
}
elsif ( $unit eq "m" ) {
return $value * 1024 * 1024;
}
elsif ( $unit eq "k" ) {
return $value * 1024;
}
else {
print "You have to supplie a supported unit\n";
exit $STATUS_CODE{'UNKNOWN'};
}
}
#added 20050414 by mw
#This function detects if an overflow occurs. If so, it returns
#a computed value for $bytes.
#If there is no counter overflow it simply returns the origin value of $bytes.
#IF there is a Counter reboot wrap, just use previous output.
sub counter_overflow {
my ( $bytes, $last_bytes, $max_bytes ) = @_;
$bytes += $max_bytes if ( $bytes < $last_bytes );
$bytes = $last_bytes if ( $bytes < $last_bytes );
return $bytes;
}
#cosmetic changes 20050614 by mw
#Couldn't sustaine "HERE";-), either.
sub print_usage {
print <<EOU;
Usage: check_iftraffic.pl -i if_descr -b if_max_speed [ -w warn ] [ -c crit ]
Options:
-r, --noregexp
Do not use regexp to match NAME in description OID
-i --interface STRING
Interface Name
-b --bandwidth INTEGER
Interface maximum speed in kilo/mega/giga/bits per second.
-u --units STRING
gigabits/s,m=megabits/s,k=kilobits/s,b=bits/s.
-w --warning INTEGER
% of bandwidth usage necessary to result in warning status (default: 85%)
-c --critical INTEGER
% of bandwidth usage necessary to result in critical status (default: 98%)
-M --max INTEGER
Max Counter Value of net devices in kilo/mega/giga/bytes.
EOU
exit( $STATUS_CODE{"UNKNOWN"} );
}