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 |
my $hostname = `hostname -f`; |
38 |
chomp($hostname); |
39 |
$gBaseURL = "http://$hostname/cgi-bin/cricket/grapher.cgi"; |
40 |
|
41 |
# change this to destination directory |
42 |
$path = "/data/mon/"; |
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 |
# |
61 |
# Set this option to 0 if you don't want JavaScript roll-up/roll-down |
62 |
# of images to be created |
63 |
# |
64 |
my $dynamic_js = 1; |
65 |
|
66 |
Common::Options::commonOptions( 'baseURL=s' => \$gBaseURL ); |
67 |
initConst(); |
68 |
|
69 |
$Common::global::gCT = new ConfigTree::Cache; |
70 |
$gCT = $Common::global::gCT; |
71 |
$gCT->Base($Common::global::gConfigRoot); |
72 |
$gCT->Warn(\&Warn); |
73 |
|
74 |
if (! $Common::global::gCT->init()) { |
75 |
Die("Failed to open compiled config tree from " . |
76 |
"$Common::global::gConfigRoot/config.db: $!"); |
77 |
} |
78 |
|
79 |
# if they gave us no subtrees to focus on, use the root of the config tree |
80 |
if ($#ARGV+1 == 0) { |
81 |
push @ARGV, '/'; |
82 |
} |
83 |
|
84 |
my %html; # this will store created html |
85 |
|
86 |
my($subtree); |
87 |
foreach $subtree (@ARGV) { |
88 |
if ($gCT->nodeExists($subtree)) { |
89 |
$gCT->visitLeafs($subtree, \&handleTarget, |
90 |
\&handleTargetInstance, \&localHandleTargetInstance); |
91 |
} else { |
92 |
Warn("Unknown subtree $subtree."); |
93 |
} |
94 |
} |
95 |
|
96 |
my %html_file; |
97 |
|
98 |
foreach my $item (sort keys %html) { |
99 |
my ($range,$target) = split(/\t/,$item,2); |
100 |
my (undef,$service,$arg) = split(/\//,$target,3); |
101 |
|
102 |
$html_file{"$range/$service"} .= $html{$item}; |
103 |
$html_file{"overview-$range"} .= $html{$item}; |
104 |
} |
105 |
|
106 |
foreach my $key (keys %html_file) { |
107 |
|
108 |
my $filename = "$path/$key.html"; |
109 |
my($dir) = dirname($filename); |
110 |
if (! -d $dir) { |
111 |
Info("Making directory $dir to hold file $filename."); |
112 |
Common::Util::MkDir($dir); |
113 |
} |
114 |
Info("Dumping HTML for $key to $filename."); |
115 |
open(OUT,"> $filename") || die "can't open output html '$filename': $!"; |
116 |
print OUT "<html><head><title>$key</title>"; |
117 |
print OUT '<meta content="no-cache" http-equiv="Pragma"> |
118 |
<meta content="300" http-equiv="Refresh">' if ($filename =~ m/Daily/i); |
119 |
print OUT <<'HTMLEND' if ($dynamic_js); |
120 |
|
121 |
<script language="JavaScript"> |
122 |
var Selected = ""; |
123 |
|
124 |
function Switch(elm) |
125 |
{ |
126 |
var cookie = Get_Cookie("overview"); |
127 |
|
128 |
var elmref; |
129 |
|
130 |
// uncomment following lines for just 1 selected content |
131 |
/* |
132 |
if (Selected != "") |
133 |
{ |
134 |
elmref = eval("document.getElementById('" + Selected + "_h1')"); |
135 |
if (elmref) elmref.style.display = 'none'; |
136 |
elmref = eval("document.getElementById('" + Selected + "_h0')"); |
137 |
// roll-up color |
138 |
if (elmref) elmref.bgColor = '#eeeeee'; |
139 |
} |
140 |
if (Selected != elm.name) |
141 |
{ |
142 |
Selected = elm.name; |
143 |
elmref = eval("document.getElementById('" + Selected + "_h1')"); |
144 |
if (elmref) |
145 |
{ |
146 |
if (elmref.style.display=='none') elmref.style.display=''; |
147 |
else elmref.style.display = 'none'; |
148 |
} |
149 |
elmref = eval("document.getElementById('" + Selected + "_h0')"); |
150 |
// roll-down color |
151 |
if (elmref) elmref.bgColor = '#ffffff'; |
152 |
} |
153 |
else |
154 |
Selected=""; |
155 |
*/ |
156 |
|
157 |
// or use this to unroll more than one line |
158 |
Selected = elm.name; |
159 |
elmref = eval("document.getElementById('" + Selected + "_h1')"); |
160 |
if (elmref) |
161 |
{ |
162 |
if (elmref.style.display=='none') elmref.style.display=''; |
163 |
else elmref.style.display = 'none'; |
164 |
cookie ^= 1 << Selected; |
165 |
} |
166 |
|
167 |
Set_Cookie("overview",cookie); |
168 |
return false; |
169 |
} |
170 |
|
171 |
function SwitchThis() |
172 |
{ |
173 |
return Switch(this); |
174 |
} |
175 |
|
176 |
|
177 |
</script> |
178 |
|
179 |
<table border=0 id=DynamicTable bgColor='#e0e0e0' cellpadding=2 cellspacing=1> |
180 |
|
181 |
HTMLEND |
182 |
print OUT "</head><body>"; |
183 |
print OUT $html_file{$key}; |
184 |
print OUT <<'HTMLEND' if ($dynamic_js); |
185 |
</table> |
186 |
|
187 |
<script language=javascript> |
188 |
|
189 |
function Get_Cookie(name) { |
190 |
var start = document.cookie.indexOf(name+"="); |
191 |
var len = start+name.length+1; |
192 |
if ((!start) && (name != document.cookie.substring(0,name.length))) return null; |
193 |
if (start == -1) return null; |
194 |
var end = document.cookie.indexOf(";",len); |
195 |
if (end == -1) end = document.cookie.length; |
196 |
return unescape(document.cookie.substring(len,end)); |
197 |
} |
198 |
|
199 |
function Set_Cookie(name,value,expires,path,domain,secure) { |
200 |
document.cookie = name + "=" +escape(value) + |
201 |
( (expires) ? ";expires=" + expires.toGMTString() : "") + |
202 |
( (path) ? ";path=" + path : "") + |
203 |
( (domain) ? ";domain=" + domain : "") + |
204 |
( (secure) ? ";secure" : ""); |
205 |
} |
206 |
|
207 |
function Delete_Cookie(name,path,domain) { |
208 |
if (Get_Cookie(name)) document.cookie = name + "=" + |
209 |
( (path) ? ";path=" + path : "") + |
210 |
( (domain) ? ";domain=" + domain : "") + |
211 |
";expires=Thu, 01-Jan-70 00:00:01 GMT"; |
212 |
} |
213 |
|
214 |
var table = document.getElementById("DynamicTable"); |
215 |
var links = table.getElementsByTagName("a"); |
216 |
var cookie = Get_Cookie("overview"); |
217 |
var new_cookie = 0; |
218 |
for (var i = 0; i < links.length; i++) { |
219 |
if (links[i].id == "DynMessLink") { |
220 |
links[i].onclick = SwitchThis; |
221 |
elmref = eval("document.getElementById('" + links[i].name + "_h1')"); |
222 |
if (elmref && cookie) { |
223 |
if (cookie & 1 << links[i].name) { |
224 |
elmref.style.display=''; |
225 |
} else { |
226 |
elmref.style.display = 'none'; |
227 |
} |
228 |
} else if(elmref) { |
229 |
if (elmref.style.display=='none') { |
230 |
elmref.style.display=''; |
231 |
new_cookie |= 1 << links[i].name; |
232 |
} else { |
233 |
elmref.style.display = 'none'; |
234 |
//new_cookie &= !( 1 << links[i].name ); |
235 |
} |
236 |
} |
237 |
} |
238 |
} |
239 |
if (new_cookie) Set_Cookie("overview",new_cookie); |
240 |
</script> |
241 |
HTMLEND |
242 |
|
243 |
print OUT "</body></html>"; |
244 |
close(OUT); |
245 |
|
246 |
} |
247 |
|
248 |
exit; |
249 |
|
250 |
sub do_html { |
251 |
my ($desc,$url,$img) = @_; |
252 |
if ($dynamic_js) { |
253 |
my $html =<<'HTMLEND'; |
254 |
<tr bgcolor='#eeeeee' id=##id##_h0> |
255 |
<td><a href="#" id=DynMessLink name=##id## >##desc##</a></td> |
256 |
</tr> |
257 |
<tr id=##id##_h1 style='display:none' bgcolor='#ffffff' > |
258 |
<td><a href="##url##"><img src="##img##"</td> |
259 |
</tr> |
260 |
HTMLEND |
261 |
$html =~ s/##desc##/$desc/; |
262 |
$html =~ s/##url##/$url/; |
263 |
$html =~ s/##img##/$img/; |
264 |
$html =~ s/##id##/$dynamic_js/g; |
265 |
$dynamic_js++; # increment usage counter |
266 |
return $html; |
267 |
} else { |
268 |
return "$desc<br><a href=\"$url\"><img src=\"$img\"></a><br>\n"; |
269 |
} |
270 |
} |
271 |
|
272 |
sub localHandleTargetInstance { |
273 |
my($Name, $target) = @_; |
274 |
|
275 |
$targetpath = $target->{'auto-target-path'}; |
276 |
$targetname = $target->{'auto-target-name'}; |
277 |
|
278 |
if (! defined($target->{'skip-overview'})) |
279 |
{ |
280 |
Info("Working on target $targetname."); |
281 |
my($reqRanges,@ranges); |
282 |
|
283 |
$reqRanges = $target->{'static-ranges'}; |
284 |
|
285 |
# if (defined($target->{'static-path'}) && |
286 |
# defined($target->{'static-name'})) |
287 |
# { |
288 |
# $path = $target->{'static-path'}; |
289 |
# $name = $target->{'static-name'}; |
290 |
|
291 |
if (1) { |
292 |
|
293 |
my($range, @ranges); |
294 |
@ranges = getRanges($reqRanges); |
295 |
|
296 |
foreach $range (@ranges) |
297 |
{ |
298 |
$rangeLabel = rangeToLabel($range); |
299 |
|
300 |
my($paramtarget) = "$targetpath/$targetname"; |
301 |
|
302 |
my($paraminst); |
303 |
|
304 |
if (defined($target->{'inst'})) |
305 |
{ |
306 |
$paraminst = $target->{'inst'}; |
307 |
} |
308 |
|
309 |
my($paramrange) = $range; |
310 |
|
311 |
# DO DSLIST STUFF |
312 |
|
313 |
# find the ds names based on the target type |
314 |
my($ttype) = lc($target->{'target-type'}); |
315 |
my($ttRef) = $main::gCT->configHash($Name, 'targettype', $ttype, $target); |
316 |
|
317 |
# If there are views defined, then we generate graphs |
318 |
# for each view. |
319 |
|
320 |
my($dslist); |
321 |
|
322 |
if (defined($ttRef->{'view'})) |
323 |
{ |
324 |
my($v); |
325 |
foreach $v (split(/\s*,\s*/, $ttRef->{'view'})) |
326 |
{ |
327 |
# views are like this: "cpu: cpu1load cpu5load" |
328 |
my($vname, $dss) = split(/\s*:\s*/, $v, 2); |
329 |
|
330 |
$dslist = $dss; |
331 |
$dslist =~ s/\s*$//; |
332 |
$dslist =~ s/\s+/,/g; |
333 |
|
334 |
$URL = "$gBaseURL?type=png&target=$paramtarget"; |
335 |
$URL .= "&dslist=$dslist&range=$paramrange"; |
336 |
my $desc = "$paramtarget $vname"; |
337 |
$desc .= " <b>".$target->{'short-desc'}."</b>" if (defined $target->{'short-desc'}); |
338 |
if ($paraminst ne "") { |
339 |
$URL .= "&inst=$paraminst"; |
340 |
} |
341 |
|
342 |
Info("Retrieving graph for $desc"); |
343 |
# getURL($URL,"$path/$name-$vname-$rangeLabel.png"); |
344 |
$tmp_URL = "$gBaseURL?target=$paramtarget&range=d:w:m:y&view=$vname"; |
345 |
$html{"$rangeLabel\t$paramtarget"}.=do_html($desc,$tmp_URL,$URL); |
346 |
} |
347 |
} else { |
348 |
$dslist = $ttRef->{'ds'}; |
349 |
# squeeze out any extra spaces |
350 |
$dslist = join(',', split(/\s*,\s*/, $dslist)); |
351 |
|
352 |
$URL = "$gBaseURL?type=png&target=$paramtarget"; |
353 |
$URL .= "&dslist=$dslist&range=$paramrange"; |
354 |
my $desc ="$paraminst $rangeLabel"; |
355 |
$desc .= " <b>".$target->{'short-desc'}."</b>" if (defined $target->{'short-desc'}); |
356 |
if ($paraminst ne "") { |
357 |
$URL .= "&inst=$paraminst"; |
358 |
} |
359 |
|
360 |
Info("Retrieving graph for $desc"); |
361 |
|
362 |
# getURL($URL,"$path/$name-$rangeLabel.png"); |
363 |
$tmp_URL = "$gBaseURL?target=$paramtarget&range=d:w:m:y"; |
364 |
$html{"$rangeLabel\t$paramtarget"}.=do_html($desc,$tmp_URL,$URL); |
365 |
} |
366 |
} |
367 |
} |
368 |
} |
369 |
|
370 |
return; |
371 |
} |
372 |
|
373 |
|
374 |
sub getRanges { |
375 |
my($scales) = @_; |
376 |
$scales = "d:w:m:y" unless (defined($scales)); |
377 |
|
378 |
# these definitions mirror how MRTG 2.5 sets up its graphs |
379 |
my(%scaleMap) = ( 'd' => $main::kHour * 42, |
380 |
'w' => $main::kDay * 10, |
381 |
'm' => $main::kWeek * 6, |
382 |
'y' => $main::kMonth * 16); |
383 |
|
384 |
my($scale, @res); |
385 |
foreach $scale (split(/\s*:\s*/, $scales)) { |
386 |
# later, we might do more sophisticated scale specification |
387 |
$scale = $scaleMap{$scale}; |
388 |
push @res, $scale; |
389 |
} |
390 |
return @res; |
391 |
} |
392 |
|
393 |
|
394 |
sub initConst { |
395 |
$main::kMinute = 60; # 60 seconds/min |
396 |
$main::kHour = 60 * $main::kMinute;# 60 minutes/hr |
397 |
$main::kDay = 24 * $main::kHour; # 24 hrs/day |
398 |
$main::kWeek = 7 * $main::kDay; # 7 days/week |
399 |
$main::kMonth = 30 * $main::kDay; # 30 days/month |
400 |
$main::kYear = 365 * $main::kDay; # 365 days/year |
401 |
|
402 |
$main::kTypeUnknown = 0; |
403 |
$main::kTypeUnknown = 0; # shut up, -w. |
404 |
$main::kTypeDaily = 1; |
405 |
$main::kTypeWeekly = 2; |
406 |
$main::kTypeMonthly = 3; |
407 |
$main::kTypeYearly = 4; |
408 |
|
409 |
@main::gRangeNameMap = ( undef, 'Daily', 'Weekly', 'Monthly', 'Yearly' ); |
410 |
|
411 |
} |
412 |
|
413 |
sub rangeToLabel { |
414 |
my($range) = @_; |
415 |
return $main::gRangeNameMap[rangeType($range)]; |
416 |
} |
417 |
|
418 |
sub rangeType { |
419 |
my($range) = @_; |
420 |
my($rangeHours) = $range / 3600; |
421 |
|
422 |
# question: when is kTypeUnknown appropriate? |
423 |
|
424 |
if ($range < $main::kWeek) { |
425 |
return $main::kTypeDaily; |
426 |
} elsif ($range < $main::kMonth) { |
427 |
return $main::kTypeWeekly; |
428 |
} elsif ($range < $main::kYear) { |
429 |
return $main::kTypeMonthly; |
430 |
} else { |
431 |
return $main::kTypeYearly; |
432 |
} |
433 |
} |
434 |
|
435 |
|
436 |
sub getURL |
437 |
{ |
438 |
my($url,$filename) = @_; |
439 |
|
440 |
Debug("Fetching url: $url"); |
441 |
|
442 |
my $ua = new LWP::UserAgent; |
443 |
my $request = new HTTP::Request('GET', $url); |
444 |
my $response = $ua->request($request); |
445 |
|
446 |
if ($response->is_success) { |
447 |
my($dir) = dirname($filename); |
448 |
if (! -d $dir) { |
449 |
Info("Making directory $dir to hold file $filename."); |
450 |
Common::Util::MkDir($dir); |
451 |
} |
452 |
|
453 |
if (!open(URL,">$filename")) |
454 |
{ |
455 |
Error("Error writing to $filename: $!"); |
456 |
return; |
457 |
} |
458 |
print URL $response->content; |
459 |
close(URL); |
460 |
} |
461 |
else |
462 |
{ |
463 |
Error("Error retrieving target graph: " . $response->message()); |
464 |
} |
465 |
} |