function utc_time()
{
    $time = time();
    $TimeToInflux = TimeToInflux($time);
    $InfluxToTime = InfluxToTime($TimeToInflux);
    echo "Today is: {$time} (" . date("Y-m-d H:i:s") . ")\n";
    echo "Inserting time in influxdb in UTC : \"{$TimeToInflux}\"\n";
    echo "Query time in influxdb in UTC : \"{$TimeToInflux}\" == {$InfluxToTime} (" . date("Y-m-d H:i:s", $InfluxToTime) . ")\n";
}
function Parse($filename)
{
    $t1 = time();
    $unix = new unix();
    $workfile = $filename;
    $stampfile = "{$filename}.last";
    if (is_file($stampfile)) {
        $LastScannLine = intval(@file_get_contents($stampfile));
    }
    $handle = @fopen($workfile, "r");
    if (!$handle) {
        events("Fopen failed on {$workfile}");
        return false;
    }
    if ($LastScannLine > 0) {
        fseek($handle, $LastScannLine, SEEK_SET);
    }
    while (!feof($handle)) {
        //1444514181;www.safe-demo.com;46.4.32.75;200;1;42354
        $buffer = trim(fgets($handle));
        if ($buffer == null) {
            continue;
        }
        $ARRAY = explode(";", $buffer);
        $TIME = $ARRAY[0];
        $SITENAME = $ARRAY[1];
        $IPADDR = $ARRAY[2];
        if ($IPADDR == "127.0.0.1") {
            continue;
        }
        $HTTP_CODE = $ARRAY[3];
        $RQS = $ARRAY[4];
        $SIZE = $ARRAY[5];
        $TIME_HOUR = date("Y-m-d H:00:00", $TIME);
        $KEYMD5 = md5("{$TIME_HOUR}{$SITENAME}");
        $KEYMD5FULL = md5("{$TIME_HOUR}{$SITENAME}{$IPADDR}{$HTTP_CODE}");
        if (!isset($GENERIC[$KEYMD5]["SIZE"])) {
            $GENERIC[$KEYMD5]["DATE"] = $TIME_HOUR;
            $GENERIC[$KEYMD5]["INFLUX_TIME"] = TimeToInflux($TIME);
            $GENERIC[$KEYMD5]["SIZE"] = intval($SIZE);
            $GENERIC[$KEYMD5]["RQS"] = intval($RQS);
            $GENERIC[$KEYMD5]["SITENAME"] = $SITENAME;
        } else {
            $GENERIC[$KEYMD5]["SIZE"] = $GENERIC[$KEYMD5]["SIZE"] + $SIZE;
            $GENERIC[$KEYMD5]["RQS"] = $GENERIC[$KEYMD5]["RQS"] + $RQS;
        }
        if (!isset($FULL[$KEYMD5FULL]["SIZE"])) {
            $FULL[$KEYMD5FULL]["DATE"] = $TIME_HOUR;
            $FULL[$KEYMD5FULL]["INFLUX_TIME"] = QueryToUTC(strtotime($TIME_HOUR), true);
            $FULL[$KEYMD5FULL]["SIZE"] = intval($SIZE);
            $FULL[$KEYMD5FULL]["RQS"] = intval($RQS);
            $FULL[$KEYMD5FULL]["SITENAME"] = $SITENAME;
            $FULL[$KEYMD5FULL]["IPADDR"] = $IPADDR;
            $FULL[$KEYMD5FULL]["HTTP_CODE"] = $HTTP_CODE;
        } else {
            $FULL[$KEYMD5FULL]["SIZE"] = $GENERIC[$KEYMD5]["SIZE"] + $SIZE;
            $FULL[$KEYMD5FULL]["RQS"] = $GENERIC[$KEYMD5]["RQS"] + $RQS;
        }
        if (count($GENERIC) > 500) {
            if (!DUMP_GENERIC($GENERIC)) {
                return;
            }
            $GENERIC = array();
        }
        if (count($FULL) > 500) {
            if (!DUMP_FULL($FULL)) {
                return;
            }
            $FULL = array();
        }
    }
    if (count($GENERIC) > 0) {
        if (!DUMP_GENERIC($GENERIC)) {
            return;
        }
    }
    if (count($FULL) > 0) {
        if (!DUMP_FULL($FULL)) {
            return;
        }
        $FULL = array();
    }
    $took = $unix->distanceOfTimeInWords($t1, time(), true);
    apache_admin_mysql(2, "Success injecting data from " . basename($workfile) . " took:{$took}", null, __FILE__, __LINE__);
    @unlink($workfile);
}
function scan()
{
    $unix = new unix();
    $pidTime = "/etc/artica-postfix/pids/exec.bandwidthd.scan.php.scan.time";
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $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());
    $time = $unix->file_time_min($pidTime);
    if ($time < 5) {
        return;
    }
    @unlink($pidTime);
    @file_put_contents($pidTime, time());
    if (system_is_overloaded(basename(__FILE__))) {
        return;
    }
    //IP Address,Timestamp,Total Sent,
    //Icmp Sent,Udp Sent,Tcp Sent,Ftp Sent,Http Sent, P2P Sent,Total Received,Icmp Received,
    //Udp Received,Tcp Received,Ftp Received,Http Received, P2P Received
    $unix = new unix();
    $hostname = $unix->hostname_g();
    $f = explode("\n", @file_get_contents("/usr/bandwidthd/log.1.0.cdf"));
    $workfile = "/usr/bandwidthd/log.1.0.cdf";
    $handle = @fopen($workfile, "r");
    if (!$handle) {
        return false;
    }
    while (!feof($handle)) {
        $buffer = trim(fgets($handle));
        $t = explode(",", $buffer);
        echo count($t) . "\n";
        if (intval($t[1]) < 100) {
            continue;
        }
        $IPAddr = $t[0];
        $date = date("Y-m-d H:i:00", $t[1]);
        $InfluxTime = TimeToInflux(strtotime($date));
        $TotalSent = $t[2];
        $ICMPSent = $t[3];
        $UDPSent = $t[4];
        $TCPSent = $t[5];
        $FTPSent = $t[6];
        $HTTPSent = $t[7];
        $P2PSent = $t[8];
        $Received = $t[9];
        $ICMPReceived = $t[10];
        $UDPReceived = $t[11];
        $TCPReceived = $t[12];
        $FTPReceived = $t[13];
        $HTTReceived = $t[14];
        $P2PReceived = $t[15];
        $key = md5("{$IPAddr}{$date}");
        if (!isset($ARRAY[$key])) {
            $ARRAY[$key]["PROXYNAME"] = $hostname;
            $ARRAY[$key]["IPADDR"] = $IPAddr;
            $ARRAY[$key]["TIME"] = $InfluxTime;
            $ARRAY[$key]["TIMSQL"] = $date;
            $ARRAY[$key]["TotalSent"] = $TotalSent;
            $ARRAY[$key]["Received"] = $Received;
        } else {
            $ARRAY[$key]["IPAddr"] = $ARRAY[$key]["IPAddr"] + $IPAddr;
            $ARRAY[$key]["TotalSent"] = $ARRAY[$key]["TotalSent"] + $TotalSent;
            $ARRAY[$key]["Received"] = $ARRAY[$key]["Received"] + $Received;
        }
    }
    while (list($KEYMD5, $subarray) = each($ARRAY)) {
        $FINAL_SQL[] = "('{$subarray["TIMSQL"]}','{$subarray["IPADDR"]}','{$subarray["TotalSent"]}','{$subarray["Received"]}')";
    }
    if (count($FINAL_SQL) == 0) {
        return;
    }
    $q = new mysql_squid_builder();
    $table_name = "bandwidthd_" . strtotime(date("Y-m-d 00:00:00"));
    if (!$q->TABLE_EXISTS($table_name)) {
        $sql = "CREATE TABLE  IF NOT EXISTS `{$table_name}` (\n\t`zDate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,\n\t`ipaddr` VARCHAR( 60 ) NOT NULL ,\n\t`TotalS` INT UNSIGNED ,\n\t`TotalR` INT UNSIGNED ,\n\t INDEX ( `zDate` , `TotalS` , `TotalR`),\n\tKEY `ipaddr` (`ipaddr`))\n\t";
        $q->QUERY_SQL($sql);
    } else {
        $q->QUERY_SQL("TRUNCATE TABLE {$table_name}");
    }
    $q->QUERY_SQL("INSERT INTO {$table_name} (zDate,ipaddr,TotalS,TotalR) VALUES " . @implode(",", $FINAL_SQL));
    frontend(true);
    compress_tables();
}