]> dxcluster.net Git - spider.git/blob - perl/Mrtg.pm
make it (seem to) work under perl 5.10
[spider.git] / perl / Mrtg.pm
1 #
2 # the MRTG handler
3 #
4 # Copyright (c) - 2002 Dirk Koopman G1TLH
5 #
6 #
7 #
8
9 package Mrtg;
10
11 use IO::File;
12 use DXVars;
13 use DXDebug;
14 use DXUtil;
15 use strict;
16
17 sub new
18 {
19         my $pkg = shift;
20         my $class = ref $pkg || $pkg;
21         my %args = @_;
22         my $self = bless { }, $class;
23
24         # argument processing
25         $self->{dir} = $args{dir} || "$main::root/mrtg";
26         $self->{html} = $args{html} || "$main::root/html/mrtg";
27         $self->{cfg} = $args{cfg} || "$self->{dir}/mrtg.cfg";
28
29         my $mc = new IO::File ">$self->{cfg}" or return undef;
30         $self->{mc} = $mc;
31         
32         # print out the header
33         print $mc <<"EOF";
34 ### Global Defaults
35 Htmldir: $self->{html}
36 Imagedir: $self->{html}
37 Logdir: $self->{dir}
38 Options[_]: growright
39 Timezone[_]: GMT
40 ##
41 ##
42 EOF
43
44         return $self;
45 }
46
47 sub run
48 {
49         my $self = shift;
50         $self->{mc}->close;
51         return `mrtg --lock-file=$self->{dir}/mrtg.lock --confcache-file=$self->{dir}/mrtg.confcache $self->{cfg}`;
52 }
53
54 sub data
55 {
56         my ($self, $name, $vali, $valo, $title) = @_;
57         my $uptime = main::uptime();
58         $vali ||= 0;
59         $valo ||= 0;
60
61         if (my $m = new IO::File ">$self->{dir}/$name" ) {
62                 $m->print("$vali\n$valo\n$uptime\n$title\n");
63                 $m->close;
64         } else {
65                 dbg("MRTG: cannot open $self->{dir}/$name $!");
66         }
67 }
68
69 sub cfgprint
70 {
71         my ($self, $name, $options, $max, $title, $legend, $iname, $oname) = @_;
72         my $opt = join ', ', @$options, qw(withzeroes growright nopercent integer);
73
74         $self->{mc}->print(<<"EOF");
75
76 #
77 # $title
78 #
79
80 Target[$name]: `cat /spider/mrtg/$name`
81 MaxBytes[$name]: $max
82 Title[$name]: $title
83 Options[$name]: $opt
84 YLegend[$name]: $legend
85 YTicsFactor[$name]: 1
86 ShortLegend[$name]: \&nbsp;
87 Legend1[$name]:Maximum No of $iname
88 Legend2[$name]:Maximum No of $oname
89 LegendI[$name]:$iname
90 LegendO[$name]:$oname
91 PageTop[$name]: <H1>$title</H1>
92  <TABLE>
93    <TR><TD>System:</TD>     <TD>$main::mycall</TD></TR>
94    <TR><TD>Maintainer:</TD> <TD>$main::myemail</TD></TR>
95    <TR><TD>Description:</TD><TD>$title</TD></TR>
96  </TABLE>
97 EOF
98
99 }
100 1;