function exec2($cmd) { $t = time(); $unix = new unix(); ouputz("Please wait... Executing a new task...", __LINE__); ouputz($cmd, __LINE__); exec($cmd . " 2>&1", $results); while (list($pattern, $line) = each($results)) { ouputz($line, __LINE__); } ouputz("Success took " . $unix->distanceOfTimeInWords($t, time(), true), __LINE__); }
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 verifycaches() { $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); $cache = new SquidCacheCenter(); $mv = $unix->find_program("mv"); $rm = $unix->find_program("rm"); $php5 = $unix->LOCATE_PHP5_BIN(); $caches = $cache->build(); $http_port = rand(55000, 65000); $f = array(); $f[] = "cache_effective_user squid"; $f[] = "pid_filename\t/var/run/squid-temp.pid"; $f[] = "http_port 127.0.0.1:{$http_port}"; $f[] = "{$caches}"; $f[] = ""; $squidconf = "/etc/squid3/squid.caches.conf"; @file_put_contents($squidconf, @implode("\n", $f)); build_progress("Generating caches {please_wait}", 25); $squidbin = $unix->LOCATE_SQUID_BIN(); $su = $unix->find_program("su"); $results = array(); $cmd = "{$su} -c \"{$squidbin} -f {$squidconf} -z\" squid"; ouputz("Building new caches {$cmd}", __LINE__); system($cmd); @unlink($squidconf); build_progress("{reconfigure}", 50); system("{$php5} /usr/share/artica-postfix/exec.squid.php --build --force"); build_progress("{restarting}", 60); system("/etc/init.d/squid restart"); for ($i = 0; $i < 30; $i++) { $array = $unix->squid_get_cache_infos(); if (count($array) > 0) { break; } build_progress("{waiting_proxy_status} {$i}/29", 50); writelogs("Waiting 1s to squid be ready...", __FUNCTION__, __FILE__, __LINE__); sleep(1); } build_progress("{waiting_proxy_status} {$i}/29", 60); system("{$php5} /usr/share/artica-postfix/exec.squid.php --cache-infos --force"); $q = new mysql(); $sql = "SELECT * FROM squid_caches_center WHERE remove=1"; $results = $q->QUERY_SQL($sql, "artica_backup"); while ($ligne = mysql_fetch_assoc($results)) { $cache_type = $ligne["cache_type"]; $cache_size = $ligne["cache_size"]; $cachename = $ligne["cachename"]; $ID = $ligne["ID"]; if ($cache_type == "Cachenull") { continue; } if ($cache_type == "tmpfs") { $ligne["cache_dir"] = "/home/squid/cache/MemBooster{$ID}"; } $Directory = $ligne["cache_dir"]; build_progress("{remove} {$Directory}", 80); if (is_dir($Directory)) { system("{$rm} -rf {$Directory}"); } $q->QUERY_SQL("DELETE FROM squid_caches_center WHERE ID='{$ID}'", "artica_backup"); } build_progress("{done}", 100); }
function emptysquidlogs() { $unix = new unix(); $sock = new sockets(); $q = new mysql(); $MYSQL_DATA_DIR = $sock->GET_INFO("ChangeMysqlDir"); if ($MYSQL_DATA_DIR == null) { $MYSQL_DATA_DIR = "/var/lib/mysql"; } $dir = "{$MYSQL_DATA_DIR}/squidlogs"; $TABLESDELETE["searchwords_*"] = true; $TABLESDELETE["UserSizeD_*"] = true; $TABLESDELETE["youtubeday_*"] = true; $TABLESDELETE["*_week"] = true; $TABLESDELETE["*_blocked_week"] = true; $TABLESDELETE["*_members"] = true; $TABLESDELETE["*_hour"] = true; $TABLESDELETE["*_visited"] = true; $TABLESDELETE["dansguardian_events_*"] = true; $TABLESDELETE["squidhour_*"] = true; $TABLESDELETE["visited_sites"] = true; $TABLESDELETE["UserAuth*"] = true; $TABLESDELETE["tables_day"] = true; $TABLESDELETE["adansguardian_events_*"] = true; $TABLESDELETE["*_day"] = true; $TABLESDELETE["youtubeweek_*"] = true; ouputz("Remove " . count($TABLESDELETE) . " family tables in {$MYSQL_DATA_DIR}/squidlogs", __LINE__); while (list($pattern, $line) = each($TABLESDELETE)) { foreach (glob("{$dir}/{$pattern}.MYD") as $filename) { $table = str_replace(".MYD", "", basename($filename)); $table = str_replace(".frm", "", $table); if (preg_match("#(.+?)\\/(.+?)\\.[A-Za-Z]+\$#", $table, $re)) { $table = $re[1]; } ouputz("Removing {$table}....", __LINE__); $q->QUERY_SQL("DROP TABLE `{$table}`", "squidlogs"); if (!$q->ok) { ouputz("Error, {$q->mysql_error}", __LINE__); if (preg_match("#Unknown table#i", $q->mysql_error)) { remove_disk_table_all($table, "squidlogs"); continue; } remove_disk_table($table, "squidlogs"); $q->QUERY_SQL("DROP TABLE `{$table}`", "squidlogs"); if (!$q->ok) { ouputz("Error, {$q->mysql_error}", __LINE__); } } } } foreach (glob("{$dir}/*.BAK") as $filename) { ouputz("Removing backup file " . basename($filename) . "....", __LINE__); @unlink($filename); } }