]>
Commit | Line | Data |
---|---|---|
e18c93b3 MG |
1 | #!/usr/bin/perl -w |
2 | ||
1c9fb9df MG |
3 | if ((@ARGV != 1) && (@ARGV != 2)) { |
4 | print STDERR "Syntax: ${0} configfile [uid]\n"; | |
e18c93b3 MG |
5 | exit(1); |
6 | } | |
7 | ||
1c9fb9df | 8 | use Net::SNMP; |
e18c93b3 | 9 | |
1c9fb9df MG |
10 | $UPSGRAPH::host = ""; |
11 | $UPSGRAPH::rrdfile = ""; | |
12 | $UPSGRAPH::outdir = ""; | |
13 | $UPSGRAPH::community = "public"; | |
14 | $UPSGRAPH::step = 60; | |
15 | @UPSGRAPH::fields = (); | |
16 | $UPSGRAPH::vars = {}; | |
e18c93b3 | 17 | |
1c9fb9df | 18 | do $ARGV[0] or die "can't read config: $!"; |
e18c93b3 | 19 | |
1c9fb9df MG |
20 | my $host = $UPSGRAPH::host; |
21 | my $rrdfile = $UPSGRAPH::rrdfile; | |
22 | my $outdir = $UPSGRAPH::outdir; | |
23 | my $community = $UPSGRAPH::community; | |
24 | my $step = $UPSGRAPH::step; | |
25 | my @fields = @UPSGRAPH::fields; | |
26 | my $vars = $UPSGRAPH::vars; | |
e18c93b3 | 27 | |
af4abf85 | 28 | if ($> == 0) { |
1c9fb9df | 29 | if (@ARGV != 2) { |
af4abf85 MG |
30 | print STDERR "Running as root, please provide UID as 4th argument!\n"; |
31 | exit(1); | |
32 | } | |
33 | ||
34 | print "Running as root, switching to ".$ARGV[3]."\n"; | |
1c9fb9df | 35 | $< = $> = $ARGV[1]; |
af4abf85 MG |
36 | } |
37 | ||
e18c93b3 MG |
38 | if (! -e "${rrdfile}") { |
39 | my $cmd = "rrdtool create \"${rrdfile}\" "; | |
40 | foreach my $var (@fields) { | |
41 | $cmd .= "DS:${var}:GAUGE:600:" . | |
42 | $vars->{$var}->{'min'} . ":" . | |
43 | $vars->{$var}->{'max'} . " "; | |
44 | } | |
45 | $cmd .= "RRA:AVERAGE:0.5:1:10080 --step ${step}"; | |
46 | ||
47 | print "Creating ${rrdfile}...\n"; | |
48 | print `${cmd}`; | |
49 | } | |
50 | ||
6aa53fc5 MG |
51 | my $child = fork(); |
52 | ||
53 | die "fork failed!" if (!defined($child)); | |
54 | ||
55 | exit 0 if ($child != 0); | |
56 | ||
e18c93b3 MG |
57 | while(1) { |
58 | ($session,$error) = Net::SNMP->session(Hostname => $host, | |
59 | Community => $community); | |
60 | ||
61 | die "session error: $error" unless ($session); | |
62 | ||
63 | $session->translate(0); | |
64 | ||
65 | foreach my $var (@fields) { | |
66 | delete $vars->{$var}->{'value'}; | |
67 | ||
68 | my $result = $session->get_request($vars->{$var}->{'oid'}); | |
69 | next unless (defined $result); | |
70 | ||
71 | $vars->{$var}->{'value'} = $result->{$vars->{$var}->{'oid'}}; | |
72 | if (defined($vars->{$var}->{'factor'})) { | |
73 | $vars->{$var}->{'value'} *= $vars->{$var}->{'factor'}; | |
74 | } | |
75 | } | |
76 | ||
77 | $session->close; | |
78 | ||
79 | my $cmd = "rrdtool update \"${rrdfile}\" \"N"; | |
80 | foreach my $var (@fields) { | |
81 | if (!(defined($vars->{$var}->{'value'}))) { | |
82 | $vars->{$var}->{'value'} = 'U'; | |
83 | } | |
e18c93b3 MG |
84 | $cmd .= ":" . $vars->{$var}->{'value'}; |
85 | } | |
86 | $cmd .= "\""; | |
87 | print `${cmd}`; | |
88 | ||
89 | open(HTML, ">${outdir}/index.html.new"); | |
90 | ||
91 | print HTML '<html><head><meta http-equiv="refresh" content="60"/><meta http-equiv="cache-control" content="no-cache"/><meta http-equiv="pragma" content="no-cache"/><meta http_equiv="expires" content="Sat, 26 Jul 1997 05:00:00 GMT"/><title>USV status</title></head>'; | |
92 | print HTML '<body bgcolor="#ffffff">'; | |
93 | ||
94 | foreach my $var (@fields) { | |
95 | my $graphdef = "-t \"" . $vars->{$var}->{'name'} . "\" \"DEF:${var}=${rrdfile}:${var}:AVERAGE\" \"LINE1:${var}#FF0000\""; | |
96 | my $cmd = "rrdtool graph \"${outdir}/${var}.png.new\" -w 720 ${graphdef}"; | |
97 | my $size = `$cmd`; | |
98 | rename("${outdir}/${var}.png.new", "${outdir}/${var}.png"); | |
99 | (my $width, my $height) = split(/x/,$size); | |
100 | ||
101 | my $cmd2 = "rrdtool graph \"${outdir}/${var}.long.png.new\" -w 1008 -e now -s end-1w ${graphdef}"; | |
102 | my $size2 = `$cmd2`; | |
103 | rename("${outdir}/${var}.long.png.new", "${outdir}/${var}.long.png"); | |
104 | ||
105 | print HTML "<a href=\"${var}.long.png\"><img src=\"${var}.png\" width=\"${width}\" height=\"${height}\" border=\"0\"></a>"; | |
106 | } | |
107 | ||
108 | print HTML "</body></html>\n"; | |
109 | print HTML "<br>Generated on: " . localtime(time()); | |
863e62dc | 110 | print HTML ' by <a href="http://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph">upsgraph</a>.'; |
e18c93b3 MG |
111 | |
112 | close(HTML); | |
113 | ||
114 | rename("${outdir}/index.html.new", "${outdir}/index.html"); | |
115 | ||
116 | sleep(${step}/2); | |
117 | } |