Пример #1
0
function awstats_mail()
{
    $users = new usersMenus();
    if (!$users->POSTFIX_INSTALLED) {
        return;
    }
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $oldpid = @file_get_contents($pidfile);
    if ($unix->process_exists($oldpid, basename(__FILE__))) {
        echo "{$oldpid} is already in memory\n";
        return;
    }
    $pid = posix_getpid();
    echo "running {$pid} PID\n";
    @file_put_contents($pidfile, $pid);
    $tt1 = time();
    $nohup = $unix->find_program("nohup");
    if (!$users->awstats_installed) {
        $unix->send_email_events("awstats is not installed, artica will install it itself", $q->mysql_error, "postfix");
        shell_exec(trim("{$nohup} /usr/share/artica-postfix/bin/artica-make APP_AWSTATS >/dev/null &"));
        return;
    }
    $sock = new sockets();
    $GLOBALS["EnablePostfixMultiInstance"] = $sock->GET_INFO("EnablePostfixMultiInstance");
    $GLOBALS["maillogconvert"] = $unix->LOCATE_maillogconvert();
    $GLOBALS["zcat"] = $unix->find_program("zcat");
    $GLOBALS["perl"] = $unix->find_program("perl");
    $GLOBALS["nice"] = EXEC_NICE();
    $GLOBALS["sed"] = $unix->find_program("sed");
    if ($GLOBALS["VERBOSE"]) {
        echo "maillogconvert..........:{$GLOBALS["maillogconvert"]}\n";
        echo "zcat....................:{$GLOBALS["zcat"]}\n";
        echo "perl....................:{$GLOBALS["perl"]}\n";
        echo "nice....................:{$GLOBALS["nice"]}\n";
        echo "sed.....................:{$GLOBALS["sed"]}\n";
    }
    if (strlen($GLOBALS["maillogconvert"]) == null) {
        echo "maillogconvert.pl, no such file\n";
        return;
    }
    @mkdir("/var/log/mail-backup", 666, true);
    foreach (glob("/var/log/mail.log.*.gz") as $filename) {
        shell_exec("{$GLOBALS["nice"]}{$GLOBALS["zcat"]} {$filename} >/tmp/mail.log");
        $t1 = time();
        prepflog("/tmp/mail.log");
        $distanceOfTimeInWords = distanceOfTimeInWords($t1, time());
        shell_exec("/bin/mv {$filename} /var/log/mail-backup/");
        if ($GLOBALS["VERBOSE"]) {
            echo basename($filename) . " {$distanceOfTimeInWords}\n";
        }
        $ev[] = basename($filename) . " " . $distanceOfTimeInWords;
        @unlink("/tmp/mail.log");
    }
    foreach (glob("/var/log/mail.log.*") as $filename) {
        if (!preg_match("#\\.[0-9]+\$#", basename($filename))) {
            if ($GLOBALS["VERBOSE"]) {
                echo basename($filename) . " SKIP\n";
            }
            continue;
        }
        $t1 = time();
        prepflog($filename);
        $distanceOfTimeInWords = distanceOfTimeInWords($t1, time());
        if ($GLOBALS["VERBOSE"]) {
            echo basename($filename) . " {$distanceOfTimeInWords}\n";
        }
        $ev[] = basename($filename) . " " . $distanceOfTimeInWords;
        shell_exec("/bin/mv {$filename} /var/log/mail-backup/");
    }
    $t1 = time();
    prepflog("/var/log/mail.log");
    $distanceOfTimeInWords = distanceOfTimeInWords($t1, time());
    $ev[] = basename("/var/log/mail.log") . " " . $distanceOfTimeInWords;
    if ($GLOBALS["VERBOSE"]) {
        echo basename("/var/log/mail.log") . " {$distanceOfTimeInWords}\n";
    }
    //$cmd="$nice$perl /usr/share/artica-postfix/bin/prepflog.pl </tmp/mail.log|$nice$perl $maillogconvert standard >>/var/log/artica-postfix/awstats-postfix.stats";
    foreach (glob("/var/log/artica-mail/*.stats") as $filename) {
        if (preg_match("#(.+?)\\.([0-9]+)\\.stats#", basename($filename), $re)) {
            $instance = $re[1];
            $time = $re[2];
            $cmd = "{$GLOBALS["nice"]}{$GLOBALS["perl"]} {$GLOBALS["maillogconvert"]} standard< {$filename} >/var/log/artica-mail/{$instance}.{$time}.aws";
            if ($GLOBALS["VERBOSE"]) {
                echo "{$cmd}\n";
            }
            shell_exec($cmd);
            @unlink($filename);
        }
    }
    $filecount = 0;
    foreach (glob("/var/log/artica-mail/*.aws") as $filename) {
        artica_parse($filename);
        $filecount++;
        $filecountl[] = $filename;
    }
    $distanceOfTimeInWords = distanceOfTimeInWords($tt1, time());
    if ($filecount > 0) {
        $unix->send_email_events("AWSTATS:: Success generating {$filecount} stats files ({$distanceOfTimeInWords})", @implode("\n", $filename), "postfix");
        $cmd = "{$GLOBALS["nice"]}" . LOCATE_PHP5_BIN() . " " . dirname(__FILE__) . "/exec.artica.meta.users.php --export-postfix-events >/dev/null 2>&1 &";
        shell_exec($cmd);
    }
    clean_maillogs();
}
Пример #2
0
function awstats_mail()
{
    $users = new usersMenus();
    if (!$users->POSTFIX_INSTALLED) {
        return;
    }
    $unix = new unix();
    $sock = new sockets();
    $ArticaMetaEnabled = trim($sock->GET_INFO("ArticaMetaEnabled"));
    if (!is_numeric($ArticaMetaEnabled)) {
        $ArticaMetaEnabled = 0;
    }
    $EnableArticaSMTPStatistics = $sock->GET_INFO("EnableArticaSMTPStatistics");
    if (!is_numeric($EnableArticaSMTPStatistics)) {
        $EnableArticaSMTPStatistics = 1;
    }
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $pidTime = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    if ($EnableArticaSMTPStatistics == 0) {
        return;
    }
    $pid = $unix->get_pid_from_file($pidfile);
    if (!$GLOBALS["VERBOSE"]) {
        if ($unix->process_exists($pid, basename(__FILE__))) {
            $pidTime = $unix->PROCCESS_TIME_MIN($pid);
            events("Already process PID: {$pid} running since {$pidTime} minutes", __FUNCTION__, __FILE__, __LINE__, "postfix-stats");
            return;
        }
        $time = $unix->file_time_min("{$pidTime}");
        if ($time < 120) {
            events("Current {$time}mn, need 60mn");
            return;
        }
        if (system_is_overloaded(basename(__FILE__))) {
            system_admin_events("Overloaded system, aborting", __FUNCTION__, __FILE__, __LINE__, "postfix-stats");
            return;
        }
        @unlink($pidTime);
        @file_put_contents($pidTime, time());
        @file_put_contents($pidfile, getmypid());
    }
    $tt1 = time();
    $nohup = $unix->find_program("nohup");
    if (!$users->awstats_installed) {
        events("awstats is not installed, artica will install it itself", __FUNCTION__, __FILE__, __LINE__, "postfix-stats");
        events("{$nohup} /usr/share/artica-postfix/bin/artica-make APP_AWSTATS >/dev/null &");
        shell_exec(trim("{$nohup} /usr/share/artica-postfix/bin/artica-make APP_AWSTATS >/dev/null &"));
        return;
    }
    $sock = new sockets();
    $GLOBALS["EnablePostfixMultiInstance"] = $sock->GET_INFO("EnablePostfixMultiInstance");
    $GLOBALS["maillogconvert"] = $unix->LOCATE_maillogconvert();
    $GLOBALS["zcat"] = $unix->find_program("zcat");
    $GLOBALS["perl"] = $unix->find_program("perl");
    $GLOBALS["nice"] = EXEC_NICE();
    $GLOBALS["sed"] = $unix->find_program("sed");
    $GLOBALS["mv"] = $unix->find_program("mv");
    $GLOBALS["cp"] = $unix->find_program("cp");
    $GLOBALS["touch"] = $unix->find_program("touch");
    $GLOBALS["echo"] = $unix->find_program("echo");
    $GLOBALS["postfix"] = $unix->find_program("postfix");
    if ($GLOBALS["VERBOSE"]) {
        echo "maillogconvert..........:{$GLOBALS["maillogconvert"]}\n";
        echo "zcat....................:{$GLOBALS["zcat"]}\n";
        echo "perl....................:{$GLOBALS["perl"]}\n";
        echo "nice....................:{$GLOBALS["nice"]}\n";
        echo "sed.....................:{$GLOBALS["sed"]}\n";
    }
    if (strlen($GLOBALS["maillogconvert"]) == null) {
        events("maillogconvert.pl, no such file", __FUNCTION__, __FILE__, __LINE__, "postfix-stats");
        return;
    }
    @mkdir("/var/log/mail-backup", 666, true);
    foreach (glob("/var/log/mail.log.*.gz") as $filename) {
        events("{$GLOBALS["nice"]}{$GLOBALS["zcat"]} {$filename} >/tmp/mail.log");
        shell_exec("{$GLOBALS["nice"]}{$GLOBALS["zcat"]} {$filename} >/tmp/mail.log");
        $t1 = time();
        prepflog("/tmp/mail.log");
        $distanceOfTimeInWords = distanceOfTimeInWords($t1, time());
        shell_exec("/bin/mv {$filename} /var/log/mail-backup/");
        events("/bin/mv {$filename} /var/log/mail-backup/", __FUNCTION__, __FILE__, __LINE__, "postfix-stats");
        events(basename($filename) . " {$distanceOfTimeInWords}");
        $ev[] = basename($filename) . " " . $distanceOfTimeInWords;
        @unlink("/tmp/mail.log");
    }
    foreach (glob("/var/log/mail.log.*") as $filename) {
        if (!preg_match("#\\.[0-9]+\$#", basename($filename))) {
            if ($GLOBALS["VERBOSE"]) {
                echo basename($filename) . " SKIP\n";
            }
            continue;
        }
        $t1 = time();
        prepflog($filename);
        $distanceOfTimeInWords = distanceOfTimeInWords($t1, time());
        if ($GLOBALS["VERBOSE"]) {
            echo basename($filename) . " {$distanceOfTimeInWords}\n";
        }
        $ev[] = basename($filename) . " " . $distanceOfTimeInWords;
        shell_exec("/bin/mv {$filename} /var/log/mail-backup/");
    }
    $t1 = time();
    if (is_file("/var/log/mail.log")) {
        $nextFile = "/var/log/mail.log." . time();
        shell_exec("{$GLOBALS["cp"]} /var/log/mail.log {$nextFile}");
        shell_exec("{$GLOBALS["echo"]} \"\" > /var/log/mail.log");
        events("Restarting Syslogs...");
        $syslog_init = $unix->RESTART_SYSLOG_FORMAIL();
        prepflog($nextFile);
        $storelogs = new mysql_storelogs();
        events("Injecting {$nextFile} to MySQL store logs");
        $storelogs->InjectFile($nextFile);
        artica_parse();
        events("reloading postfix");
        shell_exec("{$GLOBALS["postfix"]} reload");
    }
    $distanceOfTimeInWords = distanceOfTimeInWords($t1, time());
    $ev[] = basename("/var/log/mail.log") . " " . $distanceOfTimeInWords;
    if ($GLOBALS["VERBOSE"]) {
        echo basename("/var/log/mail.log") . " {$distanceOfTimeInWords}\n";
    }
    //$cmd="$nice$perl /usr/share/artica-postfix/bin/prepflog.pl </tmp/mail.log|$nice$perl $maillogconvert standard >>/var/log/artica-postfix/awstats-postfix.stats";
    foreach (glob("/var/log/artica-mail/*.stats") as $filename) {
        if (preg_match("#(.+?)\\.([0-9]+)\\.stats#", basename($filename), $re)) {
            $instance = $re[1];
            $time = $re[2];
            $cmd = "{$GLOBALS["nice"]}{$GLOBALS["perl"]} {$GLOBALS["maillogconvert"]} standard< {$filename} >/var/log/artica-mail/{$instance}.{$time}.aws";
            events($cmd);
            shell_exec($cmd);
            @unlink($filename);
            if (system_is_overloaded(basename(__FILE__))) {
                system_admin_events("Overloaded system, aborting", __FUNCTION__, __FILE__, __LINE__, "postfix-stats");
                return;
            }
        }
    }
    $filecount = 0;
    foreach (glob("/var/log/artica-mail/*.aws") as $filename) {
        artica_parse($filename);
        $filecount++;
        $filecountl[] = $filename;
    }
    $distanceOfTimeInWords = distanceOfTimeInWords($tt1, time());
    if ($filecount > 0) {
        system_admin_events("Success generating {$filecount} stats files ({$distanceOfTimeInWords})\n" . @implode("\n", $filename), __FUNCTION__, __FILE__, __LINE__, "postfix-stats");
        if ($ArticaMetaEnabled == 1) {
            $cmd = "{$GLOBALS["nice"]}" . LOCATE_PHP5_BIN() . " " . dirname(__FILE__) . "/exec.artica.meta.users.php --export-postfix-events >/dev/null 2>&1 &";
            shell_exec($cmd);
        }
    }
}