]> granicus.if.org Git - apache/blob - support/log_server_status.in
fix references / update transformation
[apache] / support / log_server_status.in
1 #!@perlbin@
2 #
3 # Copyright 2001-2004 The Apache Software Foundation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 #
18 # Log Server Status
19 # Mark J Cox, UK Web Ltd 1996, mark ukweb.com
20 #
21 # This script is designed to be run at a frequent interval by something
22 # like cron.  It connects to the server and downloads the status
23 # information.  It reformats the information to a single line and logs
24 # it to a file.  Make sure the directory $wherelog is writable by the
25 # user who runs this script.
26 #
27 require 'sys/socket.ph';
28
29 $wherelog = "/var/log/graph/";  # Logs will be like "/var/log/graph/19960312"
30 $server = "localhost";          # Name of server, could be "www.foo.com"
31 $port = "80";                   # Port on server
32 $request = "/status/?auto";     # Request to send
33
34 sub tcp_connect
35 {
36         local($host,$port) =@_;
37         $sockaddr='S n a4 x8';
38         chop($hostname=`hostname`);
39         $port=(getservbyname($port, 'tcp'))[2]  unless $port =~ /^\d+$/;
40         $me=pack($sockaddr,&AF_INET,0,(gethostbyname($hostname))[4]);
41         $them=pack($sockaddr,&AF_INET,$port,(gethostbyname($host))[4]);
42         socket(S,&PF_INET,&SOCK_STREAM,(getprotobyname('tcp'))[2]) || 
43                 die "socket: $!";
44         bind(S,$me) || return "bind: $!";
45         connect(S,$them) || return "connect: $!";
46         select(S); 
47         $| = 1; 
48         select(stdout);
49         return "";
50 }
51
52 ### Main
53
54 {
55         $year=`date +%y`;
56         chomp($year);
57         $year += ($year < 70) ? 2000 : 1900;
58         $date = $year . `date +%m%d:%H%M%S`;
59         chomp($date);
60         ($day,$time)=split(/:/,$date);
61         $res=&tcp_connect($server,$port);
62         open(OUT,">>$wherelog$day");
63         if ($res) {
64                 print OUT "$time:-1:-1:-1:-1:$res\n";
65                 exit 1;
66         }
67         print S "GET $request\n";
68         while (<S>) {
69                 $requests=$1 if ( m|^BusyServers:\ (\S+)|);
70                 $idle=$1 if ( m|^IdleServers:\ (\S+)|);
71                 $number=$1 if ( m|sses:\ (\S+)|);
72                 $cpu=$1 if (m|^CPULoad:\ (\S+)|);
73         }
74         print OUT "$time:$requests:$idle:$number:$cpu\n";
75 }
76
77