#!/usr/bin/php
<?php
/*
Copyright (C) 2011  Glen Pitt-Pladdy

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


See: http://www.pitt-pladdy.com/blog/_20111222-165926_0000_LAMP_PHP_Pagetimer_for_Cacti_via_SNMP/ 
*/

// run with args: <mode> [modeargs]
// modes:
//	histogram[log|raw|rawlog] <top(seconds)> <bandnumber(0-10)>
//	pagetimes <portion%>						- returns time in seconds
//	pagetimes <portion%> [portion%] [...]		- returns the list of portions
//	filters
//	milestones
//
/* Add this to snmpd.conf
extend pagetimerfilters /etc/snmp/getpagetimer.php filters
extend pagetimermilestones /etc/snmp/getpagetimer.php milestones
extend pagetimerhistlevels /etc/snmp/getpagetimer.php histogramlog 1 levels
extend pagetimerhist0 /etc/snmp/getpagetimer.php histogramlog 1 0
extend pagetimerhist1 /etc/snmp/getpagetimer.php histogramlog 1 1
extend pagetimerhist2 /etc/snmp/getpagetimer.php histogramlog 1 2
extend pagetimerhist3 /etc/snmp/getpagetimer.php histogramlog 1 3
extend pagetimerhist4 /etc/snmp/getpagetimer.php histogramlog 1 4
extend pagetimerhist5 /etc/snmp/getpagetimer.php histogramlog 1 5
extend pagetimerhist6 /etc/snmp/getpagetimer.php histogramlog 1 6
extend pagetimerhist7 /etc/snmp/getpagetimer.php histogramlog 1 7
extend pagetimerhist8 /etc/snmp/getpagetimer.php histogramlog 1 8
extend pagetimerhist9 /etc/snmp/getpagetimer.php histogramlog 1 9
extend pagetimerhist10 /etc/snmp/getpagetimer.php histogramlog 1 10
extend pagetimertimes0 /etc/snmp/getpagetimer.php pagetimes 90
extend pagetimertimes1 /etc/snmp/getpagetimer.php pagetimes 99
extend pagetimertimes2 /etc/snmp/getpagetimer.php pagetimes 100
extend pagetimertimes /etc/snmp/getpagetimer.php pagetimes 90 99 100
*/


#require_once ( "pagespeed.php" );
require_once ( "/usr/lib/php5/www.mozgq.com/pagetimer.php" );
$pagetimer = new pagetimer ();
$pagetimer->clearold ( 1800 );		// older than 30 min
$pagetimer->clearcache ( 86400 );	// older than 1 day
// check if we have a filter
$filters = array ();
if ( is_file ( '/etc/pagetimer/filters.php' ) ) {
	require ( '/etc/pagetimer/filters.php' );
}
// base (required) filters
$filters['allPages'] = array ( 'table'=>'Pages', 'condition'=>array () );
// get this data
if ( ! isset ( $argv[1] ) ) {
		die ( "Need \"mode\" and relevant mode arguments\n" );
}
$raw = true;
switch ( $argv[1] ) {
	case 'histogram':
	case 'histogramlog':
		$raw = false;
	case 'histogramraw':
	case 'histogramrawlog':
		$bands = 10;
		if ( ! isset ( $argv[2] ) or ! isset ( $argv[3] )
			or ! is_numeric ( $argv[2] )
			or ( ! is_numeric ( $argv[3] ) and $argv[3] != 'levels' ) ) {
			die ( "hisstograms require \"top (seconds)\" and \"bandnumber\"\n" );
		}
		if ( $argv[3] != 'levels' ) {
			foreach ( $pagetimer->histogramall ( $argv[1], $argv[3], $filters, $argv[2], $bands ) as $data ) {
				list ( $value, $total ) = $data;
				if ( $raw ) {
					echo "$value\n";
				} else {
					if ( $total > 0 ) {
						echo ( 100 * $value / $total )."\n";
					} else {
						echo "0\n";
					}
				}
			}
#			foreach ( $filters as $filter ) {
#				list ( $value, $total ) = $pagetimer->histogram ( $argv[1], $argv[3], $argv[2], $filter, $bands );
#				if ( $raw ) {
#					echo "$value\n";
#				} else {
#					if ( $total > 0 ) {
#						echo ( 100 * $value / $total )."\n";
#					} else {
#						echo "0\n";
#					}
#				}
#			}
		} else {
			foreach ( $pagetimer->histogram ( $argv[1], $argv[3], $argv[2], array(), $bands ) as $band ) {
					echo "$band\n";
			}
		}
		break;
	case 'pagetimes':
		if ( ! isset ( $argv[2] ) or ! is_numeric ( $argv[2] ) ) {
			die ( "pagetimes require \"portion\" in % (ie. between 0 - 100)\n" );
		} elseif ( isset ( $argv[3] ) and is_numeric ( $argv[3] ) ) {
			// we have a list of portions - just return them
			for ( $i = 2; isset ( $argv[$i] ) and is_numeric ( $argv[$i] ); ++$i ) {
				echo "$argv[$i]\n";
			}
		} else {
			// we need to get this proportion
			foreach ( $pagetimer->pagetimesall ( $argv[2]/100, $filters ) as $time ) {
				if ( $time !== NULL	) {
					echo "$time\n";
				} else {
					echo "U\n";
				}
			}
#			foreach ( $filters as $filter ) {
#				$time = $pagetimer->pagetimes ( $argv[2]/100, $filter );
#				if ( isset ( $time ) ) {
#					echo "$time\n";
#				} else {
#					echo "U\n";
#				}
#			}
		}
		break;
	case 'filters':
		foreach ( array_keys ( $filters ) as $filter ) {
			echo "$filter\n";
		}
		break;
	default:
		die ( "Unknown \"mode\": $argv[1]\n" );
		break;
}




?>

