File: netstat

package info (click to toggle)
mgm 1.1.cvs.20020221-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,724 kB
  • ctags: 33
  • sloc: perl: 768; makefile: 32
file content (116 lines) | stat: -rw-r--r-- 2,810 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# -*-Perl-*-

# instances allowed: one 

# (single instance modules would be silly to use more than one of
# anyway, so we use package local storage.  This is faster and places
# less artificial load on the machine than doing everything through
# the object hash)

package MGMmodule::netstat;
use vars qw($xpath @if @keys $active $field $prompt @prev @bytes $widget 
	    $graph $lastmod);

# class init
sub module_init{
    my$this=shift;
    my$toplevel=$this->{"toplevel"};
    my$xclass=$this->{"xclass"};

    # is the helper up and running?
    if(!defined(%MGMmodule::helperST::net)){
	$toplevel->optionAdd("$xclass.active",'false',21);   
    } 
    @keys=keys %MGMmodule::helperST::net;
    $active=$#keys+1;

    # three possible different formats.  
    my@test=split ' ', $MGMmodule::helperST::net{$keys[0]};
    if($#test==9){
	$field=5;
	$prompt=' packets/s';
    }else{
	$field=($#test+1)/2;
	$prompt='B/s';
    }
    $toplevel->optionAdd("$xclass.order",1,21);   
    $this;
}

# instance init
sub module_instance{
    my$this=shift;
    my$toplevel=$this->{"toplevel"};
    return undef if(defined($xpath));
    $xpath=$this->{"xpath"};

    # modify defaults
    my$i=0;
    foreach my $key (@keys){
	$toplevel->optionAdd("$xpath.bar.".($i*2).".label", "$key Rx",21);
	$toplevel->optionAdd("$xpath.bar.".($i*2).".litbackground", '#00d',21);
	$toplevel->optionAdd("$xpath.bar.".($i*2+1).".label", "$key Tx",21);
	$toplevel->optionAdd("$xpath.bar.".($i*2+1).".litbackground","#24f",21);
	$i++;
    }

    $toplevel->optionAdd("$xpath.scalewidadj", 80*$i*2,21);  # narrower
    $toplevel->optionAdd("$xpath.scalereturn", 120,21);
    
    # this relies on the above defaults

    my($minx,$miny)=MGM::Graph::calcxysize($this,1024*1024*512,
					   $prompt,$active*2);
    
    $toplevel->optionAdd("$xpath.minx",        $minx,21);      
    $toplevel->optionAdd("$xpath.miny",        $miny,21);      
    $this;
}

# instance widget build
sub module_run{
    my$this=shift;
    
    $graph=MGM::Graph->new($this,num=>$active*2,
			   prompt=>$prompt,
			   minscale=>2048);

    $lastmod=-1;
    $widget=$graph->{"widget"};        # must return the widget
}

sub read_proc{
    # now uses the 00helper to save on opens 
    my$i=0;
    foreach my $key (@keys){
	my@temp=split ' ', $MGMmodule::helperST::net{$key};
	$bytes[$i]=$temp[0];
	$bytes[$i+1]=$temp[$field];
	$i+=2;
    }
}

sub module_update{ 
    my$this=shift;
    
    # don't update unless the helper has
    if($lastmod!=$MGMmodule::helperST::lastmod){
	my$time=$MGMmodule::helperST::lastmod;
	my@vals;

	$this->read_proc;
	if(defined(@prev)){
	    for(my$i=0;$i<$active*2;$i++){
		$vals[$i]=($bytes[$i]-$prev[$i])/($time-$lastmod)*100;
	    }
	    
	    $graph->set(@vals);
	}
	
	@prev=@bytes;
	$lastmod=$time;
    }
}

bless {};