Script: interface traffic (linux)

Templates, scripts for templates, scripts and requests for templates.

Moderators: Developers, Moderators

Post Reply
gregm
Posts: 38
Joined: Mon Oct 21, 2002 4:19 pm

Script: interface traffic (linux)

Post by gregm »

Just thought this might be handy. I'm running RH7.1, and don't have IP accounting compilied into the kernel (this is on a production server, I don't want to mess with the kernel now), so netstat doesn't show the traffic data.

However, I came across the data in /proc/net/dev (couldn't find a util to extract it) so here's a quick script to extract the data:

Code: Select all

#!/usr/local/bin/php -q
<?php

/* Greg MacLellan, Oct 19, 2002

Accepts interface name as paramter: ie,

./nettraffic.php eth0

Outputs bytes recieved/sent, seperated by a space.

*/

if (empty($_SERVER["argv"][1])) {
        echo "Usage: ".$_SERVER["argv"][0]." <interface>\n";
        echo "Outputs: <bytes recvd> <bytes sent>\n";
        exit(1);
}

$info = file("/proc/net/dev");
for ($i = 2; $i < count($info); $i++) {
        $interface = trim(substr($info[$i], 0, 6));
        $data = explode(" ", trim(substr($info[$i], 7)));
        if ($_SERVER["argv"][1] == $interface) {
                // bytes recvd,        sent
                echo $data[0]." ".$data[38]."\n";
                exit(0);
        }
}

?>
raX
Lead Developer
Posts: 2243
Joined: Sat Oct 13, 2001 7:00 pm
Location: Carlisle, PA
Contact:

Post by raX »

Excellent, thanks for sharing. I am going to move this thread to the scripts and addons forum though so more people will be able to find it.

-Ian
Guest

Post by Guest »

Man, I just found this after I wrote my own script... lol... *sigh*
dbenders
Posts: 28
Joined: Mon Oct 14, 2002 2:02 pm
Location: Santiago, CHILE

I don't see the benefit of this script....

Post by dbenders »

Hi, I don't undertand for what we need this script if we can get the traffic of a linux using the "Get SNMP Network Data" that already comes with CACTI ?

I'm working with this in a LINUX and works perfect.
User avatar
bulek
Cacti Pro User
Posts: 854
Joined: Mon May 20, 2002 2:07 am
Location: Poland
Contact:

Post by bulek »

There are people that don't use or don't want to use SNMP for some reason so such script can be useful. It's good to have two options anyway ;)

- bulek
crypted
Posts: 5
Joined: Sun Sep 28, 2003 8:21 pm

Post by crypted »

Can someone help me install this script? I have it setup in scripts/ and am not too sure exacty how to load it although I have attempted. I may or may not have made the right kind of data input method but after that point I can't figure out how to get that input method into becoming a graph. So if I could get the steps from after putting it on the server all the way to setting up the graph I'd appreciate it.
gregm
Posts: 38
Joined: Mon Oct 21, 2002 4:19 pm

Post by gregm »

It's written in PHP, which means you need the PHP-CLI binary.

Paste it into a file called "nettraffic" or whatever, probably in your cacti scripts directory, but it doesnt really matter as long as cacti can run it, and run "chmod a+x nettraffic".

After that, as long as you can run it manually ("./nettraffic eth0") and get some numbers, it should be working fine. Just add it as an input source with two fields.
crypted
Posts: 5
Joined: Sun Sep 28, 2003 8:21 pm

Post by crypted »

As an input source with two fields how? I'm not following as far as setting it up via the php webconsole.

Add it as a "data input method" with the line: perl <path_cacti>/scripts/netstatint.pl <> <> and that being it? Or is there something else I'm not understanding?
gregm
Posts: 38
Joined: Mon Oct 21, 2002 4:19 pm

Post by gregm »

Uh.. it's not perl, it's php. And you need to specify names:

<path_cacti>/scripts/nettraffic <recvd> <sent>

Something like that. Then add output fields (stored in RRA) for "recvd" and "sent"

At this point, you should be reading the Cacti documentation, it's all there.
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests