mrtg fixes
[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         $DB::single = 1;
37         
38         # create the arg list
39         my %want;
40         for (split /\s+/, $line) { $want{lc $_} = 1};
41         $want{nomrtg} = 1 if $want{cfgonly} || $want{test};
42         
43         return (1, "MRTG not installed") unless $want{nomrtg} || -e '/usr/bin/mrtg' || -e '/usr/local/bin/mrtg';
44         return (1, "MRTG requires top to be installed") unless $want{nomrtg} || -e '/usr/bin/top' || -e '/usr/local/bin/top';
45
46         my @out = do_it(%want);
47         
48         return (1, @out);
49 }
50
51
52 sub do_it
53 {
54         my %want = @_;
55         
56         my $mc = new Mrtg or return (1, "cannot initialise Mrtg $!");
57
58         # do Data in / out totals
59         my $din = $Msg::total_in;
60         my $dout = $Msg::total_out;
61
62         $mc->cfgprint('msg', [ qw(integer) ], 64000, 
63                                   "Cluster Data <font color=#00cc00>in</font> and <font color=#0000ff>out</font> of $main::mycall",
64                                   'Bytes / Sec', 'Bytes In', 'Bytes Out') unless $want{dataonly};
65         $mc->data('msg', $din, $dout, "Data in and out of $main::mycall") unless $want{cfgonly};
66         dbg("mrtg: din: $din dout: $dout") if isdbg("mrtg");
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                 dbg("mrtg: agwin: $AGWMsg::total_in  agwout: $AGWMsg::total_out") if isdbg("mrtg");
75         }
76
77         if (!$main::is_win && ($want{proc} || $want{all})) {
78                 $ENV{COLUMNS} = 250;
79                 my $secs;
80                 my $f = new IO::File "top -b -o TIME+ -n 1 -c |";
81                 #       dbg("$f");
82                 if ($f) {
83                         while (<$f>) {
84                                 chomp;
85                                 my $l = $_;
86                                 dbg("mrtg: $l") if isdbg("mrtg");
87                                 next unless $l =~ m{cluster\.pl$};
88                                 next if $l =~ m{\d\s+su\s+};
89                                 next if $l =~ m{\d\s+bash\s+};
90                                 my @f = split /\s+/, $l;
91                                 my ($m, $s) = $f[10] =~ /\b(\d+):(\d\d\.\d\d)\b/;
92                                 $secs = ($m * 60) + $s;
93                                 dbg("mrtg: proc: $f[10] m: $m s: $s secs: $secs") if isdbg("mrtg");
94                                 last;
95                         }
96                         $f->close;
97
98                         $mc->cfgprint('proc', [qw(noi unknaszero withzeroes 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 integer)], 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         dbg("mrtg: din: $din dout: $dout") if isdbg("mrtg");
114
115         # do the  total users and nodes
116         if ($want{totalusers} || $want{all}) {
117                 $nodes = Route::Node::count();
118                 $users = Route::User::count();
119                 $mc->cfgprint('totalusers', [qw(integer unknaszero gauge)], 10000, 
120                                           'Total <font color=#00cc00>Users</font> and <font color=#0000ff>Nodes</font> in the Visible Cluster Network',
121                                           'Users / Nodes', 'Users', 'Nodes') unless $want{dataonly};
122                 $mc->data('totalusers', $users, $nodes, 'Total Users and Nodes in the Visible Cluster Network') unless $want{cfgonly};
123                 dbg("mrtg: users: $users nodes: $nodes") if isdbg("mrtg");
124         }
125
126         # do the total spots
127         if ($want{totalspots} || $want{all}) {
128                 $mc->cfgprint('totalspots',  [qw(integer withzeroes unknaszero noi perminute)], 1000, 'Total Spots',
129                                           'Spots / min', 'Spots', 'Spots') unless $want{dataonly};
130                 $mc->data('totalspots', $Spot::totalspots, $Spot::totalspots, 'Total Spots') unless $want{cfgonly};
131                 dbg("mrtg: total spots: $Spot::totalspots") if isdbg("mrtg");
132                 #$Spot::totalspots = 0;
133         }
134
135         # do the HF and VHF spots
136         if ($want{hfvhf} || $want{all}) {
137                 $mc->cfgprint('hfspots', [qw(integer withzeroes unknaszero perminute)], 1000, '<font color=#00cc00>HF</font> and <font color=#0000ff>VHF+</font> Spots',
138                                           'Spots / min', 'HF', 'VHF') unless $want{dataonly};
139                 $mc->data('hfspots', $Spot::hfspots, $Spot::vhfspots, 'HF and VHF+ Spots') unless $want{cfgonly};
140                 dbg("mrtg: hfspots: $Spot::hfspots vhfspots: $Spot::vhfspots") if isdbg("mrtg");
141                 #$Spot::hfspots = $Spot::vhfspots = 0;
142         }
143
144         # wwv stuff
145         if ($want{wwv} || $want{all}) {
146                 $mc->cfgprint('wwvsfi', [qw(integer gauge)], 1000, 'WWV <font color=#00cc00>SFI</font> and <font color=#0000ff>R</font>', 'SFI / R', 'SFI', 'R') unless $want{dataonly};
147                 $mc->data('wwvsfi', ($Geomag::sfi || $WCY::sfi), ($Geomag::r || $WCY::r), 'WWV SFI and R') unless $want{cfgonly};
148                 $mc->cfgprint('wwvka', [qw(gauge)], 1000, 'WWV <font color=#00cc00>A</font> and <font color=#0000ff>K</font>',
149                                           'A / K', 'A', 'K') unless $want{dataonly};
150                 $mc->data('wwvka', $Geomag::a, $Geomag::k, 'WWV A and K') unless $want{cfgonly};
151                 dbg("mrtg: WWV A: $Geomag::a K: $Geomag::k") if isdbg("mrtg");
152         }
153
154         # WCY stuff
155         if ($want{wcy} || $want{all}) {
156                 $mc->cfgprint('wcyka', [qw(integer gauge)], 1000, 'WCY <font color=#00cc00>A</font> and <font color=#0000ff>K</font>',
157                                           'A / K', 'A', 'K') unless $want{dataonly};
158                 $mc->data('wcyka', $WCY::a, $WCY::k, 'WCY A and K') unless $want{cfgonly};
159                 dbg("mrtg: WCY A: $WCY::a K: $WCY::k") if isdbg("mrtg");
160         }
161
162         if ($want{pc92} || $want{all}) {
163
164                 $mc->cfgprint('pc92ck', [qw(integer)], 1024000,
165                                           "PC92 <font color=#00cc00>C</font> and <font color=#0000ff>K</font> records into $main::mycall",
166                                           'Bytes / Sec', 'C', 'K') unless $want{dataonly};
167                 $mc->data('pc92ck', $DXProt::pc92Cin, $DXProt::pc92Kin, "PC92 C and K into $main::mycall") unless $want{cfgonly};
168                 #       $DXProt::pc92Cin = $DXProt::pc92Kin = 0;
169
170                 $mc->cfgprint('pc92ad', [qw(integer)], 1024000,
171                                           "PC92 <font color=#00cc00>A</font> and <font color=#0000ff>D</font> records into $main::mycall",
172                                           'Bytes / Sec', 'A', 'D') unless $want{dataonly};
173                 $mc->data('pc92ad', $DXProt::pc92Ain, $DXProt::pc92Din, "PC92 A and D into $main::mycall") unless $want{cfgonly};
174                 #       $DXProt::pc92Ain = $DXProt::pc92Din = 0;
175                 dbg("mrtg: PC92 C: $DXProt::pc92Cin K: $DXProt::pc92Kin A: $DXProt::pc92Ain D: $DXProt::pc92Din") if isdbg("mrtg");
176         }
177
178                 # 
179         # do the mrtg thing
180         #
181
182         my @out;
183         {
184                 local %ENV;
185                 $ENV{LANG} = 'C';
186                 @out = $mc->run unless $want{nomrtg};
187         }
188
189         return @out;
190 }
191