File: log-event.cgi.in

package info (click to toggle)
remstats 1.0.13a-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 7,176 kB
  • ctags: 1,283
  • sloc: perl: 16,135; ansic: 2,776; sh: 1,061; makefile: 688
file content (204 lines) | stat: -rwxr-xr-x 5,480 bytes parent folder | download | duplicates (2)
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
#!@@PERL@@ @@PERLCGIOPTS@@

# log-event.cgi - let people log a manual event
# $Id: log-event.cgi.in,v 1.8 2002/06/24 14:56:44 remstats Exp $
# from remstats @@VERSION@@

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

# - - -   Configuration   - - -

# What is this program called, for file-names and error-messages.
$main::prog = 'log-event.cgi';
# Where is the config dir?
$main::config_dir = '@@CONFIGDIR@@';

# Debugging anyone?
$main::debug = 0;

# - - -   Version History   - - -

$main::version = (split(' ', '$Revision: 1.8 $'))[1];

# - - -   Setup   - - -

# So -T will stop whinging.
$ENV{PATH} = '/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin';

use lib '.', '@@LIBDIR@@', '@@RRDLIBDIR@@';
require "remstats.pl";
require "accessstuff.pl";
require "cgistuff.pl";
require "htmlstuff.pl";

# Don't try to update IP_CACHE; don't have access from cgi
$main::ip_cache_readonly = 1;

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

print &cgi_fmtrequest(%R) if ($main::debug);

&abort("access prohibited") unless( &access_control( $main::prog));

&read_config_dir($main::config_dir, 'general', 'html', 'colors');
%indices = &init_indices;

# - - -   Main Line   - - -

if (defined $R{'action'}) { &process(%R); }
else { &show_form; }

0;

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

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

#--------------------------------------------------------------- show_form ---
sub show_form {
	my (%R) = @_;
	
	print &html_header('Log Event', 'Log Event', %main::indices);
	print <<"EOD_FORM";
<hr>

<h1>Log a Manual Event</h1>
<div style="text-align:center">
<form method="POST" action="$main::url">
<table border=1>
<tr>
	<td valign=top>Host:</td>
	<td valign=top><input name="host" size=30></td>
</tr>
<tr>
	<td valign=top>RRD:</td>
	<td valign=top><input name="rrd" size=30></td>
</tr>
<tr>
	<td valign=top>Variable:</td>
	<td valign=top><input name="variable" size=30></td>
</tr>
<tr>
	<td valign=top>Value:</td>
	<td valign=top><input name="value" size=30></td>
</tr>
<tr>
	<td valign=top>Message:</td>
	<td valign=top><input name="message" size=30></td>
</tr>
<tr>
	<td valign=top>Event Type:</td>
	<td valign=top><input name="eventtype" size=20 value="EVENT"></td>
</tr>

<tr>
	<td colspan=2 align=center>
		<input type=submit value="Post Event">
		<input type=reset value="Clear Form">
		<input type=hidden name="action" value="event">
	</td>
</tr>
</table>
</form>
</div>
EOD_FORM
	print &html_footer;
}

#------------------------------------------------------------- process ---
sub process {
	my %R = @_;
	my ($host, $rrd, $variable, $value, $message, $eventtype);

# Validate fields
	if (defined $R{'host'} and 
			$R{'host'} =~ /^\s*([-a-zA-Z0-9\.]{3,256})\s*$/) {
		$host = lc $1;
		&debug("host = '$host'") if ($main::debug);
	}
	elsif (defined $R{'host'} and $R{'host'} !~ /^\s*$/) { &abort("bad host format for '$R{'host'}'"); }

	if (defined $R{'rrd'} and 
			$R{'rrd'} =~ /^\s*([-a-zA-Z0-9_\.]{1,256})\s*$/) {
		$rrd = $1;
		&debug("rrd = '$rrd'") if ($main::debug);
	}
	elsif (defined $R{'rrd'} and $R{'rrd'} !~ /^\s*$/) { &abort("bad rrd format for '$R{'rrd'}'"); }
	
	if (defined $R{'variable'} and 
			$R{'variable'} =~ /^\s*([a-zA-Z0-9_\.]{1,256})\s*$/) {
		$variable = $1;
		&debug("variable = '$variable'") if ($main::debug);
	}
	elsif (defined $R{'variable'} and $R{'variable'} !~ /^\s*$/) { &abort("bad variable format for '$R{'variable'}'"); }

	if (defined $R{'value'} and
			$R{'value'} =~ /^\s*([+-]?\d*(\.\d+)?)\s*$/) {
		$value = $1;
		&debug("value = '$value'") if ($main::debug);
	}
	elsif (defined $R{'value'} and $R{'value'} !~ /^\s*$/) { &abort("bad value format for '$R{'value'}'"); }

# I.E. no <>& or unprintable
	if (defined $R{'message'} and
			$R{'message'} =~ /^\s*(.*?)\s*$/) {
		$message = $1;
		&debug("message = '$message'") if ($main::debug);
	}
	elsif (defined $R{'message'} and $R{'message'} !~ /^\s*$/) { &abort("bad message format for '$R{'message'}'"); }

	if (defined $R{'eventtype'} and
			$R{'eventtype'} =~ /^\s*([0-9a-zA-Z]+)\s*$/) {
		$eventtype = $1;
		&debug("eventtype = '$eventtype'") if ($main::debug);
	}
	elsif (defined $R{'eventtype'} and $R{'eventtype'} !~ /^\s*$/) { &abort("bad eventtype format for '$R{'eventtype'}'"); }
	else { $eventtype = 'EVENT'; }

	$message = '[from '. $ENV{'REMOTE_ADDR'} .'] '. $message;
	&logit( $eventtype, $host, $rrd, $variable, $value, $message);

	print &html_header('Log Event', 'Log Event', %main::indices);
	print "<H3 align=left>Logged event for $host</H3>\n" . &html_footer;
	
}

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

	print "<BR>DEBUG: $msg\n";
}

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

	print "<H1>ERROR: $msg</H1>\n";
}

#---------------------------------------------------- keep_strict_happy ---
sub keep_strict_happy {
	$main::ip_cache_readonly = 0;
}