/[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.5 - (hide annotations)
Wed Jul 24 15:14:08 2002 UTC (17 years, 4 months ago) by dpavlin
Branch: MAIN
Changes since 1.4: +27 -24 lines
File MIME type: text/plain
generate also Daily, Weekly,... directories with overview per service.

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     $gBaseURL = "http://romul.pliva.hr/cgi-bin/cricket/grapher.cgi";
38    
39     # change this to destination directory
40 dpavlin 1.4 $path = "/data/mon/";
41 dpavlin 1.1 }
42    
43     #use lib "$gInstallRoot/../lib";
44     use lib "$Common::global::gInstallRoot/lib";
45    
46     use File::Basename;
47     use LWP::UserAgent;
48     use HTTP::Request;
49     use HTTP::Response;
50    
51     use ConfigTree::Cache;
52    
53     use Common::HandleTarget;
54     use Common::Map;
55     use Common::Options;
56     use Common::Log;
57    
58     Common::Options::commonOptions( 'baseURL=s' => \$gBaseURL );
59     initConst();
60    
61     $Common::global::gCT = new ConfigTree::Cache;
62     $gCT = $Common::global::gCT;
63     $gCT->Base($Common::global::gConfigRoot);
64     $gCT->Warn(\&Warn);
65    
66     if (! $Common::global::gCT->init()) {
67     Die("Failed to open compiled config tree from " .
68     "$Common::global::gConfigRoot/config.db: $!");
69     }
70    
71     # if they gave us no subtrees to focus on, use the root of the config tree
72     if ($#ARGV+1 == 0) {
73     push @ARGV, '/';
74     }
75    
76     my %html; # this will store created html
77    
78     my($subtree);
79     foreach $subtree (@ARGV) {
80     if ($gCT->nodeExists($subtree)) {
81     $gCT->visitLeafs($subtree, \&handleTarget,
82     \&handleTargetInstance, \&localHandleTargetInstance);
83     } else {
84     Warn("Unknown subtree $subtree.");
85     }
86     }
87    
88 dpavlin 1.5 my %html_file;
89    
90     foreach my $item (sort keys %html) {
91     my ($range,$target) = split(/\t/,$item,2);
92     my (undef,$service,$arg) = split(/\//,$target,3);
93    
94     $html_file{"$range/$service"} .= $html{$item};
95     $html_file{"overview-$range"} .= $html{$item};
96     }
97    
98     foreach my $key (keys %html_file) {
99    
100     my $filename = "$path/$key.html";
101     my($dir) = dirname($filename);
102     if (! -d $dir) {
103     Info("Making directory $dir to hold file $filename.");
104     Common::Util::MkDir($dir);
105     }
106     Info("Dumping HTML for $key to $filename.");
107 dpavlin 1.1 open(OUT,"> $filename") || die "can't open output html '$filename': $!";
108 dpavlin 1.5 print OUT "<html><head><title>$key</title></head><body>";
109     print OUT $html_file{$key};
110     print OUT "</body></html>";
111 dpavlin 1.1 close(OUT);
112 dpavlin 1.5
113 dpavlin 1.1 }
114    
115     exit;
116    
117     sub localHandleTargetInstance {
118     my($Name, $target) = @_;
119    
120     $targetpath = $target->{'auto-target-path'};
121     $targetname = $target->{'auto-target-name'};
122    
123     if (! defined($target->{'skip-overview'}))
124     {
125     Info("Working on target $targetname.");
126     my($reqRanges,@ranges);
127    
128     $reqRanges = $target->{'static-ranges'};
129    
130     # if (defined($target->{'static-path'}) &&
131     # defined($target->{'static-name'}))
132     # {
133     # $path = $target->{'static-path'};
134     # $name = $target->{'static-name'};
135    
136     if (1) {
137    
138     my($range, @ranges);
139     @ranges = getRanges($reqRanges);
140    
141     foreach $range (@ranges)
142     {
143     $rangeLabel = rangeToLabel($range);
144    
145     my($paramtarget) = "$targetpath/$targetname";
146    
147     my($paraminst);
148    
149     if (defined($target->{'inst'}))
150     {
151     $paraminst = $target->{'inst'};
152     }
153    
154     my($paramrange) = $range;
155    
156     # DO DSLIST STUFF
157    
158     # find the ds names based on the target type
159     my($ttype) = lc($target->{'target-type'});
160     my($ttRef) = $main::gCT->configHash($Name, 'targettype', $ttype, $target);
161    
162     # If there are views defined, then we generate graphs
163     # for each view.
164    
165     my($dslist);
166    
167     if (defined($ttRef->{'view'}))
168     {
169     my($v);
170     foreach $v (split(/\s*,\s*/, $ttRef->{'view'}))
171     {
172     # views are like this: "cpu: cpu1load cpu5load"
173     my($vname, $dss) = split(/\s*:\s*/, $v, 2);
174    
175     $dslist = $dss;
176     $dslist =~ s/\s*$//;
177     $dslist =~ s/\s+/,/g;
178    
179     $URL = "$gBaseURL?type=png&target=$paramtarget";
180     $URL .= "&dslist=$dslist&range=$paramrange";
181 dpavlin 1.5 my $desc = "$paramtarget $vname";
182 dpavlin 1.1 if ($paraminst ne "") {
183     $URL .= "&inst=$paraminst";
184     }
185    
186     Info("Retrieving graph for $desc");
187     # getURL($URL,"$path/$name-$vname-$rangeLabel.png");
188     $tmp_URL = "$gBaseURL?target=$paramtarget&range=d:w:m:y&view=$vname";
189 dpavlin 1.5 $html{"$rangeLabel\t$paramtarget"}.="$desc<br><a href=\"$tmp_URL\"><img src=$URL></a><br>\n";
190 dpavlin 1.1 }
191     } else {
192     $dslist = $ttRef->{'ds'};
193     # squeeze out any extra spaces
194     $dslist = join(',', split(/\s*,\s*/, $dslist));
195    
196     $URL = "$gBaseURL?type=png&target=$paramtarget";
197     $URL .= "&dslist=$dslist&range=$paramrange";
198 dpavlin 1.5 my $desc ="$paraminst $rangeLabel";
199 dpavlin 1.1 if ($paraminst ne "") {
200     $URL .= "&inst=$paraminst";
201     }
202    
203     Info("Retrieving graph for $desc");
204    
205     # getURL($URL,"$path/$name-$rangeLabel.png");
206     $tmp_URL = "$gBaseURL?target=$paramtarget&range=d:w:m:y";
207 dpavlin 1.5 $html{"$rangeLabel\t$paramtarget"}.="$desc:<br><a href=\"$tmp_URL\"><img src=$URL><a/><br>";
208 dpavlin 1.1 }
209     }
210     }
211     }
212    
213     return;
214     }
215    
216    
217     sub getRanges {
218     my($scales) = @_;
219     $scales = "d:w:m:y" unless (defined($scales));
220    
221     # these definitions mirror how MRTG 2.5 sets up its graphs
222     my(%scaleMap) = ( 'd' => $main::kHour * 42,
223     'w' => $main::kDay * 10,
224     'm' => $main::kWeek * 6,
225     'y' => $main::kMonth * 16);
226    
227     my($scale, @res);
228     foreach $scale (split(/\s*:\s*/, $scales)) {
229     # later, we might do more sophisticated scale specification
230     $scale = $scaleMap{$scale};
231     push @res, $scale;
232     }
233     return @res;
234     }
235    
236    
237     sub initConst {
238     $main::kMinute = 60; # 60 seconds/min
239     $main::kHour = 60 * $main::kMinute;# 60 minutes/hr
240     $main::kDay = 24 * $main::kHour; # 24 hrs/day
241     $main::kWeek = 7 * $main::kDay; # 7 days/week
242     $main::kMonth = 30 * $main::kDay; # 30 days/month
243     $main::kYear = 365 * $main::kDay; # 365 days/year
244    
245     $main::kTypeUnknown = 0;
246     $main::kTypeUnknown = 0; # shut up, -w.
247     $main::kTypeDaily = 1;
248     $main::kTypeWeekly = 2;
249     $main::kTypeMonthly = 3;
250     $main::kTypeYearly = 4;
251    
252     @main::gRangeNameMap = ( undef, 'Daily', 'Weekly', 'Monthly', 'Yearly' );
253    
254     }
255    
256     sub rangeToLabel {
257     my($range) = @_;
258     return $main::gRangeNameMap[rangeType($range)];
259     }
260    
261     sub rangeType {
262     my($range) = @_;
263     my($rangeHours) = $range / 3600;
264    
265     # question: when is kTypeUnknown appropriate?
266    
267     if ($range < $main::kWeek) {
268     return $main::kTypeDaily;
269     } elsif ($range < $main::kMonth) {
270     return $main::kTypeWeekly;
271     } elsif ($range < $main::kYear) {
272     return $main::kTypeMonthly;
273     } else {
274     return $main::kTypeYearly;
275     }
276     }
277    
278    
279     sub getURL
280     {
281     my($url,$filename) = @_;
282    
283     Debug("Fetching url: $url");
284    
285     my $ua = new LWP::UserAgent;
286     my $request = new HTTP::Request('GET', $url);
287     my $response = $ua->request($request);
288    
289     if ($response->is_success) {
290     my($dir) = dirname($filename);
291     if (! -d $dir) {
292     Info("Making directory $dir to hold file $filename.");
293     Common::Util::MkDir($dir);
294     }
295    
296     if (!open(URL,">$filename"))
297     {
298     Error("Error writing to $filename: $!");
299     return;
300     }
301     print URL $response->content;
302     close(URL);
303     }
304     else
305     {
306     Error("Error retrieving target graph: " . $response->message());
307     }
308     }

  ViewVC Help
Powered by ViewVC 1.1.26