Example #1
0
function logrotatelogs($nopid = false)
{
    $unix = new unix();
    $sock = new sockets();
    if ($nopid) {
        $pidpath = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
        $pid = @file_get_contents($pidpath);
        if ($unix->process_exists($pid)) {
            $pidtime = $unix->PROCCESS_TIME_MIN($pid);
            system_admin_events(basename(__FILE__) . ":: " . __FUNCTION__ . " Already process {$pid} running since {$pidtime} Mn.. Aborting", __FUNCTION__, __FILE__, __LINE__);
            return;
        }
        @file_put_contents($pidpath, getmypid());
    }
    $echo = $unix->find_program("echo");
    $LogsRotateDeleteSize = $sock->GET_INFO("LogsRotateDeleteSize");
    if (!is_numeric($LogsRotateDeleteSize)) {
        $LogsRotateDeleteSize = 5000;
    }
    include_once dirname(__FILE__) . "/ressources/class.mysql.syslog.inc";
    if ($GLOBALS["VERBOSE"]) {
        echo __FUNCTION__ . " line:" . __LINE__ . "\n";
    }
    $q = new mysql_syslog();
    if ($q->COUNT_ROWS("logrotate") == 0) {
        $q->CheckDefaults();
    }
    $sql = "SELECT RotateFiles FROM logrotate WHERE enabled=1";
    $results = $q->QUERY_SQL($sql);
    if (!$q->ok) {
        echo $q->mysql_error;
    }
    while ($ligne = mysql_fetch_assoc($results)) {
        $filepath = $ligne["RotateFiles"];
        if (strpos($filepath, "*") > 0) {
            if ($GLOBALS["VERBOSE"]) {
                echo __FUNCTION__ . ":: Scanning {$filepath} line:" . __LINE__ . "\n";
            }
            foreach (glob($filepath) as $filename) {
                $size = $unix->file_size($filename);
                $size = $size / 1024;
                $size = round($size / 1000, 2);
                $ARRAY[$filename] = $size;
            }
        } else {
            if (is_file($filepath)) {
                $size = $unix->file_size($filepath);
                $size = $size / 1024;
                $size = round($size / 1000, 2);
                $ARRAY[$filepath] = $size;
            }
            if (is_dir($filepath)) {
                while (list($num, $filename) = each($f)) {
                    $filepath = "/var/log/{$filename}";
                    $f = $unix->DirFiles("{$filepath}");
                    $size = $unix->file_size($filepath);
                    $size = $size / 1024;
                    $size = round($size / 1000, 2);
                    $ARRAY[$filepath] = $size;
                }
            }
        }
    }
    $f = $unix->DirFiles("/var/log");
    while (list($num, $filename) = each($f)) {
        $filepath = "/var/log/{$filename}";
        $size = $unix->file_size($filepath);
        $size = $size / 1024;
        $size = round($size / 1000, 2);
        $ARRAY[$filepath] = $size;
    }
    $f = $unix->DirFiles("/var/log/artica-postfix");
    while (list($num, $filename) = each($f)) {
        $filepath = "/var/log/artica-postfix/{$filename}";
        $size = $unix->file_size($filepath);
        $size = $size / 1024;
        $size = round($size / 1000, 2);
        $ARRAY[$filepath] = $size;
    }
    $restart = false;
    while (list($filepath, $sizeM) = each($ARRAY)) {
        if ($sizeM > $LogsRotateDeleteSize) {
            shell_exec("{$echo} \"\" >{$filepath}");
            $restart = true;
            $unix->send_email_events("{$filepath} was cleaned ({$sizeM}M)", "It exceed maximal size {$LogsRotateDeleteSize}M", "system");
        }
    }
    if ($restart) {
        shell_exec("/etc/init.d/syslog restart");
        shell_exec("/etc/init.d/artica-syslog restart");
        shell_exec("/etc/init.d/auth-tail restart");
        shell_exec("/etc/init.d/postfix-logger restart");
    }
}
function restore($filename, $storeid)
{
    $filename = trim($filename);
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . ".{$filename}.pid";
    $pid = @file_get_contents("{$pidfile}");
    if ($unix->process_exists($pid, basename(__FILE__))) {
        die;
    }
    @file_put_contents($pidfile, getmypid());
    $EnableSyslogDB = @file_get_contents("/etc/artica-postfix/settings/Daemons/EnableSyslogDB");
    if (!is_numeric($EnableSyslogDB)) {
        $EnableSyslogDB = 0;
    }
    @mkdir("/var/log/artica-postfix/squid-brut", 0777, true);
    @mkdir("/var/log/artica-postfix/squid-reverse", 0777, true);
    $GLOBALS["filename"] = $filename;
    $sock = new sockets();
    $TempDir = "/home/artica-extract-temp";
    @mkdir($TempDir, 0777);
    @chown($TempDir, "mysql");
    @chdir($TempDir, "mysql");
    $BackupMaxDaysDir = $sock->GET_INFO("BackupMaxDaysDir");
    $bzip2 = $unix->find_program("bzip2");
    $gunzip = $unix->find_program("gunzip");
    progress("Extract {$filename} from MySQL database into {$TempDir}", 4);
    if ($EnableSyslogDB == 1) {
        $q = new mysql_storelogs();
        $sql = "SELECT filecontent INTO DUMPFILE '{$TempDir}/{$filename}' FROM files_store WHERE ID = '{$storeid}'";
        $q->QUERY_SQL($sql);
    } else {
        $q = new mysql_syslog();
        $sql = "SELECT filedata INTO DUMPFILE '{$TempDir}/{$filename}' FROM store WHERE filename = '{$filename}'";
        $q->QUERY_SQL($sql);
    }
    if (!$q->ok) {
        progress("Failed!!! {$q->mysql_error}", 100);
        return;
    }
    $file_extension = file_extension($filename);
    progress("Extract {$filename} extension: {$file_extension}", 5);
    $newtFile = $filename . ".log";
    if ($file_extension == "bz2") {
        $cmdline = "bzip2 -d \"{$TempDir}/{$filename}\" -c >\"{$TempDir}/{$newtFile}.log\" 2>&1";
        exec($cmdline, $results);
    }
    if ($file_extension == "gz") {
        $cmdline = "gunzip -d \"{$TempDir}/{$filename}\" -c >\"{$TempDir}/{$newtFile}.log\" 2>&1";
    }
    if ($cmdline != null) {
        exec($cmdline, $results);
        progress("Extract done " . @implode(" ", $results), 7);
    } else {
        if (!@copy("{$TempDir}/{$filename}", "{$TempDir}/{$newtFile}.log")) {
            progress("Failed!!! Copy error", 100);
            return;
        }
    }
    @unlink("{$TempDir}/{$filename}");
    if (!is_file("{$TempDir}/{$newtFile}.log")) {
        progress("Failed!!! {$TempDir}/{$newtFile}.log error no such file", 100);
        return;
    }
    $linesNumber = $unix->COUNT_LINES_OF_FILE("{$TempDir}/{$newtFile}.log");
    progress("Open {$TempDir}/{$newtFile}.log {$linesNumber}", 10);
    $handle = @fopen("{$TempDir}/{$newtFile}.log", "r");
    if (!$handle) {
        progress("Failed!!! {$TempDir}/{$newtFile}.log open failed", 100);
        return;
    }
    $c = 0;
    $d = 0;
    $TTEV = 0;
    while (!feof($handle)) {
        $c++;
        $buffer = trim(fgets($handle, 4096));
        if (!preg_match("#MAC:.*?\\[([0-9]+)\\/(.*?)\\/([0-9]+).*?:([0-9]+):([0-9]+):([0-9]+)\\s+(.*?)\\]\\s+\"#", $buffer, $re)) {
            continue;
        }
        $dteStr = "{$re[1]}/{$re[2]}/{$re[3]}:{$re[4]}:{$re[5]}:{$re[6]} {$re[7]}";
        $ttime = strtotime($dteStr);
        $newDate = date("Y-m-d H", $ttime) . "h";
        $datelog = date("Y-m-d-h", $ttime);
        $MD5Buffer = md5($buffer);
        $TTEV++;
        @mkdir("/var/log/artica-postfix/squid-brut/{$datelog}", 0777, true);
        @file_put_contents("/var/log/artica-postfix/squid-brut/{$datelog}/{$MD5Buffer}", $buffer);
        if ($c > 10) {
            $d = $d + $c;
            $pp = $d / $linesNumber;
            $pp = $pp * 100;
            $pp = round($pp, 1);
            if ($pp > 10) {
                if ($pp > 100) {
                    $pp = 99;
                }
                progress("Processing {$d}/{$linesNumber} - {$newDate} ", $pp);
                $c = 0;
            }
        }
    }
    progress("Success, {$TTEV} events sent to MySQL injector ", 100);
    @unlink("{$TempDir}/{$newtFile}.log");
}
Example #3
0
function storage_view_extract()
{
    @chmod("ressources/logs", 0777);
    $q = new mysql_syslog();
    $mydir = dirname(__FILE__);
    $newtFile = $_POST["extract-file"];
    $sock = new sockets();
    @unlink("{$mydir}/ressources/logs/{$newtFile}");
    $EnableSyslogDB = $sock->GET_INFO("EnableSyslogDB");
    if (!is_numeric($EnableSyslogDB)) {
        $EnableSyslogDB = 0;
    }
    if ($EnableSyslogDB == 0) {
        $ligne = mysql_fetch_array($q->QUERY_SQL("SELECT SavedInDisk,FileStorePath FROM store WHERE filename = '{$newtFile}'"));
        writelogs("SavedInDisk = {$ligne["SavedInDisk"]}", __FUNCTION__, __FILE__, __LINE__);
        if ($ligne["SavedInDisk"] == 1) {
            $array["FROM"] = $ligne["FileStorePath"];
            $array["TO"] = "{$mydir}/ressources/logs/{$newtFile}";
            $sock->getFrameWork("services.php?copyFiles=" . base64_encode(serialize($array)));
            return;
        }
        $sql = "SELECT filedata INTO DUMPFILE '{$mydir}/ressources/logs/{$newtFile}' FROM access_store WHERE ID = '{$newtFile}'";
        writelogs("{$sql}", __FUNCTION__, __FILE__, __LINE__);
        $q->QUERY_SQL($sql);
        if (!$q->ok) {
            writelogs("{$q->mysql_error}", __FUNCTION__, __FILE__, __LINE__);
            echo $q->mysql_error;
            return;
        }
    } else {
        $q = new mysql_storelogs();
        $sql = "SELECT filecontent INTO DUMPFILE '{$mydir}/ressources/logs/{$newtFile}' FROM files_store WHERE ID = '{$_POST["storeid"]}'";
        writelogs("{$sql}", __FUNCTION__, __FILE__, __LINE__);
        $q->QUERY_SQL($sql);
    }
    $ext = file_extension($newtFile);
    writelogs("{$mydir}/ressources/logs/{$newtFile} -> " . @filesize("{$mydir}/ressources/logs/{$newtFile}") . " bytes...", __FUNCTION__, __FILE__, __LINE__);
    $cmdline = "cp -f {$mydir}/ressources/logs/{$newtFile} {$mydir}/ressources/logs/{$newtFile}.log";
    if ($ext == "bz2") {
        $cmdline = "bzip2 -d \"{$mydir}/ressources/logs/{$newtFile}\" -c >\"{$mydir}/ressources/logs/{$newtFile}.log\" 2>&1";
        exec($cmdline, $results);
    }
    if ($ext == "gz") {
        $cmdline = "gunzip -d \"{$mydir}/ressources/logs/{$newtFile}\" -c >\"{$mydir}/ressources/logs/{$newtFile}.log\"";
    }
    if ($cmdline != null) {
        writelogs("{$cmdline}", __FUNCTION__, __FILE__, __LINE__);
        exec($cmdline, $results);
        while (list($key, $line) = each($results)) {
            writelogs("{$line}", __FUNCTION__, __FILE__, __LINE__);
        }
    }
    @unlink("{$mydir}/ressources/logs/{$newtFile}");
    writelogs(@filesize("{$mydir}/ressources/logs/{$newtFile}.log") . " bytes...", __FUNCTION__, __FILE__, __LINE__);
}
Example #4
0
function reconfigure()
{
    $sock = new sockets();
    $unix = new unix();
    $php5 = $unix->LOCATE_PHP5_BIN();
    $postfix = $unix->find_program("postfix");
    $squidbin = $unix->find_program("squid3");
    $gzip = $unix->find_program("gzip");
    if ($squidbin == null) {
        $squidbin = $unix->find_program("squid");
    }
    $SystemLogsPath = $sock->GET_INFO("SystemLogsPath");
    $LogRotatePath = $sock->GET_INFO("LogRotatePath");
    if (!is_numeric($LogRotatePath)) {
        $LogRotatePath = "/home/logrotate";
    }
    if ($SystemLogsPath == null) {
        $SystemLogsPath = "/var/log";
    }
    if ($SystemLogsPath != "/var/log") {
        CheckLogStorageDir($SystemLogsPath);
    }
    $LogsDirectoryStorage = $sock->GET_INFO("LogsDirectoryStorage");
    if (trim($LogsDirectoryStorage) == null) {
        $LogsDirectoryStorage = "/home/logs-backup";
    }
    @mkdir($LogsDirectoryStorage, 0755, true);
    $q = new mysql_syslog();
    //RotateFiles,RotateType,RotateFreq,MaxSize,RotateCount,postrotate,description,enabled
    $sql = "SELECT *  FROM `logrotate` WHERE enabled=1";
    $results = $q->QUERY_SQL($sql);
    if (!$q->ok) {
        system_admin_events($q->mysql_error, __FUNCTION__, __FILE__, __LINE__, "logrotate");
        return;
    }
    foreach (glob("/etc/logrotate.d/*") as $filename) {
        if ($GLOBALS["VERBOSE"]) {
            echo "Remove {$filename}\n";
        }
        @unlink($filename);
    }
    while ($ligne = mysql_fetch_assoc($results)) {
        $f = array();
        $dir = $ligne["RotateFiles"];
        $dir = dirname($ligne["RotateFiles"]);
        if (!is_dir($dir)) {
            continue;
        }
        if (is_numeric($ligne["MaxSize"])) {
            $ligne["MaxSize"] = 100;
        }
        if (!is_numeric($ligne["RotateCount"])) {
            $ligne["RotateCount"] = 5;
        }
        if (preg_match("#\\/var\\/log\\/squid#is", $ligne["RotateFiles"])) {
            continue;
        }
        $f[] = "{$ligne["RotateFiles"]} {";
        $f[] = "\t{$ligne["RotateFreq"]}";
        $f[] = "\tmissingok";
        if ($ligne["MaxSize"] > 0) {
            $f[] = "\tsize {$ligne["MaxSize"]}M";
        }
        if ($ligne["RotateCount"] > 0) {
            $f[] = "\trotate {$ligne["RotateCount"]}";
        }
        $f[] = "\tcompress";
        $f[] = "\tsharedscripts";
        $f[] = "\tcreate 640 root";
        $f[] = "\tdateext";
        $f[] = "\tcompresscmd {$gzip}";
        $f[] = "\tcompressoptions -9";
        $f[] = "\tcompressext .gz";
        $f[] = "\textension -TASK-{$ligne["ID"]}";
        if ($ligne["postrotate"] != null) {
            $ligne["postrotate"] = str_replace("%SQUIDBIN%", $squidbin, $ligne["postrotate"]);
            $ligne["postrotate"] = str_replace("%POSTFIX%", $postfix, $ligne["postrotate"]);
            $ligne["postrotate"] = str_replace("%PHP%", $php5, $ligne["postrotate"]);
            $f[] = "\tpostrotate";
            $f[] = $ligne["postrotate"];
            $f[] = "endscript";
        }
        $f[] = "}\n";
        @file_put_contents("/etc/logrotate.d/rotate-{$ligne["ID"]}", @implode("\n", $f));
    }
    LoagRotateApache();
}