Copyright (C) 2011 Sergey Dvoriancev Copyright (C) 2015 ESF, LLC All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* Squid Proxy Server realtime stats */ require_once('guiconfig.inc'); require_once('sqstat.class.php'); // init $squidclass = new squidstat(); /* * Requests */ /* AJAX response */ if ($_REQUEST['getactivity']) { header("Content-type: text/javascript"); echo sqstat_AJAX_response( $_REQUEST ); exit; } /* * HTML Page */ $pgtitle = "Squid Proxy Server: Realtime Stats (SQStat)"; require_once("head.inc"); $csrf_token = csrf_get_tokens(); ?> query_exec(); } if ($squidclass->errno == 0) { $data = sqstat_resultHTML($data); } else { // error $data = sqstat_errorHTML(); } ?>
query_exec(); $ver = sqstat_serverInfoHTML(); $res .= "el('sqstat_serverver').innerHTML = '$ver';"; $time = date("h:i:s d/m/Y"); $res .= "el('sqstat_updtime').innerHTML = '$time';"; $data = sqstat_resultHTML( $data ); if ($squidclass->errno == 0) { $data = sqstat_AJAX_prep($data); $res .= "el('sqstat_result').innerHTML = '$data';"; } else { // error $res .= sqstat_AJAX_error(sqstat_errorHTML()); } return $res; } function sqstat_AJAX_prep($text) { $text = str_replace("'", "\'", $text); $text = str_replace("\n", "\\r\\n", $text); return $text; } function sqstat_AJAX_error($err) { $err = sqstat_AJAX_prep($err); $t .= "el('sqstat_result').innerHTML = '$err';"; return $t; } /* *Reports */ function sqstat_headerHTML() { global $squidclass; $date = date("h:i:s d/m/Y"); $squidinfo = sqstat_serverInfoHTML(); if (empty($squidclass->autorefresh)) { $squidclass->autorefresh = 0; } return <<< EOD
Squid RealTime stat {$squidclass->sqstat_version} for the {$servers} proxy server {$squidinfo}.
Auto refresh: sec. Created at: {$date}
EOD; } function sqstat_serverInfoHTML() { global $squidclass; return $squidclass->server_version . " ({$squidclass->squidhost}:{$squidclass->squidport})"; } function sqstat_resultHTML($data) { global $squidclass; $group_by_name = $squidclass->group_by_name; $use_js = true; $t = array(); // table header $t[] = ""; $t[] = ""; $t[] = ""; if ($squidclass->use_sessions) { $t[] = ""; } $t[] = ""; $t[] = ""; // table body if (is_array($data['users'])) { $tbl = array(); $con_color = 0; foreach($data['users'] as $key => $v) { // skip total info if ($key == 'total') { continue; } // group row $tbl[] = ""; $tbl[] = ""; $tbl[] = ""; $tbl[] = ""; // connections row foreach ($v['con'] as $con) { if ($use_js) { $js = "onMouseout='hideddrivetip()' onMouseover='ddrivetip(\"" . $squidclass->implode_with_keys($con,"
") . "\")'"; } else { $js=''; } // begin new row $class = (++$con_color % 2 == 0) ? " class='odd'" : ""; $tbl[] = ""; // URL $uri = "{$con['uritext']}"; $tbl[] = ""; $tbl[] = ""; // speed if ($squidclass->use_sessions) { $cur_s = round($con['cur_speed'], 2) > 0 ? sprintf("%01.2f KB/s", $con['cur_speed']) : ''; $avg_s = round($con['avg_speed'], 2) > 0 ? sprintf("%01.2f KB/s", $con['avg_speed']) : ''; $tbl[] = ""; $tbl[] = ""; } // file size $filesize = $squidclass->filesize_format($con["bytes"]); $duration = $squidclass->duration($con["seconds"], "short"); $tbl[] = ""; $tbl[] = ""; // end row $tbl[] = ""; } // total user speed if ($squidclass->use_sessions) { $user_curr = sprintf("%01.2f KB/s", $v['user_curr']); $user_avg = sprintf("%01.2f KB/s", $v['user_avg']); $tbl[] =""; $tbl[] =""; $tbl[] =""; $tbl[] =""; $tbl[] =""; } } // status row $stat = array(); $ausers = sprintf("%d", $data['ausers']); $acon = sprintf("%d", $data['acon']); $stat[] = ""; if ($squidclass->use_sessions) { $total_curr = sprintf("%01.2f", $data['total_curr']); $total_avg = sprintf("%01.2f", $data['total_avg']); $stat[] = ""; } else { $stat[] = ""; } $t[] = ""; } // ENDIF (is_array($data['users'])) if ($ausers == 0) { $t[] = ""; } else { $stat = implode("\n", $stat); $tbl = implode("\n", $tbl); $t[] = $stat . $tbl . $stat; } $t[] = "
{$group_by_name}URICurr. SpeedAvg. SpeedSizeTime
" . (is_int($key) ? long2ip($key) : $key) . " 
{$uri}{$cur_s}{$avg_s}{$filesize}{$duration}
{$user_curr}{$user_avg}
Total:{$ausers} users and {$acon} connections @ {$total_curr}/{$total_avg} KB/s (CURR/AVG){$ausers} users and {$acon} connections
No active connections
"; $t[] = "

Report based on SQStat © Alex Samorukov, 2006

"; return implode("\n", $t); } function sqstat_errorHTML() { global $squidclass; $t = array(); // table header $t[] = ""; $t[] = ""; $t[] = ""; $t[] = "
SqStat error
"; $t[] = '

Error (' . $squidclass->errno . '): ' . $squidclass->errstr . '

'; $t[] = "
"; return implode ("\n", $t); } function sqstat_loadconfig() { global $squidclass, $config; $squidclass->errno = 0; $squidclass->errstr = ''; $squidclass->sqstat_version = SQSTAT_VERSION; /* Load config from pfSense */ $iface = '127.0.0.1'; $iport = 3128; if (is_array($config['installedpackages']['squid']['config'][0])) { $squid_settings = $config['installedpackages']['squid']['config'][0]; } else { $squid_settings = array(); } // Squid interface IP & port $realif = array(); $iface = ($squid_settings['active_interface'] ? $squid_settings['active_interface'] : 'lan'); $iface = explode(",", $iface); foreach ($iface as $i => $if) { $realif[] = sqstat_get_real_interface_address($if); $iface = $realif[$i][0] ? $realif[$i][0] : '127.0.0.1'; } $iport = $squid_settings['proxy_port'] ? $squid_settings['proxy_port'] : 3128; $squidclass->squidhost = $iface; $squidclass->squidport = $iport; $squidclass->group_by = "host"; $squidclass->resolveip = true; $squidclass->hosts_file = ''; // hosts file not used $squidclass->autorefresh = 3; // refresh 3 secs by default $squidclass->cachemgr_passwd = ''; // Load hosts file if defined if (!empty($squidclass->hosts_file)) { $squidclass->load_hosts(); } return $squidclass->errno; } function sqstat_get_real_interface_address($iface) { global $config; $iface = convert_friendly_interface_to_real_interface_name($iface); $line = trim(shell_exec("ifconfig $iface | grep inet | grep -v inet6")); list($dummy, $ip, $dummy2, $netmask) = explode(" ", $line); return array($ip, long2ip(hexdec($netmask))); } ?>