put back msg totals for mrtg and background mrtg
[spider.git] / cmd / mrtg.pl
1 #
2 # This is a local command to generate the various statistics that
3 # can then be displayed on an MRTG plot
4 #
5 # Your mrtg binary must live in one of the standard places
6 #
7 # The arguments (keywords) to the mrtg command are these
8 #
9 # a) content          (you always get the node users and nodes and data in/out)
10 #    proc             - get the processor usage
11 #    agw              - include the AGW stats separately 
12 #    totalspots       - all spots
13 #    hfvhf            - all spots split into HF and VHF
14 #    wwv              - two graphs of WWV, one SFI and R other A and K
15 #    wcy              - WCY A and K 
16 #    pc92             - PC92 C and K, PC92 A and D
17 #    all              - all of the above 
18 #    
19 # b) actions          
20 #    test             - do everything except check for and run mrtg
21 #    nomrtg           - ditto (better name)
22 #    dataonly         - only generate the data files for mrtg
23 #    cfgonly          - only generate the mrtg.cfg file (like cfgmaker)
24 #    runmrtg          - run mrtg, this is probably used with dataonly
25 #                     - together with a home rolled mrtg.cfg 
26 #
27 # Copyright (c) 2002 Dirk Koopman G1TLH
28 #
29 #
30 #
31
32 sub handle
33 {
34         my ($self, $line) = @_;
35         
36         # create the arg list
37         my %want;
38         for (split /\s+/, $line) { $want{lc $_} = 1};
39         $want{nomrtg} = 1 if $want{cfgonly} || $want{test};
40         
41         return (1, "MRTG not installed") unless $want{nomrtg} || -e '/usr/bin/mrtg' || -e '/usr/local/bin/mrtg';
42
43         my @out = $self->spawn_cmd("mrtg $line", \&do_it, args=>[%want]);
44         
45         return (1, @out);
46 }
47
48
49 sub do_it
50 {
51         my %want = @_;
52         
53         my $mc = new Mrtg or return (1, "cannot initialise Mrtg $!");
54
55         # do Data in / out totals
56         my $din = $Msg::total_in;
57         my $dout = $Msg::total_out;
58         unless ($want{agw}) {
59                 $din += $AGWMsg::total_in;
60                 $dout += $AGWMsg::total_out;
61         }
62
63         $mc->cfgprint('msg', [], 64000, 
64                                   "Cluster Data <font color=#00cc00>in</font> and <font color=#0000ff>out</font> of $main::mycall",
65                                   'Bytes / Sec', 'Bytes In', 'Bytes Out') unless $want{dataonly};
66         $mc->data('msg', $din, $dout, "Data in and out of $main::mycall") unless $want{cfgonly};
67
68         # do AGW stats if they apply
69         if ($want{agw}) {
70                 $mc->cfgprint('agw', [], 64000, 
71                                           "AGW Data <font color=#00cc00>in</font> and <font color=#0000ff>out</font> of $main::mycall",
72                                           'Bytes / Sec', 'Bytes In', 'Bytes Out') unless $want{dataonly};
73                 $mc->data('agw', $AGWMsg::total_in, $AGWMsg::total_out, "AGW Data in and out of $main::mycall") unless $want{cfgonly};
74         }
75
76         if (!$main::is_win && ($want{proc} || $want{all})) {
77                 $ENV{COLUMNS} = 250;
78                 my $secs;
79                 my $f = new IO::File "ps ax -ocputime,args |";
80                 #       dbg("$f");
81                 if ($f) {
82                         while (<$f>) {
83                                 chomp;
84                                 my $l = $_;
85                                 #                       dbg($l);
86                                 next unless $l =~ m{cluster\.pl$};
87                                 next if $l =~ m{bash\s+\-c};
88                                 my @f = split /\s+/, $l;
89                                 #                       dbg("$f[9]");
90                                 my ($d, $h, $m, $s) = $f[0] =~ /(?:(\d+)-)?(\d+):(\d\d):(\d\d)$/;
91                                 $d ||= 0;
92                                 $secs = ($d * 86400) + ($h * 3600) + ($m * 60) + $s;
93                                 last;
94                         }
95                         $f->close;
96                 }
97                 if ($secs) {
98                         $mc->cfgprint('proc', [qw(noo perminute)], 5*60, 
99                                                   "Processor Usage",
100                                                   'Proc Secs / min', 'Proc Secs', 'Proc Secs') unless $want{dataonly};
101                         $mc->data('proc', $secs, $secs, "Processor Usage") unless $want{cfgonly};
102                 }
103         }
104
105         # do the users and nodes
106         my $users = DXChannel::get_all_users();
107         my $nodes = DXChannel::get_all_nodes();
108
109         $mc->cfgprint('users', [qw(unknaszero gauge)], 500, 
110                                   "<font color=#00cc00>Users</font> and <font color=#0000ff>Nodes</font> on $main::mycall",
111                                   'Users / Nodes', 'Users', 'Nodes') unless $want{dataonly};
112         $mc->data('users', $users, $nodes, 'Users / Nodes') unless $want{cfgonly};
113
114         # do the  total users and nodes
115         if ($want{totalusers} || $want{all}) {
116                 $nodes = Route::Node::count();
117                 $users = Route::User::count();
118                 $mc->cfgprint('totalusers', [qw(unknaszero gauge)], 10000, 
119                                           'Total <font color=#00cc00>Users</font> and <font color=#0000ff>Nodes</font> in the Visible Cluster Network',
120                                           'Users / Nodes', 'Users', 'Nodes') unless $want{dataonly};
121                 $mc->data('totalusers', $users, $nodes, 'Total Users and Nodes in the Visible Cluster Network') unless $want{cfgonly};
122         }
123
124         # do the total spots
125         if ($want{totalspots} || $want{all}) {
126                 $mc->cfgprint('totalspots',  [qw(unknaszero gauge noi)], 1000, 'Total Spots',
127                                           'Spots / min', 'Spots', 'Spots') unless $want{dataonly};
128                 $mc->data('totalspots', int ($Spot::totalspots/5+0.5), int($Spot::totalspots/5+0.5), 'Total Spots') unless $want{cfgonly};
129                 $Spot::totalspots = 0;
130         }
131
132         # do the HF and VHF spots
133         if ($want{hfvhf} || $want{all}) {
134                 $mc->cfgprint('hfspots', [qw(unknaszero gauge)], 1000, '<font color=#00cc00>HF</font> and <font color=#0000ff>VHF+</font> Spots',
135                                           'Spots / min', 'HF', 'VHF') unless $want{dataonly};
136                 $mc->data('hfspots', int($Spot::hfspots/5+0.5), int($Spot::vhfspots/5+0.5), 'HF and VHF+ Spots') unless $want{cfgonly};
137                 $Spot::hfspots = $Spot::vhfspots = 0;
138         }
139
140         # wwv stuff
141         if ($want{wwv} || $want{all}) {
142                 $mc->cfgprint('wwvsfi', [qw(gauge)], 1000, 'WWV <font color=#00cc00>SFI</font> and <font color=#0000ff>R</font>', 'SFI / R', 'SFI', 'R') unless $want{dataonly};
143                 $mc->data('wwvsfi', ($Geomag::sfi || $WCY::sfi), ($Geomag::r || $WCY::r), 'WWV SFI and R') unless $want{cfgonly};
144                 $mc->cfgprint('wwvka', [qw(gauge)], 1000, 'WWV <font color=#00cc00>A</font> and <font color=#0000ff>K</font>',
145                                           'A / K', 'A', 'K') unless $want{dataonly};
146                 $mc->data('wwvka', $Geomag::a, $Geomag::k, 'WWV A and K') unless $want{cfgonly};
147         }
148
149         # WCY stuff
150         if ($want{wcy} || $want{all}) {
151                 $mc->cfgprint('wcyka', [qw(gauge)], 1000, 'WCY <font color=#00cc00>A</font> and <font color=#0000ff>K</font>',
152                                           'A / K', 'A', 'K') unless $want{dataonly};
153                 $mc->data('wcyka', $WCY::a, $WCY::k, 'WCY A and K') unless $want{cfgonly};
154         }
155
156         if ($want{pc92} || $want{all}) {
157
158                 $mc->cfgprint('pc92ck', [], 1024000,
159                                           "PC92 <font color=#00cc00>C</font> and <font color=#0000ff>K</font> records into $main::mycall",
160                                           'Bytes / Sec', 'C', 'K') unless $want{dataonly};
161                 $mc->data('pc92ck', $DXProt::pc92Cin, $DXProt::pc92Kin, "PC92 C and K into $main::mycall") unless $want{cfgonly};
162                 #       $DXProt::pc92Cin = $DXProt::pc92Kin = 0;
163
164                 $mc->cfgprint('pc92ad', [], 1024000,
165                                           "PC92 <font color=#00cc00>A</font> and <font color=#0000ff>D</font> records into $main::mycall",
166                                           'Bytes / Sec', 'A', 'D') unless $want{dataonly};
167                 $mc->data('pc92ad', $DXProt::pc92Ain, $DXProt::pc92Din, "PC92 A and D into $main::mycall") unless $want{cfgonly};
168                 #       $DXProt::pc92Ain = $DXProt::pc92Din = 0;
169
170         }
171
172                 # 
173         # do the mrtg thing
174         #
175
176         my @out;
177         {
178                 local %ENV;
179                 $ENV{LANG} = 'C';
180                 @out = $mc->run unless $want{nomrtg};
181         }
182
183         return @out;
184 }
185