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 = "/data/mon/"; |
41 |
} |
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 |
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 |
open(OUT,"> $filename") || die "can't open output html '$filename': $!"; |
108 |
print OUT "<html><head><title>$key</title></head><body>"; |
109 |
print OUT $html_file{$key}; |
110 |
print OUT "</body></html>"; |
111 |
close(OUT); |
112 |
|
113 |
} |
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 |
my $desc = "$paramtarget $vname"; |
182 |
$desc .= " <b>".$target->{'short-desc'}."</b>" if (defined $target->{'short-desc'}); |
183 |
if ($paraminst ne "") { |
184 |
$URL .= "&inst=$paraminst"; |
185 |
} |
186 |
|
187 |
Info("Retrieving graph for $desc"); |
188 |
# getURL($URL,"$path/$name-$vname-$rangeLabel.png"); |
189 |
$tmp_URL = "$gBaseURL?target=$paramtarget&range=d:w:m:y&view=$vname"; |
190 |
$html{"$rangeLabel\t$paramtarget"}.="$desc<br><a href=\"$tmp_URL\"><img src=$URL></a><br>\n"; |
191 |
} |
192 |
} else { |
193 |
$dslist = $ttRef->{'ds'}; |
194 |
# squeeze out any extra spaces |
195 |
$dslist = join(',', split(/\s*,\s*/, $dslist)); |
196 |
|
197 |
$URL = "$gBaseURL?type=png&target=$paramtarget"; |
198 |
$URL .= "&dslist=$dslist&range=$paramrange"; |
199 |
my $desc ="$paraminst $rangeLabel"; |
200 |
$desc .= " <b>".$target->{'short-desc'}."</b>" if (defined $target->{'short-desc'}); |
201 |
if ($paraminst ne "") { |
202 |
$URL .= "&inst=$paraminst"; |
203 |
} |
204 |
|
205 |
Info("Retrieving graph for $desc"); |
206 |
|
207 |
# getURL($URL,"$path/$name-$rangeLabel.png"); |
208 |
$tmp_URL = "$gBaseURL?target=$paramtarget&range=d:w:m:y"; |
209 |
$html{"$rangeLabel\t$paramtarget"}.="$desc:<br><a href=\"$tmp_URL\"><img src=$URL><a/><br>"; |
210 |
} |
211 |
} |
212 |
} |
213 |
} |
214 |
|
215 |
return; |
216 |
} |
217 |
|
218 |
|
219 |
sub getRanges { |
220 |
my($scales) = @_; |
221 |
$scales = "d:w:m:y" unless (defined($scales)); |
222 |
|
223 |
# these definitions mirror how MRTG 2.5 sets up its graphs |
224 |
my(%scaleMap) = ( 'd' => $main::kHour * 42, |
225 |
'w' => $main::kDay * 10, |
226 |
'm' => $main::kWeek * 6, |
227 |
'y' => $main::kMonth * 16); |
228 |
|
229 |
my($scale, @res); |
230 |
foreach $scale (split(/\s*:\s*/, $scales)) { |
231 |
# later, we might do more sophisticated scale specification |
232 |
$scale = $scaleMap{$scale}; |
233 |
push @res, $scale; |
234 |
} |
235 |
return @res; |
236 |
} |
237 |
|
238 |
|
239 |
sub initConst { |
240 |
$main::kMinute = 60; # 60 seconds/min |
241 |
$main::kHour = 60 * $main::kMinute;# 60 minutes/hr |
242 |
$main::kDay = 24 * $main::kHour; # 24 hrs/day |
243 |
$main::kWeek = 7 * $main::kDay; # 7 days/week |
244 |
$main::kMonth = 30 * $main::kDay; # 30 days/month |
245 |
$main::kYear = 365 * $main::kDay; # 365 days/year |
246 |
|
247 |
$main::kTypeUnknown = 0; |
248 |
$main::kTypeUnknown = 0; # shut up, -w. |
249 |
$main::kTypeDaily = 1; |
250 |
$main::kTypeWeekly = 2; |
251 |
$main::kTypeMonthly = 3; |
252 |
$main::kTypeYearly = 4; |
253 |
|
254 |
@main::gRangeNameMap = ( undef, 'Daily', 'Weekly', 'Monthly', 'Yearly' ); |
255 |
|
256 |
} |
257 |
|
258 |
sub rangeToLabel { |
259 |
my($range) = @_; |
260 |
return $main::gRangeNameMap[rangeType($range)]; |
261 |
} |
262 |
|
263 |
sub rangeType { |
264 |
my($range) = @_; |
265 |
my($rangeHours) = $range / 3600; |
266 |
|
267 |
# question: when is kTypeUnknown appropriate? |
268 |
|
269 |
if ($range < $main::kWeek) { |
270 |
return $main::kTypeDaily; |
271 |
} elsif ($range < $main::kMonth) { |
272 |
return $main::kTypeWeekly; |
273 |
} elsif ($range < $main::kYear) { |
274 |
return $main::kTypeMonthly; |
275 |
} else { |
276 |
return $main::kTypeYearly; |
277 |
} |
278 |
} |
279 |
|
280 |
|
281 |
sub getURL |
282 |
{ |
283 |
my($url,$filename) = @_; |
284 |
|
285 |
Debug("Fetching url: $url"); |
286 |
|
287 |
my $ua = new LWP::UserAgent; |
288 |
my $request = new HTTP::Request('GET', $url); |
289 |
my $response = $ua->request($request); |
290 |
|
291 |
if ($response->is_success) { |
292 |
my($dir) = dirname($filename); |
293 |
if (! -d $dir) { |
294 |
Info("Making directory $dir to hold file $filename."); |
295 |
Common::Util::MkDir($dir); |
296 |
} |
297 |
|
298 |
if (!open(URL,">$filename")) |
299 |
{ |
300 |
Error("Error writing to $filename: $!"); |
301 |
return; |
302 |
} |
303 |
print URL $response->content; |
304 |
close(URL); |
305 |
} |
306 |
else |
307 |
{ |
308 |
Error("Error retrieving target graph: " . $response->message()); |
309 |
} |
310 |
} |