Added stat_net.pl

This commit is contained in:
Yavuz Aydın 2021-06-11 09:17:24 +02:00
parent 245fbfe820
commit c11f7652b1
2 changed files with 144 additions and 0 deletions

View File

@ -5,3 +5,6 @@ Monitoring plugins for Nagios, Icinga and all other NRPE compatible monitoring s
#### check_reboot-required
Plugin for NRPE (Nagios Remote Plugin Executor) for Debian/Ubuntu and Red Hat/CentOS
to check if a reboot is required and also list packages that requires reboot
#### stat_net.pl
Plugin for checking network errors and statictics

141
stat_net.pl Normal file
View File

@ -0,0 +1,141 @@
#!/usr/bin/perl -w
################################################
#
# Checks the net-stat (only for status).
# - warning state only for to much percent of errs or drop packages
# - unknown is for unknown device
#
# Thomas Sesselmann <t.sesselmann@dkfz.de> 2010
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
# Changelog:
# v0.10 2018.02.16 (Snel.com - Yavuz Aydin) Added -d flag for devices argument
# v0.9 2010.10.22 (ts)
#
####
use Getopt::Long;
&Getopt::Long::config('bundling');
my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
my $help = 0;
my $warn = "10";
my $proc_net_file = "/proc/net/dev";
my %devices = ();
my $arg_devices = undef;
my $status = GetOptions(
"h|help|?" => \$help,
"w=i" => \$warn,
"d|devices=s" => \$arg_devices
);
&print_usage() if $help;
if ( defined($arg_devices) ) {
foreach my $device ( split ' ', $arg_devices ) {
$devices{$device}=0;
}
} else {
$devices{all}=0;
}
&test_net();
#####################################################
sub test_net() {
my $return_txt='';
my $perfdata='';
my $status = 'OK';
open FILE,"<$proc_net_file";
while(<FILE>) {
chomp $_;
if ($_ =~ /^\s*(\w+):(.*)$/){
my $key = lc $1;
if(( exists $devices{all} )or( exists $devices{$key} )) {
$devices{$key}=$2;
}
}
}
close FILE;
foreach my $k ( sort keys %devices ) {
if ( $k eq "all" ) { next; }
if ( $devices{$k} eq "0" ) {
$status = 'UNKNOWN';
if ( $return_txt ne "" ) { $return_txt .= ", " }
$return_txt .= "$k=(-/-)";
next;
}
my @val = split " ",$devices{$k};
my $rx = $val[0];
my $tx = $val[8];
my $rx_pkg = $val[1];
my $tx_pkg = $val[9];
$perfdata .= "${k}_in=${rx}c; "; #In Bytes (Counter)
$perfdata .= "${k}_out=${tx}c; "; #Out Bytes (Counter)
## calculate dops and errors to percent
if ( ( ($val[2]+$val[3]) * 100 > $warn * $rx_pkg ) or
( ($val[10]+$val[11]) * 100 > $warn * $tx_pkg ) ) {
if ( $ERRORS{$status} < $ERRORS{'WARNING'} ) { $status ='WARNING'; }
}
my $rx_unit = "B";
if ( $rx > 1024 * 4 ) { $rx /= 1024; $rx_unit="kB"; }
if ( $rx > 1024 * 4 ) { $rx /= 1024; $rx_unit="MB"; }
if ( $rx > 1024 * 4 ) { $rx /= 1024; $rx_unit="GB"; }
if ( $rx > 1024 * 4 ) { $rx /= 1024; $rx_unit="TB"; }
my $tx_unit = "B";
if ( $tx > 1024 * 4 ) { $tx /= 1024; $tx_unit="kB"; }
if ( $tx > 1024 * 4 ) { $tx /= 1024; $tx_unit="MB"; }
if ( $tx > 1024 * 4 ) { $tx /= 1024; $tx_unit="GB"; }
if ( $tx > 1024 * 4 ) { $tx /= 1024; $tx_unit="TB"; }
if ( $return_txt ne "" ) { $return_txt .= ", " }
$return_txt .= sprintf "$k=(%.1f$rx_unit/%.1f$tx_unit)",$rx,$tx;
}
$return_txt = "NET $status - (Rx/Tx) $return_txt|$perfdata\n";
&print_exit($return_txt,$ERRORS{$status});
}
sub print_usage() {
print "Usage: $0 [-w <warn-level in percent>] -d [devices]*\n";
exit $ERRORS{'UNKNOWN'};
}
sub print_exit() {
my $msg = shift;
my $return_code = shift;
print "$msg";
exit $return_code;
}
__END__