/[cricket]/generate-overview.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /generate-overview.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (hide annotations)
Sat May 24 13:22:24 2003 UTC (16 years, 6 months ago) by dpavlin
Branch: MAIN
Changes since 1.11: +15 -4 lines
File MIME type: text/plain
generate rolled-up titles darker

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

  ViewVC Help
Powered by ViewVC 1.1.26