/[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.7 - (show annotations)
Sun May 18 02:08:04 2003 UTC (17 years ago) by dpavlin
Branch: MAIN
Changes since 1.6: +3 -1 lines
File MIME type: text/plain
auto-configure hostname using hostname -f

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></head><body>";
111 print OUT $html_file{$key};
112 print OUT "</body></html>";
113 close(OUT);
114
115 }
116
117 exit;
118
119 sub localHandleTargetInstance {
120 my($Name, $target) = @_;
121
122 $targetpath = $target->{'auto-target-path'};
123 $targetname = $target->{'auto-target-name'};
124
125 if (! defined($target->{'skip-overview'}))
126 {
127 Info("Working on target $targetname.");
128 my($reqRanges,@ranges);
129
130 $reqRanges = $target->{'static-ranges'};
131
132 # if (defined($target->{'static-path'}) &&
133 # defined($target->{'static-name'}))
134 # {
135 # $path = $target->{'static-path'};
136 # $name = $target->{'static-name'};
137
138 if (1) {
139
140 my($range, @ranges);
141 @ranges = getRanges($reqRanges);
142
143 foreach $range (@ranges)
144 {
145 $rangeLabel = rangeToLabel($range);
146
147 my($paramtarget) = "$targetpath/$targetname";
148
149 my($paraminst);
150
151 if (defined($target->{'inst'}))
152 {
153 $paraminst = $target->{'inst'};
154 }
155
156 my($paramrange) = $range;
157
158 # DO DSLIST STUFF
159
160 # find the ds names based on the target type
161 my($ttype) = lc($target->{'target-type'});
162 my($ttRef) = $main::gCT->configHash($Name, 'targettype', $ttype, $target);
163
164 # If there are views defined, then we generate graphs
165 # for each view.
166
167 my($dslist);
168
169 if (defined($ttRef->{'view'}))
170 {
171 my($v);
172 foreach $v (split(/\s*,\s*/, $ttRef->{'view'}))
173 {
174 # views are like this: "cpu: cpu1load cpu5load"
175 my($vname, $dss) = split(/\s*:\s*/, $v, 2);
176
177 $dslist = $dss;
178 $dslist =~ s/\s*$//;
179 $dslist =~ s/\s+/,/g;
180
181 $URL = "$gBaseURL?type=png&target=$paramtarget";
182 $URL .= "&dslist=$dslist&range=$paramrange";
183 my $desc = "$paramtarget $vname";
184 $desc .= " <b>".$target->{'short-desc'}."</b>" if (defined $target->{'short-desc'});
185 if ($paraminst ne "") {
186 $URL .= "&inst=$paraminst";
187 }
188
189 Info("Retrieving graph for $desc");
190 # getURL($URL,"$path/$name-$vname-$rangeLabel.png");
191 $tmp_URL = "$gBaseURL?target=$paramtarget&range=d:w:m:y&view=$vname";
192 $html{"$rangeLabel\t$paramtarget"}.="$desc<br><a href=\"$tmp_URL\"><img src=$URL></a><br>\n";
193 }
194 } else {
195 $dslist = $ttRef->{'ds'};
196 # squeeze out any extra spaces
197 $dslist = join(',', split(/\s*,\s*/, $dslist));
198
199 $URL = "$gBaseURL?type=png&target=$paramtarget";
200 $URL .= "&dslist=$dslist&range=$paramrange";
201 my $desc ="$paraminst $rangeLabel";
202 $desc .= " <b>".$target->{'short-desc'}."</b>" if (defined $target->{'short-desc'});
203 if ($paraminst ne "") {
204 $URL .= "&inst=$paraminst";
205 }
206
207 Info("Retrieving graph for $desc");
208
209 # getURL($URL,"$path/$name-$rangeLabel.png");
210 $tmp_URL = "$gBaseURL?target=$paramtarget&range=d:w:m:y";
211 $html{"$rangeLabel\t$paramtarget"}.="$desc:<br><a href=\"$tmp_URL\"><img src=$URL><a/><br>";
212 }
213 }
214 }
215 }
216
217 return;
218 }
219
220
221 sub getRanges {
222 my($scales) = @_;
223 $scales = "d:w:m:y" unless (defined($scales));
224
225 # these definitions mirror how MRTG 2.5 sets up its graphs
226 my(%scaleMap) = ( 'd' => $main::kHour * 42,
227 'w' => $main::kDay * 10,
228 'm' => $main::kWeek * 6,
229 'y' => $main::kMonth * 16);
230
231 my($scale, @res);
232 foreach $scale (split(/\s*:\s*/, $scales)) {
233 # later, we might do more sophisticated scale specification
234 $scale = $scaleMap{$scale};
235 push @res, $scale;
236 }
237 return @res;
238 }
239
240
241 sub initConst {
242 $main::kMinute = 60; # 60 seconds/min
243 $main::kHour = 60 * $main::kMinute;# 60 minutes/hr
244 $main::kDay = 24 * $main::kHour; # 24 hrs/day
245 $main::kWeek = 7 * $main::kDay; # 7 days/week
246 $main::kMonth = 30 * $main::kDay; # 30 days/month
247 $main::kYear = 365 * $main::kDay; # 365 days/year
248
249 $main::kTypeUnknown = 0;
250 $main::kTypeUnknown = 0; # shut up, -w.
251 $main::kTypeDaily = 1;
252 $main::kTypeWeekly = 2;
253 $main::kTypeMonthly = 3;
254 $main::kTypeYearly = 4;
255
256 @main::gRangeNameMap = ( undef, 'Daily', 'Weekly', 'Monthly', 'Yearly' );
257
258 }
259
260 sub rangeToLabel {
261 my($range) = @_;
262 return $main::gRangeNameMap[rangeType($range)];
263 }
264
265 sub rangeType {
266 my($range) = @_;
267 my($rangeHours) = $range / 3600;
268
269 # question: when is kTypeUnknown appropriate?
270
271 if ($range < $main::kWeek) {
272 return $main::kTypeDaily;
273 } elsif ($range < $main::kMonth) {
274 return $main::kTypeWeekly;
275 } elsif ($range < $main::kYear) {
276 return $main::kTypeMonthly;
277 } else {
278 return $main::kTypeYearly;
279 }
280 }
281
282
283 sub getURL
284 {
285 my($url,$filename) = @_;
286
287 Debug("Fetching url: $url");
288
289 my $ua = new LWP::UserAgent;
290 my $request = new HTTP::Request('GET', $url);
291 my $response = $ua->request($request);
292
293 if ($response->is_success) {
294 my($dir) = dirname($filename);
295 if (! -d $dir) {
296 Info("Making directory $dir to hold file $filename.");
297 Common::Util::MkDir($dir);
298 }
299
300 if (!open(URL,">$filename"))
301 {
302 Error("Error writing to $filename: $!");
303 return;
304 }
305 print URL $response->content;
306 close(URL);
307 }
308 else
309 {
310 Error("Error retrieving target graph: " . $response->message());
311 }
312 }

  ViewVC Help
Powered by ViewVC 1.1.26