--- lighttpd_stats_1.0.orig/ss_lighttpd_stats.php 2007-02-13 23:47:00.000000000 +0200 +++ lighttpd_stats_1.0/ss_lighttpd_stats.php 2008-10-05 20:11:44.391167671 +0300 @@ -1,3 +1,4 @@ +#!/usr/bin/php // // As a script server object: -// ss_lighttpd_stats.php ss_apache_stats +// ss_lighttpd_stats.php ss_lighttpd_stats // // -/* display no errors */ -error_reporting(0); +/* do NOT run this script through a web browser */ +if (!isset($_SERVER["argv"][0]) || isset($_SERVER['REQUEST_METHOD']) || isset($_SERVER['REMOTE_ADDR'])) { + die("
This script is only meant to run at the command line."); +} + +$no_http_headers = true; if (!isset($called_by_script_server)) { - include_once(dirname(__FILE__) . "/../include/config.php"); + include_once(dirname(__FILE__) . "/../include/global.php"); array_shift($_SERVER["argv"]); print call_user_func_array("ss_lighttpd_stats", $_SERVER["argv"]); } @@ -47,8 +52,8 @@ $status = lighttpdStatus::getStatus($host); $return = ''; - foreach($variables as $status_var => $cacti_var){ - if(isset($status[$status_var])){ + foreach ($variables as $status_var => $cacti_var){ + if (isset($status[$status_var])) { $return .= sprintf('%s:%s ', $cacti_var, $status[$status_var]); } } @@ -84,7 +89,7 @@ throw new Exception('Host not found'); } - $fh = @fsockopen($address, 80, $errno, $errstr, self::QUERY_TIMEOUT); + $fh = fsockopen($address, 80, $errno, $errstr, self::QUERY_TIMEOUT); if(!$fh || !is_resource($fh)){ throw new Exception($errstr, $errno); @@ -112,20 +117,24 @@ throw new Exception(''); } - $lines = explode("\n", $status); + list($headers, $body) = explode("\r\n\r\n", $status); + $headers = explode("\r\n", $headers); // check response code // should be "HTTP/1.1 200 OK" - if(trim($lines[0]) != 'HTTP/1.1 200 OK'){ - throw new Exception($lines[0]); + // can be also "HTTP/1.0 200 OK" if routed via proxy + $http_status = trim($headers[0]); + if ($http_status != 'HTTP/1.0 200 OK' && $http_status != 'HTTP/1.1 200 OK'){ + throw new Exception($http_status); } $vars = array(); - foreach($lines as $line){ + $body = explode("\n", $body); + foreach($body as $line){ $line = trim($line); - if(preg_match('/(.+): (.+)/', $line, $matches)){ + if (preg_match('/^(.+): (.+)/', $line, $matches)){ $vars[$matches[1]] = intval($matches[2]); } } @@ -137,5 +146,3 @@ } } - -?>