function show_list() { $tpl = new templates(); $page = 1; $FORCE_FILTER = null; $total = 0; $cpu = $_GET["cpu"]; include_once dirname(__FILE__) . "/ressources/class.squid.manager.inc"; $cache_manager = new cache_manager(); $datas = explode("\n", $cache_manager->makeQuery("ntlmauthenticator")); if (!$cache_manager->ok) { json_error_show("Err!"); } $CPU_NUMBER = 0; while (list($num, $ligne) = each($datas)) { if (preg_match("#by kid([0-9]+)#", $ligne, $re)) { $CPU_NUMBER = $re[1]; continue; } if (!preg_match("#([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+(.*?)\\s+([0-9\\.]+)#", $ligne, $re)) { continue; } $pid = $re[3]; $rqs = $re[4]; $rply = $re[5]; $flags = $re[6]; $time = $re[7]; $MAIN[$CPU_NUMBER][$pid]["PID"] = $pid; $MAIN[$CPU_NUMBER][$pid]["RQS"] = $rqs; $MAIN[$CPU_NUMBER][$pid]["RPLY"] = $rply; $MAIN[$CPU_NUMBER][$pid]["FLAG"] = $flags; $MAIN[$CPU_NUMBER][$pid]["TIME"] = $time; } //print_r($MAIN); $data = array(); $data['page'] = 1; $data['total'] = count($MAIN[$cpu]); $data['rows'] = array(); while (list($pid, $ligne) = each($MAIN[$cpu])) { $md5 = md5(serialize($ligne)); $PID = numberFormat($ligne["PID"], 0, "", " "); $RQS = numberFormat($ligne["RQS"], 0, "", " "); $RPLY = numberFormat($ligne["RPLY"], 0, "", " "); $FLAG = $ligne["FLAG"]; $TIME = $ligne["TIME"]; $data['rows'][] = array('id' => $md5, 'cell' => array("<span style='font-size:16px'>{$PID}</a></span>", "<span style='font-size:16px'>{$RQS}</a></span>", "<span style='font-size:16px'>{$RPLY}</span>", "<span style='font-size:16px'>{$RQS}</span>", "<span style='font-size:16px'>{$FLAG}</span>", "<span style='font-size:16px'>{$TIME}</span>")); } echo json_encode($data); }
function _ntmlauthenticators() { include_once dirname(__FILE__) . "/ressources/class.squid.manager.inc"; $cache_manager = new cache_manager(); $datas = explode("\n", $cache_manager->makeQuery("ntlmauthenticator")); if (!$cache_manager->ok) { return; } $CPU_NUMBER = 0; while (list($num, $ligne) = each($datas)) { if (preg_match("#by kid([0-9]+)#", $ligne, $re)) { $CPU_NUMBER = $re[1]; continue; } if (preg_match("#number active: ([0-9]+) of ([0-9]+)#", $ligne, $re)) { $Active = intval($re[1]); $Max = intval($re[2]); $prc = round($Active / $Max * 100); $ARRAY[$CPU_NUMBER] = $prc; } } return $ARRAY; }
function CACHES_AVG() { $unix = new unix(); $cache_manager = new cache_manager(); $data = $cache_manager->makeQuery("storedir", true); $StoreDir = null; foreach ($data as $ligne) { if (preg_match("#Current Capacity.*?:\\s+(.+?)% used#", $ligne, $re)) { @file_put_contents("{$GLOBALS["BASEDIR"]}/CACHES_AVG", $re[1]); @chmod("{$GLOBALS["BASEDIR"]}/CACHES_AVG", 0777); } if (preg_match("#Store Directory.*?:(.+)#", $ligne, $re)) { $StoreDir = trim($re[1]); continue; } if (preg_match("#Percent Used:\\s+([0-9\\.]+)%#", $ligne, $re)) { if ($StoreDir == null) { continue; } $dats[$StoreDir]["PERC"] = $re[1]; continue; } if (preg_match("#Maximum Size:\\s+([0-9\\.]+)#", $ligne, $re)) { if ($StoreDir == null) { continue; } $dats[$StoreDir]["SIZE"] = $re[1]; continue; } if (preg_match("#Current Size:\\s+([0-9\\.]+)#", $ligne, $re)) { if (isset($dats[$StoreDir]["USED"])) { continue; } if ($GLOBALS["OUTPUT"]) { echo "* * * * * * * * *\nDISK \"{$ligne}\"\nDISK {$StoreDir} Current Size: {$re[1]}\n* * * * * * * * *\n"; } $dats[$StoreDir]["USED"] = $re[1]; continue; } if (preg_match("#Current entries:\\s+([0-9\\.]+)\\s+([0-9\\.]+)%#", $ligne, $re)) { if ($GLOBALS["OUTPUT"]) { echo "* * * * * * * * *\nDISK \"{$ligne}\"\nDISK {$StoreDir} Current entries:{$re[1]} {$re[2]}%\n* * * * * * * * *\n"; } if ($StoreDir == null) { continue; } $dats[$StoreDir]["ENTRIES"] = $re[1]; $dats[$StoreDir]["PERC"] = $re[2]; continue; } if (preg_match("#Filesystem Space in use:\\s+([0-9]+)\\/#", $ligne, $re)) { if ($StoreDir == null) { continue; } if (isset($dats[$StoreDir]["USED"])) { continue; } if ($GLOBALS["OUTPUT"]) { echo "* * * * * * * * *\nDISK \"{$ligne}\"\nDISK (2) {$StoreDir} Current Size: {$re[1]}\n* * * * * * * * *\n"; } $dats[$StoreDir]["USED"] = $re[1]; } } $q = new mysql(); while (list($directory, $arrayStore) = each($dats)) { $arrayStore["USED"] = intval($arrayStore["USED"]); $arrayStore["PERC"] = intval($arrayStore["PERC"]); if ($directory == "MEM") { continue; } if ($arrayStore["USED"] == 0) { continue; } $PERC = $arrayStore["PERC"]; $USED = $arrayStore["USED"]; if (preg_match("#\\/home\\/squid\\/cache\\/MemBooster([0-9]+)#", $directory, $re)) { $sql = "UPDATE squid_caches_center SET percentcache='{$PERC}',percenttext='{$PERC}', `usedcache`='{$USED}' WHERE ID={$re[1]}"; echo $sql . "\n"; $q->QUERY_SQL($sql, "artica_backup"); continue; } if ($GLOBALS["VERBOSE"]) { echo "{$directory} -> {$USED} / {$PERC}%\n"; } $sql = "UPDATE squid_caches_center SET percentcache='{$PERC}',percenttext='{$PERC}', `usedcache`='{$USED}' WHERE `cache_dir`='{$directory}'"; echo $sql . "\n"; $q->QUERY_SQL($sql, "artica_backup"); } }
function TEST_PORT($aspid = false) { $sock = new sockets(); $unix = new unix(); $suffix = null; if (isset($GLOBALS["BY_CRON"])) { $suffix = "[CRON] "; } $nice = $unix->EXEC_NICE(); $php5 = $unix->LOCATE_PHP5_BIN(); if (!is_file("/etc/cron.d/squid-watch-sockets")) { $me = __FILE__; $cmdline = trim("{$nice} {$php5} {$me} --test-port --cron"); $f[] = "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin:/usr/share/artica-postfix/bin"; $f[] = "MAILTO=\"\""; $f[] = "0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * root {$cmdline} >/dev/null 2>&1"; $f[] = ""; @file_put_contents("/etc/cron.d/squid-watch-sockets", @implode("\n", $f)); shell_exec("/etc/init.d/cron reload"); } $f = array(); $SquidPerformance = intval($sock->GET_INFO("SquidPerformance")); if (is_file("/etc/cron.d/squid-statsmembers5mn")) { @unlink("/etc/cron.d/squid-statsmembers5mn"); } $f = array(); if (!is_file("/etc/cron.d/squid-statsclean")) { $cmdline = trim("{$nice} {$php5} " . dirname(__FILE__) . "/exec.influxdb.php --clean"); $f[] = "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin:/usr/share/artica-postfix/bin"; $f[] = "MAILTO=\"\""; $f[] = "0 1,5 * * * root {$cmdline} >/dev/null 2>&1"; $f[] = ""; @file_put_contents("/etc/cron.d/squid-statsclean", @implode("\n", $f)); shell_exec("/etc/init.d/cron reload"); } $php5 = $unix->LOCATE_PHP5_BIN(); if ($aspid) { $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; $pid = $unix->get_pid_from_file($pidfile); if ($unix->process_exists($pid, basename(__FILE__))) { $time = $unix->PROCCESS_TIME_MIN($pid); Events("{$suffix}A task already exists pid {$pid} and running since {$time}mn"); return; } } Events("{$suffix}Testing Manager port...."); @file_put_contents($pidfile, getmypid()); $MonitConfig = unserialize(base64_decode($sock->GET_INFO("SquidWatchdogMonitConfig"))); $MonitConfig = watchdog_config_default($MonitConfig); if ($MonitConfig["TEST_PORT"] == 0) { Events("Test port is disabled, aborting"); return; } if ($MonitConfig["watchdog"] == 0) { Events("Watchog is disabled, aborting"); return; } if (!isset($MonitConfig["TEST_PORT_MAX"])) { $MonitConfig["TEST_PORT_MAX"] = 5; } if (!isset($MonitConfig["TEST_PORT_INTERVAL"])) { $MonitConfig["TEST_PORT_INTERVAL"] = 2; } if (!isset($MonitConfig["TEST_PORT_TIMEOUT"])) { $MonitConfig["TEST_PORT_TIMEOUT"] = 2; } if (!isset($MonitConfig["TEST_PORT_RESTART"])) { $MonitConfig["TEST_PORT_RESTART"] = 1; } if (!isset($MonitConfig["MAX_RESTART"])) { $MonitConfig["MAX_RESTART"] = 2; } if ($MonitConfig["TEST_PORT_MAX"] == 0) { $MonitConfig["TEST_PORT_MAX"] = 5; } if ($MonitConfig["TEST_PORT_TIMEOUT"] == 0) { $MonitConfig["TEST_PORT_TIMEOUT"] = 2; } $MAX_RESTART = $MonitConfig["MAX_RESTART"]; $SquidPortTimeOudRestart = intval($sock->GET_INFO("SquidPortTimeOudRestart")); $squidpid = SQUID_PID(); $timemin = $unix->PROCCESS_TIME_MIN($squidpid); Events("{$suffix}Squid running since {$timemin}mn"); if ($GLOBALS["VERBOSE"]) { echo "Process Running since {$timemin}mn\n"; } if ($timemin < 2) { Events("{$suffix}Can test a port only if Squid run after 2mn , current {$timemin}mn"); return; } $cache_manager = new cache_manager(); $cache_manager->TimeOutsec = $MonitConfig["TEST_PORT_TIMEOUT"]; $data = $cache_manager->makeQuery("info"); if ($cache_manager->ok) { if ($GLOBALS["VERBOSE"]) { echo "{$data}\n"; } if ($GLOBALS["VERBOSE"]) { echo "Process Running since {$timemin}mn\n"; } $sock->SET_INFO("SquidPortTimeOudRestart", 0); return; } Events("{$suffix}Cache Manager report failed with error {$cache_manager->errstr}"); PROXY_TESTS_PORTS_EVENTS("{$suffix}Cache Manager report failed with error {$cache_manager->errstr}"); sleep($MonitConfig["TEST_PORT_INTERVAL"]); for ($i = 1; $i < $MonitConfig["TEST_PORT_MAX"]; $i++) { $cache_manager = new cache_manager(); $cache_manager->TimeOutsec = $MonitConfig["TEST_PORT_TIMEOUT"]; $data = $cache_manager->makeQuery("info"); if ($cache_manager->ok) { squid_admin_mysql(2, "{$suffix}Manager port retreive information after {$i} attempts", null, __FILE__, __LINE__); $sock->SET_INFO("SquidPortTimeOudRestart", 0); @unlink("/var/log/artica.proxy.watchdog.test.ports.log"); return; } if ($cache_manager->errno == 3) { squid_admin_mysql(0, "{$suffix}Proxy Port report {$cache_manager->errstr} ({$cache_manager->errstr_plus}) [action=disable-icap]", @file_get_contents("/var/log/artica.proxy.watchdog.test.ports.log"), __FILE__, __LINE__); @file_put_contents("/etc/squid3/icap.conf", "\n"); reload_squid(true); sleep(10); continue; } if ($cache_manager->errno == 4) { squid_admin_mysql(1, "{$suffix}Proxy Port report {$cache_manager->errstr} ({$cache_manager->errstr_plus}) [action=disable-icap]", @file_get_contents("/var/log/artica.proxy.watchdog.test.ports.log"), __FILE__, __LINE__); return true; } if ($cache_manager->errno == 5) { squid_admin_mysql(1, "{$suffix}Proxy Port report invalid URL {$cache_manager->URL_SENDED} suggest to restart proxy service", @file_get_contents("/var/log/artica.proxy.watchdog.test.ports.log"), __FILE__, __LINE__); return true; } if ($cache_manager->errno == 6) { squid_admin_mysql(1, "{$suffix}Proxy Port report {$cache_manager->errstr} suggest to check your paranoid rules", @file_get_contents("/var/log/artica.proxy.watchdog.test.ports.log"), __FILE__, __LINE__); return true; } PROXY_TESTS_PORTS_EVENTS("{$suffix} ({$i}/{$MonitConfig["TEST_PORT_TIMEOUT"]}) Cache Manager report failed with error {$cache_manager->errstr} ({$cache_manager->errstr_plus})"); $LOGS[] = "{$suffix}Connection {$i}/{$MonitConfig["TEST_PORT_TIMEOUT"]} Error:{$cache_manager->errstr} sleeping {$MonitConfig["TEST_PORT_INTERVAL"]}"; Events("{$suffix}Connection {$i}/{$MonitConfig["TEST_PORT_TIMEOUT"]} Error:{$cache_manager->errstr} sleeping {$MonitConfig["TEST_PORT_INTERVAL"]}"); sleep($MonitConfig["TEST_PORT_INTERVAL"]); } if (!is_numeric($MAX_RESTART)) { $MAX_RESTART = 2; } $STAMP_MAX_RESTART = STAMP_MAX_RESTART_GET(); if ($STAMP_MAX_RESTART >= $MAX_RESTART) { Events("{$suffix}Restarting Squid aborted, max {$MAX_RESTART} restarts has already been made (waiting Squid restart correctly to return back to 0)..."); return; } $SquidPortTimeOudRestart++; $sock->SET_INFO("SquidPortTimeOudRestart", $SquidPortTimeOudRestart); if (function_exists("debug_backtrace")) { $trace = debug_backtrace(); if (isset($trace[1])) { $sourcefunction = $trace[1]["function"]; $sourceline = $trace[1]["line"]; $executed = "Executed by {$sourcefunction}() line {$sourceline}\nusing argv:{$GLOBALS["ARGVS"]}\n"; } } PROXY_TESTS_PORTS_EVENTS("{$suffix} Restarting squid Max restarts: {$SquidPortTimeOudRestart}/{$MAX_RESTART}"); Events("{$suffix}Restarting squid Max restarts: {$SquidPortTimeOudRestart}/{$MAX_RESTART}"); $SecondsToWaint = $MonitConfig["TEST_PORT_INTERVAL"] + $MonitConfig["TEST_PORT_TIMEOUT"]; $max_duration = $MonitConfig["TEST_PORT_MAX"] * $SecondsToWaint; if ($MonitConfig["TEST_PORT_RESTART"] == 0) { squid_admin_mysql(0, "{$suffix}Proxy Port did not respond during {$max_duration} seconds [action=notify]", @file_get_contents("/var/log/artica.proxy.watchdog.test.ports.log"), __FILE__, __LINE__); @unlink("/var/log/artica.proxy.watchdog.test.ports.log"); return; } if ($SquidPortTimeOudRestart > $MAX_RESTART) { squid_admin_mysql(0, "{$suffix}Proxy Port did not respond during {$max_duration} seconds (max restarts) [action=notify]", @file_get_contents("/var/log/artica.proxy.watchdog.test.ports.log"), __FILE__, __LINE__); @unlink("/var/log/artica.proxy.watchdog.test.ports.log"); return; } squid_admin_mysql(0, "{$suffix}Proxy Port did not respond during {$max_duration} seconds [action=restart]", @file_get_contents("/var/log/artica.proxy.watchdog.test.ports.log"), __FILE__, __LINE__); @unlink("/var/log/artica.proxy.watchdog.test.ports.log"); restart_squid(true); $GLOBALS["ALL_SCORES_WHY"][] = "function " . __FUNCTION__ . " return failed"; $GLOBALS["ALL_SCORES"]++; }
include_once dirname(__FILE__) . "/ressources/class.influx.inc"; $date = date("YW"); $sock = new sockets(); $unix = new unix(); $squidbin = $unix->LOCATE_SQUID_BIN(); if (!is_file($squidbin)) { die; } $SQUIDEnable = $sock->GET_INFO("SQUIDEnable"); if (!is_numeric($SQUIDEnable)) { $SQUIDEnable = 1; } if ($SQUIDEnable == 0) { die; } $cache_manager = new cache_manager(); $data = $cache_manager->makeQuery("5min", true); $hostname = $unix->hostname_g(); foreach ($data as $ligne) { if (preg_match("#server\\.http\\.kbytes_in.*?([0-9\\.]+)#", $ligne, $re)) { $kbytes_in = $re[1]; continue; } if (preg_match("#client_http\\.kbytes_out.*?([0-9\\.]+)#", $ligne, $re)) { $kbytes_out = $re[1]; continue; } if (preg_match("#client_http\\.requests.*?([0-9\\.]+)#", $ligne, $re)) { $client_http_req = $re[1]; continue; }