/[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.14 - (hide annotations)
Sat May 31 13:52:51 2003 UTC (20 years, 9 months ago) by dpavlin
Branch: MAIN
Changes since 1.13: +7 -5 lines
File MIME type: text/plain
fix for show all when there is no cookie, when clicking locate at graph
which is clicked

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 dpavlin 1.14 // default: show all
246     new_cookie |= 1 << links[i].name;
247 dpavlin 1.10 if (elmref.style.display=='none') {
248     elmref.style.display='';
249 dpavlin 1.12 if (elmref0) elmref0.bgColor = '#eeeeee';
250 dpavlin 1.10 } else {
251 dpavlin 1.14 // hide? no...
252     //elmref.style.display = 'none';
253     //if (elmref0) elmref0.bgColor = '#e0e0e0';
254     if (elmref0) elmref0.bgColor = '#eeeeee';
255 dpavlin 1.10 }
256     }
257     }
258     }
259 dpavlin 1.11 if (new_cookie) Set_Cookie("##cookie_name##",new_cookie);
260 dpavlin 1.10 </script>
261     HTMLEND
262 dpavlin 1.11
263     if ($dynamic_js) {
264     my $cookie_name = $key;
265     $cookie_name =~ s/[^a-zA-Z0-9]//g;
266     $html =~ s/##cookie_name##/$cookie_name/g;
267     print OUT $html;
268     }
269 dpavlin 1.10
270 dpavlin 1.5 print OUT "</body></html>";
271 dpavlin 1.1 close(OUT);
272 dpavlin 1.5
273 dpavlin 1.1 }
274    
275     exit;
276    
277 dpavlin 1.10 sub do_html {
278     my ($desc,$url,$img) = @_;
279     if ($dynamic_js) {
280     my $html =<<'HTMLEND';
281 dpavlin 1.12 <tr bgcolor='#e0e0e0' id=##id##_h0>
282 dpavlin 1.14 <td><a name="##id##"><a href="###id##" id=DynMessLink name=##id## >##desc##</a></td>
283 dpavlin 1.10 </tr>
284 dpavlin 1.13 <tr id=##id##_h1 style='' bgcolor='#ffffff' >
285 dpavlin 1.10 <td><a href="##url##"><img src="##img##"</td>
286     </tr>
287     HTMLEND
288     $html =~ s/##desc##/$desc/;
289     $html =~ s/##url##/$url/;
290     $html =~ s/##img##/$img/;
291     $html =~ s/##id##/$dynamic_js/g;
292     $dynamic_js++; # increment usage counter
293     return $html;
294     } else {
295     return "$desc<br><a href=\"$url\"><img src=\"$img\"></a><br>\n";
296     }
297     }
298    
299 dpavlin 1.1 sub localHandleTargetInstance {
300     my($Name, $target) = @_;
301    
302     $targetpath = $target->{'auto-target-path'};
303     $targetname = $target->{'auto-target-name'};
304    
305     if (! defined($target->{'skip-overview'}))
306     {
307     Info("Working on target $targetname.");
308     my($reqRanges,@ranges);
309    
310     $reqRanges = $target->{'static-ranges'};
311    
312     # if (defined($target->{'static-path'}) &&
313     # defined($target->{'static-name'}))
314     # {
315     # $path = $target->{'static-path'};
316     # $name = $target->{'static-name'};
317    
318     if (1) {
319    
320     my($range, @ranges);
321     @ranges = getRanges($reqRanges);
322    
323     foreach $range (@ranges)
324     {
325     $rangeLabel = rangeToLabel($range);
326    
327     my($paramtarget) = "$targetpath/$targetname";
328    
329     my($paraminst);
330    
331     if (defined($target->{'inst'}))
332     {
333     $paraminst = $target->{'inst'};
334     }
335    
336     my($paramrange) = $range;
337    
338     # DO DSLIST STUFF
339    
340     # find the ds names based on the target type
341     my($ttype) = lc($target->{'target-type'});
342     my($ttRef) = $main::gCT->configHash($Name, 'targettype', $ttype, $target);
343    
344     # If there are views defined, then we generate graphs
345     # for each view.
346    
347     my($dslist);
348    
349     if (defined($ttRef->{'view'}))
350     {
351     my($v);
352     foreach $v (split(/\s*,\s*/, $ttRef->{'view'}))
353     {
354     # views are like this: "cpu: cpu1load cpu5load"
355     my($vname, $dss) = split(/\s*:\s*/, $v, 2);
356    
357     $dslist = $dss;
358     $dslist =~ s/\s*$//;
359     $dslist =~ s/\s+/,/g;
360    
361     $URL = "$gBaseURL?type=png&target=$paramtarget";
362     $URL .= "&dslist=$dslist&range=$paramrange";
363 dpavlin 1.5 my $desc = "$paramtarget $vname";
364 dpavlin 1.6 $desc .= " <b>".$target->{'short-desc'}."</b>" if (defined $target->{'short-desc'});
365 dpavlin 1.1 if ($paraminst ne "") {
366     $URL .= "&inst=$paraminst";
367     }
368    
369     Info("Retrieving graph for $desc");
370     # getURL($URL,"$path/$name-$vname-$rangeLabel.png");
371     $tmp_URL = "$gBaseURL?target=$paramtarget&range=d:w:m:y&view=$vname";
372 dpavlin 1.10 $html{"$rangeLabel\t$paramtarget"}.=do_html($desc,$tmp_URL,$URL);
373 dpavlin 1.1 }
374     } else {
375     $dslist = $ttRef->{'ds'};
376     # squeeze out any extra spaces
377     $dslist = join(',', split(/\s*,\s*/, $dslist));
378    
379     $URL = "$gBaseURL?type=png&target=$paramtarget";
380     $URL .= "&dslist=$dslist&range=$paramrange";
381 dpavlin 1.5 my $desc ="$paraminst $rangeLabel";
382 dpavlin 1.6 $desc .= " <b>".$target->{'short-desc'}."</b>" if (defined $target->{'short-desc'});
383 dpavlin 1.1 if ($paraminst ne "") {
384     $URL .= "&inst=$paraminst";
385     }
386    
387     Info("Retrieving graph for $desc");
388    
389     # getURL($URL,"$path/$name-$rangeLabel.png");
390     $tmp_URL = "$gBaseURL?target=$paramtarget&range=d:w:m:y";
391 dpavlin 1.10 $html{"$rangeLabel\t$paramtarget"}.=do_html($desc,$tmp_URL,$URL);
392 dpavlin 1.1 }
393     }
394     }
395     }
396    
397     return;
398     }
399    
400    
401     sub getRanges {
402     my($scales) = @_;
403     $scales = "d:w:m:y" unless (defined($scales));
404    
405     # these definitions mirror how MRTG 2.5 sets up its graphs
406     my(%scaleMap) = ( 'd' => $main::kHour * 42,
407     'w' => $main::kDay * 10,
408     'm' => $main::kWeek * 6,
409     'y' => $main::kMonth * 16);
410    
411     my($scale, @res);
412     foreach $scale (split(/\s*:\s*/, $scales)) {
413     # later, we might do more sophisticated scale specification
414     $scale = $scaleMap{$scale};
415     push @res, $scale;
416     }
417     return @res;
418     }
419    
420    
421     sub initConst {
422     $main::kMinute = 60; # 60 seconds/min
423     $main::kHour = 60 * $main::kMinute;# 60 minutes/hr
424     $main::kDay = 24 * $main::kHour; # 24 hrs/day
425     $main::kWeek = 7 * $main::kDay; # 7 days/week
426     $main::kMonth = 30 * $main::kDay; # 30 days/month
427     $main::kYear = 365 * $main::kDay; # 365 days/year
428    
429     $main::kTypeUnknown = 0;
430     $main::kTypeUnknown = 0; # shut up, -w.
431     $main::kTypeDaily = 1;
432     $main::kTypeWeekly = 2;
433     $main::kTypeMonthly = 3;
434     $main::kTypeYearly = 4;
435    
436     @main::gRangeNameMap = ( undef, 'Daily', 'Weekly', 'Monthly', 'Yearly' );
437    
438     }
439    
440     sub rangeToLabel {
441     my($range) = @_;
442     return $main::gRangeNameMap[rangeType($range)];
443     }
444    
445     sub rangeType {
446     my($range) = @_;
447     my($rangeHours) = $range / 3600;
448    
449     # question: when is kTypeUnknown appropriate?
450    
451     if ($range < $main::kWeek) {
452     return $main::kTypeDaily;
453     } elsif ($range < $main::kMonth) {
454     return $main::kTypeWeekly;
455     } elsif ($range < $main::kYear) {
456     return $main::kTypeMonthly;
457     } else {
458     return $main::kTypeYearly;
459     }
460     }
461    
462    
463     sub getURL
464     {
465     my($url,$filename) = @_;
466    
467     Debug("Fetching url: $url");
468    
469     my $ua = new LWP::UserAgent;
470     my $request = new HTTP::Request('GET', $url);
471     my $response = $ua->request($request);
472    
473     if ($response->is_success) {
474     my($dir) = dirname($filename);
475     if (! -d $dir) {
476     Info("Making directory $dir to hold file $filename.");
477     Common::Util::MkDir($dir);
478     }
479    
480     if (!open(URL,">$filename"))
481     {
482     Error("Error writing to $filename: $!");
483     return;
484     }
485     print URL $response->content;
486     close(URL);
487     }
488     else
489     {
490     Error("Error retrieving target graph: " . $response->message());
491     }
492     }

  ViewVC Help
Powered by ViewVC 1.1.26