]> scm.dxcluster.org Git - spider.git/blob - perl/Mrtg.pm
move ::process things back into persec
[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, $yticsfactor) = @_;
72         my $opt = join ', ', @$options, qw(withzeroes growright nopercent integer);
73         $yticsfactor ||= 1;
74
75         $self->{mc}->print(<<"EOF");
76
77 #
78 # $title
79 #
80
81 Target[$name]: `cat /spider/mrtg/$name`
82 MaxBytes[$name]: $max
83 Title[$name]: $title
84 Options[$name]: $opt
85 YLegend[$name]: $legend
86 YTicsFactor[$name]: $yticsfactor
87 ShortLegend[$name]: \&nbsp;
88 Legend1[$name]:Maximum No of $iname
89 Legend2[$name]:Maximum No of $oname
90 LegendI[$name]:$iname
91 LegendO[$name]:$oname
92 PageTop[$name]: <H1>$title</H1>
93  <TABLE>
94    <TR><TD>System:</TD>     <TD>$main::mycall</TD></TR>
95    <TR><TD>Maintainer:</TD> <TD>$main::myemail</TD></TR>
96    <TR><TD>Description:</TD><TD>$title</TD></TR>
97  </TABLE>
98 EOF
99
100 }
101 1;