Announcement

Collapse
No announcement yet.

Linux interfaces all statistic check from /proc/net/dev

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Linux interfaces all statistic check from /proc/net/dev

    Hello, everyone.

    We tried to find a plugin that could gather all information from /proc/net/dev and store it at the nagios/centreon. But at existing plugins we could find only gathering for interfaces load and errors or capturing data only at the time of plugin run (not between time of plugin runs but at the time of plugin run).
    At our search we found this script: https://github.com/nguttman/Nagios-C..._network_stats. So we did a little modification of this script and got script bellow. The biggest problem of this script is it doesnít check collected data for warning/critical values and we donít know perl. So we canít add algorithm for checking warning/critical levels.
    Can anybody help us with it or suggest a better script for checking all statistics of interfaces?
    Best Regards.

    Script:
    Code:
    #!/usr/bin/perl -w
    
    
    ## Script written by Noah Guttman and Copyright (C) 2014 Noah Guttman. This script is released and distributed under the terms of the GNU General Public License
    
    #Libraries to use
    use lib "/usr/lib/perl5/5.8.8/";
    use lib "/usr/lib/perl5/5.8.8/Getopt/";
    
    use strict;
    use Getopt::Std;
    
    our ($opt_h);
    
    my @ointerfaces;
    my @tinterfaces;
    my $RX_BPS;                 # Recieved bytes
    my $RX_PPS;                 # Recieved packets
    my $RX_EPT;                 # Recieved errors
    my $RX_DPT;                 # Recieved packets droped by driver
    my $RX_FIPT;                # Recieved FIFO buffer errors
    my $RX_FRPT;                # Recieved fragmented errors
    my $RX_MPS;                 # Recieved multicast packets
    my $TX_BPS;                 # Transferred bytes
    my $TX_PPS;                 # Transferred packets
    my $TX_EPT;                 # Transferred errors
    my $TX_DPT;                 # Transferred packets droped by driver
    my $TX_FIPT;                # Transferred FIFO buffer errors
    my $TX_CPT;                 # Transferred with collision errors
    my $TX_CAPT;                # Transferred with carrier losses by driver
    
    my $interfacename;
    my $opt_t = "/tmp/network_statistics2.tmp";
    my $longoutput = "Collected statistics - OK|";
    my $currenttime;
    my $lastchecktime;
    my $deltatime;
    my $commandstring;
            getopts('h');
    
    ## Display Help
    if ($opt_h){
            print "::Network Statistics Instructions::\n\n";
            print " -h,             Display this help information\n";
    #    print " -t,             Path to store the pervious check results\n";
    #    print "             used to calculate deltas.\n";
        print "                 Writes temporary data to $opt_t\n";
            print "Script written by Noah Guttman and Copyright (C) 2014 Noah Guttman.\n";
            print "This script is released and distributed under the terms of the GNU\n";
            print "General Public License.     >>>>    http://www.gnu.org/licenses/\n";
            print "";
            print "This program is free software: you can redistribute it and/or modify\n";
            print "it under the terms of the GNU General Public License as published by\n";
            print "the Free Software Foundation.\n\n";
            print "This program is distributed in the hope that it will be useful,\n";
            print "but WITHOUT ANY WARRANTY; without even the implied warranty of\n";
            print "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n";
            print "GNU General Public License for more details.\n";
            print ">>>>    http://www.gnu.org/licenses/\n";
            exit 0;
    }
    
    
    #First we get the current time
    $currenttime = time();
    #Second we need to check if there is history file in /tmp/.If so then we pull in the data
    if (-e "$opt_t"){
        $lastchecktime = (`cat $opt_t |grep time |awk \'{print \$2}\'`);
        chomp ($lastchecktime);
        $deltatime=($currenttime - $lastchecktime);
        @ointerfaces= (`cat $opt_t`);
    }else{
        print ("First time running test - Results will be available starting next test\n");
    
    #We need to pull the data and write it to the tmp file.
    
    #    exit 0;
    }
    ## Now we pull the current data and write it to the tmp file.
    #we check is each stat exists before we pull it.
    
        @tinterfaces= (`cat /proc/net/dev |grep : |awk -F : \'{print \$1" "\$2}\'|awk \'{print \$1" "\$2" "\$3" "\$4" "\$5" "\$6" "\$7" "\$9" "\$10" "\$11" "\$12" "\$13" "\$14" "\$15" "\$16}\'`);
    
    ## Next we write the data to the tmp file
    #First we wipe out the old file
    $commandstring = ("/bin/rm -f $opt_t");
    system ($commandstring);
    #Next we populate the string
    $commandstring = ("/bin/echo \'time $currenttime\' \>\> $opt_t");
    system ($commandstring);
    $commandstring = ("cat /proc/net/dev |grep :|awk -F : \'{print \$1\" \"\$2}\'|awk \'{print \$1\" \"\$2\" \"\$3\" \"\$4\" \"\$5\" \"\$6\" \"\$7\" \"\$9\" \"\$10\" \"\$11\" \"\$12\" \"\$13\" \"\$14\" \"\$15\" \"\$16}\' \>\> $opt_t");
    #print ("$commandstring\n");
    system ($commandstring);
    
    
    ##Here goes the math
    foreach my $interface (@tinterfaces){
        foreach my $oldinterface (@ointerfaces){
            if (((split(" ",$interface))[0]) eq ((split(" ",$oldinterface))[0])){
                $interfacename = (split(" ",$interface))[0];
                $RX_BPS = sprintf("%.2f",((split(" ",$interface))[1] - (split(" ",$oldinterface))[1]) / $deltatime);
                            $RX_PPS = sprintf("%.2f",((split(" ",$interface))[2] - (split(" ",$oldinterface))[2]) / $deltatime);
                            $RX_EPT = ((split(" ",$interface))[3] - (split(" ",$oldinterface))[3]);
                            $RX_DPT = ((split(" ",$interface))[4] - (split(" ",$oldinterface))[4]);
                            $RX_FIPT = ((split(" ",$interface))[5] - (split(" ",$oldinterface))[5]);
                            $RX_FRPT = ((split(" ",$interface))[6] - (split(" ",$oldinterface))[6]);
                            $RX_MPS = sprintf("%.2f",((split(" ",$interface))[7] - (split(" ",$oldinterface))[7]) / $deltatime);
                            $TX_BPS = sprintf("%.2f",((split(" ",$interface))[8] - (split(" ",$oldinterface))[8]) / $deltatime);
                            $TX_PPS = sprintf("%.2f",((split(" ",$interface))[9] - (split(" ",$oldinterface))[9]) / $deltatime);
                            $TX_EPT = ((split(" ",$interface))[10] - (split(" ",$oldinterface))[10]);
                            $TX_DPT = ((split(" ",$interface))[11] - (split(" ",$oldinterface))[11]);
                            $TX_FIPT = ((split(" ",$interface))[12] - (split(" ",$oldinterface))[12]);
                            $TX_CPT = ((split(" ",$interface))[13] - (split(" ",$oldinterface))[13]);
                            $TX_CAPT = ((split(" ",$interface))[14] - (split(" ",$oldinterface))[14]);
    
                $longoutput = $longoutput .$interfacename."_RX_Bytes"."=$RX_BPS;; ";
                            $longoutput = $longoutput .$interfacename."_RX_Packets"."=$RX_PPS;; ";
                            $longoutput = $longoutput .$interfacename."_RX_Errors"."=$RX_EPT;; ";
                            $longoutput = $longoutput .$interfacename."_RX_Drops"."=$RX_DPT;; ";
                            $longoutput = $longoutput .$interfacename."_RX_Fifo"."=$RX_FIPT;; ";
                            $longoutput = $longoutput .$interfacename."_RX_Frag"."=$RX_FRPT;; ";
                            $longoutput = $longoutput .$interfacename."_RX_Multicast"."=$RX_MPS;; ";
                            $longoutput = $longoutput .$interfacename."_TX_Bytes"."=$TX_BPS;; ";
                            $longoutput = $longoutput .$interfacename."_TX_Packets"."=$TX_PPS;; ";
                            $longoutput = $longoutput .$interfacename."_TX_Errors"."=$TX_EPT;; ";
                            $longoutput = $longoutput .$interfacename."_TX_Drops"."=$TX_DPT;; ";
                            $longoutput = $longoutput .$interfacename."_TX_Fifo"."=$TX_FIPT;; ";
                            $longoutput = $longoutput .$interfacename."_TX_Collis"."=$TX_CPT;; ";
                            $longoutput = $longoutput .$interfacename."_TX_Carrier"."=$TX_CAPT;; ";
            }
        }
    
    }
    
    print ("$longoutput\n");
    exit 0;

  • #2
    Greetings,

    You could have contacted directly.

    Do you still need this?

    Comment

    Working...
    X