function rebuildcaches() { $logFile = "/usr/share/artica-postfix/ressources/logs/web/rebuild-cache.txt"; $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; $pid = @file_get_contents($pidfile); if ($pid < 100) { $pid = null; } $unix = new unix(); $sock = new sockets(); if ($unix->process_exists($pid, basename(__FILE__))) { ouputz("Already process exists {$pid}, aborting", __LINE__); build_progress("Already process exists {$pid}, aborting", 110); die; } $mypid = getmypid(); @file_put_contents($pidfile, $mypid); @unlink($logFile); build_progress("Listing caches....", 10); ouputz("Please wait, rebuild caches....", __LINE__); $t = time(); ouputz("Listing caches....", __LINE__); $array = ListCaches(); $mv = $unix->find_program("mv"); $rm = $unix->find_program("rm"); $php5 = $unix->LOCATE_PHP5_BIN(); build_progress(count($array) . " Caches to delete...", 15); ouputz(count($array) . " caches to delete...", __LINE__); if (count($array) == 0) { build_progress("Fatal, unable to list available caches.", 110); ouputz("Fatal, unable to list available caches...", __LINE__); squid_admin_mysql(0, "Fatal, unable to list available caches", null, __FILE__, __LINE__); @unlink("/etc/artica-postfix/squid.lock"); die; } $t = time(); @unlink("/etc/artica-postfix/squid.lock"); @file_put_contents("/etc/artica-postfix/squid.lock", time()); $squidbin = $unix->LOCATE_SQUID_BIN(); if (!is_file($squidbin)) { ouputz("squid, no such binary file", __LINE__); @unlink("/etc/artica-postfix/squid.lock"); return; } build_progress("{stopping_proxy_service}", 20); squid_admin_mysql(1, "Stopping Proxy service in order to rebuild caches", null, __FILE__, __LINE__); ouputz("Stopping squid, please wait...", __LINE__); 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"; } } shell_exec("/etc/init.d/squid stop --force --script=" . basename(__FILE__)); if ($GLOBALS["REPORT"]) { while (list($cache_dir, $ligne) = each($array)) { $DIRARRAY = $unix->DIR_STATUS($cache_dir); $size = $array["SIZE"]; $used = $array["USED"]; $pourc = $array["POURC"]; $mounted = $array["MOUNTED"]; $logs[] = "{$cache_dir} size: {$size}, used:{$used} {$pourc}% mounted on {$mounted}"; } squid_admin_mysql(2, "Report on caches status", @implode("\n", $logs), __FILE__, __LINE__); } reset($array); while (list($cache_dir, $ligne) = each($array)) { build_progress("Checking {$cache_dir}", 30); if (preg_match("#MemBooster#", $cache_dir)) { squid_admin_mysql(1, "Removing cache {$cache_dir}", null, __FILE__, __LINE__); ouputz("Removing {$cache_dir} content...", __LINE__); squid_admin_mysql(2, "Removing cache {$cache_dir} done", null, __FILE__, __LINE__); shell_exec("{$rm} -rf {$cache_dir}/*"); continue; } $DISK_STATUS = $unix->DF_SATUS_K($cache_dir); $DIRECTORY_SIZE = $unix->DIRSIZE_BYTES($cache_dir) / 1024 / 1024; $AIVA = $DISK_STATUS["AIVA"] * 1024; ouputz("Removing {$cache_dir} '{$DIRECTORY_SIZE}'M Available {$AIVA}M", __LINE__); shell_exec("{$rm} -rf {$cache_dir}"); ouputz("re-create {$cache_dir}", __LINE__); squid_admin_mysql(2, "Re-create {$cache_dir}", null, __FILE__, __LINE__); @mkdir($cache_dir, 0755, true); @chown($cache_dir, "squid"); @chgrp($cache_dir, "squid"); } $su = $unix->find_program("su"); $results = array(); build_progress("Create {$cache_dir}", 30); ouputz("Building new caches {$su} -c \"{$squidbin} -z\" squid", __LINE__); exec("{$su} -c \"{$squidbin} -z\" squid 2>&1", $results); while (list($num, $ligne) = each($results)) { ouputz("{$ligne}", __LINE__); } ouputz("Remove lock file...", __LINE__); @unlink("/etc/artica-postfix/squid.lock"); ouputz("Starting squid, please wait...", __LINE__); build_progress("{starting_proxy_service}", 35); 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"; } } squid_admin_mysql(2, "Starting Proxy Service after rebuilding caches", null, __FILE__, __LINE__); shell_exec("{$php5} /usr/share/artica-postfix/exec.squid.watchdog.php --start --script=" . basename(__FILE__)); for ($i = 0; $i < 60; $i++) { $array = $unix->squid_get_cache_infos(); if (count($array) > 0) { break; } ouputz("Waiting {$i}s/60 cache is not ready...", __LINE__); sleep(1); } ouputz("Done... Squid-cache seems to be ready...", __LINE__); $NICE = $unix->EXEC_NICE(); $nohup = $unix->find_program("nohup"); build_progress("Refresh caches infos...", 50); ouputz("Refresh caches information, please wait...", __LINE__); shell_exec("{$php5} /usr/share/artica-postfix/exec.squid.php --cache-infos --force"); if (is_array($cachesRename)) { reset($cachesRename); while (list($index, $cache_dir) = each($cachesRename)) { build_progress("Removing old {$cache_dir}", 60); $cmd = "{$NICE} {$rm} -rf {$cache_dir} >/dev/null 2>&1 &"; squid_admin_mysql(2, "Ask to delete old cache dir {$cache_dir} done", "{$called}", __FILE__, __LINE__); ouputz("Deleting {$cache_dir} {$cmd}", __LINE__); shell_exec($cmd); } } build_progress("{done}", 100); $took = $unix->distanceOfTimeInWords($t, time()); squid_admin_mysql(2, "All Proxy caches was rebuilded took: {$took}", "{$called}", __FILE__, __LINE__); }
function clean_old_caches() { $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; $PidTime = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time"; if ($GLOBALS["VERBOSE"]) { echo "pidfile = {$pidfile}\n"; echo "PidTime = {$PidTime}\n"; } $pid = @file_get_contents($pidfile); if ($pid < 100) { $pid = null; } $unix = new unix(); $sock = new sockets(); if ($unix->process_exists($pid, basename(__FILE__))) { if ($GLOBALS["VERBOSE"]) { echo "Already executed pid {$pid}\n"; } die; } $mypid = getmypid(); @file_put_contents($pidfile, $mypid); $Time = $unix->file_time_min($PidTime); if (!$GLOBALS["FORCE"]) { if ($Time < 15) { if ($GLOBALS["VERBOSE"]) { echo "Only each 15mn {$PidTime}, die()\n"; } die; } } @unlink($PidTime); @file_put_contents($PidTime, time()); $rm = $unix->find_program("rm"); $dirs = $unix->dirdir("/home"); while (list($directoryName, $ligne) = each($dirs)) { $basename = basename($directoryName); if (is_numeric($basename)) { if ($GLOBALS["VERBOSE"]) { echo "remove {$directoryName}\n"; } shell_exec("{$rm} -rf {$directoryName}"); } } $Todelete = array(); if ($GLOBALS["VERBOSE"]) { echo "Get list of caches....\n"; } $ListCaches = ListCaches(); while (list($directory, $ligne) = each($ListCaches)) { if ($GLOBALS["VERBOSE"]) { echo "squid-cache........: Using {$directory}\n"; } $directory = dirname($directory); if ($GLOBALS["VERBOSE"]) { echo "Add {$directory} into collection\n"; } $FINAL[$directory] = true; } reset($ListCaches); $dirs = $unix->dirdir("/home/squid/cache"); while (list($directory, $ligne) = each($dirs)) { if (!IfDirIsACache($directory)) { continue; } if (!isset($ListCaches[$directory])) { $Todelete[$directory] = true; if ($GLOBALS["VERBOSE"]) { echo "*** Not used {$directory} i should remove it.. ***\n"; } } } $dirs = $unix->dirdir("/home/squid/caches"); while (list($directory, $ligne) = each($dirs)) { if (!IfDirIsACache($directory)) { continue; } if (!isset($ListCaches[$directory])) { $Todelete[$directory] = true; if ($GLOBALS["VERBOSE"]) { echo "*** Not used {$directory} i should remove it.. ***\n"; } } } $FINAL["/home/squid/cache"] = true; while (list($directory, $ligne) = each($FINAL)) { if ($GLOBALS["VERBOSE"]) { echo "{$directory}\n"; } $dirs = $unix->dirdir($directory); while (list($directoryName, $ligne) = each($dirs)) { $basename = basename($directoryName); if (preg_match("#-delete-#", $basename)) { if ($GLOBALS["VERBOSE"]) { echo "Found {$directoryName}\n"; } $Todelete[$directoryName] = true; } } } if (count($Todelete) == 0) { if ($GLOBALS["VERBOSE"]) { echo "No directory\n"; } return; } while (list($directory, $ligne) = each($Todelete)) { if (!is_dir($directory)) { continue; } if ($GLOBALS["VERBOSE"]) { echo "Must remove `{$directory}`\n"; } $t = time(); $distance = $unix->distanceOfTimeInWords($t, time(), true); shell_exec("{$rm} -rf \"{$directory}\" >/dev/null 2>&1"); squid_admin_mysql(2, "{$directory} removed", "Task took {$distance} for this directory"); } }