function check_status()
{
    $unix = new unix();
    $php = $unix->LOCATE_PHP5_BIN();
    build_progress_status2("{checking_proxy_service}", 20);
    sleep(1);
    $pid = SQUID_PID();
    echo "PID: {$pid}\n";
    if ($unix->process_exists($pid)) {
        $ttl = $unix->PROCCESS_TIME_MIN($pid);
        build_progress_status2("{APP_SQUID}: {running} {since} {$ttl}Mn", 30);
        echo "Running since {$ttl}Mn\n";
    } else {
        build_progress_status2("{APP_SQUID}: {starting_service}", 30);
        start_squid(true);
    }
    $pid = SQUID_PID();
    echo "PID: {$pid}\n";
    if (!$unix->process_exists($pid)) {
        build_progress_status2("{APP_SQUID}: {failed}", 110);
    }
    $GLOBALS["FORCE"] = true;
    build_progress_status2("{APP_SQUID}: {remove_cached_datas}", 50);
    sleep(1);
    echo "Remove ALL_SQUID_STATUS\n";
    @unlink("/usr/share/artica-postfix/ressources/logs/web/squid.status.html");
    @unlink("/usr/share/artica-postfix/ressources/logs/web/status.right.image.cache");
    @unlink("/usr/share/artica-postfix/ressources/databases/ALL_SQUID_STATUS");
    $cmd = "{$php} /usr/share/artica-postfix/exec.status.php --all-squid --nowachdog >/usr/share/artica-postfix/ressources/databases/ALL_SQUID_STATUS 2>&1";
    echo "{$cmd}\n";
    shell_exec($cmd);
    build_progress_status2("{APP_SQUID}: {checking_caches}", 55);
    sleep(1);
    CheckGlobalInfos();
    build_progress_status2("{APP_SQUID}: {checking_caches}", 60);
    sleep(1);
    CheckStoreDirs(true);
    build_progress_status2("{APP_SQUID}: {checking_caches}", 61);
    sleep(1);
    BuilRequestsStats();
    build_progress_status2("{APP_SQUID}: {checking_caches}", 62);
    counters(true);
    sleep(1);
    build_progress_status2("{APP_SQUID}: {checking_caches}", 63);
    ALLKIDS(true);
    sleep(1);
    build_progress_status2("{APP_SQUID}: {checking_caches}", 64);
    sleep(1);
    build_progress_status2("{APP_SQUID}: {checking_caches}", 70);
    sleep(1);
    system("{$php} /usr/share/artica-postfix/exec.cache.pages.php --force");
    build_progress_status2("{APP_SQUID}: {done}", 100);
    sleep(1);
}
function CHECK_MEMORY_USE_ACTION()
{
    $sys = new os_system();
    $unix = new unix();
    $mem = $sys->realMemory();
    $pourc = $mem["ram"]["percent"];
    $ram_used = $mem["ram"]["used"];
    $ram_total = $mem["ram"]["total"];
    $MAX_MEM_ALERT_TTIME = "/etc/artica-postfix/pids/MAX_MEM_ALERT_TTIME";
    $MAX_MEM_PRC_TTIME = "/etc/artica-postfix/pids/MAX_MEM_PRC_TTIME";
    $ram_log[] = "Before Action = {$pourc}% {$ram_used}/{$ram_total}";
    $w = new squid_watchdog();
    $MonitConfig = $w->MonitConfig;
    $MAX_MEM_RST_MYSQL = $MonitConfig["MAX_MEM_RST_MYSQL"];
    $MAX_MEM_RST_UFDB = $MonitConfig["MAX_MEM_RST_UFDB"];
    $MAX_MEM_RST_APACHE = $MonitConfig["MAX_MEM_RST_APACHE"];
    $MAX_MEM_RST_SQUID = $MonitConfig["MAX_MEM_RST_SQUID"];
    $MAX_MEM_PRC = $MonitConfig["MAX_MEM_PRC"];
    $text_ram = FormatBytes($ram_used, true) . "/" . FormatBytes($ram_total, true);
    Events("#1 Memory {$pourc}% Used:{$ram_used} Total: {$ram_total}");
    $unix->send_email_events("Memory exceed {$MAX_MEM_PRC}% ({$text_ram}) [action=stop-services]", "MySQL and Categories database will be restarted\n", "system");
    squid_admin_mysql(0, "Memory exceed {$MAX_MEM_PRC}% ({$text_ram}) [action=stop-services]", null, __FILE__, __LINE__);
    if ($MAX_MEM_RST_MYSQL == 1) {
        squid_admin_mysql(1, "Memory exceed - Restarting MySQL services", null, __FILE__, __LINE__);
        shell_exec("/etc/init.d/mysql restart --force --framework=" . basename(__FILE__));
        shell_exec("/etc/init.d/squid-db restart --force");
        shell_exec("/etc/init.d/ufdbcat restart --force");
        $mem = $sys->realMemory();
        $pourc = $mem["ram"]["percent"];
        $ram_used = $mem["ram"]["used"];
        $ram_total = $mem["ram"]["total"];
        $text_ram = FormatBytes($ram_used, true) . "/" . FormatBytes($ram_total, true);
        $ram_log[] = "After restarting MySQL services {$pourc}% {$text_ram}";
    }
    $mem = $sys->realMemory();
    $pourc = $mem["ram"]["percent"];
    $ram_used = $mem["ram"]["used"];
    $ram_total = $mem["ram"]["total"];
    Events("#2 Memory {$pourc}% Used:{$ram_used} Total: {$ram_total}");
    if ($pourc < $MAX_MEM_PRC) {
        $report = $unix->ps_mem_report();
        squid_admin_mysql(1, "Memory OK {$pourc}% [action=report]", @implode("\n", $ram_log) . "\n{$report}", __FILE__, __LINE__);
        @unlink($MAX_MEM_ALERT_TTIME);
        @unlink($MAX_MEM_PRC_TTIME);
        return;
    }
    if ($MAX_MEM_RST_UFDB == 1) {
        squid_admin_mysql(1, "Memory exceed {$pourc}% - Restarting Webfiltering service", null, __FILE__, __LINE__);
        shell_exec("/etc/init.d/ufdb restart --force --framework=" . basename(__FILE__));
        $mem = $sys->realMemory();
        $pourc = $mem["ram"]["percent"];
        $ram_used = $mem["ram"]["used"];
        $ram_total = $mem["ram"]["total"];
        $text_ram = FormatBytes($ram_used, true) . "/" . FormatBytes($ram_total, true);
        $ram_log[] = "After restarting Webfiltering service {$pourc}% {$text_ram}";
    }
    $mem = $sys->realMemory();
    $pourc = $mem["ram"]["percent"];
    $ram_used = $mem["ram"]["used"];
    $ram_total = $mem["ram"]["total"];
    Events("#3 Memory {$pourc}% Used:{$ram_used} Total: {$ram_total}");
    if ($pourc < $MAX_MEM_PRC) {
        $report = $unix->ps_mem_report();
        squid_admin_mysql(1, "Memory OK {$pourc}% [action=report]", @implode("\n", $ram_log) . "\n{$report}", __FILE__, __LINE__);
        @unlink($MAX_MEM_ALERT_TTIME);
        @unlink($MAX_MEM_PRC_TTIME);
        return;
    }
    if ($MAX_MEM_RST_APACHE == 1) {
        squid_admin_mysql(1, "Memory exceed {$pourc}% - Restarting Web Servers services", null, __FILE__, __LINE__);
        shell_exec("/etc/init.d/apache2 restart --force --framework=" . basename(__FILE__));
        shell_exec("/etc/init.d/artica-webconsole restart --force --framework=" . basename(__FILE__));
        shell_exec("/etc/init.d/squidguard-http restart --force --framework=" . basename(__FILE__));
        shell_exec("/etc/init.d/artica-memcache restart --force --framework=" . basename(__FILE__));
        shell_exec("/etc/init.d/ntopng restart  --force --framework=" . basename(__FILE__));
        $mem = $sys->realMemory();
        $pourc = $mem["ram"]["percent"];
        $ram_used = $mem["ram"]["used"];
        $ram_total = $mem["ram"]["total"];
        $ram_log[] = "After restarting web servers services {$pourc}% {$ram_used}/{$ram_total}";
    }
    $mem = $sys->realMemory();
    $pourc = $mem["ram"]["percent"];
    $ram_used = $mem["ram"]["used"];
    $ram_total = $mem["ram"]["total"];
    Events("#4 Memory {$pourc}% Used:{$ram_used} Total: {$ram_total}");
    if ($pourc < $MAX_MEM_PRC) {
        $report = $unix->ps_mem_report();
        squid_admin_mysql(1, "Memory OK {$pourc}% [action=report]", @implode("\n", $ram_log) . "\n{$report}", __FILE__, __LINE__);
        @unlink($MAX_MEM_ALERT_TTIME);
        @unlink($MAX_MEM_PRC_TTIME);
        return;
    }
    if ($MAX_MEM_RST_SQUID == 1) {
        squid_admin_mysql(1, "Memory exceed {$pourc}% - Restarting Proxy service", null, __FILE__, __LINE__);
        stop_squid(true);
        start_squid(true);
        $mem = $sys->realMemory();
        $pourc = $mem["ram"]["percent"];
        $ram_used = $mem["ram"]["used"];
        $ram_total = $mem["ram"]["total"];
        $text_ram = FormatBytes($ram_used, true) . "/" . FormatBytes($ram_total, true);
        $ram_log[] = "After restarting Proxy service {$pourc}% {$text_ram}";
    }
    $mem = $sys->realMemory();
    $pourc = $mem["ram"]["percent"];
    $ram_used = $mem["ram"]["used"];
    $ram_total = $mem["ram"]["total"];
    Events("#5 Memory {$pourc}% Used:{$ram_used} Total: {$ram_total}");
    $report = $unix->ps_mem_report();
    if ($pourc < $MAX_MEM_PRC) {
        squid_admin_mysql(1, "Memory OK {$pourc}% [action=report]", @implode("\n", $ram_log) . "\n{$report}", __FILE__, __LINE__);
        @unlink($MAX_MEM_ALERT_TTIME);
        @unlink($MAX_MEM_PRC_TTIME);
        return;
    }
    squid_admin_mysql(0, "Clean memory failed after restarting all services!!! [action=report]", @implode("\n", $ram_log) . "\n{$report}", __FILE__, __LINE__);
}