예제 #1
0
function artica_parse($filename)
{
    echo "Parsing {$filename}\n";
    if (preg_match("#^(.+?)\\.[0-9]+\\.aws\$#", basename($filename), $re)) {
        $instancename = $re[1];
    }
    $f = explode("\n", @file_get_contents($filename));
    $prefixsql = "INSERT IGNORE INTO `mails_stats`(`zmd5`,`zDate`,`instance`,`sender`,`sender_domain`,`recipient`,`recipient_domain`,\n\t`sender_ip`,`recipient_ip`,`smtpcode`,`mailsize`,`artica_meta`) VALUES\n\t";
    $events_number = 0;
    while (list($num, $ligne) = each($f)) {
        if (trim($ligne) == null) {
            continue;
        }
        if (preg_match("#([0-9\\-]+)\\s+([0-9\\:]+)\\s+(.*?)\\s+(.*?)\\s+(.*?)\\s+(.*?)\\s+SMTP\\s+-\\s+([0-9]+)\\s+([0-9\\?]+)#", $ligne, $re)) {
            $day = $re[1];
            $time = $re[2];
            $from = strtolower($re[3]);
            $to = strtolower($re[4]);
            $ipfrom = $re[5];
            $ipto = $re[6];
            $smtpcode = $re[7];
            $size = $re[8];
            if (!is_numeric($size)) {
                $size = 0;
            }
            $zdate = "{$day} {$time}";
            $domainfrom = "";
            $domainto = "";
            if ($from == "<>") {
                $from = "Unknown";
            }
            if ($to == "<>") {
                $to = "Unknown";
            }
            if (preg_match("#(.+?)@(.+)#", $from, $re)) {
                $domainfrom = $re[2];
            }
            if (preg_match("#(.+?)@(.+)#", $to, $re)) {
                $domainto = $re[2];
            }
            if ($domainfrom == null) {
                $domainfrom = "Unknown";
            }
            if ($domainto == null) {
                $domainto = "Unknown";
            }
            $md5 = md5("{$instancename}{$day}{$time}{$from}{$to}{$size}");
            $sq[] = "('{$md5}','{$zdate}','{$instancename}','{$from}','{$domainfrom}','{$to}','{$domainto}','{$ipfrom}','{$ipto}','{$smtpcode}','{$size}',0)";
            $events_number++;
        } else {
            echo $ligne . "FAILED\n";
        }
    }
    if (is_array($sq)) {
        $sql = "{$prefixsql}" . @implode(",", $sq);
    }
    $q = new mysql();
    $unix = new unix();
    $q->QUERY_SQL($sql, "artica_events");
    if (!$q->ok) {
        echo "Mysql error:{$q->msql_error}\n";
        return;
    }
    $unix = new unix();
    $awstats = $unix->LOCATE_AWSTATS_BIN();
    $GLOBALS["perl"] = $unix->find_program("perl");
    $GLOBALS["nice"] = EXEC_NICE();
    $awstats_buildstaticpages = $unix->LOCATE_AWSTATS_BUILDSTATICPAGES_BIN();
    $awstats_conf[] = "LogFile={$filename}";
    $awstats_conf[] = "LogType=M";
    $awstats_conf[] = "LogFormat=\"%time2 %email %email_r %host %host_r %method %url %code %bytesd\"";
    $awstats_conf[] = "LevelForBrowsersDetection=0";
    $awstats_conf[] = "LevelForOSDetection=0";
    $awstats_conf[] = "LevelForRefererAnalyze=0";
    $awstats_conf[] = "LevelForRobotsDetection=0";
    $awstats_conf[] = "LevelForWormsDetection=0";
    $awstats_conf[] = "LevelForSearchEnginesDetection=0";
    $awstats_conf[] = "LevelForFileTypesDetection=0";
    $awstats_conf[] = "ShowMenu=1";
    $awstats_conf[] = "ShowSummary=HB";
    $awstats_conf[] = "ShowMonthStats=HB";
    $awstats_conf[] = "ShowDaysOfMonthStats=HB";
    $awstats_conf[] = "ShowDaysOfWeekStats=HB";
    $awstats_conf[] = "ShowHoursStats=HB";
    $awstats_conf[] = "ShowDomainsStats=0";
    $awstats_conf[] = "ShowHostsStats=HBL";
    $awstats_conf[] = "ShowAuthenticatedUsers=0";
    $awstats_conf[] = "ShowRobotsStats=0";
    $awstats_conf[] = "ShowEMailSenders=HBML";
    $awstats_conf[] = "ShowEMailReceivers=HBML";
    $awstats_conf[] = "ShowSessionsStats=0";
    $awstats_conf[] = "ShowPagesStats=0";
    $awstats_conf[] = "ShowFileTypesStats=0";
    $awstats_conf[] = "ShowFileSizesStats=0";
    $awstats_conf[] = "ShowBrowsersStats=0";
    $awstats_conf[] = "ShowOSStats=0";
    $awstats_conf[] = "ShowOriginStats=0";
    $awstats_conf[] = "ShowKeyphrasesStats=0";
    $awstats_conf[] = "ShowKeywordsStats=0";
    $awstats_conf[] = "ShowMiscStats=0";
    $awstats_conf[] = "ShowHTTPErrorsStats=0";
    $awstats_conf[] = "ShowSMTPErrorsStats=1";
    @file_put_contents("/etc/awstats/awstats.{$instancename}.conf", @implode("\n", $awstats_conf));
    @chmod("/etc/awstats/awstats.{$instancename}.conf", 644);
    $t1 = time();
    @mkdir("/var/tmp/awstats/{$instancename}", 0666, true);
    $cmd = "{$GLOBALS["nice"]}{$GLOBALS["perl"]} {$awstats_buildstaticpages} -config={$instancename} -update -lang=auto -awstatsprog={$awstats} -dir=/var/tmp/awstats/{$instancename} 2>&1";
    if ($GLOBALS["VERBOSE"]) {
        echo $cmd . "\n";
    }
    exec($cmd, $results);
    if ($GLOBALS["VERBOSE"]) {
        echo @implode("\n", $results) . "\n";
    }
    awstats_import_sql($instancename, $GLOBALS["ARTICAMETA"]);
    $t2 = time();
    @unlink($filename);
}
예제 #2
0
function awstats()
{
    $sock = new sockets();
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $pidTime = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    if ($unix->file_time_min($pidTime) < 60) {
        return;
    }
    $pid = $unix->get_pid_from_file($pidfile);
    if ($unix->process_exists($pid, basename(__FILE__))) {
        $time = $unix->PROCCESS_TIME_MIN($pid);
        return;
    }
    @file_put_contents($pidfile, getmypid());
    @unlink($pidTime);
    @file_put_contents($pidTime, time());
    $sock = new sockets();
    $EnableNginxStats = $sock->GET_INFO("EnableNginxStats");
    if (!is_numeric($EnableNginxStats)) {
        $EnableNginxStats = 0;
    }
    if ($EnableNginxStats == 1) {
        return;
    }
    include_once dirname(__FILE__) . "/ressources/class.awstats.inc";
    include_once dirname(__FILE__) . "/ressources/class.mysql.syslogs.inc";
    $awstats_bin = $unix->LOCATE_AWSTATS_BIN();
    $nice = EXEC_NICE();
    $perl = $unix->find_program("perl");
    $awstats_buildstaticpages = $unix->LOCATE_AWSTATS_BUILDSTATICPAGES_BIN();
    if ($GLOBALS["VERBOSE"]) {
        echo "awstats......: {$awstats_bin}\n";
        echo "statics Pages: {$awstats_buildstaticpages}\n";
        echo "Nice.........: {$nice}\n";
        echo "perl.........: {$perl}\n";
    }
    if (!is_file($awstats_buildstaticpages)) {
        echo "buildstaticpages no such binary...\n";
        return;
    }
    $sock = new sockets();
    $kill = $unix->find_program("kill");
    $NginxWorkLogsDir = $sock->GET_INFO("NginxWorkLogsDir");
    if ($NginxWorkLogsDir == null) {
        $NginxWorkLogsDir = "/home/nginx/logsWork";
    }
    $sys = new mysql_storelogs();
    $files = $unix->DirFiles($NginxWorkLogsDir, "-([0-9\\-]+)\\.log");
    while (list($filename, $line) = each($files)) {
        if (!preg_match("#^(.+?)-[0-9]+-[0-9]+-[0-9]+-[0-9]+\\.log\$#", $filename, $re)) {
            if ($GLOBALS["VERBOSE"]) {
                echo "{$filename}, skip\n";
            }
            continue;
        }
        if ($GLOBALS["VERBOSE"]) {
            echo "{$filename}, domain:{$re[1]}\n";
        }
        $servername = $re[1];
        $GLOBALS["nice"] = $nice;
        $aw = new awstats($servername);
        $aw->set_LogFile("{$NginxWorkLogsDir}/{$filename}");
        $aw->set_LogType("W");
        $aw->set_LogFormat(1);
        $config = $aw->buildconf();
        $SOURCE_FILE_PATH = "{$NginxWorkLogsDir}/{$filename}";
        $configlength = strlen($config);
        if ($configlength < 10) {
            if ($GLOBALS["VERBOSE"]) {
                echo "configuration file lenght failed {$configlength} bytes, aborting {$servername}\n";
            }
            return;
        }
        @file_put_contents("/etc/awstats/awstats.{$servername}.conf", $config);
        @chmod("/etc/awstats/awstats.{$servername}.conf", 644);
        $Lang = $aw->GET("Lang");
        if ($Lang == null) {
            $Lang = "auto";
        }
        @mkdir("/var/tmp/awstats/{$servername}", 666, true);
        $t1 = time();
        $cmd = "{$nice}{$perl} {$awstats_buildstaticpages} -config={$servername} -update -lang={$Lang} -awstatsprog={$awstats_bin} -dir=/var/tmp/awstats/{$servername} -LogFile=\"{$SOURCE_FILE_PATH}\" 2>&1";
        if ($GLOBALS["VERBOSE"]) {
            echo $cmd . "\n";
        }
        shell_exec($cmd);
        $filedate = date('Y-m-d H:i:s', filemtime($SOURCE_FILE_PATH));
        if (!awstats_import_sql($servername)) {
            continue;
        }
        $sys->ROTATE_TOMYSQL($SOURCE_FILE_PATH, $filedate);
    }
}
예제 #3
0
function exectute_awstats($servername, $articameta = false)
{
    $unix = new unix();
    $perl = $unix->find_program("perl");
    $awstats = $unix->LOCATE_AWSTATS_BIN();
    $GLOBALS["ARTICAMETA"] = $articameta;
    $awstats_buildstaticpages = $unix->LOCATE_AWSTATS_BUILDSTATICPAGES_BIN();
    $q = new mysql();
    $nice = EXEC_NICE();
    $GLOBALS["nice"] = $nice;
    $aw = new awstats($servername);
    $config = $aw->buildconf();
    $configlength = strlen($config);
    if ($configlength < 10) {
        if ($GLOBALS["VERBOSE"]) {
            echo "configuration file lenght failed {$configlength} bytes, aborting {$servername}\n";
        }
        return;
    }
    @file_put_contents("/etc/awstats/awstats.{$servername}.conf", $config);
    @chmod("/etc/awstats/awstats.{$servername}.conf", 644);
    $Lang = $aw->GET("Lang");
    if ($Lang == null) {
        $Lang = "auto";
    }
    @mkdir("/var/tmp/awstats/{$servername}", 666, true);
    $t1 = time();
    $cmd = "{$nice}{$perl} {$awstats_buildstaticpages} -config={$servername} -update -lang={$Lang} -awstatsprog={$awstats} -dir=/var/tmp/awstats/{$servername} 2>&1";
    if ($GLOBALS["VERBOSE"]) {
        echo $cmd . "\n";
    }
    exec($cmd, $results);
    if ($GLOBALS["VERBOSE"]) {
        echo @implode("\n", $results) . "\n";
    }
    $t2 = time();
    awstats_import_sql($servername, $articameta);
    $time_duration = distanceOfTimeInWords($t1, $t2);
    if ($GLOBALS["VERBOSE"]) {
        echo "{$time_duration}\n";
    }
    $unix->send_email_events("generating awstats statistics for {$servername} success {$time_duration}", @implode("\n", $results), "system");
}