Beispiel #1
0
function DUMP_FULL($MAIN)
{
    $backupdir = "/home/apache/artica-stats/works/backup";
    $failedPath = "/home/apache/artica-stats/works/failed";
    @mkdir($backupdir, 0755, true);
    @mkdir($failedPath, 0755, true);
    $PROXYNAME = $GLOBALS["MYHOSTNAME_PROXY"];
    $AS_POSTGRES = false;
    $suffix = "influx";
    if (is_file("/usr/local/ArticaStats/bin/postgres")) {
        $AS_POSTGRES = true;
        $suffix = "postgres";
    }
    $prefix = "(zDate,IPADDR,SITENAME,HTTP_CODE,RQS,SIZE,PROXYNAME)";
    $q = new influx();
    $FINAL = array();
    while (list($MD5, $ARRAY) = each($MAIN)) {
        $SIZE = $ARRAY["SIZE"];
        $RQS = $ARRAY["RQS"];
        $SITENAME = $ARRAY["SITENAME"];
        $DATE = $ARRAY["DATE"];
        $INFLUX_TIME = $ARRAY["INFLUX_TIME"];
        $IPADDR = $ARRAY["IPADDR"];
        $HTTP_CODE = $ARRAY["HTTP_CODE"];
        $zDate = $ARRAY["DATE"];
        if ($AS_POSTGRES) {
            $FINAL[] = "('{$zDate}','{$IPADDR}','{$SITENAME}','{$HTTP_CODE}','{$RQS}','{$SIZE}','{$PROXYNAME}')";
            continue;
        }
        $zArray["precision"] = "s";
        $zArray["time"] = $INFLUX_TIME;
        $zArray["fields"]["RQS"] = $RQS;
        $zArray["fields"]["SIZE"] = $SIZE;
        $zArray["fields"]["HTTP_CODE"] = $HTTP_CODE;
        $zArray["tags"]["SITENAME"] = $SITENAME;
        $zArray["tags"]["IPADDR"] = $IPADDR;
        $line = $q->prepare("apache_size", $zArray);
        $FINAL[] = $line;
    }
    if (count($FINAL) > 0) {
        $backupfile = "{$backupdir}/apache." . time() . ".{$suffix}.log";
        $failedPath = "{$failedPath}/apache." . time() . ".{$suffix}.log";
        if ($AS_POSTGRES) {
            $sql = "INSERT INTO apache_size {$prefix} VALUES " . @implode(",", $FINAL);
            $q = new postgres_sql();
            $q->QUERY_SQL($sql);
            if (!$q->ok) {
                events("INJECTION Failed: backup to {$failedPath} ({$q->curl_error})");
                @file_put_contents($failedPath, @implode("\n", $sql));
                return false;
            }
        }
        if (!$AS_POSTGRES) {
            if (!$q->bulk_inject($FINAL)) {
                apache_admin_mysql(0, "INJECTION Failed ({$q->curl_error})", ": backup to {$failedPath}", __FILE__, __LINE__);
                @file_put_contents($failedPath, @implode("\n", $FINAL));
                sleep(1);
                return true;
            }
        }
        events("INJECTION Success: backup to {$backupfile}");
        @file_put_contents($backupfile, @implode("\n", $FINAL));
        $FINAL = array();
    }
    sleep(1);
    return true;
}
function UFDB_LOG_HOURLY_DUMP($MEM)
{
    $AS_POSTGRES = false;
    $suffix = "influx";
    if (is_file("/usr/local/ArticaStats/bin/postgres")) {
        $AS_POSTGRES = true;
        $suffix = "postgres";
    }
    events("Dumping " . count($MEM) . " entries");
    $q = new influx();
    $PROXYNAME = $GLOBALS["MYHOSTNAME_PROXY"];
    $prefix_sql = "(zDate,website,category,rulename,public_ip,blocktype,why,hostname,client,PROXYNAME,rqs)";
    while (list($KEYMD5, $subarray) = each($MEM)) {
        $website = $subarray["website"];
        $category = $subarray["category"];
        $rulename = $subarray["rulename"];
        $public_ip = $subarray["public_ip"];
        $blocktype = $subarray["blocktype"];
        $why = $subarray["why"];
        $hostname = $subarray["hostname"];
        $client = $subarray["client"];
        $zDate = date("Y-m-d H:i:s", $subarray["ZDATE"]);
        $RQS = $subarray["RQS"];
        if ($AS_POSTGRES) {
            $FINAL[] = "('{$zDate}','{$website}','{$category}','{$rulename}','{$public_ip}','{$blocktype}','{$why}','{$hostname}','{$client}','{$PROXYNAME}','{$RQS}')";
            continue;
        }
        $array["precision"] = "s";
        $array["time"] = $subarray["TIME"];
        $array["tags"]["uid"] = $subarray["uid"];
        $array["fields"]["TIME"] = $subarray["ZDATE"];
        $array["fields"]["RQS"] = $subarray["RQS"];
        $array["tags"]["category"] = $subarray["category"];
        $array["tags"]["rulename"] = $subarray["rulename"];
        $array["tags"]["public_ip"] = $subarray["public_ip"];
        $array["tags"]["blocktype"] = $subarray["blocktype"];
        $array["tags"]["why"] = $subarray["why"];
        $array["tags"]["hostname"] = $subarray["hostname"];
        $array["tags"]["website"] = $subarray["website"];
        $array["tags"]["client"] = $subarray["client"];
        $line = $q->prepare("webfilter", $array);
        if ($GLOBALS["VERBOSE"]) {
            echo "{$line}\n";
        }
        $FINAL[] = $line;
    }
    if (count($FINAL) > 0) {
        $backupdir = "{$GLOBALS["LogFileDeamonLogDir"]}/webfilter-backup";
        $faildir = "{$GLOBALS["LogFileDeamonLogDir"]}/webfilter-failed";
        @mkdir($faildir, 0755, true);
        @mkdir($backupdir, 0755, true);
        $backupfile = "{$backupdir}/" . time() . ".{$suffix}.log";
        $failedPath = "{$faildir}/" . time() . ".{$suffix}.log";
        if ($AS_POSTGRES) {
            $sql = "INSERT INTO webfilter {$prefix_sql} VALUES " . @implode(",", $FINAL);
            $q = new postgres_sql();
            $q->QUERY_SQL($sql);
            if (!$q->ok) {
                events("WEBFILTERING INJECTION Failed: backup to {$failedPath} ({$q->mysql_error})");
                @file_put_contents($failedPath, @implode("\n", $sql));
                return false;
            }
        }
        if (!$AS_POSTGRES) {
            if (!$q->bulk_inject($FINAL)) {
                events("WEBFILTERING INJECTION Failed: backup to {$failedPath} ({$q->curl_error})");
                @file_put_contents($failedPath, @implode("\n", $FINAL));
                return false;
            }
        }
        events("WEBFILTERING INJECTION Success: backup to {$backupfile}");
        @file_put_contents($backupfile, @implode("\n", $FINAL));
        $FINAL = array();
    }
    return true;
}