Linux Advanced Routing and Traffic Control list
 help / color / mirror / Atom feed
* [LARTC] classmon 2
@ 2003-12-20  9:07 raptor
  2003-12-20  9:57 ` raptor
  0 siblings, 1 reply; 2+ messages in thread
From: raptor @ 2003-12-20  9:07 UTC (permalink / raw)
  To: lartc

[-- Attachment #1: Type: text/plain, Size: 1011 bytes --]

thanx alot for the script, what i can say it is cool :")

I made some modifications... and I'm sending modified script  as attachment..
In setup like mine with thousand of classes it is killing to know the classid's :"),
so the changes are that u first must create a username --to--> classid |-dellimed file with the following format :

classid|username|parent-id-or-name-or-whatever

and with combination with class id u see the user name its rate as specified on class-htb line,
its parent if specified(so that u can distinguish if user has many channels) , +overlimits..
I added flag (it probably has to be -r :):
-n  regex

to match for names, it matches the whole line not only the username, so that more different matches 
are possible ex. :

classmon -n 'user1|user5'
classmon -n 'user1|2:3e4'
classmon -n '\|parent1$'

The output is changed like this :

Class           name          rate     kbps    pps   backlog  dropped  olimits borrowed lended   tokens   ctokens

thanx again for the cool script..

[-- Attachment #2: classmon --]
[-- Type: application/octet-stream, Size: 4584 bytes --]

#!/usr/bin/perl

# Classy HTB Operations Monitor...in Perl
# Toby Cantor

# The following short command line options are parsed as you might expect.
# There is NO error checking.  The options below are used as follows:
# 
# -i <name of the interface to monitor>
# -d <single EWMA parameter determines averager's "memory".  Should be
#     positive but less than unity. Lower equates to faster response>
# -u <number of updates per second.  Any positive value is acceptible here,
#     including decimals.  A value of 0.2 would update once every 5 seconds>
# -w <a pattern which determines which classes are displayed.  May include
#     the wildcard characters '*' and '?' and they behave as in bash>
#     
## EWMA is calculated every 2nd update.  This was tested on a Pentium 90, on
## which the updates/second has a maximum of 6 or so.  Do the math.  The
## default in this case is decent.  Speaking of which...
#  
# The defaults are first and should require no explanation.

($iface, $parm, $persec, $watchclass) = ('eth0', 0.6, 1, '*');
while ($argument = shift @ARGV)
{
    if    ($argument =~ '-i')    {      $iface = shift @ARGV; }
    elsif ($argument =~ '-d')    {       $parm = shift @ARGV; }
    elsif ($argument =~ '-u')    {     $persec = shift @ARGV; }
    elsif ($argument =~ '-w')    { $watchclass = shift @ARGV; }
    elsif ($argument =~ '-n')    { $re = shift @ARGV; }
    
}

if ($re) {
 $watchclass = '';
 my $u2c = '/etc/conf.d/u2cid.db';
 if (-e $u2c) {
	open FILE, "<$u2c" or die "cant open u2cid db !\n";
	while (<FILE>) {
		if (/$re/) {
		 chop;
		 my @info = split /\|/;
		 #print "$info[1]\n";
		 $watchclass .= "$info[0]\\b|";
		 $$classdata{$info[0]}{'name'} = "$info[1]-$info[2]";
  		}
	}
	chop $watchclass;
	close FILE;
	#print $watchclass;
 }
}

use Term::Cap;
use Time::HiRes qw(gettimeofday sleep);

$clear = `clear`;
$terminal = Tgetent Term::Cap;
$origin = $terminal->Tgoto('cm',0,0);
$origin2 = $terminal->Tgoto('cm',0,3);

$printrates=0;
$wait_time = 1/$persec - .1;
$starttime = $oldtime = gettimeofday();

$header = $origin . "$iface-$watchclass
Class           name          rate     kbps    pps   backlog  dropped  olimits borrowed lended   tokens   ctokens
------ --------------------- -------- ------- ------ -------- -------- ------- -------- -------- -------- --------\n";

format STDOUT =
@<<<<< @<<<<<<<<<<<<<<<<<<<< @<<<<<<< @####.# @###.# @####### @####### @###### @####### @####### @####### @#######
{$classid, $$classdata{$classid}{'name'},$$classdata{$classid}{'rate'}, @{$smoothed{$classid}}{'bytes','packets'},
@{$classdata{$classid}}{'backlog','dropped','overlimits','borrowed','lended','tokens','ctokens'}}
.

print $clear.$header;

#$watchclass =~ s/\?/\./g;
#$watchclass =~ s/\*/\.\*/g;

while (@allinfo = (readpipe("tc -s class show dev $iface")))
{
    foreach (@allinfo)
    {
        next unless /\S/;
        chomp;
        
        if ((/class htb/ && ($classid = (split)[2])) || ($classid !~ /$watchclass/) )
			 {  ($$classdata{$classid}{'rate'}) = $_ =~ /rate\s+?(\S+?)\s/; next  };#rate can on 3th,5th or 10th position
        next if (/Sent/ && 
            (@{$classdata{$classid}}{'bytes','packets','dropped','overlimits'} = (split)[1,3,6,8]));
        next if (/backlog/ && chop && chop &&
            ($classdata{$classid}{'backlog'} = (split)[4]));#we have current rate if avail at [1]
        next if (/lended/ && 
            (@{$classdata{$classid}}{'lended','borrowed'} = (split)[1,3]));
        next if (/tokens/ &&
            (@{$classdata{$classid}}{'tokens','ctokens'} = (split)[1,3]));
        $classdata{$classid}{'backlog'} = 0;
    }
    
    $nowtime = gettimeofday();
    $difftime=$nowtime-$oldtime;
    $oldtime = $nowtime;
    
    print $origin2;
    foreach $classid (sort keys %classdata)
    {
        chop $classdata{$classid}{'dropped'};
        $classdata{$classid}{'bytes'} *= (8/1024);

        foreach $keyn ('bytes','packets')
        {
            last if $printrates;
            
            $smoothed{$classid}{$keyn} = 
	           $parm * (($classdata{$classid}{$keyn} - $olddata{$classid}{$keyn})/($nowtime - $last_time)) +
              	(1-$parm) * $smoothed{$classid}{$keyn};
            
            if ($nowtime-$starttime < 2/$persec) { $smoothed{$classid}{$keyn} = 0; }
            $olddata{$classid}{$keyn} = $classdata{$classid}{$keyn};
        }
    
    
    write;
    }    
    $last_time = $nowtime unless $printrates;
    $time_err = $difftime - 1/$persec;
    $wait_time -= 3*$time_err/5;
    sleep($wait_time);
    $printrates++;
    $printrates %=2;
}

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2003-12-20  9:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-12-20  9:07 [LARTC] classmon 2 raptor
2003-12-20  9:57 ` raptor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox