4a872d6172810942393cc967357689a2b90110ad
[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                 $ENV{COLUMNS} = 250;
80                 my $secs;
81
82                 $secs = clock_gettime(CLOCK_PROCESS_CPUTIME_ID);
83                 
84                 $mc->cfgprint('proc', [qw(noi unknaszero withzeroes perminute)], 5*60, 
85                                           "Processor Usage",
86                                           'Proc Secs/Min', 'Proc Secs', 'Proc Secs') unless $want{dataonly};
87                 $mc->data('proc', $secs, $secs, "Processor Usage") unless $want{cfgonly};
88         }
89
90         # do the users and nodes
91         my $users = DXChannel::get_all_users();
92         my $nodes = DXChannel::get_all_nodes();
93
94         $mc->cfgprint('users', [qw(unknaszero gauge integer)], 500, 
95                                   "<font color=#00cc00>Users</font> and <font color=#0000ff>Nodes</font> on $main::mycall",
96                                   'Users / Nodes', 'Users', 'Nodes') unless $want{dataonly};
97         $mc->data('users', $users, $nodes, 'Users / Nodes') unless $want{cfgonly};
98         dbg("mrtg: din: $din dout: $dout") if isdbg("mrtg");
99
100         # do the  total users and nodes
101         if ($want{totalusers} || $want{all}) {
102                 $nodes = Route::Node::count();
103                 $users = Route::User::count();
104                 $mc->cfgprint('totalusers', [qw(integer unknaszero gauge)], 10000, 
105                                           'Total <font color=#00cc00>Users</font> and <font color=#0000ff>Nodes</font> in the Visible Cluster Network',
106                                           'Users / Nodes', 'Users', 'Nodes') unless $want{dataonly};
107                 $mc->data('totalusers', $users, $nodes, 'Total Users and Nodes in the Visible Cluster Network') unless $want{cfgonly};
108                 dbg("mrtg: users: $users nodes: $nodes") if isdbg("mrtg");
109         }
110
111         # do the total spots
112         if ($want{totalspots} || $want{all}) {
113                 $mc->cfgprint('totalspots',  [qw(integer withzeroes unknaszero noi perminute)], 1000, 'Total Spots',
114                                           'Spots / min', 'Spots', 'Spots') unless $want{dataonly};
115                 $mc->data('totalspots', $Spot::totalspots, $Spot::totalspots, 'Total Spots') unless $want{cfgonly};
116                 dbg("mrtg: total spots: $Spot::totalspots") if isdbg("mrtg");
117                 #$Spot::totalspots = 0;
118         }
119
120         # do the HF and VHF spots
121         if ($want{hfvhf} || $want{all}) {
122                 $mc->cfgprint('hfspots', [qw(integer withzeroes unknaszero perminute)], 1000, '<font color=#00cc00>HF</font> and <font color=#0000ff>VHF+</font> Spots',
123                                           'Spots / min', 'HF', 'VHF') unless $want{dataonly};
124                 $mc->data('hfspots', $Spot::hfspots, $Spot::vhfspots, 'HF and VHF+ Spots') unless $want{cfgonly};
125                 dbg("mrtg: hfspots: $Spot::hfspots vhfspots: $Spot::vhfspots") if isdbg("mrtg");
126                 #$Spot::hfspots = $Spot::vhfspots = 0;
127         }
128
129         # wwv stuff
130         if ($want{wwv} || $want{all}) {
131                 $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};
132                 $mc->data('wwvsfi', ($Geomag::sfi || $WCY::sfi), ($Geomag::r || $WCY::r), 'WWV SFI and R') unless $want{cfgonly};
133                 $mc->cfgprint('wwvka', [qw(gauge)], 1000, 'WWV <font color=#00cc00>A</font> and <font color=#0000ff>K</font>',
134                                           'A / K', 'A', 'K') unless $want{dataonly};
135                 $mc->data('wwvka', $Geomag::a, $Geomag::k, 'WWV A and K') unless $want{cfgonly};
136                 dbg("mrtg: WWV A: $Geomag::a K: $Geomag::k") if isdbg("mrtg");
137         }
138
139         # WCY stuff
140         if ($want{wcy} || $want{all}) {
141                 $mc->cfgprint('wcyka', [qw(integer gauge)], 1000, 'WCY <font color=#00cc00>A</font> and <font color=#0000ff>K</font>',
142                                           'A / K', 'A', 'K') unless $want{dataonly};
143                 $mc->data('wcyka', $WCY::a, $WCY::k, 'WCY A and K') unless $want{cfgonly};
144                 dbg("mrtg: WCY A: $WCY::a K: $WCY::k") if isdbg("mrtg");
145         }
146
147         if ($want{pc92} || $want{all}) {
148
149                 $mc->cfgprint('pc92ck', [qw(integer)], 1024000,
150                                           "PC92 <font color=#00cc00>C</font> and <font color=#0000ff>K</font> records into $main::mycall",
151                                           'Bytes / Sec', 'C', 'K') unless $want{dataonly};
152                 $mc->data('pc92ck', $DXProt::pc92Cin, $DXProt::pc92Kin, "PC92 C and K into $main::mycall") unless $want{cfgonly};
153                 #       $DXProt::pc92Cin = $DXProt::pc92Kin = 0;
154
155                 $mc->cfgprint('pc92ad', [qw(integer)], 1024000,
156                                           "PC92 <font color=#00cc00>A</font> and <font color=#0000ff>D</font> records into $main::mycall",
157                                           'Bytes / Sec', 'A', 'D') unless $want{dataonly};
158                 $mc->data('pc92ad', $DXProt::pc92Ain, $DXProt::pc92Din, "PC92 A and D into $main::mycall") unless $want{cfgonly};
159                 #       $DXProt::pc92Ain = $DXProt::pc92Din = 0;
160                 dbg("mrtg: PC92 C: $DXProt::pc92Cin K: $DXProt::pc92Kin A: $DXProt::pc92Ain D: $DXProt::pc92Din") if isdbg("mrtg");
161         }
162
163                 # 
164         # do the mrtg thing
165         #
166
167         my @out;
168         {
169                 local %ENV;
170                 $ENV{LANG} = 'C';
171                 @out = $mc->run unless $want{nomrtg};
172         }
173
174         return @out;
175 }
176