cosmetic change to mrtg.pl
[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 my ($self, $line) = @_;
33
34 # create the arg list
35 my %want;
36 for (split /\s+/, $line) { $want{lc $_} = 1};
37 $want{nomrtg} = 1 if $want{cfgonly} || $want{test};
38                          
39 return (1, "MRTG not installed") unless $want{nomrtg} || -e '/usr/bin/mrtg' || -e '/usr/local/bin/mrtg';
40
41 my $mc = new Mrtg or return (1, "cannot initialise Mrtg $!");
42
43 # do Data in / out totals
44 my $din = $Msg::total_in;
45 my $dout = $Msg::total_out;
46 unless ($want{agw}) {
47         $din += $AGWMsg::total_in;
48         $dout += $AGWMsg::total_out;
49 }
50
51 $mc->cfgprint('msg', [], 64000, 
52                  "Cluster Data <font color=#00cc00>in</font> and <font color=#0000ff>out</font> of $main::mycall",
53                  'Bytes / Sec', 'Bytes In', 'Bytes Out') unless $want{dataonly};
54 $mc->data('msg', $din, $dout, "Data in and out of $main::mycall") unless $want{cfgonly};
55
56 # do AGW stats if they apply
57 if ($want{agw}) {
58         $mc->cfgprint('agw', [], 64000, 
59                                   "AGW Data <font color=#00cc00>in</font> and <font color=#0000ff>out</font> of $main::mycall",
60                                   'Bytes / Sec', 'Bytes In', 'Bytes Out') unless $want{dataonly};
61         $mc->data('agw', $AGWMsg::total_in, $AGWMsg::total_out, "AGW Data in and out of $main::mycall") unless $want{cfgonly};
62 }
63
64 if (!$main::is_win && ($want{proc} || $want{all})) {
65         $ENV{COLUMNS} = 250;
66         my $secs;
67         my $f = new IO::File "ps aux |";
68 #       dbg("$f");
69         if ($f) {
70                 while (<$f>) {
71                         chomp;
72                         my $l = $_;
73 #                       dbg($l);
74                         next unless $l =~ m{cluster\.pl$};
75                         next if $l =~ m{bash\s+\-c};
76                         my @f = split /\s+/, $l;
77 #                       dbg("$f[9]");
78                         my ($m, $s) = split /:/, $f[9];
79                         $secs = ($m * 60) + $s;
80                         last;
81                 }
82                 $f->close;
83         }
84         if ($secs) {
85                 $mc->cfgprint('proc', [qw(noo perminute)], 5*60, 
86                                           "Processor Usage",
87                                           'Proc Secs / min', 'Proc Secs', 'Proc Secs') unless $want{dataonly};
88                 $mc->data('proc', $secs, $secs, "Processor Usage") unless $want{cfgonly};
89         }
90 }
91
92 # do the users and nodes
93 my $users = DXChannel::get_all_users();
94 my $nodes = DXChannel::get_all_nodes();
95
96 $mc->cfgprint('users', [qw(unknaszero gauge)], 500, 
97                  "<font color=#00cc00>Users</font> and <font color=#0000ff>Nodes</font> on $main::mycall",
98                  'Users / Nodes', 'Users', 'Nodes') unless $want{dataonly};
99 $mc->data('users', $users, $nodes, 'Users / Nodes') unless $want{cfgonly};
100
101 # do the  total users and nodes
102 if ($want{totalusers} || $want{all}) {
103         $nodes = Route::Node::count();
104         $users = Route::User::count();
105         $mc->cfgprint('totalusers', [qw(unknaszero gauge)], 10000, 
106                         'Total <font color=#00cc00>Users</font> and <font color=#0000ff>Nodes</font> in the Visible Cluster Network',
107                          'Users / Nodes', 'Users', 'Nodes') unless $want{dataonly};
108         $mc->data('totalusers', $users, $nodes, 'Total Users and Nodes in the Visible Cluster Network') unless $want{cfgonly};
109 }
110
111 # do the total spots
112 if ($want{totalspots} || $want{all}) {
113         $mc->cfgprint('totalspots',  [qw(unknaszero gauge noi)], 1000, 'Total Spots',
114                          'Spots / min', 'Spots', 'Spots') unless $want{dataonly};
115         $mc->data('totalspots', int ($Spot::totalspots/5+0.5), int($Spot::totalspots/5+0.5), 'Total Spots') unless $want{cfgonly};
116         $Spot::totalspots = 0;
117 }
118
119 # do the HF and VHF spots
120 if ($want{hfvhf} || $want{all}) {
121         $mc->cfgprint('hfspots', [qw(unknaszero gauge)], 1000, '<font color=#00cc00>HF</font> and <font color=#0000ff>VHF+</font> Spots',
122                          'Spots / min', 'HF', 'VHF') unless $want{dataonly};
123         $mc->data('hfspots', int($Spot::hfspots/5+0.5), int($Spot::vhfspots/5+0.5), 'HF and VHF+ Spots') unless $want{cfgonly};
124         $Spot::hfspots = $Spot::vhfspots = 0;
125 }
126
127 # wwv stuff
128 if ($want{wwv} || $want{all}) {
129         $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};
130         $mc->data('wwvsfi', ($Geomag::sfi || $WCY::sfi), ($Geomag::r || $WCY::r), 'WWV SFI and R') unless $want{cfgonly};
131         $mc->cfgprint('wwvka', [qw(gauge)], 1000, 'WWV <font color=#00cc00>A</font> and <font color=#0000ff>K</font>',
132                          'A / K', 'A', 'K') unless $want{dataonly};
133         $mc->data('wwvka', $Geomag::a, $Geomag::k, 'WWV A and K') unless $want{cfgonly};
134 }
135
136 # WCY stuff
137 if ($want{wcy} || $want{all}) {
138         $mc->cfgprint('wcyka', [qw(gauge)], 1000, 'WCY <font color=#00cc00>A</font> and <font color=#0000ff>K</font>',
139                          'A / K', 'A', 'K') unless $want{dataonly};
140         $mc->data('wcyka', $WCY::a, $WCY::k, 'WCY A and K') unless $want{cfgonly};
141 }
142
143 if ($want{pc92} || $want{all}) {
144
145         $mc->cfgprint('pc92ck', [], 1024000,
146                                   "PC92 <font color=#00cc00>C</font> and <font color=#0000ff>K</font> records into $main::mycall",
147                                   'Bytes / Sec', 'C', 'K') unless $want{dataonly};
148         $mc->data('pc92ck', $DXProt::pc92Cin, $DXProt::pc92Kin, "PC92 C and K into $main::mycall") unless $want{cfgonly};
149 #       $DXProt::pc92Cin = $DXProt::pc92Kin = 0;
150
151         $mc->cfgprint('pc92ad', [], 1024000,
152                                   "PC92 <font color=#00cc00>A</font> and <font color=#0000ff>D</font> records into $main::mycall",
153                                   'Bytes / Sec', 'A', 'D') unless $want{dataonly};
154         $mc->data('pc92ad', $DXProt::pc92Ain, $DXProt::pc92Din, "PC92 A and D into $main::mycall") unless $want{cfgonly};
155 #       $DXProt::pc92Ain = $DXProt::pc92Din = 0;
156
157 }
158
159
160 # do the mrtg thing
161 #
162
163 my @out;
164 {
165 local %ENV;
166 $ENV{LANG} = 'C';
167 @out = $mc->run unless $want{nomrtg};
168 }
169 return (1, @out);