function tasks(){
	$t=time();
	$q=new mysql_squid_builder();
	$q->CheckDefaultSchedules();
	$html="
	<div id='tasks-$t'></div>
	
	<script>
		LoadAjax('tasks-$t','squid.databases.schedules.php');
	</script>
	
	
	";
	
	echo $html;
	
	
}
Beispiel #2
0
function build_schedules()
{
    $unix = new unix();
    $sock = new sockets();
    $q = new mysql();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $pidTime = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    $pid = $unix->get_pid_from_file($pidfile);
    if ($unix->process_exists($pid, basename(__FILE__))) {
        writelogs("Already executed pid {$pid}", __FILE__, __FUNCTION__, __LINE__);
        return;
    }
    @file_put_contents($pidfile, getmypid());
    $pidTimeINT = $unix->file_time_min($pidTime);
    if (!$GLOBALS["VERBOSE"]) {
        if ($pidTimeINT < 1) {
            writelogs("To short time to execute the process {$pidTime} = {$pidTimeINT}Mn < 1", __FILE__, __FUNCTION__, __LINE__);
            return;
        }
    }
    @file_put_contents($pidTime, time());
    if (!$q->TABLE_EXISTS("system_schedules", "artica_backup")) {
        $task->CheckDefaultSchedules();
    }
    $task = new system_tasks();
    $task->CheckDefaultSchedules();
    $squidbin = $unix->LOCATE_SQUID_BIN();
    if (file_exists($squidbin)) {
        $q = new mysql_squid_builder();
        $q->CheckDefaultSchedules();
    }
    if ($q->COUNT_ROWS("system_schedules", "artica_backup") == 0) {
        echo "Starting......: " . date("H:i:s") . " artica-postfix watchdog (fcron) system_schedules is empty !!\n";
        die;
    }
    $sql = "SELECT * FROM system_schedules WHERE enabled=1";
    $results = $q->QUERY_SQL($sql, "artica_backup");
    if (!$q->ok) {
        echo "Starting......: " . date("H:i:s") . " artica-postfix watchdog (fcron) {$q->mysql_error} on line " . __LINE__ . "\n";
        return;
    }
    $php5 = $unix->LOCATE_PHP5_BIN();
    $WorkingDirectory = dirname(__FILE__);
    $chmod = $unix->find_program("chmod");
    $settings = unserialize(base64_decode($sock->GET_INFO("FcronSchedulesParams")));
    if (!isset($settings["max_nice"])) {
        $settings["max_nice"] = null;
    }
    if (!isset($settings["max_load_wait"])) {
        $settings["max_load_wait"] = null;
    }
    if (!isset($settings["max_load_avg5"])) {
        $settings["max_load_avg5"] = null;
    }
    if (!is_numeric($settings["max_load_avg5"])) {
        $settings["max_load_avg5"] = "2.5";
    }
    if (!is_numeric($settings["max_load_wait"])) {
        $settings["max_load_wait"] = "10";
    }
    if (!is_numeric($settings["max_nice"])) {
        $settings["max_nice"] = "19";
    }
    $max_load_wait = $settings["max_load_wait"];
    @unlink("/etc/cron.d/artica-cron");
    foreach (glob("/etc/cron.d/*") as $filename) {
        $file = basename($filename);
        if (preg_match("#syssch-[0-9]+#", $filename)) {
            if ($GLOBALS["OUTPUT"]) {
                echo "Starting......: " . date("H:i:s") . " artica-postfix watchdog (fcron) remove {$filename}\n";
            }
            @unlink($filename);
        }
    }
    @unlink("/etc/artica-postfix/TASKS_CACHE.DB");
    @unlink("/etc/artica-postfix/system.schedules");
    $TRASNCODE["0 * * * *"] = "1h";
    $TRASNCODE["0 4,8,12,16,20 * * *"] = "4h";
    $TRASNCODE["0 0,4,8,12,16,20 * * *"] = "4h";
    $TRASNCODE["0 3,5,7,9,11,13,15,17,19,23 * * *"] = "3h";
    $TRASNCODE["0 0,3,5,7,9,11,13,15,17,19,23 * * *"] = "3h";
    $TRASNCODE["0 2,4,6,8,10,12,14,16,18,20,22 * * *"] = "2h";
    $TRASNCODE["0 0,2,4,6,8,10,12,14,16,18,20,22 * * *"] = "2h";
    $TRASNCODE["20,40,59 * * * *"] = "20";
    $TRASNCODE["0,20,40 * * * *"] = "20";
    $TRASNCODE["0,10,20,30,40,50 * * * *"] = "10";
    $nice = $unix->EXEC_NICE();
    build_system_defaults();
    $me = __FILE__;
    while ($ligne = mysql_fetch_assoc($results)) {
        $TaskType = $ligne["TaskType"];
        $TimeText = $ligne["TimeText"];
        if ($TaskType == 0) {
            continue;
        }
        if ($ligne["TimeText"] == null) {
            continue;
        }
        $md5 = md5("{$TimeText}{$TaskType}");
        if (isset($alreadydone[$md5])) {
            if ($GLOBALS["OUTPUT"]) {
                echo "Starting......: " . date("H:i:s") . " artica-postfix watchdog task {$ligne["ID"]} already set\n";
            }
            continue;
        }
        $alreadydone[$md5] = true;
        if (!isset($task->tasks_processes[$TaskType])) {
            if ($GLOBALS["OUTPUT"]) {
                echo "Starting......: " . date("H:i:s") . " artica-postfix watchdog (fcron) Unable to stat task process of `{$TaskType}`\n";
            }
            continue;
        }
        if (isset($task->task_disabled[$TaskType])) {
            if ($GLOBALS["OUTPUT"]) {
                echo "Starting......: " . date("H:i:s") . " artica-postfix`{$TaskType}` disabled\n";
            }
            continue;
        }
        $script = $task->tasks_processes[$TaskType];
        $f = array();
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " scheduling {$script} /etc/cron.d/syssch-{$ligne["ID"]}\n";
        }
        $cmdline = trim("{$nice} {$php5} {$me} --run {$ligne["ID"]}");
        $f[] = "MAILTO=\"\"";
        $f[] = "{$ligne["TimeText"]}  root {$cmdline} >/dev/null 2>&1";
        $f[] = "";
        @file_put_contents("/etc/cron.d/syssch-{$ligne["ID"]}", @implode("\n", $f));
    }
    shell_exec("/etc/init.d/cron reload");
}
function Addefaults()
{
    $q = new mysql_squid_builder();
    $q->CheckDefaultSchedules();
    $tpl = new templates();
    echo $tpl->javascript_parse_text("{add_defaults_added}");
}
Beispiel #4
0
function build_schedules($notfcron = false)
{
    $unix = new unix();
    $sock = new sockets();
    $squid = $unix->LOCATE_SQUID_BIN();
    if (!is_file($squid)) {
        return;
    }
    @mkdir("/var/log/artica-postfix/youtube", 0755, true);
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $pidTime = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    $pid = $unix->get_pid_from_file($pidfile);
    if ($unix->process_exists($pid, basename(__FILE__))) {
        writelogs("Already executed pid {$pid}", __FILE__, __FUNCTION__, __LINE__);
        return;
    }
    $EnableRemoteStatisticsAppliance = $sock->GET_INFO("EnableRemoteStatisticsAppliance");
    if (!is_numeric($EnableRemoteStatisticsAppliance)) {
        $EnableRemoteStatisticsAppliance = 0;
    }
    @file_put_contents($pidfile, getmypid());
    $pidTimeINT = $unix->file_time_min($pidTime);
    if (!$GLOBALS["VERBOSE"]) {
        if ($pidTimeINT < 2) {
            writelogs("To short time to execute the process", __FILE__, __FUNCTION__, __LINE__);
            return;
        }
    }
    @file_put_contents($pidTime, time());
    if (!$unix->IsSquidTaskCanBeExecuted()) {
        if ($GLOBALS["VERBOSE"]) {
            echo "These tasks cannot be executed in this server\n";
        }
        return;
    }
    $q = new mysql_squid_builder();
    $q->CheckDefaultSchedules();
    if ($q->COUNT_ROWS("webfilters_schedules") == 0) {
        return;
    }
    $sql = "SELECT *  FROM webfilters_schedules WHERE enabled=1";
    $results = $q->QUERY_SQL($sql);
    if (!$q->ok) {
        return;
    }
    @unlink("/etc/cron.d/SquidTailInjector");
    $php5 = $unix->LOCATE_PHP5_BIN();
    $WorkingDirectory = dirname(__FILE__);
    $chmod = $unix->find_program("chmod");
    foreach (glob("/etc/cron.d/*") as $filename) {
        $file = basename($filename);
        if (preg_match("#squidsch-[0-9]+#", $filename)) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Removing old task {$file}\n";
            }
            @unlink($filename);
        }
    }
    @unlink("/etc/artica-postfix/TASKS_SQUID_CACHE.DB");
    $settings = unserialize(base64_decode($sock->GET_INFO("FcronSchedulesParams")));
    if (!isset($settings["max_nice"])) {
        $settings["max_nice"] = 19;
    }
    if (!isset($settings["max_load_avg5"])) {
        $settings["max_load_avg5"] = 3;
    }
    if (!isset($settings["max_load_wait"])) {
        $settings["max_load_wait"] = 10;
    }
    if (!is_numeric($settings["max_load_avg5"])) {
        $settings["max_load_avg5"] = "3";
    }
    if (!is_numeric($settings["max_load_wait"])) {
        $settings["max_load_wait"] = "10";
    }
    if (!is_numeric($settings["max_nice"])) {
        $settings["max_nice"] = "19";
    }
    $max_load_wait = $settings["max_load_wait"];
    $finalsettings = "nice({$settings["max_nice"]}),lavg5({$settings["max_load_avg5"]}),until({$max_load_wait})";
    @unlink("/etc/artica-postfix/squid.schedules");
    $nice = EXEC_NICE();
    $q = new mysql_squid_builder();
    $c = 0;
    $d = 0;
    while ($ligne = mysql_fetch_assoc($results)) {
        $allminutes = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59";
        $TaskType = $ligne["TaskType"];
        $TimeText = $ligne["TimeText"];
        if ($TaskType == 0) {
            continue;
        }
        if ($ligne["TimeText"] == null) {
            continue;
        }
        if ($EnableRemoteStatisticsAppliance == 1) {
            if ($q->tasks_remote_appliance[$TaskType]) {
                $d++;
                continue;
            }
        }
        $md5 = md5("{$TimeText}{$TaskType}");
        if (isset($alreadydone[$md5])) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Starting......: " . date("H:i:s") . " artica-postfix watchdog task {$ligne["ID"]} already set\n";
            }
            continue;
        }
        $alreadydone[$md5] = true;
        if (!isset($q->tasks_processes[$TaskType])) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Starting......: " . date("H:i:s") . " artica-postfix task {$ligne["ID"]} no such task...\n";
            }
            $d++;
            continue;
        }
        if (isset($q->tasks_disabled[$TaskType])) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Starting......: " . date("H:i:s") . " artica-postfix task {$ligne["ID"]} is disabled or did not make sense...\n";
            }
            $d++;
            continue;
        }
        $script = $q->tasks_processes[$TaskType];
        if ($GLOBALS["VERBOSE"]) {
            echo "Starting......: " . date("H:i:s") . " artica-postfix create task {$ligne["ID"]} type {$TaskType}..\n";
        }
        if (trim($ligne["TimeText"] == "{$allminutes} * * * *")) {
            $ligne["TimeText"] = "* * * * *";
        }
        $f = array();
        $f[] = "MAILTO=\"\"";
        $f[] = "{$ligne["TimeText"]}  root {$nice} {$php5} {$WorkingDirectory}/exec.schedules.php --run-squid {$ligne["ID"]} >/dev/null 2>&1";
        $f[] = "";
        @file_put_contents("/etc/cron.d/squidsch-{$ligne["ID"]}", @implode("\n", $f));
        $c++;
        continue;
        if (trim($ligne["TimeText"] == "20,40,59 * * * *")) {
            $f[] = "@{$finalsettings},mail(false) 20 {$php5} {$WorkingDirectory}/{$script} --schedule-id={$ligne["ID"]} >/dev/null 2>&1";
            continue;
        }
        if (trim($ligne["TimeText"] == "0 * * * *")) {
            $f[] = "@{$finalsettings},mail(false) 1h {$php5} {$WorkingDirectory}/{$script} --schedule-id={$ligne["ID"]} >/dev/null 2>&1";
            continue;
        }
        if (trim($ligne["TimeText"] == "10,20,30,40,50 * * * *")) {
            $f[] = "@{$finalsettings},mail(false) 10 {$php5} {$WorkingDirectory}/{$script} --schedule-id={$ligne["ID"]} >/dev/null 2>&1";
            continue;
        }
        if (trim($ligne["TimeText"] == "0 0,3,5,7,9,11,13,15,17,19,23 * * *")) {
            $f[] = "@{$finalsettings},mail(false) 3h {$php5} {$WorkingDirectory}/{$script} --schedule-id={$ligne["ID"]} >/dev/null 2>&1";
            continue;
        }
        if (trim($ligne["TimeText"] == "0 2,4,6,8,10,12,14,16,18,20,22 * * *")) {
            $f[] = "@{$finalsettings},mail(false) 2h {$php5} {$WorkingDirectory}/{$script} --schedule-id={$ligne["ID"]} >/dev/null 2>&1";
            continue;
        }
        if (trim($ligne["TimeText"] == "0 0,2,4,6,8,10,12,14,16,18,20,22 * * *")) {
            $f[] = "@{$finalsettings},mail(false) 2h {$php5} {$WorkingDirectory}/{$script} --schedule-id={$ligne["ID"]} >/dev/null 2>&1";
            continue;
        }
        $f[] = "&{$finalsettings},mail(false) {$ligne["TimeText"]} {$php5} {$WorkingDirectory}/{$script} --schedule-id={$ligne["ID"]} >/dev/null 2>&1";
    }
    @file_put_contents("/etc/artica-postfix/squid.schedules", implode("\n", $f));
    if ($notfcron) {
        echo "Starting......: " . date("H:i:s") . " Squid {$c} scheduled tasks ({$d} disabled)\n";
        return;
    }
    $cron_path = $unix->find_program("cron");
    $kill = $unix->find_program("kill");
    $cron_pid = null;
    if (is_file("/var/run/cron.pid")) {
        $cron_pid = $unix->get_pid_from_file("/var/run/cron.pid");
    }
    if (!$unix->process_exists($cron_pid)) {
        $cron_pid = 0;
    }
    if (!is_numeric($cron_pid) or $cron_pid < 5) {
        $cron_pid = $unix->PIDOF("{$cron_path}");
    }
    if ($cron_pid > 5) {
        if ($GLOBALS["VERBOSE"]) {
            echo "Starting......: " . date("H:i:s") . " artica-postfix reloading {$cron_path} [{$cron_pid}]...\n";
        }
        unix_system_HUP("{$cron_pid}");
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "Starting......: " . date("H:i:s") . " artica-postfix reloading fcron...\n";
    }
    $nohup = $unix->find_program("nohup");
    shell_exec("{$nohup} /etc/init.d/artica-postfix restart fcron >/dev/null 2>&1 &");
    shell_exec("/etc/init.d/cron reload");
}