コード例 #1
0
function LogRotateTimeAndSize($BaseWorkDir)
{
    if (!is_dir($BaseWorkDir)) {
        return;
    }
    $unix = new unix();
    $sock = new sockets();
    $php5 = $unix->LOCATE_PHP5_BIN();
    $squidbin = $unix->LOCATE_SQUID_BIN();
    $echo = $unix->find_program("echo");
    $syslog = new mysql_storelogs();
    $LogsRotateDefaultSizeRotation = $sock->GET_INFO("LogsRotateDefaultSizeRotation");
    $SquidRotateOnlySchedule = intval($sock->GET_INFO("SquidRotateOnlySchedule"));
    if (!is_numeric($LogsRotateDefaultSizeRotation)) {
        $LogsRotateDefaultSizeRotation = 100;
    }
    $LogsRotateDeleteSize = intval($sock->GET_INFO("LogsRotateDeleteSize"));
    if ($LogsRotateDeleteSize == 0) {
        $LogsRotateDeleteSize = 5000;
    }
    $syslog = new mysql_storelogs();
    if ($BaseWorkDir == "/var/log/squid") {
        return;
    }
    if (!($handle = opendir($BaseWorkDir))) {
        return;
    }
    while (false !== ($fileZ = readdir($handle))) {
        if ($fileZ == ".") {
            continue;
        }
        if ($fileZ == "..") {
            continue;
        }
        $filename = "{$BaseWorkDir}/{$fileZ}";
        if (is_dir($filename)) {
            continue;
        }
        $size = $unix->file_size($filename);
        $sizePHP = round(unix_file_size($filename) / 1024);
        $size = round($size / 1024 / 1000, 2);
        if ($size >= $LogsRotateDeleteSize) {
            $syslog->events("{$filename} -> set to 0bytes {$size} >{$LogsRotateDeleteSize}", __FUNCTION__, __LINE__);
            shell_exec("{$echo} \" \" > \"{$filename}\"");
            $GLOBALS["DELETED_SIZE"] = $GLOBALS["DELETED_SIZE"] + $sizePHP;
            $GLOBALS["DELETED_FILES"]++;
            $GLOBALS["DELETED_FILES"][] = "{$filename}";
            continue;
        }
        if ($GLOBALS["VERBOSE"]) {
            echo "Found file: {$filename} {$size}M !== {$LogsRotateDefaultSizeRotation}M\n";
        }
        if ($fileZ == "cache.log") {
            if ($size > $LogsRotateDefaultSizeRotation) {
                if ($SquidRotateOnlySchedule == 0) {
                    exec("{$php5} /usr/share/artica-postfix/exec.squid.php --rotate --force 2>&1", $results);
                    squid_admin_mysql(2, "{$fileZ} {$size}M Exceed {$LogsRotateDefaultSizeRotation}M", "Artica flush a log rotation\n" . @implode("\n", $results));
                    return;
                } else {
                    if ($size > $LogsRotateDeleteSize) {
                        $warnfile = "/etc/artica-postfix/pids/squid.rotate.cache.warn";
                        if ($unix->file_time_min($warnfile) > 30) {
                            squid_admin_mysql(1, "Warning {$fileZ} {$size}M Exceed {$LogsRotateDeleteSize}M", null, __FILE__, __LINE__);
                            @unlink($warnfile);
                            @file_put_contents($warnfile, time());
                        }
                    }
                }
            }
            continue;
        }
        if ($fileZ == "external-acl.log") {
            continue;
        }
        if ($fileZ == "ufdbguardd.log") {
            continue;
        }
        if ($fileZ == "access.log") {
            if ($size > $LogsRotateDefaultSizeRotation) {
                if ($SquidRotateOnlySchedule == 0) {
                    $syslog->events("{$php5} /usr/share/artica-postfix/exec.squid.php --rotate --smooth", __FUNCTION__, __LINE__);
                    exec("{$php5} /usr/share/artica-postfix/exec.squid.php --rotate --smooth 2>&1", $results);
                    rotate_admin_events("{$fileZ} {$size}M Exceed {$LogsRotateDefaultSizeRotation}M perform a Proxy log rotation", __FUNCTION__, __FILE__, __LINE__, "proxy", $GLOBALS["SCHEDULE_ID"]);
                    squid_admin_mysql(2, "{$fileZ} {$size}M Exceed {$LogsRotateDefaultSizeRotation}M", "Artica flush a log rotation\n" . @implode("\n", $results));
                    return;
                } else {
                    if ($size > $LogsRotateDeleteSize) {
                        $warnfile = "/etc/artica-postfix/pids/squid.rotate.access.warn";
                        if ($unix->file_time_min($warnfile) > 30) {
                            $syslog->events("Warning {$fileZ} {$size}M Exceed {$LogsRotateDeleteSize}M", __FUNCTION__, __LINE__);
                            rotate_admin_events(1, "Warning {$fileZ} {$size}M Exceed {$LogsRotateDeleteSize}M", null, __FILE__, __LINE__);
                            squid_admin_mysql(1, "Warning {$fileZ} {$size}M Exceed {$LogsRotateDeleteSize}M", null, __FILE__, __LINE__);
                            @unlink($warnfile);
                            @file_put_contents($warnfile, time());
                        }
                    }
                }
            }
        }
        if ($fileZ == "netdb.state") {
            continue;
        }
        $time = $unix->file_time_min($filename);
        $filedate = date('Y-m-d H:i:s', filemtime($filename));
        if (preg_match("#access\\.log[0-9]+\$#", $filename)) {
            continue;
        }
        if (preg_match("#access\\.log\\.[0-9]+\$#", $filename)) {
            continue;
        }
        if (preg_match("#sarg\\.log\\.[0-9]+\$#", $filename)) {
            @mkdir("/home/squid/sarg_logs");
            $syslog->events("copy {$filename} -> /home/squid/sarg_logs/" . basename($filename) . "." . filemtime($filename), __FUNCTION__, __LINE__);
            if (@copy($filename, "/home/squid/sarg_logs/" . basename($filename) . "." . filemtime($filename))) {
                @unlink($filename);
            }
            continue;
        }
        $extension = pathinfo($filename, PATHINFO_EXTENSION);
        if ($GLOBALS["VERBOSE"]) {
            echo "Analyze {$filename} ({$extension}) {$filedate}\n";
        }
        if (is_numeric($extension)) {
            $syslog->events("ROTATE_TOMYSQL {$filename}", __FUNCTION__, __LINE__);
            $syslog->ROTATE_TOMYSQL($filename, $filedate);
            continue;
        }
        if ($extension == "gz") {
            $syslog->events("ROTATE_TOMYSQL {$filename}", __FUNCTION__, __LINE__);
            $syslog->ROTATE_TOMYSQL($filename, $filedate);
            continue;
        }
        if ($extension == "state") {
            continue;
        }
        if ($extension == "bz2") {
            $syslog->events("ROTATE_TOMYSQL {$filename}", __FUNCTION__, __LINE__);
            $syslog->ROTATE_TOMYSQL($filename, $filedate);
            continue;
        }
        $time = $unix->file_time_min($filename);
        echo "{$filename} {$time}Mn\n";
        $syslog->events("ROTATE_TOMYSQL {$filename}", __FUNCTION__, __LINE__);
        $syslog->ROTATE_TOMYSQL($filename, $filedate);
    }
}
コード例 #2
0
function xscan()
{
    $ARRAY = array();
    $sock = new sockets();
    $unix = new unix();
    $Pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $PidTime = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    // /etc/artica-postfix/pids/exec.clean.logs.php.squidClean.time
    if ($GLOBALS["VERBOSE"]) {
        echo "Pidfile: {$Pidfile}\n";
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "PidTime: {$PidTime}\n";
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "Memory Free: " . $unix->TOTAL_MEMORY_MB_FREE() . "/" . $unix->TOTAL_MEMORY_MB() . "\n";
        echo "Memory Use: " . $unix->TOTAL_MEMORY_MB_USED() . " - " . $unix->TOTAL_MEM_POURCENT_USED() . "%\n";
    }
    $pid = $unix->get_pid_from_file($Pidfile);
    if ($unix->process_exists($pid, basename(__FILE__))) {
        if ($GLOBALS["VERBOSE"]) {
            echo "Aborting Task already running pid {$pid} " . __FUNCTION__ . "()\n";
        }
        return;
    }
    if (!$GLOBALS["FORCE"]) {
        $TimeExec = $unix->file_time_min($PidTime);
        if ($TimeExec < 20) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Aborting Task {$TimeExec}mn, require 20mn " . __FUNCTION__ . "()\n";
            }
            return;
        }
    }
    @unlink($PidTime);
    @file_put_contents($PidTime, time());
    @file_put_contents($Pidfile, getmypid());
    xscan2();
    $q = new mysql_storelogs();
    $LogRotatePath = $sock->GET_INFO("LogRotatePath");
    $SystemLogsPath = $sock->GET_INFO("SystemLogsPath");
    $BackupMaxDays = intval($sock->GET_INFO("BackupMaxDays"));
    $BackupMaxDaysDir = $sock->GET_INFO("BackupMaxDaysDir");
    if ($BackupMaxDaysDir == null) {
        $BackupMaxDaysDir = "/home/logrotate_backup";
    }
    if (!is_dir("{$BackupMaxDaysDir}")) {
        @mkdir("{$BackupMaxDaysDir}", true);
    }
    if ($BackupMaxDays == 0) {
        $BackupMaxDays = 30;
    }
    $BackupLogsMaxStoragePercent = intval($sock->GET_INFO("BackupLogsMaxStoragePercent"));
    if ($BackupLogsMaxStoragePercent == 0) {
        $BackupLogsMaxStoragePercent = 50;
    }
    $BackupMaxDaysMins = $BackupMaxDays * 1440;
    echo "Max TTL : {$BackupMaxDaysMins} Minutes\n";
    $files = $unix->DirFiles($BackupMaxDaysDir);
    while (list($filepath, $none) = each($files)) {
        $filename = "{$BackupMaxDaysDir}/{$filepath}";
        $filetime = $unix->file_time_min($filename);
        echo "{$filepath} = {$filetime}/{$BackupMaxDaysMins}\n";
        if ($filetime > $BackupMaxDaysMins) {
            rotate_admin_events("Removed {$filepath} {$filetime}mn, exceed {$BackupMaxDays} days", __FUNCTION__, __FILE__, __LINE__);
            @unlink($filename);
            continue;
        }
        $filesecs = filemtime($filename);
        $ARRAY[$filesecs] = $filepath;
    }
    $DIRPART_INFO = $unix->DIRPART_INFO($BackupMaxDaysDir);
    $TOTAL_PART = $DIRPART_INFO["TOT"];
    $percent = $BackupLogsMaxStoragePercent / 100;
    $TOTAL_AVAILABLE = $TOTAL_PART * $percent;
    $DIRSIZE = $unix->DIRSIZE_BYTES($BackupMaxDaysDir);
    $TOTAL_PART = $DIRPART_INFO["TOT"] / 1024;
    $q->events("Directory size = {$DIRSIZE}/{$TOTAL_AVAILABLE} " . FormatBytes($DIRSIZE / 1024, true) . "/" . FormatBytes($TOTAL_AVAILABLE / 1024, true), __FUNCTION__, __LINE__);
    if ($DIRSIZE > $TOTAL_AVAILABLE) {
        CleanPercent($BackupMaxDaysDir, $TOTAL_AVAILABLE);
    }
}