/[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.16 - (hide annotations)
Mon Nov 3 10:30:48 2003 UTC (15 years, 7 months ago) by dpavlin
Branch: MAIN
CVS Tags: HEAD
Changes since 1.15: +26 -15 lines
File MIME type: text/plain
added skip-view directive to target which enables some views to be hidden
from overview page

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

  ViewVC Help
Powered by ViewVC 1.1.26