File: availability-report.cgi.in

package info (click to toggle)
remstats 1.00a4-8woody1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,576 kB
  • ctags: 1,020
  • sloc: perl: 11,706; ansic: 2,776; makefile: 944; sh: 869
file content (284 lines) | stat: -rw-r--r-- 8,525 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!@@PERL@@ @@PERLCGIOPTS@@

# Copyright 1999, 2000, 2001 (c) Thomas Erskine <@@AUTHOR@@>
# See the COPYRIGHT file with the distribution.

# availability-report.cgi - show the availability report, or appropriate sections therof
# $Id: availability-report.cgi.in,v 1.8 2001/08/28 15:22:24 remstats Exp $

# - - -   Configuration   - - -

use strict;

# What is this program called, for file-names and error-messages.
$main::prog = 'availability-report.cgi';
# So -T will stop whinging.
$ENV{PATH} = '/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:@@BINDIR@@';
# Where is the config dir
$main::config_dir = '@@CONFIGDIR@@';
# Debugging anyone?
$main::debug = 0;
# Where is the availability-report script?
$main::availability_report = '@@BINDIR@@/availability-report';

# - - -   Version History   - - -

(undef, $main::version) = split(' ', '$Revision: 1.8 $');

# - - -   Setup   - - -

use lib '.', '@@LIBDIR@@', '@@RRDLIBDIR@@';
use Time::Local;
require "remstats.pl";
require "cgistuff.pl";
require "htmlstuff.pl";

&read_config_dir($main::config_dir, 'general', 'html', 'links', 'tools');

# Initialize some useful variables
%main::R = &cgi_request;
&cgi_sendheaders('Description: Remstats Availability Report');
$main::url = 'http://' . &cgi_var("SERVER_NAME") . ':' . &cgi_var('SERVER_PORT');
my $script_name = &cgi_var('SCRIPT_NAME');
if ($script_name =~ m#^/#) { $main::url .= $script_name; }
else { $main::url .= '/'. $script_name; }

$| = 1; # no buffering please

%main::indices = &init_indices; # for headers

my ($errors, $time, $hosts, $rrds, $variables, $previous_day, $previous_week, 
	$previous_month, $next_day, $next_week, $next_month, $previous_day_start, 
	$previous_week_start, $previous_month_start, $next_day_start, 
	$next_week_start, $next_month_start, $previous_day_end, $previous_week_end, 
	$previous_month_end, $next_day_end, $next_week_end, $next_month_end);

# When are we interested in?
my ($start, $end);
if (defined $main::R{'start'} and defined $main::R{'end'}) {

	if ($main::R{'start'} =~ /^(\d+)$/) { $start = $1; }
	elsif ($main::R{'start'} =~ /^\s*$/) {
		my (undef, undef, undef, $mday, $mon, $year) = localtime();
		$start = timelocal(0,0,0, $mday, $mon, $year);
	}
	else { &abort("invalid start '$main::R{'start'}'"); }
	if ($main::R{'end'} =~ /^(\d+)$/) { $end = $1; }
	elsif ($main::R{'end'} =~ /^\s*$/) { $end = $start + 24*60*60; }
	else { &abort("invalid end '$main::R{'end'}'"); }

}
elsif (defined $main::R{'start'} or defined $main::R{'end'}) {
	&abort("you must specify both start and end, or neither");
}
else {
	my (undef, undef, undef, $mday, $mon, $year) = localtime();
	$start = timelocal(0,0,0, $mday, $mon, $year);
	$end = $start + 24*60*60;
	if ($end > time()) { $end = time(); }
}
if ($end > time()) { $end = time(); }
$time = " -t $start,$end";

$previous_day_start = &shift_time( $start, -1);
$previous_week_start = &shift_time( $start, -7);
$previous_month_start = &shift_time( $start, -30);
$next_day_start = &shift_time( $start, +1);
$next_week_start = &shift_time( $start, +7);
$next_month_start = &shift_time( $start, +30);

$previous_day_end = $previous_day_start + 24*60*60;
$previous_week_end = $previous_week_start + 7*24*60*60;
$previous_month_end = &shift_time( $previous_month_start, +30);
$next_day_end = $next_day_start + 24*60*60;
$next_week_end = $next_week_start + 7*24*60*60;
$next_month_end = &shift_time( $next_month_start, +30);

$previous_day = 'start='. $previous_day_start .'&end='. $previous_day_end;
$previous_week = 'start='. $previous_week_start .'&end='. $previous_week_end;
$previous_month = 'start='. $previous_month_start .'&end='. $previous_month_end;
$next_day = 'start='. $next_day_start .'&end='. $next_day_end;
$next_week = 'start='. $next_week_start .'&end='. $next_week_end;
$next_month = 'start='. $next_month_start .'&end='. $next_month_end;

# Any more restrictions?
if (defined $main::R{'host'} and $main::R{'host'} =~ /^([-a-zA-Z0-9\.,_]+)$/) {
	$hosts = ' -H '. lc $1;
}
elsif (defined $main::R{'host'} and length($main::R{'host'})>0) {
	&error("invalid host '$main::R{'host'}'");
	++$errors;
}
else { $hosts = ''; }

if (defined $main::R{'rrd'} and $main::R{'rrd'} =~ /^([-a-zA-Z0-9\.,]+)$/) {
	$rrds = ' -R '. lc $1;
}
elsif (defined $main::R{'rrd'} and length($main::R{'rrd'})>0) {
	&error("invalid rrd '$main::R{'rrd'}'");
	++$errors;
}
else { $rrds = ''; }

if (defined $main::R{'variable'} and $main::R{'variable'} =~ /^([-a-zA-Z0-9\.,]+)$/) {
	$variables = ' -V '. lc $1;
}
elsif (defined $main::R{'variable'} and length($main::R{'variable'}>0)) {
	&error("invalid variable '$main::R{'variable'}'");
	++$errors;
}
else { $variables = ''; }

if ($errors) { &abort("too many errors"); }

# - - -   Main Line   - - -

print &html_header('Availability Report', 'Availability Report', %main::indices);
print cgi_fmtrequest(%main::R) if ($main::debug);

my $cmd = $main::availability_report .' -c '. $time . $hosts . $rrds . $variables;
&debug("cmd='$cmd'") if ($main::debug);
open (PIPE, "$cmd 2>&1|") or &abort("can't open pipe to $main::availability_report: $!");
print <PIPE>;
close(PIPE);

# Show the selection form
print <<"EOD_TAIL";
</TABLE>

<P>
<TABLE WIDTH="100%"><TR><TD BGCOLOR="#DDDDDD"><H2>Selection</H2></TD></TR></TABLE>
</P>


<FORM METHOD="POST" ACTION="$main::url">
<TABLE ALIGN="CENTER">
<TR>
<TD VALIGN="TOP">Previous: <A HREF="$main::url?$previous_day">day</A> 
	<A HREF="$main::url?$previous_week">week</A> 
	<A HREF="$main::url?$previous_month">month</A></TD>
<TD VALIGN="TOP">Next: <A HREF="$main::url?$next_day">day</A>
	<A HREF="$main::url?$next_week">week</A> 
	<A HREF="$main::url?$next_month">month</A></TD>
</TR>
<TR>
<TD VALIGN="TOP">Date&nbsp;From:&nbsp;<INPUT NAME="start" SIZE=14></TD>
<TD VALIGN="TOP">To:&nbsp<INPUT NAME="end" SIZE=14></TD>
</TR>

<TR>
<TD VALIGN="TOP" COLSPAN="2">Host:&nbsp;<INPUT NAME="host" SIZE="12"></TD>
</TR>
<TR>
<TD VALIGN="TOP">RRD:&nbsp;<INPUT NAME="rrd" SIZE="10"></TD>
<TD VALIGN="TOP">Variable:&nbsp;<INPUT NAME="variable" SIZE="10"></TD>
</TR>

<TR>
<TD ALIGN="CENTER" VALIGN="TOP"><INPUT TYPE="SUBMIT" VALUE="Send Query"></TD>
<TD ALIGN="CENTER" VALIGN="TOP"><INPUT TYPE="RESET" VALUE="Clear Form"></TD>
</TR>

</TABLE>
</FORM>
EOD_TAIL

print &html_footer;

0;

#----------------------------------------------------------------- abort ---
sub abort {
	my ($msg) = @_;
	print <<"EOD_ABORT";
content-type: text/html

<html>
<head><title>$main::prog Abort</title></head>
<body>
<h1>$main::prog (version $main::version) Abort</h1>
$msg
</body>
</html>
EOD_ABORT
	exit 1;
}

#------------------------------------------------- debug ---
sub debug {
	my ($msg) = @_;

	print "<br><B>DEBUG</b>: $msg\n";
}

#-------------------------------------------------- error ---
sub error {
	my ($msg) = @_;

	print "<br><B>ERROR</b>: $msg\n";
}

#--------------------------------------------------- shift_time ---
# shift a date (either timestamp or YYYYMMDD or YYMMDDHHMMSS) by a day
# week or month.
sub shift_time {
	my ($date, $shift) = @_;
	my ($sec, $min, $hour, $mday, $mon, $year);

	if (length($date) == 8) {
		$year = substr($date,0,4) + 0;
		$mon = substr($date,4,2) - 1;
		$mday = substr($date,6,2) + 0;
		($sec, $min, $hour) = (0,0,0);
		if ($shift == 30) {
			$mon += 1;
			if ($mon > 11) { $mon = 0; $year += 1; }
		}
		if ($shift == -30) {
			$mon -= 1;
			if ($mon < 0) { $mon = 11; $year -= 1; }
		}
		$date = timelocal($sec, $min, $hour, $mday, $mon, $year);
		if ($shift >= -7 and $shift <= 7) {
			$date += $shift * 60*60*24;
		}
	}
	elsif (length($date) == 14) {
		$year = substr($date,0,4) + 0;
		$mon = substr($date,4,2) - 1;
		$mday = substr($date,6,2) + 0;
		$hour = substr($date,8,2) + 0;
		$min = substr($date,10,2) + 0;
		$sec = substr($date,12,2) + 0;
		if ($shift == 30) {
			$mon += 1;
			if ($mon > 11) { $mon = 0; ++$year; }
		}
		elsif ($shift == -30) {
			$mon -= 1;
			if ($mon < 0) { $mon = 11; --$year; }
		}
		$date = timelocal($sec, $min, $hour, $mday, $mon, $year);
		if ($shift >= -7 and $shift <= 7) {
			$date += $shift * 60*60*24;
		}
	}
	else {
		if ($shift == 30) {
			($sec, $min, $hour, $mday, $mon, $year) = localtime($date);
			$mon ++;
			if ($mon > 11) { $mon = 0; ++$year; }
			$date = timelocal($sec, $min, $hour, $mday, $mon, $year);
		}
		elsif ($shift == -30) {
			($sec, $min, $hour, $mday, $mon, $year) = localtime($date);
			$mon --;
			if ($mon < 0) { $mon = 11; --$year; }
			$date = timelocal($sec, $min, $hour, $mday, $mon, $year);
		}
		else {
			$date += $shift * 60*60*24;
		}
	}
$date;
}