1 |
#!/usr/bin/perl -w |
2 |
# -*- perl -*- |
3 |
|
4 |
# Generate overview-[Daily|Monthly|Weekly|Yearly].html files |
5 |
# with links to latest graphs produced by cricket |
6 |
# |
7 |
# Maintained by Dobrica Pavlinusic <dpavlin@rot13.org> |
8 |
# |
9 |
# Based on generate-statics.franky from |
10 |
# Copyright (C) 1999 Noam Freedman <noam@noam.com> |
11 |
# |
12 |
# |
13 |
# |
14 |
# This program is free software; you can redistribute it and/or modify |
15 |
# it under the terms of the GNU General Public License as published by |
16 |
# the Free Software Foundation; either version 2 of the License, or |
17 |
# (at your option) any later version. |
18 |
# |
19 |
# This program is distributed in the hope that it will be useful, |
20 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 |
# GNU General Public License for more details. |
23 |
# |
24 |
# You should have received a copy of the GNU General Public License |
25 |
# along with this program; if not, write to the Free Software |
26 |
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
27 |
|
28 |
# Target can be skipped in overview report with skip-overview |
29 |
|
30 |
BEGIN { |
31 |
# $gInstallRoot = (($0 =~ m:^(.*/):)[0] || "./") . "."; |
32 |
require '/etc/cricket/cricket-conf.pl'; |
33 |
|
34 |
# You need to update this to point to the URL |
35 |
# you use to access Cricket. |
36 |
# $gBaseURL = "http://localhost/~cricket/grapher.cgi"; |
37 |
$gBaseURL = "http://romul.pliva.hr/cgi-bin/cricket/grapher.cgi"; |
38 |
|
39 |
# change this to destination directory |
40 |
$path = "/data/mon/"; |
41 |
# this is basename of all html files |
42 |
$name = "overview"; |
43 |
} |
44 |
|
45 |
#use lib "$gInstallRoot/../lib"; |
46 |
use lib "$Common::global::gInstallRoot/lib"; |
47 |
|
48 |
use File::Basename; |
49 |
use LWP::UserAgent; |
50 |
use HTTP::Request; |
51 |
use HTTP::Response; |
52 |
|
53 |
use ConfigTree::Cache; |
54 |
|
55 |
use Common::HandleTarget; |
56 |
use Common::Map; |
57 |
use Common::Options; |
58 |
use Common::Log; |
59 |
|
60 |
Common::Options::commonOptions( 'baseURL=s' => \$gBaseURL ); |
61 |
initConst(); |
62 |
|
63 |
$Common::global::gCT = new ConfigTree::Cache; |
64 |
$gCT = $Common::global::gCT; |
65 |
$gCT->Base($Common::global::gConfigRoot); |
66 |
$gCT->Warn(\&Warn); |
67 |
|
68 |
if (! $Common::global::gCT->init()) { |
69 |
Die("Failed to open compiled config tree from " . |
70 |
"$Common::global::gConfigRoot/config.db: $!"); |
71 |
} |
72 |
|
73 |
# if they gave us no subtrees to focus on, use the root of the config tree |
74 |
if ($#ARGV+1 == 0) { |
75 |
push @ARGV, '/'; |
76 |
} |
77 |
|
78 |
my %html; # this will store created html |
79 |
|
80 |
my($subtree); |
81 |
foreach $subtree (@ARGV) { |
82 |
if ($gCT->nodeExists($subtree)) { |
83 |
$gCT->visitLeafs($subtree, \&handleTarget, |
84 |
\&handleTargetInstance, \&localHandleTargetInstance); |
85 |
} else { |
86 |
Warn("Unknown subtree $subtree."); |
87 |
} |
88 |
} |
89 |
|
90 |
foreach my $filename (keys %html) { |
91 |
open(OUT,"> $filename") || die "can't open output html '$filename': $!"; |
92 |
print OUT $html{$filename},"</body></html>"; |
93 |
close(OUT); |
94 |
} |
95 |
|
96 |
exit; |
97 |
|
98 |
sub localHandleTargetInstance { |
99 |
my($Name, $target) = @_; |
100 |
|
101 |
$targetpath = $target->{'auto-target-path'}; |
102 |
$targetname = $target->{'auto-target-name'}; |
103 |
|
104 |
if (! defined($target->{'skip-overview'})) |
105 |
{ |
106 |
Info("Working on target $targetname."); |
107 |
my($reqRanges,@ranges); |
108 |
|
109 |
$reqRanges = $target->{'static-ranges'}; |
110 |
|
111 |
# if (defined($target->{'static-path'}) && |
112 |
# defined($target->{'static-name'})) |
113 |
# { |
114 |
# $path = $target->{'static-path'}; |
115 |
# $name = $target->{'static-name'}; |
116 |
|
117 |
if (1) { |
118 |
|
119 |
my($range, @ranges); |
120 |
@ranges = getRanges($reqRanges); |
121 |
|
122 |
foreach $range (@ranges) |
123 |
{ |
124 |
$rangeLabel = rangeToLabel($range); |
125 |
|
126 |
my($paramtarget) = "$targetpath/$targetname"; |
127 |
|
128 |
my($paraminst); |
129 |
|
130 |
if (defined($target->{'inst'})) |
131 |
{ |
132 |
$paraminst = $target->{'inst'}; |
133 |
} |
134 |
|
135 |
my($paramrange) = $range; |
136 |
|
137 |
# DO DSLIST STUFF |
138 |
|
139 |
# find the ds names based on the target type |
140 |
my($ttype) = lc($target->{'target-type'}); |
141 |
my($ttRef) = $main::gCT->configHash($Name, 'targettype', $ttype, $target); |
142 |
|
143 |
# If there are views defined, then we generate graphs |
144 |
# for each view. |
145 |
|
146 |
my($dslist); |
147 |
|
148 |
my($filename)="$path/$name-$rangeLabel.html"; |
149 |
if (! defined $html{$filename}) { |
150 |
$html{$filename}="<html><head><title>$rangeLabel</title></head><body>"; |
151 |
} |
152 |
my($dir) = dirname($filename); |
153 |
if (! -d $dir) { |
154 |
Info("Making directory $dir to hold file $filename."); |
155 |
Common::Util::MkDir($dir); |
156 |
} |
157 |
|
158 |
if (defined($ttRef->{'view'})) |
159 |
{ |
160 |
my($v); |
161 |
foreach $v (split(/\s*,\s*/, $ttRef->{'view'})) |
162 |
{ |
163 |
# views are like this: "cpu: cpu1load cpu5load" |
164 |
my($vname, $dss) = split(/\s*:\s*/, $v, 2); |
165 |
|
166 |
$dslist = $dss; |
167 |
$dslist =~ s/\s*$//; |
168 |
$dslist =~ s/\s+/,/g; |
169 |
|
170 |
$URL = "$gBaseURL?type=png&target=$paramtarget"; |
171 |
$URL .= "&dslist=$dslist&range=$paramrange"; |
172 |
my $desc = "view: "; |
173 |
if ($paraminst ne "") { |
174 |
$URL .= "&inst=$paraminst"; |
175 |
$desc.="$paraminst $paramtarget $vname"; |
176 |
} else { |
177 |
$desc.="$paramtarget $vname"; |
178 |
} |
179 |
|
180 |
Info("Retrieving graph for $desc"); |
181 |
# getURL($URL,"$path/$name-$vname-$rangeLabel.png"); |
182 |
$tmp_URL = "$gBaseURL?target=$paramtarget&range=d:w:m:y&view=$vname"; |
183 |
$html{$filename}.="$desc<br><a href=\"$tmp_URL\"><img src=$URL></a><br>"; |
184 |
} |
185 |
} else { |
186 |
$dslist = $ttRef->{'ds'}; |
187 |
# squeeze out any extra spaces |
188 |
$dslist = join(',', split(/\s*,\s*/, $dslist)); |
189 |
|
190 |
$URL = "$gBaseURL?type=png&target=$paramtarget"; |
191 |
$URL .= "&dslist=$dslist&range=$paramrange"; |
192 |
my $desc = "ds: "; |
193 |
if ($paraminst ne "") { |
194 |
$URL .= "&inst=$paraminst"; |
195 |
$desc.="$paraminst $rangeLabel" |
196 |
} else { |
197 |
$desc.="$paramtarget $rangeLabel"; |
198 |
} |
199 |
|
200 |
Info("Retrieving graph for $desc"); |
201 |
|
202 |
# getURL($URL,"$path/$name-$rangeLabel.png"); |
203 |
$tmp_URL = "$gBaseURL?target=$paramtarget&range=d:w:m:y"; |
204 |
$html{$filename}.="$desc:<br><a href=\"$tmp_URL\"><img src=$URL><a/><br>"; |
205 |
} |
206 |
} |
207 |
} |
208 |
} |
209 |
|
210 |
return; |
211 |
} |
212 |
|
213 |
|
214 |
sub getRanges { |
215 |
my($scales) = @_; |
216 |
$scales = "d:w:m:y" unless (defined($scales)); |
217 |
|
218 |
# these definitions mirror how MRTG 2.5 sets up its graphs |
219 |
my(%scaleMap) = ( 'd' => $main::kHour * 42, |
220 |
'w' => $main::kDay * 10, |
221 |
'm' => $main::kWeek * 6, |
222 |
'y' => $main::kMonth * 16); |
223 |
|
224 |
my($scale, @res); |
225 |
foreach $scale (split(/\s*:\s*/, $scales)) { |
226 |
# later, we might do more sophisticated scale specification |
227 |
$scale = $scaleMap{$scale}; |
228 |
push @res, $scale; |
229 |
} |
230 |
return @res; |
231 |
} |
232 |
|
233 |
|
234 |
sub initConst { |
235 |
$main::kMinute = 60; # 60 seconds/min |
236 |
$main::kHour = 60 * $main::kMinute;# 60 minutes/hr |
237 |
$main::kDay = 24 * $main::kHour; # 24 hrs/day |
238 |
$main::kWeek = 7 * $main::kDay; # 7 days/week |
239 |
$main::kMonth = 30 * $main::kDay; # 30 days/month |
240 |
$main::kYear = 365 * $main::kDay; # 365 days/year |
241 |
|
242 |
$main::kTypeUnknown = 0; |
243 |
$main::kTypeUnknown = 0; # shut up, -w. |
244 |
$main::kTypeDaily = 1; |
245 |
$main::kTypeWeekly = 2; |
246 |
$main::kTypeMonthly = 3; |
247 |
$main::kTypeYearly = 4; |
248 |
|
249 |
@main::gRangeNameMap = ( undef, 'Daily', 'Weekly', 'Monthly', 'Yearly' ); |
250 |
|
251 |
} |
252 |
|
253 |
sub rangeToLabel { |
254 |
my($range) = @_; |
255 |
return $main::gRangeNameMap[rangeType($range)]; |
256 |
} |
257 |
|
258 |
sub rangeType { |
259 |
my($range) = @_; |
260 |
my($rangeHours) = $range / 3600; |
261 |
|
262 |
# question: when is kTypeUnknown appropriate? |
263 |
|
264 |
if ($range < $main::kWeek) { |
265 |
return $main::kTypeDaily; |
266 |
} elsif ($range < $main::kMonth) { |
267 |
return $main::kTypeWeekly; |
268 |
} elsif ($range < $main::kYear) { |
269 |
return $main::kTypeMonthly; |
270 |
} else { |
271 |
return $main::kTypeYearly; |
272 |
} |
273 |
} |
274 |
|
275 |
|
276 |
sub getURL |
277 |
{ |
278 |
my($url,$filename) = @_; |
279 |
|
280 |
Debug("Fetching url: $url"); |
281 |
|
282 |
my $ua = new LWP::UserAgent; |
283 |
my $request = new HTTP::Request('GET', $url); |
284 |
my $response = $ua->request($request); |
285 |
|
286 |
if ($response->is_success) { |
287 |
my($dir) = dirname($filename); |
288 |
if (! -d $dir) { |
289 |
Info("Making directory $dir to hold file $filename."); |
290 |
Common::Util::MkDir($dir); |
291 |
} |
292 |
|
293 |
if (!open(URL,">$filename")) |
294 |
{ |
295 |
Error("Error writing to $filename: $!"); |
296 |
return; |
297 |
} |
298 |
print URL $response->content; |
299 |
close(URL); |
300 |
} |
301 |
else |
302 |
{ |
303 |
Error("Error retrieving target graph: " . $response->message()); |
304 |
} |
305 |
} |