Skip to content

Instantly share code, notes, and snippets.

@adrianlzt

adrianlzt/stat_net.pl

Last active Feb 9, 2016
Embed
What would you like to do?
#!/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.9 2010.10.22 (ts)
#
####
my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
my $warn = "10";
my $proc_net_file = "/proc/net/dev";
my %devices = ();
&get_params(@ARGV);
&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 get_params() {
# get params
my $test = '';
foreach $key (@_){
if($test eq '') { $test = $key; next; }
if($test eq '-w') { $warn = $key; $test = ''; next; }
if($test =~ /^-/) { &print_usage(); }
$devices{$key}=0;
}
if($test ne '') {
$devices{$test}=0;
}
if( keys %devices == 0 ) { $devices{all}=0; }
if( $warn =~ /^(\d+)%?$/ ) {
$warn = $1;
} else {
&print_usage;
}
}
sub print_usage() {
print "Usage: $0 [-w <warn-level in percent>] [devices]*\n";
exit $ERRORS{'UNKNOWN'};
}
sub print_exit() {
my $msg = shift;
my $return_code = shift;
print "$msg";
exit $return_code;
}
__END__
--- stat_net.pl.err 2015-05-11 10:57:50.352604861 +0200
+++ stat_net.pl 2015-05-11 10:58:05.612874107 +0200
@@ -82,16 +82,16 @@
}
my $rx_unit = "B";
- if ( $rx > 1024 * 4 ) { $rx /= 1024; $rx_unix="kB"; }
- if ( $rx > 1024 * 4 ) { $rx /= 1024; $rx_unix="MB"; }
- if ( $rx > 1024 * 4 ) { $rx /= 1024; $rx_unix="GB"; }
- if ( $rx > 1024 * 4 ) { $rx /= 1024; $rx_unix="TB"; }
+ 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_unix="kB"; }
- if ( $tx > 1024 * 4 ) { $tx /= 1024; $tx_unix="MB"; }
- if ( $tx > 1024 * 4 ) { $tx /= 1024; $tx_unix="GB"; }
- if ( $tx > 1024 * 4 ) { $tx /= 1024; $tx_unix="TB"; }
+ 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;
--- a/scripts/stat_net.pl
+++ b/scripts/stat_net.pl
@@ -72,12 +72,15 @@ sub test_net() {
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 ) or
- ( ($val[10]+$val[11]) * 100 > $warn * $tx ) ) {
+ 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'; }
}
--- a/stat_net.pl
+++ b/stat_net.pl
@@ -49,7 +49,7 @@ sub test_net() {
open FILE,"<$proc_net_file";
while(<FILE>) {
chomp $_;
- if ($_ =~ /^\s*(\w+):(.*)$/){
+ if ($_ =~ /^\s*([^:]+):(.*)$/){
my $key = lc $1;
if(( exists $devices{all} )or( exists $devices{$key} )) {
$devices{$key}=$2;
@@ -78,10 +78,23 @@ sub test_net() {
$perfdata .= "${k}_in=${rx}c; "; #In Bytes (Counter)
$perfdata .= "${k}_out=${tx}c; "; #Out Bytes (Counter)
- ## calculate dops and errors to percent
+ ## calculate drops and errors to percent
+ my $errs_rx = $val[2];
+ my $drops_rx = $val[3];
+ my $errs_tx = $val[10];
+ my $drops_tx = $val[11];
+
+ $perfdata .= "${k}_errs_rx=${errs_rx}c; ";
+ $perfdata .= "${k}_drops_rx=${drops_rx}c; ";
+ $perfdata .= "${k}_errs_tx=${errs_tx}c; ";
+ $perfdata .= "${k}_drops_tx=${drops_tx}c; ";
+
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'; }
+ if ( $ERRORS{$status} < $ERRORS{'WARNING'} ) {
+ $status ='WARNING';
+ }
+ $err_txt .= " - Check ${k} errors and drops";
}
my $rx_unit = "B";
@@ -101,7 +114,7 @@ sub test_net() {
}
- $return_txt = "NET $status - (Rx/Tx) $return_txt|$perfdata\n";
+ $return_txt = "NET ${status} $err_txt\n(Rx/Tx) $return_txt|$perfdata\n";
&print_exit($return_txt,$ERRORS{$status});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment