sqstat_version = SQSTAT_VERSION; $this->squidhost = '127.0.0.1'; $this->squidport = '3128'; $This->group_by = 'host'; $this->resolveip = true; $this->hosts_file = ''; $this->autorefresh = 0; $this->cachemgr_passwd = ''; $errno = 0; $errstr = ''; if (!function_exists("preg_match")) { $this->errorMsg(5, 'You need to install PHP pcre extension to run this script'); $this->showError(); exit(5); } // we need session support to gather avg. speed if (function_exists("session_start")){ $this->use_sessions=true; } } function formatXHTML($body, $refresh, $use_js = false){ $text=''."\n". ''."\n" .'' .'
' .''; if($refresh) $text.=''; $text.='© Alex Samorukov, 2006
'; return $this->formatXHTML($text,$refresh,$use_js); } function parseRequest($data, $group_by = 'host', $resolveip = false) { $parsed = array(); if ($this->use_sessions) { session_name('SQDATA'); session_start(); } # resort data array $users = array(); switch ($group_by) { case "username": $group_by_name = "User"; $group_by_key = "username"; break; case "host": default: $group_by_name = "Host"; $group_by_key = "peer"; break; } # resolve IP & group foreach ($data["con"] as $key => $v) { # skip myself if (substr($v["uri"], 0, 13) == "cache_object:") continue; $ip = substr($v["peer"], 0, strpos($v["peer"], ":")); $v["peer"] = $ip; # name from hosts if (isset($this->hosts[$ip])) { $ip = $this->hosts[$ip]; } else # i use ip2long() to make ip sorting work correctly if ($resolveip) { $hostname = gethostbyaddr($ip); if ($hostname == $ip) $ip = ip2long($ip); # resolve failed. use (ip2long) key else $ip = $hostname; } else { $ip = ip2long(substr($v["peer"], 0, strpos($v["peer"], ":"))); } $v['con_id'] = $key; $v["username"] = isset($v["username"]) ? $v["username"] : "N/A"; # users [key => conn_array] $users[$v[$group_by_key]][] = $v; } ksort($users); unset($session_data); if (isset($_SESSION['time']) && ((time() - $_SESSION['time']) < 3*60) && isset($_SESSION['sqdata']) && is_array($_SESSION['sqdata'])) { # only if the latest data was less than 3 minutes ago $session_data = $_SESSION['sqdata']; } # users count & con cont $ausers = $acon = 0; $total_avg = $total_curr = 0; foreach ($users as $key => $v) { $ausers++; $user_avg = $user_curr = $con_color = 0; foreach ($v as $con_key => $con){ $cres = array(); $acon++; $uritext = $con["uri"]; if (substr($con["uri"], 0, 7) == "http://" || substr($con["uri"], 0, 6) == "ftp://") { if (strlen($uritext) > SQSTAT_SHOWLEN) $uritext = htmlspecialchars(substr($uritext, 0, SQSTAT_SHOWLEN)) . ' ....'; } else $uritext = htmlspecialchars($uritext); $cres['uritext'] = $uritext; $cres['uri'] = $con["uri"]; # speed stuff $con_id = $con['connection']; $is_time = time(); $curr_speed = $avg_speed = 0; if (isset($session_data[$con_id]) && $con_data = $session_data[$con_id] ) { # if we have info about current connection, we do analyze its data # current speed $was_time = $con_data['time']; $was_size = $con_data['size']; if ($was_time && $was_size) { $delta = $is_time - $was_time; if ($delta == 0) { $delta = 1; } if ($con['bytes'] >= $was_size) { $curr_speed = ($con['bytes'] - $was_size) / 1024 / $delta; } } else { $curr_speed = $con['bytes'] / 1024; } # avg speed $avg_speed = $con['bytes'] / 1024; if ($con['seconds'] > 0) { $avg_speed /= $con['seconds']; } } $cres['cur_speed'] = $curr_speed; $cres['avg_speed'] = $avg_speed; $cres['seconds'] = $con["seconds"]; $cres['bytes'] = $con["bytes"]; # groupped parsed[key => conn_key] $parsed['users'][$key]['con'][$con_key] = $cres; # for sessions $new_data[$con_id]['time'] = $is_time; $new_data[$con_id]['size'] = $con['bytes']; # sum speeds $total_avg += $avg_speed; $user_avg += $avg_speed; $total_curr += $curr_speed; $user_curr += $curr_speed; } # total per user $parsed['users'][$key]['user_curr'] = $user_curr; $parsed['users'][$key]['user_avg'] = $user_avg; } # total info $parsed['ausers'] = $ausers; $parsed['acon'] = $acon; $parsed['total_avg'] = $total_avg; $parsed['total_curr'] = $total_curr; # update session info $_SESSION['time'] = time(); if (isset($new_data)) $_SESSION['sqdata'] = $new_data; return $parsed; } function errorMsg($errno, $errstr) { $this->errno = $errno; $this->errstr = $errstr; } function load_hosts() { # loading hosts file $hosts_array = array(); if (!empty($this->hosts_file)) { if (is_file($this->hosts_file)) { $handle = @fopen($this->hosts_file, "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle, 4096); unset($matches); if (preg_match('/^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})[ \t]+(.+)$/i', $buffer, $matches)) { $hosts_array[$matches[1]]=$matches[2]; } } fclose($handle); } $this->hosts = $hosts_array; } else { #error $this->errorMsg(4, "Hosts file not found. Cant read '{$this->hosts_file}'."); return $this->errno; } } return 0; } function query_exec() { $data = ""; $this->server_version = '(unknown)'; if ($this->connect($this->squidhost, $this->squidport)) { $data = $this->makeQuery($this->cachemgr_passwd); if ($this->errno == 0) { $this->server_version = $data['server_version']; $data = $this->parseRequest($data, 'host', true); } } return $data; } } ?>