/[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.11 - (hide annotations)
Sat May 24 12:35:19 2003 UTC (16 years, 6 months ago) by dpavlin
Branch: MAIN
Changes since 1.10: +21 -7 lines
File MIME type: text/plain
use separate cookie for each html file

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

  ViewVC Help
Powered by ViewVC 1.1.26