function BuildCaches($NOTSTART = false) { echo "Starting......: " . date("H:i:s") . " Squid Check *** caches ***\n"; $squid = new squidbee(); $unix = new unix(); $SQUID_CONFIG_PATH = $unix->SQUID_CONFIG_PATH(); $unix = new unix(); $sock = new sockets(); $su_bin = $unix->find_program("su"); $chown = $unix->find_program("chown"); $chmod = $unix->find_program("chmod"); $nohup = $unix->find_program("nohup"); $TimeFileChown = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time"; $SquidBoosterMem = $sock->GET_INFO("SquidBoosterMem"); if (!is_numeric($SquidBoosterMem)) { $SquidBoosterMem = 0; } $squid_user = SquidUser(); writelogs("Using squid user: \"{$squid_user}\"", __FUNCTION__, __FILE__, __LINE__); writelogs("{$chown} cache directories...", __FUNCTION__, __FILE__, __LINE__); $unix->chown_func($squid_user, null, "/etc/squid3/*"); if (is_dir("/usr/share/squid-langpack")) { $unix->chown_func($squid_user, null, "/usr/share/squid-langpack"); } $GetCachesInsquidConf = $unix->SQUID_CACHE_FROM_SQUIDCONF(); if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " Squid " . count($GetCachesInsquidConf) . " caches to check\n"; } writelogs(count($GetCachesInsquidConf) . " caches to check", __FUNCTION__, __FILE__, __LINE__); $MustBuild = false; if ($SquidBoosterMem > 0) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " Squid Cache booster set to {$SquidBoosterMem}Mb\n"; } @mkdir("/var/squid/cache_booster", 0755, true); @chown("/var/squid/cache_booster", "squid"); @chgrp("/var/squid/cache_booster", "squid"); if (!is_dir("/var/squid/cache_booster/00")) { echo "Starting......: " . date("H:i:s") . " Squid *** /var/squid/cache_booster/00 *** No such directory ask to rebuild caches\n"; $MustBuild = true; } } $nice = $unix->EXEC_NICE(); $rm = $unix->find_program("rm"); if (!$GLOBALS["NOCACHES"]) { $TimeFileChownTime = $unix->file_time_min($TimeFileChown); $SH[] = "#!/bin/sh"; while (list($CacheDirectory, $type) = each($GetCachesInsquidConf)) { if (trim($CacheDirectory) == null) { continue; } if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " Squid Check *** {$CacheDirectory} ***\n"; } $subdir = basename($CacheDirectory); $MainDir = dirname($CacheDirectory); writelogs("Directory \"{$CacheDirectory}\" SUBDIR={$subdir} Main dir={$MainDir}", __FUNCTION__, __FILE__, __LINE__); if (isDirInFsTab($MainDir)) { if ($GLOBALS["OUTPUT"]) { echo "Starting......: " . date("H:i:s") . " Squid Check *** {$MainDir} -> Mounted ? ***\n"; } } if (!is_dir($CacheDirectory)) { echo "Starting......: " . date("H:i:s") . " Squid Check creating cache \"{$CacheDirectory}\" no such directory\n"; @mkdir($CacheDirectory, 0755, true); $MustBuild = true; } echo "Starting......: " . date("H:i:s") . " Squid Check cache \"{$CacheDirectory}\" owned by {$squid_user} (" . __LINE__ . ")\n"; build_progress("{reconfigure} Checking {$CacheDirectory}", 86); $SH[] = "{$nice} {$chown} -R {$squid_user}:{$squid_user} {$CacheDirectory}"; @chmod($CacheDirectory, 0755); } $TMPFILE = $unix->FILE_TEMP(); $SH[] = "{$rm} -f {$TMPFILE}.sh"; @file_put_contents("{$TMPFILE}.sh", @implode("\n", $SH)); @chmod("{$TMPFILE}.sh", 0755); build_progress("{reconfigure} Checking {$TMPFILE}.sh ok", 86); shell_exec("{$nohup} {$TMPFILE}.sh >/dev/null 2>&1 &"); $SH = array(); } if ($unix->file_time_min($TimeFileChown) > 120) { @unlink($TimeFileChown); @file_put_contents($TimeFileChown, time()); } if (!$GLOBALS["NOCACHES"]) { $MustBuild = false; return; } if (!$MustBuild) { if ($GLOBALS["OUTPUT"]) { caches_infos(); } echo "Starting......: " . date("H:i:s") . " Squid all caches are OK\n"; return; } if (preg_match("#(.+?):#", $squid_user, $re)) { $squid_uid = $re[1]; } else { $squid_uid = "squid"; } writelogs("Stopping squid...", __FUNCTION__, __FILE__, __LINE__); shell_exec("/etc/init.d/artica-postfix stop squid-cache"); writelogs("Building caches with user: \"{$squid_uid}\"", __FUNCTION__, __FILE__, __LINE__); writelogs("{$su_bin} {$squid_uid} -c \"{$GLOBALS["SQUIDBIN"]} -z\" 2>&1", __FUNCTION__, __FILE__, __LINE__); exec("{$su_bin} {$squid_uid} -c \"{$GLOBALS["SQUIDBIN"]} -z\" 2>&1", $results); while (list($agent, $val) = each($results)) { writelogs("{$val}", __FUNCTION__, __FILE__, __LINE__); } writelogs("Send Notifications", __FUNCTION__, __FILE__, __LINE__); send_email_events("Squid Cache: reconfigure caches", "Here it is the results\n", @implode("\n", $results), "proxy"); writelogs("Starting squid", __FUNCTION__, __FILE__, __LINE__); unset($results); if (!$NOTSTART) { reconfigure_squid(); } }
function caches_list() { $squid = new squidbee(); $page = CurrentPageName(); $tpl = new templates(); $sock = new sockets(); $html = "\n<table cellspacing='0' cellpadding='0' border='0' class='tableView' style='width:100%'>\n<thead class='thead'>\n\t<tr>\n\t<th colspan=2>{cache}</th>\n\t<th>{type}</th>\n\t<th>{size}</th>\n\t<th> </th>\n\t<th> </th>\n\t</tr>\n</thead>\n<tbody class='tbody'>"; $classtr = "oddRow"; $unit = " MB"; $cacheinfo = unserialize(base64_decode($sock->getFrameWork("cmd.php?squid-cache-infos=yes"))); if ($squid->CACHE_SIZE > 1000) { $squid->CACHE_SIZE = $squid->CACHE_SIZE / 1000; $unit = " GB"; } $html = $html . "\n\t\t\t<tr class={$classtr}>\n\t\t\t<td width=1%>" . imgtootltip("database-32.png", "{edit}", "AddCache('{$squid->CACHE_PATH}')") . "</td>\n\t\t\t<td nowrap><strong style='font-size:14px'>" . basename($squid->CACHE_PATH) . "</strong><div style='font-size:11px'><i>{$squid->CACHE_PATH}</i></div></td>\n\t\t\t<td width=1%><strong style='font-size:14px'>{$squid->CACHE_TYPE}</strong></td>\n\t\t\t<td width=1%><strong style='font-size:14px'>{$squid->CACHE_SIZE}{$unit}</strong></td>\n\t\t\t<td>" . caches_infos($cacheinfo[$squid->CACHE_PATH]) . "</td>\n\t\t\t<td width=1%> </td>\n\t\t\t</tr>\n\t\t\t"; while (list($path, $array) = each($squid->cache_list)) { if ($classtr == "oddRow") { $classtr = null; } else { $classtr = "oddRow"; } $unit = " MB"; $maxcachesize = null; if ($array["cache_type"] == "rock") { $maxcachesize = " ({max_objects_size} {$array["cache_maxsize"]}{$unit})"; } if (is_numeric($array["cache_size"])) { if ($array["cache_size"] > 1000) { $array["cache_size"] = $array["cache_size"] / 1000; $unit = " GB"; } } $html = $html . "\n\t\t\t<tr class={$classtr}>\n\t\t\t<td width=1%>" . imgtootltip("database-32.png", "{edit}", "AddCache('{$path}')") . "</td>\n\t\t\t<td nowrap><strong style='font-size:14px'>" . basename($path) . "</strong><div style='font-size:11px'><i>{$path} {$maxcachesize}</i></div></td>\n\t\t\t<td width=1%><strong style='font-size:14px'>{$array["cache_type"]}</strong></td>\n\t\t\t<td width=1%><strong style='font-size:14px'>{$array["cache_size"]}{$unit}</strong></td>\n\t\t\t<td>" . caches_infos($cacheinfo[$path]) . "</td>\n\t\t\t<td width=1%>" . imgtootltip("delete-32.png", "{delete}", "DeleteCache('{$path}')") . "</td>\n\t\t\t</tr>\n\t\t\t"; } $html = $html . "\n\t</tbody>\n\t</table>"; echo $tpl->_ENGINE_parse_body($html); }
function squid_stores_status($ByExec = false) { $cachefile = "/usr/share/artica-postfix/ressources/logs/web/squid_stores_status.html"; $unix = new unix(); $pidFile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; if ($ByExec) { $pid = $unix->get_pid_from_file($pidFile); if ($unix->process_exists($pid)) { if ($unix->PROCCESS_TIME_MIN($pid, 10) < 2) { return; } } @file_put_contents($pidFile, getmypid()); } $squid_pid = SQUID_PID(); if (!$unix->process_exists($squid_pid)) { if ($GLOBALS["OUTPUT"]) { echo "Squid-Cache is not running\n"; } if (is_file("/usr/share/artica-postfix/ressources/logs/web/status.squid")) { @chmod("/usr/share/artica-postfix/ressources/logs/web/status.squid", 0777); } return; } if (!$GLOBALS["FORCE"]) { $ttl = $unix->PROCCESS_TIME_MIN($squid_pid); if ($ttl < 1) { if ($GLOBALS["OUTPUT"]) { echo "squid_stores_status(): Squid-Cache is running since {$ttl}Mn, please wait at least 1mn\n"; } if (is_file("/usr/share/artica-postfix/ressources/logs/web/status.squid")) { @chmod("/usr/share/artica-postfix/ressources/logs/web/status.squid", 0777); } return; } } if ($GLOBALS["OUTPUT"]) { echo "squid_stores_status(): Squid-Cache is running since {$ttl}Mn\n"; } $StoreDirs = unserialize(base64_decode(squid_get_storage_info())); caches_infos(); $imgRefresh = imgtootltip("20-refresh.png", "{refresh}", "Loadjs('squid.store.status.php',true)"); @unlink($cachefile); while (list($directory, $arrayStore) = each($StoreDirs)) { $FROM_TIME = 0; if ($directory == "MEM") { continue; } if ($directory == "CURCAP") { $TTR[] = "<tr>\n\t\t<td style='font-weight:bold;font-size:12px' align='right'>{capacity}:</td>\n\t\t<td style='font-weight:bold;font-size:12px'> </td>\n\t\t\t\t</tr>\n\t\t\t\t<tr>\n\t\t\t\t\t<td> </td>\n\t\t\t\t\t<td>" . pourcentage($arrayStore, 10) . "</td>\n\t\t\t\t</tr>"; continue; } $FROM_TIME = $unix->file_time_min("{$directory}/."); if (is_dir("{$directory}/00")) { $FROM_TIME = filemtime("{$directory}/00/."); } $FROM_TIME_TEXT = $unix->distanceOfTimeInWords($FROM_TIME, time()) . "\n"; $directory = basename($directory); $TTR[] = "\n\t\t\t<tr>\n\t\t\t\t<td style='font-weight:bold;font-size:12px' align='right'>{$directory}:</td>\n\t\t\t\t<td style='font-weight:bold;font-size:12px'>" . FormatBytes($arrayStore["SIZE"]) . "</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td> </td>\n\t\t\t\t<td>" . pourcentage($arrayStore["PERC"], 10, "green") . "\n\t\t\t\t\t<div style='font-size:11px;text-align:right'>{$FROM_TIME_TEXT}</span>\n\t\t\t\t</td>\n\t\t\t</tr>"; } $TTR[] = "<tr><td colspan=2 align='right'>{$imgRefresh}</td></tr>"; if ($GLOBALS["OUTPUT"]) { echo "Saving new cache file: " . basename($cachefile) . "\n"; } if (count($TTR) > 0) { $datas = RoundedLightGreen("<div style='min-height:147px'>\n\t\t<table style='width:100%'>" . @implode($TTR, "\n") . "</table></div>") . "<br>"; @mkdir(dirname($cachefile), 0755); @unlink($cachefile); @file_put_contents($cachefile, $datas); @chmod($datas, 0755); } if (is_file("/usr/share/artica-postfix/ressources/logs/web/status.squid")) { @chmod("/usr/share/artica-postfix/ressources/logs/web/status.squid", 0777); } }