#!/usr/bin/perl

use Socket;
use IO::Socket;
use IO::Select;


#
# script for RRDtool to collect apache statistics
#

$step=60;
$base="/var/rrd/apache.rrd";

%params = (  "TA"   => 'Total accesses: (\d*)',
	     "RPS"  => '(\d*\.\d*)\srequests/sec',
	     "BPS"  => '(\d*)\sB/second',
	     "CPU"  => '(\d*\.\d*)\%\sCPU\sload',
	     "CUR"  => '(\d*)\srequests\scurrently\sbeing\sprocessed',
	     "IDLE" => '(\d*)\sidle'  );


if ($ARGV[0] eq init){
    
    system("rrdtool create $base --step $step".
    " DS:total_acceses:COUNTER:200:0:999999".
    " DS:requests_per_second:GAUGE:200:0:99999".
    " DS:bytes_per_second:GAUGE:200:0:9999999".
    " DS:cpu:GAUGE:200:0:1000".
    " DS:current_process:GAUGE:200:0:999".
    " DS:current_idle:GAUGE:200:0:999".

    " RRA=AVERAGE:0.5:1:1440".	# 1440 samples every minute - day
    " RRA=AVERAGE:0.5:5:2016"); # 2016 samples every 5 minute - week
    
    exit;
}

if ($ARGV[0] eq update){

# get stats
    my $info=getdata();
    $ta  =getparam('TA')+0;
    $tt  =getparam("TT")+0;
    $rps =getparam("RPS")+0;
    $bps =getparam("BPS")+0;
    $cpu =getparam("CPU")+0;
    $cur =getparam("CUR")+0;
    $idle=getparam("IDLE")+0;

    $n=time;
    system("rrdtool update $base $n:$ta:$rps:$bps:$cpu:$cur:$idle ");
    #system("echo update $base $n:$ta:$tt:$rps:$bps:$cpu:$cur:$idle ");
    
    exit;
}

if ($ARGV[0] eq day){
    system("rrdtool graph /tmp/day-apache-acceses.gif -t \"Acceses to Apache WEB Server\" ".
	"DEF:total_acceses=$base:total_acceses:AVERAGE ".
	"LINE2:total_acceses#0000ff:\"Acceses\" ".
	"-h 200 ");    

    system("rrdtool graph /tmp/day-apache-per-second.gif -t \"Apache Per second Statistics\" ".
	" DEF:requests_per_second=$base:requests_per_second:AVERAGE ".
	" DEF:bytes_per_second=$base:bytes_per_second:AVERAGE ".	
	" CDEF:kbytes_per_second=bytes_per_second,1024,/ ".
	" LINE2:requests_per_second#00ff00:\"Requests per Second\" ".
	" LINE2:kbytes_per_second#ff0000:\"KBytes per Second\" ".	
	"-h 200 ");    

    system("rrdtool graph /tmp/day-apache-cpu.gif -t \"Apache CPU Usage\" ".
	"DEF:cpu=$base:cpu:AVERAGE ".
	"LINE2:cpu#0000ff:\"\" ".
	"-h 200 ");    

    system("rrdtool graph /tmp/day-apache-workers.gif -t \"Apache Workers Stastitics\" ".
	"DEF:current_process=$base:current_process:AVERAGE ".
	"DEF:current_idle=$base:current_idle:AVERAGE ".	
	"AREA:current_process#ff0000:\"Current Processing\" ".
	"STACK:current_idle#00ff00:\"IDLE\" ".	
	"-h 200 ");    

    exit;
}

if($ARGV[0] eq remove){
    unlink $base;
}


if($ARGV[0] eq descr){
    # here, i should write description and path to gif whith picture
    print("Apache Acceses:/tmp/day-apache-acceses.gif\n");
    print("Apache per second requests and traffic:/tmp/day-apache-per-second.gif\n");
    print("Apache CPU usage:/tmp/day-apache-cpu.gif\n");
    print("Apahce workers statistics:/tmp/day-apache-workers.gif\n");    
}

sub trim{
    my @out=@_;
    for(@out){
        s/^\s+//;
        s/\s+$//;
    }
    return wantarray? @out : $out[0];
};


sub getdata()
{
my $s = IO::Socket::INET->new(PeerAddr => "127.0.0.1", 
			      PeerPort => "80", 
			      Type => SOCK_STREAM,
			      Protocol => "tcp");

if(! $s){
    print("connection failed");
    exit;
}

print $s "GET /server-status HTTP/1.0\n\n";

$s->read($res,1050);
return $s;

}

sub getparam
{

$i=$_[0];
#print "asked $i\n";

$v=$params{$i};
#print ("= $v : $i\n");
if ( $res =~ m/$v/s ){
    
    $val=$1;
    if($2 eq MB){
	$val*=1024;
    }

    if($2 eq GB){
	$val*=(1024*1024);
    }

#    print ("$i - $val");
#    print ("\n");    
    return $val;    
#}
}

}
