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