function restart_squid($aspid = false)
{
    $unix = new unix();
    $sock = new sockets();
    build_progress_restart("{please_wait}", 10);
    if (!$aspid) {
        $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
        $pid = $unix->get_pid_from_file($pidfile);
        if ($unix->process_exists($pid, basename(__FILE__))) {
            $time = $unix->PROCCESS_TIME_MIN($pid);
            system_admin_events("restart_squid::Already task running PID {$pid} since {$time}mn", __FUNCTION__, __FILE__, __LINE__, "proxy");
            build_progress_restart("{failed}: Already task running PID {$pid} since {$time}mn", 110);
            return;
        }
        @file_put_contents($pidfile, getmypid());
    }
    $squidbin = $unix->LOCATE_SQUID_BIN();
    if (!is_file($squidbin)) {
        build_progress_restart("{failed}", 110);
        if ($GLOBALS["OUTPUT"]) {
            echo "Restart.......: " . date("H:i:s") . " Squid-cache, not installed\n";
        }
        return;
    }
    $t1 = time();
    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";
        }
    }
    if ($GLOBALS["OUTPUT"]) {
        echo "Restart.......: " . date("H:i:s") . " Restarting Squid-cache...\n";
    }
    $suffix = null;
    $reconfigure = null;
    $suffix = get_action_script_source();
    if ($GLOBALS["RECONFIGURE"]) {
        $reconfigure = " - with reconfigure";
    }
    build_progress_restart("{stopping_service}", 20);
    stop_squid(true);
    $date = date("Y-m-d H:i:s");
    squid_admin_mysql(1, "Restarting Squid-Cache service: {$suffix}{$reconfigure}", "{$suffix} - {$date}\n a process ask to restart it\nCalled by function:{$sourcefunction} in line {$sourceline}", __FILE__, __LINE__);
    $php5 = $unix->LOCATE_PHP5_BIN();
    if ($GLOBALS["RECONFIGURE"]) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " Reconfiguring Squid-cache...\n";
        }
        build_progress_restart("{building_parameters}", 30);
        system("{$php5} /usr/share/artica-postfix/exec.squid.php --build --force");
    }
    if ($GLOBALS["OUTPUT"]) {
        echo "Restart.......: " . date("H:i:s") . " Stopping Squid...\n";
    }
    if ($GLOBALS["SWAPSTATE"]) {
        $GLOBALS["FORCE"] = true;
        swap_state();
    }
    if ($GLOBALS["OUTPUT"]) {
        echo "Restart.......: Starting Squid...\n";
    }
    build_progress_restart("{starting_service}", 40);
    start_squid(true);
    $took = $unix->distanceOfTimeInWords($t1, time());
    $EnableTransparent27 = intval($sock->GET_INFO("EnableTransparent27"));
    if ($EnableTransparent27 == 1) {
        build_progress_restart("{restart_cache_nat}", 60);
        if ($GLOBALS["OUTPUT"]) {
            echo "Restart.......: Restarting Cache NAT\n";
        }
        shell_exec("/etc/init.d/squid-nat restart --force 2>&1 >> /usr/share/artica-postfix/ressources/logs/web/restart.squid");
    }
    if ($GLOBALS["BY_FRAMEWORK"]) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Restart.......: Restarting DNS...\n";
        }
        build_progress_restart("{restarting_dns_service}", 70);
        shell_exec("/etc/init.d/dnsmasq restart --force --framework 2>&1 >> /usr/share/artica-postfix/ressources/logs/web/restart.squid");
    }
    build_progress_restart("{starting_service} {done}", 100);
    system_admin_events("Squid restarted took: {$took}", __FUNCTION__, __FILE__, __LINE__, "proxy");
}
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__);
}