/[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.1 - (hide annotations)
Tue Jun 11 19:27:37 2002 UTC (17 years, 5 months ago) by dpavlin
Branch: MAIN
File MIME type: text/plain
*** empty log message ***

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

  ViewVC Help
Powered by ViewVC 1.1.26