#!/usr/bin/perl
#
# Program: Apache Response Code Viewer <responsecodes.pl>
#
# Author: Matty <matty91 at gmail dot com>
#
# Current Version: 0.1a
#
# Revision History:
#  Version 0.1a
#
# Last Updated: 11-25-2005
#
# Purpose: Prints the number of requests processed by an
#          Apache web server
#
# Installation:
#   Copy the shell script to a suitable location
#
# CDDL HEADER START
#  The contents of this file are subject to the terms of the
#  Common Development and Distribution License, Version 1.0 only
#  (the "License").  You may not use this file except in compliance
#  with the License.
#
#  You can obtain a copy of the license at Docs/cddl1.txt
#  or http://www.opensolaris.org/os/licensing.
#  See the License for the specific language governing permissions
#  and limitations under the License.
# CDDL HEADER END
#
# Example:
#   The following example prints all responses:
#   $ responsecodes.pl        
#   Response Code  URI                        File Name                
#   200            /                          /tmp/apache/htdocs/index.html.var
#   304            /apache_pb.gif             /tmp/apache/htdocs/apache_pb.gif
#   200            /                          /tmp/apache/htdocs/index.html.var
#   200            /apache_pb.gif             /tmp/apache/htdocs/apache_pb.gif
#   404            /favicon.ico               /tmp/apache/htdocs/favicon.ico
#   ^C
#
#   The following example prints all responses with a 404 return code (document not found):
#   $ responsecodes.pl  -c 404
#   Response Code  URI                        File Name                
#   404            /favicon.ico               /tmp/apache/htdocs/favicon.ico
#   404            /favicon.ico               /tmp/apache/htdocs/favicon.ico
#   404            /favicon.ico               /tmp/apache/htdocs/favicon.ico
#   404            /favicon.ico               /tmp/apache/htdocs/favicon.ico
#   ^C

### Standard includes
use Getopt::Std;
use POSIX;

# Process the command line arguments
%options=();
getopts("c:",\%options);

my $code = $options{c} || 0;

$dtrace = <<END;
/usr/sbin/dtrace -Z -32 -q -n'
::apache_log_request:log-request
{
    /* The response code (r->status) is 48-bytes from arg0 on 32-bit Apache */
    this->responsecode = (int)*(uintptr_t *)copyin(arg0 + 68,sizeof(int));

    /* The unparsed URI is (r->unparsed) 200-bytes from arg0  on 32-bit Apache */
    this->uuri = copyinstr(*(uintptr_t *)copyin(arg0 + 200, sizeof(uintptr_t)));

    /* The filename (r->filename) is 208-bytes from arg0 on 32-bit Apache */
    this->file = copyinstr(*(uintptr_t *)copyin(arg0 + 208,sizeof(uintptr_t)));
    
    printf("%d %s %s\\n",this->responsecode, this->uuri, this->file);
}'
END

open(DTRACE,"$dtrace |") || die "cannot open dtrace $@\n";


printf("%-13s  %-25s  %-25s\n","Response Code", "URI", "File Name");

while (<DTRACE>) {
    chomp();
   
    ($responsecode, $uuri, $filename) = split(' ',$_);

    if (($code eq $responsecode) || !$code ) {
        printf("%-13s  %-25s  %-25s\n", $responsecode, $uuri, $filename);
    }
}
