-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScoreboard.pm
323 lines (209 loc) · 6.75 KB
/
Scoreboard.pm
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package Apache::Scoreboard;
use strict;
use constant DEBUG => 0;
use Carp;
BEGIN {
use mod_perl;
die "mod_perl < 2.0 is required" unless $mod_perl::VERSION < 1.99;
}
BEGIN {
no strict;
$VERSION = '0.16';
@ISA = qw(DynaLoader);
if ($ENV{MOD_PERL}) {
__PACKAGE__->bootstrap($VERSION);
}
else {
require Apache::DummyScoreboard;
}
}
my $ua;
sub http_fetch {
my($self, $url) = @_;
Carp::croak("no url argument was passed") unless $url;
require LWP::UserAgent;
unless ($ua) {
no strict 'vars';
$ua = LWP::UserAgent->new;
$ua->agent(join '/', __PACKAGE__, $VERSION);
}
my $request = HTTP::Request->new('GET', $url);
my $response = $ua->request($request);
unless ($response->is_success) {
warn "request failed: ", $response->status_line if DEBUG;
return undef;
}
my $type = $response->header('Content-type');
unless ($type eq Apache::Scoreboard::REMOTE_SCOREBOARD_TYPE) {
warn "invalid scoreboard Content-type: $type" if DEBUG;
return undef;
}
$response->content;
}
sub fetch {
my($self, $url) = @_;
$self->thaw($self->http_fetch($url));
}
sub fetch_store {
my($self, $url, $file) = @_;
local *FH;
open FH, ">$file" or die "open $file: $!";
print FH $self->http_fetch($url);
close FH;
}
sub retrieve {
my($self, $file) = @_;
local *FH;
open FH, $file or die "open $file: $!";
local $/;
my $data = <FH>;
$self->thaw($data);
}
1;
__END__
=head1 NAME
Apache::Scoreboard - Perl interface to the Apache scoreboard structure
=head1 SYNOPSIS
use Apache::Scoreboard ();
#inside httpd
my $image = Apache::Scoreboard->image;
#outside httpd
my $image = Apache::Scoreboard->fetch("http://localhost/scoreboard");
=head1 DESCRIPTION
Apache keeps track of server activity in a structure known as the
I<scoreboard>. There is a I<slot> in the scoreboard for each child
server, containing information such as status, access count, bytes
served and cpu time. This same information is used by I<mod_status>
to provide current server statistics in a human readable form.
=head1 METHODS
=over 4
=item image
This method returns an object for accessing the scoreboard structure
when running inside the server:
my $image = Apache::Scoreboard->image;
=item fetch
This method fetches the scoreboard structure from a remote server,
which must contain the following configuration:
PerlModule Apache::Scoreboard
<Location /scoreboard>
SetHandler perl-script
PerlHandler Apache::Scoreboard::send
order deny,allow
deny from all
#same config you have for mod_status
allow from 127.0.0.1 ...
</Location>
If the remote server is not configured to use mod_perl or simply for a
smaller footprint, see the I<apxs> directory for I<mod_scoreboard_send>:
LoadModule scoreboard_send_module libexec/mod_scoreboard_send.so
<Location /scoreboard>
SetHandler scoreboard-send-handler
order deny,allow
deny from all
allow from 127.0.0.1 ...
</Location>
The image can then be fetched via http:
my $image = Apache::Scoreboard->fetch("http://remote-hostname/scoreboard");
=item fetch_store
=item retrieve
The I<fetch_store> method is used to fetch the image once from and
remote server and save it to disk. The image can then be read by
other processes with the I<retrieve> function.
This way, multiple processes can access a remote scoreboard with just
a single request to the remote server. Example:
Apache::Scoreboard->fetch_store($url, $local_filename);
my $image = Apache::Scoreboard->retrieve($local_filename);
=item parent
This method returns a reference to the first parent score entry in the
list, blessed into the I<Apache::ParentScore> class:
my $parent = $image->parent;
Iterating over the list of scoreboard slots is done like so:
for (my $parent = $image->parent; $parent; $parent = $parent->next) {
my $pid = $parent->pid; #pid of the child
my $server = $parent->server; #Apache::ServerScore object
...
}
=item server_limit
$server_limit = $image->server_limit;
same as C<Apache::Constants::HARD_SERVER_LIMIT>, but added for future
compat with 2.x. Use that method to ease migration to mod_perl 2
=item pids
Returns an array reference of all child pids:
my $pids = $image->pids;
=back
=head2 The Apache::ParentScore Class
=over 4
=item pid
The parent keeps track of child pids with this field:
my $pid = $parent->pid;
=item server
Returns a reference to the corresponding I<Apache::ServerScore>
structure:
my $server = $parent->server;
=item next
Returns a reference to the next I<Apache::ParentScore> object in the list:
my $p = $parent->next;
=back
=head2 The Apache::ServerScore Class
=over 4
=item status
This method returns the status of child server, which is one of:
"_" Waiting for Connection
"S" Starting up
"R" Reading Request
"W" Sending Reply
"K" Keepalive (read)
"D" DNS Lookup
"L" Logging
"G" Gracefully finishing
"." Open slot with no current process
=item access_count
The access count of the child server:
my $count = $server->access_count;
=item request
The first 64 characters of the HTTP request:
#e.g.: GET /scoreboard HTTP/1.0
my $request = $server->request;
=item client
The ip address or hostname of the client:
#e.g.: 127.0.0.1
my $client = $server->client;
=item bytes_served
Total number of bytes served by this child:
my $bytes = $server->bytes_served;
=item conn_bytes
Number of bytes served by the last connection in this child:
my $bytes = $server->conn_bytes;
=item conn_count
Number of requests served by the last connection in this child:
my $count = $server->conn_count;
=item times
In a list context, returns a four-element list giving the user and
system times, in seconds, for this process and the children of this
process.
my($user, $system, $cuser, $csystem) = $server->times;
In a scalar context, returns the overall CPU percentage for this server:
my $cpu = $server->times;
=item start_time
In a list context this method returns a 2 element list with the seconds and
microseconds since the epoch, when the request was started. In scalar
context it returns floating seconds like Time::HiRes::time()
my($tv_sec, $tv_usec) = $server->start_time;
my $secs = $server->start_time;
=item stop_time
In a list context this method returns a 2 element list with the seconds and
microseconds since the epoch, when the request was finished. In scalar
context it returns floating seconds like Time::HiRes::time()
my($tv_sec, $tv_usec) = $server->stop_time;
my $secs = $server->stop_time;
=item req_time
Returns the time taken to process the request in milliseconds:
my $req_time = $server->req_time;
=item vhost
Returns the vhost entry
my $vhost = $server->vhost;
=back
=head1 SEE ALSO
Apache::VMonitor(3), GTop(3)
=head1 AUTHOR
Doug MacEachern