Beispiel #1
0
function SendStatus()
{
    emergency(true);
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $sock = new sockets();
    $EnableSargGenerator = $sock->GET_INFO("EnableSargGenerator");
    if (TestsCron($pidfile)) {
        $ArticaMetaPingEnable = $sock->GET_INFO("ArticaMetaPingEnable");
        events("SendPing={$ArticaMetaPingEnable}", __FUNCTION__, __FILE__, __LINE__);
        if ($ArticaMetaPingEnable == 1) {
            SendPing();
        }
        return true;
    }
    $ArticaMetaEnabled = $sock->GET_INFO("ArticaMetaEnabled");
    if ($ArticaMetaEnabled != 1) {
        return;
    }
    $t1 = time();
    if (!is_file("/usr/share/artica-postfix/ressources/logs/global.status.ini")) {
        events("Unable to stat /usr/share/artica-postfix/ressources/logs/global.status.ini", __FUNCTION__, __FILE__, __LINE__);
        return null;
    }
    $unix = new unix();
    $pid = @file_get_contents($pidfile);
    if ($unix->process_exists($pid, basename(__FILE__))) {
        $ptime = $unix->PROCESS_TTL($pid);
        if ($ptime > $GLOBALS["MAXTTL"]) {
            events("killing process {$pid} ttl:{$ptime} minutes", __FUNCTION__, __FILE__, __LINE__);
            unix_system_kill_force($pid);
        } else {
            events("Already executed, process {$pid}", __FUNCTION__, __FILE__, __LINE__);
            die;
        }
    }
    events("Running pid " . getmypid(), __FUNCTION__, __FILE__, __LINE__);
    @file_put_contents($pidfile, getmypid());
    CheckNetwork();
    $http = new httpget();
    $meta = new artica_meta();
    $filecache = "/etc/artica-postfix/artica-meta-files.cache";
    events("My uuid=\"{$meta->uuid}\"", __FUNCTION__, __FILE__, __LINE__);
    $memCache = "/usr/share/artica-postfix/ressources/logs/status.memory.hash";
    $cpu_graphs = "/opt/artica/share/www/system/rrd/01cpu-1day.png";
    $server_status = "/usr/share/artica-postfix/ressources/logs/status.right.1.html";
    $squid_realtime = "/etc/artica-postfix/squid-realtime.cache";
    $datasToSend = base64_encode(serialize($meta->GLOBAL_ARRAY));
    $ini = new Bs_IniHandler();
    $ini->loadFile("/usr/share/artica-postfix/ressources/logs/global.status.ini");
    $ArrayFileCache = unserialize(@file_get_contents($filecache));
    if (is_file($memCache)) {
        $MEM_CACHE = base64_encode(@file_get_contents($memCache));
    } else {
        include_once "ressources/class.os.system.tools.inc";
        $os = new os_system();
        $os->html_Memory_usage();
        $MEM_CACHE = base64_encode(serialize($os->meta_array));
    }
    if (is_file($cpu_graphs)) {
        if ($ArrayFileCache["STATS_DAY"] != filemtime($cpu_graphs)) {
            $http->uploads["STATS_DAY"] = $cpu_graphs;
            $ArrayFileCache["STATS_DAY"] = filemtime($cpu_graphs);
            @file_put_contents($filecache, serialize($ArrayFileCache));
        }
    }
    if (is_file($server_status)) {
        if ($ArrayFileCache["SERVER_STATUS"] != filemtime($server_status)) {
            $http->uploads["SERVER_STATUS"] = $server_status;
            $ArrayFileCache["SERVER_STATUS"] = filemtime($server_status);
            @file_put_contents($filecache, serialize($ArrayFileCache));
        }
    }
    if (is_file($squid_realtime)) {
        if ($ArrayFileCache["SQUID_REALTIME"] != filemtime($squid_realtime)) {
            $http->uploads["SQUID_REALTIME"] = $squid_realtime;
            $ArrayFileCache["SQUID_REALTIME"] = filemtime($squid_realtime);
            @file_put_contents($filecache, serialize($ArrayFileCache));
        }
    }
    if ($EnableSargGenerator == 1) {
        $push_sarg = false;
        $sock = new sockets();
        $SargOutputDir = $sock->GET_INFO("SargOutputDir");
        if ($SargOutputDir == null) {
            $SargOutputDir = "/var/www/html/squid-reports";
        }
        if (is_file("{$SargOutputDir}/index.html")) {
            if (!is_file("/etc/artica-postfix/sarg.tgz")) {
                shell_exec("cd {$SargOutputDir} && tar -cjf /etc/artica-postfix/sarg.tgz ./*");
                $push_sarg = true;
            } else {
                if ($ArrayFileCache["SQUID_SARG"] != filemtime("{$SargOutputDir}/index.html")) {
                    @unlink("/etc/artica-postfix/sarg.tgz");
                    shell_exec("cd {$SargOutputDir} && tar -cjf /etc/artica-postfix/sarg.tgz ./*");
                    $push_sarg = true;
                }
            }
            if ($push_sarg) {
                $http->uploads["SQUID_SARG"] = "/etc/artica-postfix/sarg.tgz";
            }
        }
    }
    $users = new usersMenus();
    $status = base64_encode(serialize($ini->_params));
    $pasmoinsaux = pasmoinsaux();
    if ($users->VMWARE_HOST) {
        $VMWARE_HOST = 1;
    } else {
        $VMWARE_HOST = 0;
    }
    shell_exec(LOCATE_PHP5_BIN2() . " " . dirname(__FILE__) . "/exec.dmidecode.php");
    $dmidecode = base64_encode(@file_get_contents("/etc/artica-postfix/dmidecode.cache"));
    //SQUID
    if ($users->SQUID_INSTALLED) {
        $sock = new sockets();
        $SQUIDEnable = $sock->GET_INFO("SQUIDEnable");
        if (!is_numeric($SQUIDEnable)) {
            $SQUIDEnable = 1;
        }
        if ($SQUIDEnable == 1) {
            $SQUID_CACHES = base64_encode(serialize($unix->squid_get_cache_infos()));
        }
        if (is_array($SQUID_CACHES)) {
            $squid = new squidbee();
            $cacheconf = $squid->cache_list;
            $cacheconf[$squid->CACHE_PATH]["cache_type"] = $squid->CACHE_TYPE;
            $cacheconf[$squid->CACHE_PATH]["cache_size"] = $squid->CACHE_SIZE;
            $cacheconf[$squid->CACHE_PATH]["cache_dir_level1"] = 16;
            $cacheconf[$squid->CACHE_PATH]["cache_dir_level2"] = 256;
            events("Caches: " . count($cacheconf), __FILE__, __LINE__);
            $SQUID_CACHES_CONFIG = base64_encode(serialize($cacheconf));
        }
    }
    if ($users->SAMBA_INSTALLED) {
        _CheckSambaConfig();
    }
    if ($users->ZARAFA_INSTALLED) {
        if (is_file("/etc/artica-postfix/settings/Daemons/ZarafaLicenseInfos")) {
            $ZARAFA_LICENSE = @file_get_contents("/etc/artica-postfix/settings/Daemons/ZarafaLicenseInfos");
        } else {
            $ZARAFA_LICENSE = "Free edition";
        }
    }
    if (is_file("/etc/artica-postfix/zarafa-export.db")) {
        $ZARAFA_DB = @file_get_contents("/etc/artica-postfix/zarafa-export.db");
    }
    $body = $http->send("{$meta->ArticaMetaHostname}/lic.status.server.php", "post", array("DATAS" => $datasToSend, "STATUS" => $status, "MEMORIES" => $MEM_CACHE, "VERSION" => $users->ARTICA_VERSION, "DISTRI" => $users->LinuxDistriCode, "UPTIME" => getUptime(), "DISTRINAME" => $users->LinuxDistriFullName, "MAIN_PRODUCTS" => base64_encode(serialize(array("ZARAFA" => $users->ZARAFA_INSTALLED, "POSTFIX" => $users->POSTFIX_INSTALLED, "SQUID" => $users->SQUID_INSTALLED, "SAMBA" => $users->SAMBA_INSTALLED, "CYRUS" => $users->cyrus_imapd_installed, "OPENVPN" => $users->OPENVPN_INSTALLED))), "PROCESSES" => base64_encode($pasmoinsaux), "TOP_PROCESSES" => top10cpumem(), "NETS" => Networks(), "VMWARE_HOST" => $VMWARE_HOST, "SETTINGS_INC" => base64_encode(serialize(settings_inc())), "LOCAL_VERSIONS" => LocalVersions(), "VBOXGUESTS" => VirtualBoxList(), "APTCHECK" => APTCHECK(), "DMIDECODE" => $dmidecode, "SQUID_CACHES" => $SQUID_CACHES, "SQUID_CACHES_CONFIG" => $SQUID_CACHES_CONFIG, "OPENPORTS" => OpenPorts($meta->serial, $meta->uuid), "OPENVPN_CLIENTS_STATUS" => @file_get_contents("/usr/share/artica-postfix/ressources/logs/openvpn-clients.status"), "ZARAFA_DB" => $ZARAFA_DB, "ZARAFA_LICENSE" => $ZARAFA_LICENSE));
    $EXEC_NICE = EXEC_NICE();
    if (is_file("/usr/bin/nohup")) {
        $nohup = "/usr/bin/nohup ";
    }
    if (preg_match("#NOTIFY_DISCONNECT#is", $body)) {
        events("NOTIFY_DISCONNECT detected -> unregister_server()", __FUNCTION__, __FILE__, __LINE__);
        unregister_server();
        return;
    }
    if (preg_match("#NOTIFY_EXPORT_USERS#is", $body)) {
        events("NOTIFY_EXPORT_USERS -> {$nohup}{$EXEC_NICE}exec.artica.meta.users.php --export-all", __FUNCTION__, __FILE__, __LINE__);
        shell_exec($nohup . $EXEC_NICE . LOCATE_PHP5_BIN() . " " . dirname(__FILE__) . "/exec.artica.meta.users.php --export-all >/dev/null 2>&1 &");
    }
    if (preg_match("#NOTIFY_EXPORT_DOMAINS#is", $body)) {
        events("NOTIFY_EXPORT_DOMAINS -> {$nohup}{$EXEC_NICE}exec.artica.meta.users.php --export-all-domains", __FUNCTION__, __FILE__, __LINE__);
        shell_exec($nohup . $EXEC_NICE . LOCATE_PHP5_BIN() . " " . dirname(__FILE__) . "/exec.artica.meta.users.php --export-all-domains >/dev/null 2>&1 &");
    }
    if (preg_match("#NOTIFY_EXPORT_OU#is", $body)) {
        events("NOTIFY_EXPORT_OU -> {$nohup}{$EXEC_NICE}exec.artica.meta.users.php --export-all-ou", __FUNCTION__, __FILE__, __LINE__);
        shell_exec($nohup . $EXEC_NICE . LOCATE_PHP5_BIN() . " " . dirname(__FILE__) . "/exec.artica.meta.users.php --export-all-ou >/dev/null 2>&1 &");
    }
    if (preg_match("#NOTIFY_EXPORT_GROUPS#is", $body)) {
        events("NOTIFY_EXPORT_GROUPS -> {$nohup}{$EXEC_NICE}exec.artica.meta.users.php --export-all-groups", __FUNCTION__, __FILE__, __LINE__);
        shell_exec($nohup . $EXEC_NICE . LOCATE_PHP5_BIN() . " " . dirname(__FILE__) . "/exec.artica.meta.users.php --export-all-groups >/dev/null 2>&1 &");
    }
    if (preg_match("#NOTIFY_EXPORT_SETTINGS#is", $body)) {
        $cmd = $EXEC_NICE . LOCATE_PHP5_BIN() . " " . dirname(__FILE__) . "/exec.artica.meta.users.php --export-all-settings >/dev/null 2>&1 &";
        events("NOTIFY_EXPORT_SETTINGS -> {$cmd}", __FUNCTION__, __FILE__, __LINE__);
        shell_exec($cmd);
    }
    if (preg_match("#NOTIFY_EXPORT_COMPUTERS#is", $body)) {
        $cmd = $nohup . $EXEC_NICE . LOCATE_PHP5_BIN() . " " . dirname(__FILE__) . "/exec.artica.meta.users.php --export-all-computers >/dev/null 2>&1 &";
        events("NOTIFY_EXPORT_SETTINGS -> {$cmd}", __FUNCTION__, __FILE__, __LINE__);
        shell_exec($cmd);
    }
    if (preg_match("#NOTIFY_EXPORT_DNS_ENTRIES#is", $body)) {
        $cmd = $nohup . $EXEC_NICE . LOCATE_PHP5_BIN() . " " . dirname(__FILE__) . "/exec.artica.meta.users.php --export-all-dns >/dev/null 2>&1 &";
        events("NOTIFY_EXPORT_DNS_ENTRIES -> {$cmd}", __FUNCTION__, __FILE__, __LINE__);
        shell_exec($cmd);
    }
    if (preg_match("#NOTIFY_EXPORT_GROUPWARES#is", $body)) {
        $cmd = $nohup . $EXEC_NICE . LOCATE_PHP5_BIN() . " " . dirname(__FILE__) . "/exec.artica.meta.users.php --export-all-groupwares >/dev/null 2>&1 &";
        events("NOTIFY_EXPORT_GROUPWARES -> {$cmd}", __FUNCTION__, __FILE__, __LINE__);
        shell_exec($cmd);
    }
    if (preg_match("#NOTIFY_EXPORT_FETCHMAIL_RULES#is", $body)) {
        $cmd = $nohup . $EXEC_NICE . LOCATE_PHP5_BIN() . " " . dirname(__FILE__) . "/exec.artica.meta.users.php --export-fetchmail-rules >/dev/null 2>&1 &";
        events("NOTIFY_EXPORT_FETCHMAIL_RULES -> {$cmd}", __FUNCTION__, __FILE__, __LINE__);
        shell_exec($cmd);
    }
    if (preg_match("#<TASKS>(.+?)</TASKS>#is", $body, $re)) {
        events("Save tasks to /etc/artica-postfix/artica-meta.tasks", __FUNCTION__, __FILE__, __LINE__);
        @file_put_contents("/etc/artica-postfix/artica-meta.tasks", $re[1]);
        $cmd = $nohup . $EXEC_NICE . LOCATE_PHP5_BIN() . " " . dirname(__FILE__) . "/exec.artica.meta.tasks.php >/dev/null 2>&1 &";
        events("TASKS ->{$cmd}", __FUNCTION__, __FILE__, __LINE__);
        shell_exec($cmd);
    } else {
        events("No tasks ordered for me...", __FUNCTION__, __FILE__, __LINE__);
        @unlink("/etc/artica-postfix/artica-meta.tasks");
    }
    if (preg_match("#<HOST_CONF>(.+?)</HOST_CONF>#is", $body, $re)) {
        ParseMyConf($re[1]);
    } else {
        events("No configuration for me...", __FUNCTION__, __FILE__, __LINE__);
    }
    shell_exec($nohup . $EXEC_NICE . LOCATE_PHP5_BIN() . " " . dirname(__FILE__) . "/exec.artica.meta.users.php --socks >/dev/null 2>&1 &");
    if (users_queue()) {
        $cmd = $nohup . $EXEC_NICE . LOCATE_PHP5_BIN() . " " . dirname(__FILE__) . "/exec.artica.meta.users.php --user-queue >/dev/null 2>&1 &";
        events("users settings queue is not empty -> {$cmd}", __FUNCTION__, __FILE__, __LINE__);
        shell_exec($cmd);
    }
    if (computer_queue()) {
        $cmd = $nohup . $EXEC_NICE . LOCATE_PHP5_BIN() . " " . dirname(__FILE__) . "/exec.artica.meta.users.php --computer-queue >/dev/null 2>&1 &";
        events("computer settings queue is not empty -> {$cmd}", __FUNCTION__, __FILE__, __LINE__);
        shell_exec($cmd);
    }
    if ($users->OPENVPN_INSTALLED) {
        $cmd = $nohup . $EXEC_NICE . LOCATE_PHP5_BIN() . " " . dirname(__FILE__) . "/exec.artica.meta.users.php --export-openvpn-logs >/dev/null 2>&1 &";
        events("OpenVpn is installed -> {$cmd}", __FUNCTION__, __FILE__, __LINE__);
        shell_exec($cmd);
    }
    $time_iptables = file_time_min("/etc/artica-postfix/artica.meta.iptables.time");
    if ($time_iptables > 180) {
        $cmd = $nohup . $EXEC_NICE . LOCATE_PHP5_BIN() . " " . dirname(__FILE__) . "/exec.artica.meta.users.php --iptables >/dev/null 2>&1 &";
        events("iptables -> {$cmd}", __FUNCTION__, __FILE__, __LINE__);
        shell_exec($cmd);
        $cmd = $EXEC_NICE . LOCATE_PHP5_BIN() . " " . dirname(__FILE__) . "/exec.artica.meta.users.php --export-all-settings >/dev/null 2>&1 &";
        events("{$cmd}", __FUNCTION__, __FILE__, __LINE__);
        @unlink("/etc/artica-postfix/artica.meta.iptables.time");
        @file_put_contents("/etc/artica-postfix/artica.meta.iptables.time", "#");
    }
    $t2 = time();
    $time_duration = distanceOfTimeInWords($t1, $t2);
    events("Send status to {$meta->ArticaMetaHostname} DONE ({$time_duration})", __FUNCTION__, __FILE__, __LINE__);
}
Beispiel #2
0
function SQUID_CACHE_INFOS()
{
    $cache_file = "/usr/share/artica-postfix/ressources/logs/web/squid.caches.infos";
    if (is_file($cache_file)) {
        $time = file_time_min($cache_file);
        $datas = @file_get_contents($cache_file);
        writelogs_framework("{$cache_file} time:{$time} bytes:" . strlen($datas), __FUNCTION__, __FILE__, __LINE__);
        if (strlen($datas) > 20) {
            if ($time < 10) {
                echo "<articadatascgi>" . base64_encode($datas) . "</articadatascgi>";
                return;
            }
        }
    }
    $unix = new unix();
    $array = $unix->squid_get_cache_infos();
    $serialized = serialize($array);
    @file_put_contents($cache_file, $serialized);
    chmod($cache_file, 0777);
    echo "<articadatascgi>" . base64_encode($serialized) . "</articadatascgi>";
}
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 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__);
}
Beispiel #5
0
function caches_infos($aspid = false)
{
    $cacheFile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    $cachePID = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    if ($GLOBALS["FORCE"]) {
        ToSyslog("caches_infos(): Executed in force mode");
    }
    $unix = new unix();
    $sock = new sockets();
    if (!$GLOBALS["VERBOSE"]) {
        if (system_is_overloaded(basename(__FILE__))) {
            EventsWatchdog("Overloaded system, aborting task...");
            ToSyslog("caches_infos(): Overloaded system, aborting task");
            writelogs("Overloaded system, aborting task...", __FUNCTION__, __FILE__, __LINE__);
            return;
        }
        if ($aspid) {
            $pid = @file_get_contents($cachePID);
            if ($unix->process_exists($pid)) {
                $timepid = $unix->PROCCESS_TIME_MIN($pid);
                if ($timepid < 15) {
                    ToSyslog("caches_infos(): Already task PID: {$pid} running since {$timepid}Mn");
                }
                $kill = $unix->find_program("{$kill}");
                unix_system_kill_force($pid);
            }
        }
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "init...\n";
    }
    $q = new mysql_squid_builder();
    if (!$GLOBALS["FORCE"]) {
        $CacheTime = $unix->file_time_min($cacheFile);
        if ($CacheTime < 15) {
            EventsWatchdog("Max 15Mn, current={$CacheTime} ({$cacheFile})...");
            if ($GLOBALS["VERBOSE"]) {
                echo "Max 15Mn, current={$CacheTime}\n";
            }
            return;
        }
    }
    $squid_pid = SQUID_PID();
    if (!$unix->process_exists($squid_pid)) {
        $nohup = $unix->find_program("nohup");
        squid_admin_mysql(0, "Squid-Cache is not running..., start it", null, __FILE__, __LINE__);
        ToSyslog("caches_infos(): Squid-Cache is not running..., start it");
        shell_exec("{$nohup} /etc/init.d/squid start >/dev/null 2>&1 &");
        return;
    }
    @unlink($cacheFile);
    @file_put_contents($cacheFile, time());
    $ttl = $unix->PROCCESS_TIME_MIN($squid_pid);
    if ($unix->PROCCESS_TIME_MIN($squid_pid) < 5) {
        ToSyslog("caches_infos(): squid-cache running only since {$ttl}mn, aborting");
        return;
    }
    ToSyslog("caches_infos(): Starting get Squid-cache informations.");
    $array = $unix->squid_get_cache_infos();
    for ($i = 0; $i < 10; $i++) {
        $check = true;
        if (!is_array($array)) {
            if ($GLOBALS["VERBOSE"]) {
                echo "unix->squid_get_cache_infos() Not an array...\n";
            }
            $check = false;
            sleep(1);
            $array = $unix->squid_get_cache_infos();
            continue;
        }
        if (count($array) == 0) {
            if ($GLOBALS["VERBOSE"]) {
                echo "unix->squid_get_cache_infos() O items !!\n";
            }
            $check = false;
            sleep(1);
            $array = $unix->squid_get_cache_infos();
            continue;
        }
        if ($check) {
            break;
        }
    }
    if (!is_array($array)) {
        if ($GLOBALS["VERBOSE"]) {
            echo "unix->squid_get_cache_infos() Not an array...\n";
        }
        return;
    }
    if (count($array) == 0) {
        if ($GLOBALS["VERBOSE"]) {
            echo basename(__FILE__) . "[" . __LINE__ . "] unix->squid_get_cache_infos() O items !!...\n";
        }
        @file_put_contents("/usr/share/artica-postfix/ressources/logs/web/squid_get_cache_infos.db", serialize($array));
        @chmod("/usr/share/artica-postfix/ressources/logs/web/squid_get_cache_infos.db", 0755);
        return;
    }
    @unlink("/usr/share/artica-postfix/ressources/logs/web/squid_get_cache_infos.db");
    @file_put_contents("/usr/share/artica-postfix/ressources/logs/web/squid_get_cache_infos.db", serialize($array));
    @chmod("/usr/share/artica-postfix/ressources/logs/web/squid_get_cache_infos.db", 0755);
    $uuid = $unix->GetUniqueID();
    $profix = "INSERT IGNORE INTO cachestatus(uuid,cachedir,maxsize,currentsize,pourc) VALUES ";
    while (list($directory, $arrayDir) = each($array)) {
        $directory = trim($directory);
        if ($directory == null) {
            continue;
        }
        if ($GLOBALS["VERBOSE"]) {
            echo "('{$uuid}','{$directory}','{$arrayDir["MAX"]}','{$arrayDir["CURRENT"]}','{$arrayDir["POURC"]}')\n";
        }
        $f[] = "('{$uuid}','{$directory}','{$arrayDir["MAX"]}','{$arrayDir["CURRENT"]}','{$arrayDir["POURC"]}')";
    }
    if (count($f) > 0) {
        $q->QUERY_SQL("DELETE FROM cachestatus WHERE uuid='{$uuid}'");
        $q->QUERY_SQL("{$profix}" . @implode(",", $f));
        if (!$q->ok) {
            echo $q->mysql_error . "\n";
        }
    }
}
function caches_infos()
{
    $unix = new unix();
    $squid_pid = SQUID_PID();
    if (!$unix->process_exists($squid_pid)) {
        $nohup = $unix->find_program("nohup");
        squid_admin_mysql(0, "Squid-Cache is not running...", null, __FILE__, __LINE__);
        return;
    }
    $ttl = $unix->PROCCESS_TIME_MIN($squid_pid);
    if ($unix->PROCCESS_TIME_MIN($squid_pid) < 5) {
        ToSyslog("caches_infos(): squid-cache running only since {$ttl}mn, aborting");
        return;
    }
    ToSyslog("caches_infos(): Starting get Squid-cache informations.");
    $array = $unix->squid_get_cache_infos();
    for ($i = 0; $i < 10; $i++) {
        $check = true;
        if (!is_array($array)) {
            if ($GLOBALS["VERBOSE"]) {
                echo "unix->squid_get_cache_infos() Not an array...\n";
            }
            $check = false;
            sleep(1);
            $array = $unix->squid_get_cache_infos();
            continue;
        }
        if (count($array) == 0) {
            if ($GLOBALS["VERBOSE"]) {
                echo "unix->squid_get_cache_infos() O items !!\n";
            }
            $check = false;
            sleep(1);
            $array = $unix->squid_get_cache_infos();
            continue;
        }
        if ($check) {
            break;
        }
    }
    if (!is_array($array)) {
        if ($GLOBALS["VERBOSE"]) {
            echo "unix->squid_get_cache_infos() Not an array...\n";
        }
        return;
    }
    if (count($array) == 0) {
        if ($GLOBALS["VERBOSE"]) {
            echo basename(__FILE__) . "[" . __LINE__ . "] unix->squid_get_cache_infos() O items !!...\n";
        }
        @file_put_contents("/usr/share/artica-postfix/ressources/logs/web/squid_get_cache_infos.db", serialize($array));
        @chmod("/usr/share/artica-postfix/ressources/logs/web/squid_get_cache_infos.db", 0755);
        return;
    }
    @unlink("/usr/share/artica-postfix/ressources/logs/web/squid_get_cache_infos.db");
    @file_put_contents("/usr/share/artica-postfix/ressources/logs/web/squid_get_cache_infos.db", serialize($array));
    @chmod("/usr/share/artica-postfix/ressources/logs/web/squid_get_cache_infos.db", 0755);
    $q = new mysql_squid_builder();
    $uuid = $unix->GetUniqueID();
    $profix = "INSERT IGNORE INTO cachestatus(uuid,cachedir,maxsize,currentsize,pourc) VALUES ";
    while (list($directory, $arrayDir) = each($array)) {
        $directory = trim($directory);
        if ($directory == null) {
            continue;
        }
        if ($GLOBALS["VERBOSE"]) {
            echo "('{$uuid}','{$directory}','{$arrayDir["MAX"]}','{$arrayDir["CURRENT"]}','{$arrayDir["POURC"]}')\n";
        }
        $f[] = "('{$uuid}','{$directory}','{$arrayDir["MAX"]}','{$arrayDir["CURRENT"]}','{$arrayDir["POURC"]}')";
    }
    if (count($f) > 0) {
        $q->QUERY_SQL("TRUNCATE TABLE cachestatus");
        $q->QUERY_SQL("{$profix}" . @implode(",", $f));
        if (!$q->ok) {
            echo $q->mysql_error . "\n";
        }
    }
}