Request: Network Traffic Monitor

nnCron and nnCron LITE discussion

Request: Network Traffic Monitor

Postby Yikes2000 » Wed, 20 Oct 2004, 08:30

I would like a few variables to monitor network traffic (similar to CPU-USAGE). For example, NETIN-USAGE (incoming network traffic measured in KB/sec), NETOUT-USAGE (outgoing network traffic), and NET-USAGE (incoming + outgoing). This is needed because I often start my computer remotely through Wake-On-LAN, and I don't want it to hibernate while I'm send/receiving files. CPU-USAGE is very low during file transfer, so I can't use it as a condition to stop hibernation.

Thank you.
Yikes2000
 
Posts: 9
Joined: Mon, 18 Oct 2004, 00:17

Postby VK » Wed, 20 Oct 2004, 10:06

As far as I know this feature request is included in Nicholas's todo-list.

BTW, take a look in the docs at the description of net.spf plugin. Maybe it's a good idea not to hibernate the PC when there is more than X user logged or there are more than X open sessions...
CU
VK
User avatar
VK
 
Posts: 680
Joined: Wed, 14 Jul 2004, 19:17

Postby Yikes2000 » Thu, 21 Oct 2004, 00:45

I've tried the net.spf plugin already. It doesn't detect when I'm printing to a server, or playing an AVI file from the server on another computer. Here's my test task:

Code: Select all
#( test_net
NoLog
Action:
MSG: "Sessions(%SESSIONSCOUNT%) Users(%LOGGEDUSERCOUNT%)"
)#


The task is running on the server. It always reports "Sessions() Users()" regardless of anyone printing to the server or accessing files from it (through Window's File Sharing).

Currently, I'm looking into writing a simple Perl script to grab the Bytes Sent and Bytes Received numbers from 'netstat -e', and compare it over a short time interval (5 seconds) to calculate KB/sec. Then pass the data back to nnCron task.
Yikes2000
 
Posts: 9
Joined: Mon, 18 Oct 2004, 00:17

Postby Yikes2000 » Thu, 21 Oct 2004, 19:52

Ok, I did it. I wrote a simple Perl script to calculate the network traffic (in, out, and total) and pass the values (in KB/sec) back to nnCron so I could hibernate my computer based on network traffic. This is Windows XP, and you will need to install ActiveState Perl for it to work. (I suppose if I'm more experienced with Forth, I could write the whole thing as a nnCron plugin.)

The script is named 'netstat.pl' and I store it in C:\Program Files\nnCron. It compares 'netstat' results over a 5 second period to calculate the network traffic input, output, and total. The results (in KB/sec) are stored in the files netstat.in, netstat.out, and netstat.io in the same directory.

Code: Select all
#!/bin/perl

my $secs = 5; # network traffic monitor period (in seconds)
my $outfile = 'C:\Progra~1\nnCron\netstat';

sub netstat {
    my @toks = split( /\s+/, `netstat -e` );
    return ( $toks[5], $toks[6] );

    # print "$#toks\n";
    # my $i;
    # for ($i=0; $i<=$#toks; $i++) { print "$i $toks[$i]\n"; }
}

my @a = netstat();
sleep $secs;
my @b = netstat();

my $seck = $secs * 1024;
my $netin = ($b[0] - $a[0]) / $seck;
my $netout = ($b[1] - $a[1]) / $seck;
my $netio = ($b[0] + $b[1] - $a[0] - $a[1]) / $seck;

open( OUTFILE, ">$outfile.in" );
printf OUTFILE "%.0f", ($netin);
close( OUTFILE );

open( OUTFILE, ">$outfile.out" );
printf OUTFILE "%.0f", ($netout);
close( OUTFILE );

open( OUTFILE, ">$outfile.io" );
printf OUTFILE "%.0f", ($netio);
close( OUTFILE );


I only use the combined in/out traffic in my hibernation script. I setup a task to check every 5 minutes the network traffic. If above 20KB/sec, then it update the timestamp on a marker file. Hibernation will occur only if the marker file is more than 30 minutes old.

Code: Select all
# Hibernate conditions: (all must be met)
# 1) User idle > 30 mins.
# 2) Wake-from-LAN > 30 mins ago.
# 3) Printer idle > 30 mins. (C:\Windows\system32\spool\printers)
# 4) CPU Load < 10%.
# 5) Network activity > 30 mins ago.

SET SLEEP_MARKER="sleep_marker"
SET ACTIVE_MARKER="active_marker"
SET PRINTER_DIR="C:\Windows\system32\spool\printers"

#( smart_hibernate
NoLog
Action:
    FILE-EXIST: %SLEEP_MARKER%
    IF
        FILE-WRITE: %ACTIVE_MARKER% "touch"
        FILE-DELETE: %SLEEP_MARKER%
    ELSE
        IDLE: 1800 \
        FT-CUR FILE-WTIME: %ACTIVE_MARKER% FTIME- 1800 > AND \
        FT-CUR FILE-WTIME: %PRINTER_DIR% FTIME- 1800 > AND \
        10000 (CPU-USAGE) 10 < AND
        IF
            POWER-HIBERNATE
        THEN
    THEN
)#

#( smart_marker
NoLog WatchSuspend WatchShutdown
Action: FILE-WRITE: %SLEEP_MARKER% "touch"
)#

#( netstat_check
NoLog
Time: */5 * * * * *
Action:
   StartIn: "C:\Progra~1\nnCron"
   SWHide NormalPriority
   START-APPW: "C:\Progra~1\nnCron\netstat.pl"

   S" netstat.io" FILE S>NUM 20 >
   IF
      FILE-WRITE: %ACTIVE_MARKER% "touch"
   THEN
#)


Now I can remotely wake-up this computer (Wake-On-LAN). Print on its printer and watch AVI files through WinXP File Sharing for as long as I wish. The computer will hibernate only if there is no activity for 30 minutes. This should save me $$$ on electricity.

:D
Yikes2000
 
Posts: 9
Joined: Mon, 18 Oct 2004, 00:17


Return to nnCron forum (English)

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest