From c11f7652b1e93e989c1ba72665b0ee9e89889217 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yavuz=20Ayd=C4=B1n?= Date: Fri, 11 Jun 2021 09:17:24 +0200 Subject: [PATCH] Added stat_net.pl --- README.md | 3 ++ stat_net.pl | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 stat_net.pl diff --git a/README.md b/README.md index 64fe8dc..e9409c3 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/stat_net.pl b/stat_net.pl new file mode 100644 index 0000000..2707cb0 --- /dev/null +++ b/stat_net.pl @@ -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 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 . +# +# +# 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() { + 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 ] -d [devices]*\n"; + exit $ERRORS{'UNKNOWN'}; +} + +sub print_exit() { + my $msg = shift; + my $return_code = shift; + + print "$msg"; + exit $return_code; +} + + +__END__ +