Problem with a script in PHP, it is instable, some help?

Post support questions that directly relate to Linux/Unix operating systems.

Moderators: Developers, Moderators

Post Reply
apereira
Posts: 22
Joined: Mon Sep 29, 2008 9:58 am

Problem with a script in PHP, it is instable, some help?

Post by apereira »

I found a script on a blog, that make a page load statistics, its easy to configure, but is instable too, sometime it returns all parameters and other times it returns just one parameter.

I'll post here this script, and explain more what are running here.

PHP: class.pageload.php

Code: Select all

<?php
class PageLoad {

	var $siteURL = "";
	var $pageInfo = "";
	
	/*
	* sets the URLs to check for loadtime into an array $siteURLs
	*/
	function setURL($url) {
		if (!empty($url)) {
			$this->siteURL = $url;
			return true;
		}
		return false;
	}
	
	/*
	* extract the header information of the url 
	*/
	function doPageLoad() {
		$u = $this->siteURL;
		if(function_exists('curl_init') && !empty($u)) {
			$ch = curl_init($u);
			curl_setopt($ch, CURLOPT_HEADER, true);
			curl_setopt($ch, CURLOPT_ENCODING, "gzip");
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_NOBODY, false);
			curl_setopt($ch, CURLOPT_FRESH_CONNECT, false); 
			
			curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
			$pageBody = curl_exec($ch);
			$this->pageInfo = curl_getinfo($ch);
			curl_close ($ch);
			
			return true;
		}
		return false;
	}
	
		
	/*
	* compile the page load statistics only
	*/
	function getPageLoadStats() {
		$info = $this->pageInfo;

		//stats from info
/*		$s['dest_url'] = $info['url'];
		$s['content_type'] = $info['content_type'];
		$s['http_code'] = $info['http_code'];
		$s['total_time'] = $info['total_time'];
		$s['size_download'] = $info['size_download'];
		$s['speed_download'] = $info['speed_download'];
		$s['redirect_count'] = $info['redirect_count'];
		$s['namelookup_time'] = $info['namelookup_time'];
		$s['connect_time'] = $info['connect_time'];
		$s['pretransfer_time'] = $info['pretransfer_time'];
		$s['starttransfer_time'] = $info['starttransfer_time'];
*/
		return $info;
	}	
}
?>

PHP: pageload-agent.php

Code: Select all

<?php
//include the class
include_once 'class.pageload.php';
// read in an argument - must make sure there's an argument to use
if ($argc==2) {

	//read in the arg.
	$url_argv = $argv[1];
	if (!eregi('^https://', $url_argv)) {
		$url_argv = "https://$url_argv";
	}
	// check that the arg is not empty
	if ($url_argv!="") {

		//initiate the results array
		$results = array();
			
		//initiate the class
		$lt = new PageLoad();
			
		//set the page to check the loadtime
		$lt->setURL($url_argv);
				
		//load the page
		if ($lt->doPageLoad()) {
			//load the page stats into the results array
			$results = $lt->getPageLoadStats();
		} else {
			//do nothing
			print "";
		}
		
		//print out the results
		if (is_array($results)) {
			//expecting only one record as we only passed in 1 page.
			$output = $results;
			
			print "dns:".$output['namelookup_time'];
			print " con:".$output['connect_time'];
			print " pre:".$output['pretransfer_time'];
			print " str:".$output['starttransfer_time'];
			print " ttl:".$output['total_time'];
			print " sze:".$output['size_download'];
			print " spd:".$output['speed_download'];
		} else {
			//do nothing
			print "";
		}
	}
} else {
	//do nothing
	print "";
}
?>

Let's go...
This is a PHP script, just need a Data Input Method, Data Source and the Graph to get its working, right...

In this script on pageload-agent.php return 5 parameters (con, dns, ttl, str, pre), when it is runing on command line, allways return all parameters.... but when its runing on Cacti, it return sometimes all parameters... but almost all polling just return the parameter "dns".
So, on graph I have gaps just in some parameters, and DNS allways work. I'll atach a ss to check waht I m talking about.

What can be the reason of this problem? I made all step by step, I've checked all points... and its allright... Well this script can be very usefull if works.

The blog where I found this script is:
http://www.askaboutphp.com/tutorials/44 ... art-1.html


Hope someone can understand this problem and help me.
Thx!
Attachments
The gap on graph
The gap on graph
Page Load Time GAP.JPG (43.03 KiB) Viewed 2732 times
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Post by gandalf »

Please _always_ spit out the parms on a single "print" statement. Cacti will not wait 'til the next "print" occurs
Reinhard
HV
Posts: 12
Joined: Mon Apr 23, 2007 5:44 am

Post by HV »

Solution is here:
http://www.karlesnine.com/post/2009/12/ ... ce-de-site
In the script pageload-agent.php you have to change these:
print ”dns:”.$output['namelookup_time'];
print ” con:”.$output['connect_time'];
print ” pre:”.$output['pretransfer_time'];
print ” str:”.$output['starttransfer_time'];
print ” ttl:”.$output['total_time'];
print ” sze:”.$output['size_download'];
print ” spd:”.$output['speed_download']

by these:
$outstring = "ttl:".$output['total_time']." str:".$output['starttransfer_time']." dns:".$output['namelookup_time'].
" con:".$output['connect_time']." pre:".$output['pretransfer_time']." sze:".$output['size_download'].
" spd:".$output['speed_download'];

print chop($outstring,”\n\r”);
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Post by gandalf »

This is what I tried to express
R.
Runan
Posts: 14
Joined: Fri Nov 13, 2009 2:08 am

Pageload problem with yardstick

Post by Runan »

I had the same problem and the new script fix it !

Thanks :D

I have two websites in monitoring www.google.fr and another.
Graphs aren't in the same yardstick.

When I want to configure a graph with two lines (1 line by website ttl), I see bad values for google.

For the ohter site data are in m and in the google data without m. So data for google are not in the same yardstick but there are in the same grah.

How it is possible ?
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Post by gandalf »

m denotes "milli" = 1/1000
R.
Runan
Posts: 14
Joined: Fri Nov 13, 2009 2:08 am

Post by Runan »

I understand m is for millisecond but when I want to graph google the data seems to be incorrect because data for google is too short ...

Anyone tries ?
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests