/[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

Contents of /generate-overview.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.9 - (show annotations)
Fri May 23 07:41:26 2003 UTC (16 years, 6 months ago) by dpavlin
Branch: MAIN
Changes since 1.8: +4 -4 lines
File MIME type: text/plain
refresh just daily pages every 5 minutes

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

  ViewVC Help
Powered by ViewVC 1.1.26