improved version of 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 # you will need perl 5.6 (probably) to be able to run this command
8 #
9
10 sub cfgprint($$@$$$$$);
11
12 my ($self, $line) = @_;
13
14 # create the arg list
15 my %want;
16 for (split /\s+/, $line) { $want{lc $_} = 1};
17                          
18 return (1, "MRTG not installed") unless $want{test} || -e '/usr/bin/mrtg' || -e '/usr/local/bin/mrtg';
19
20 my $dir = "$main::root/mrtg";
21 my $html = "$main::root/html/mrtg";
22 my $cfg = "$dir/mrtg.cfg";
23
24 my $mc = new IO::File ">$cfg" or return(1, "cannot open $cfg for mrtg writing");
25
26 # print out the header
27 print $mc <<"EOF";
28 ### Global Defaults
29
30 #  to get bits instead of bytes and graphs growing to the right
31 # Options[_]: growright, bits
32
33 Htmldir: $html
34 Imagedir: $html
35 Logdir: $dir
36 Options[_]: growright
37
38 ##
39 ##
40
41 EOF
42
43
44 #dbg "$dir\n$html\n";
45                          
46 # do the users and nodes
47 my $users = DXChannel::get_all_users();
48 my $nodes = DXChannel::get_all_nodes();
49 my $uptime = main::uptime();
50 #dbg "$users $nodes $uptime\n";
51 if (my $m = new IO::File ">$dir/users") {
52         print $m "$users\n$nodes\n$uptime\nUsers and Nodes\n";
53         close $m;
54 }
55 cfgprint($mc, 'users', [qw(gauge)], 500, 
56                  "Users and Nodes on $main::mycall",
57                  'Users / Nodes', 'Users', 'Nodes');
58
59 # do the  total users and nodes
60 if ($want{totalusers} || $want{all}) {
61         $nodes = Route::Node::count();
62         $users = Route::User::count();
63         #dbg "$users $nodes $uptime\n";
64         if (my $m = new IO::File ">$dir/totalusers") {
65                 print $m "$users\n$nodes\n$uptime\nTotal Users and Nodes\n";
66                 close $m;
67         }
68         cfgprint($mc, 'totalusers', [qw(gauge)], 10000, 
69                         'Total Users and Nodes in the Visible Cluster Network',
70                          'Users / Nodes', 'Users', 'Nodes');
71 }
72
73 # do the total spots
74 if ($want{totalspots} || $want{all}) {
75         if (my $m = new IO::File ">$dir/totalspots") {
76                 print $m "$Spot::totalspots\n$Spot::totalspots\n$uptime\nTotal Spots\n";
77                 close $m;
78         }
79         $Spot::totalspots = 0;
80         cfgprint($mc, 'totalspots', [qw(unknaszero gauge noi)], 1000, 'Total Spots',
81                          'Spots', 'Spots', 'Spots');
82 }
83
84 # do the HF and VHF spots
85 if ($want{hfvhf} || $want{all}) {
86         if (my $m = new IO::File ">$dir/hfspots") {
87                 print $m "$Spot::hfspots\n$Spot::vhfspots\n$uptime\nHF and VHF+ Spots\n";
88                 close $m;
89         }
90         $Spot::hfspots = $Spot::vhfspots = 0;
91         cfgprint($mc, 'hfspots', [qw(unknaszero gauge)], 1000, 'HF and VHF+ Spots',
92                          'Spots', 'HF', 'VHF');
93 }
94
95
96 # do the mrtg thing
97 #
98 close $mc;
99 my @out = `mrtg $cfg`;
100 return (1, @out);
101
102 sub cfgprint
103 {
104         my ($mc, $name, $options, $max, $title, $legend, $iname, $oname) = @_;
105         my $opt = join ', ', @$options, qw(withzeroes gauge growright nopercent integer);
106                 
107         print $mc <<"EOF";
108
109 #
110 # $title
111 #
112
113 Target[$name]: `cat /spider/mrtg/$name`
114 MaxBytes[$name]: $max
115 Title[$name]: $title
116 Options[$name]: $opt
117 YLegend[$name]: $legend
118 YTicsFactor[$name]: 1
119 ShortLegend[$name]: \&nbsp;
120 Legend1[$name]:Maximum No of $iname
121 Legend2[$name]:Maximum No of $oname
122 LegendI[$name]:$iname
123 LegendO[$name]:$oname
124 PageTop[$name]: <H1>$title</H1>
125  <TABLE>
126    <TR><TD>System:</TD>     <TD>$main::mycall</TD></TR>
127    <TR><TD>Maintainer:</TD> <TD>$main::myemail</TD></TR>
128    <TR><TD>Description:</TD><TD>$title</TD></TR>
129  </TABLE>
130 EOF
131
132 }