Beispiel #1
0
function dump_main()
{
    $date = date("Y-m-d H:i:00");
    $influx = new influx();
    while (list($xdate, $array) = each($GLOBALS["MAIN"])) {
        if ($xdate == $date) {
            continue;
        }
        while (list($category, $count) = each($array)) {
            $zArray = array();
            $zArray["tags"]["category"] = $category;
            $zArray["fields"]["hits"] = intval($count);
            $zArray["tags"]["proxyname"] = $GLOBALS["MYHOSTNAME"];
            squid_watchdog_events("Influx -> {$xdate}/{$date}: {$category}: {$count}");
            $influx->insert("hypercache", $zArray);
        }
        unset($GLOBALS["MAIN"][$xdate]);
    }
    reset($GLOBALS["MAIN"]);
}
Beispiel #2
0
}
WLOG("Starting PID:{$GLOBALS["MYPID"]}");
WLOG("SSL : Starting SNI certificate verification.. ARGV=[" . @implode(" ", $argv) . "]");
$q = new influx();
$c = 0;
$DCOUNT = 0;
while (!feof(STDIN)) {
    $data = trim(fgets(STDIN));
    if ($data == null) {
        continue;
    }
    $c++;
    $DCOUNT++;
    $array["tags"]["website"] = $data;
    $array["fields"]["RQS"] = 1;
    $q->insert("sni_certs", $array);
    $result = isMatches($data);
    if ($c > 500) {
        WLOG("{$DCOUNT} requests...");
        $c = 0;
    }
    if (!$result) {
        fwrite(STDOUT, "ERR\n");
        continue;
    }
    if ($GLOBALS["DEBUG"]) {
        WLOG("{$DCOUNT}] OK");
    }
    fwrite(STDOUT, "OK\n");
}
WLOG("Stopping PID:{$GLOBALS["MYPID"]} After {$DCOUNT} events");
Beispiel #3
0
function clamd()
{
    $EnableClamavDaemon = $GLOBALS["CLASS_UNIX"]->EnableClamavDaemon();
    if (!is_numeric($EnableClamavDaemon)) {
        $EnableClamavDaemon = 0;
    }
    $AS_SQUID = false;
    $squidbin = $GLOBALS["CLASS_UNIX"]->LOCATE_SQUID_BIN();
    if (is_file($squidbin)) {
        $AS_SQUID = true;
    }
    $master_pid = clamd_pid();
    $l[] = "[CLAMAV]";
    $l[] = "service_name=APP_CLAMAV";
    $l[] = "master_version=" . clamd_version();
    $l[] = "service_cmd=/etc/init.d/clamav-daemon";
    $l[] = "service_disabled={$EnableClamavDaemon}";
    $l[] = "binpath=";
    $l[] = "family=system";
    $l[] = "watchdog_features=1";
    $l[] = "";
    if ($EnableClamavDaemon == 0) {
        if ($GLOBALS["CLASS_UNIX"]->process_exists($master_pid)) {
            $GLOBALS["CLASS_UNIX"]->THREAD_COMMAND_SET("/etc/init.d/clamav-daemon stop");
        }
    }
    if ($EnableClamavDaemon == 0) {
        $l[] = "";
        return implode("\n", $l);
        return;
    }
    if (!$GLOBALS["CLASS_UNIX"]->process_exists($master_pid)) {
        if (!$GLOBALS["DISABLE_WATCHDOG"]) {
            $cmd = trim("{$GLOBALS["nohup"]} {$GLOBALS["NICE"]} {$GLOBALS["PHP5"]} " . dirname(__FILE__) . "/exec.clamd.php --start >/dev/null 2>&1 &");
            shell_exec2($cmd);
        }
        $l[] = "";
        return implode("\n", $l);
        return;
    }
    $l[] = GetMemoriesOf($master_pid);
    $l[] = "";
    $timeFile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    $timeTimeFile = $GLOBALS["CLASS_UNIX"]->file_time_min($timeFile);
    if (!$GLOBALS["DISABLE_WATCHDOG"]) {
        if (!$GLOBALS["CLASS_UNIX"]->is_socket("/var/run/clamav/clamav.sock")) {
            if ($AS_SQUID) {
                squid_admin_mysql(1, "{reconfigure} clamav /var/run/clamav/clamav.sock socket missing", "", __FILE__, __LINE__);
            }
            $cmd = trim("{$GLOBALS["nohup"]} /etc/init.d/clamav-daemon restart >/dev/null 2>&1 &");
            shell_exec2($cmd);
        }
        if (!is_file("/var/lib/clamav/main.cvd")) {
            if ($AS_SQUID) {
                squid_admin_mysql(1, "Run Clamav Updates main.cvd missing", "", __FILE__, __LINE__);
            }
            $cmd = trim("{$GLOBALS["NICE"]} {$GLOBALS["PHP5"]} " . dirname(__FILE__) . "/exec.freshclam.php --exec --force >/dev/null 2>&1 &");
            shell_exec2($cmd);
        }
    }
    if ($timeTimeFile > 5) {
        @unlink($timeFile);
        @file_put_contents($timeFile, time());
        $ClamavRefreshDaemonTime = intval($GLOBALS["CLASS_SOCKETS"]->GET_INFO("ClamavRefreshDaemonTime"));
        $ClamavRefreshDaemonMemory = intval($GLOBALS["CLASS_SOCKETS"]->GET_INFO("ClamavRefreshDaemonMemory"));
        if (!is_numeric($ClamavRefreshDaemonMemory)) {
            $ClamavRefreshDaemonMemory = 350;
        }
        if (!is_numeric($ClamavRefreshDaemonTime)) {
            $ClamavRefreshDaemonTime = 60;
        }
        if ($ClamavRefreshDaemonTime > 2) {
            $ClamavRefreshDaemonTime = $ClamavRefreshDaemonTime - 1;
        }
        $rss = $GLOBALS["CLASS_UNIX"]->PROCESS_MEMORY($master_pid, false);
        $vm = $GLOBALS["CLASS_UNIX"]->PROCESS_CACHE_MEMORY($master_pid, false);
        $time = time();
        $influx = new influx();
        $array["fields"]["RSS"] = $rss;
        $array["fields"]["VM"] = $vm;
        $array["tags"]["proxyname"] = $GLOBALS["CLASS_UNIX"]->hostname_g();
        $influx->insert("clamd_mem", $array);
        if ($ClamavRefreshDaemonTime > 10) {
            if ($ClamavRefreshDaemonMemory > 10) {
                if ($rss > $ClamavRefreshDaemonMemory) {
                    if ($AS_SQUID) {
                        squid_admin_mysql(2, "Reboot ClamAV Antivirus Daemon", "ClamAV Antivirus Daemon memory {$rss}MB exceed {$ClamavRefreshDaemonMemory}MB", __FILE__, __LINE__);
                    }
                    $cmd = trim("{$GLOBALS["nohup"]} /etc/init.d/clamav-daemon restart >/dev/null 2>&1 &");
                    shell_exec2($cmd);
                }
            }
            $time = $GLOBALS["CLASS_UNIX"]->PROCCESS_TIME_MIN($master_pid);
            if ($time > $ClamavRefreshDaemonTime) {
                if ($AS_SQUID) {
                    squid_admin_mysql(2, "Reboot ClamAV Antivirus Daemon", "ClamAV Antivirus Daemon TTL {$time} minutes exceed {$ClamavRefreshDaemonTime} minutes", __FILE__, __LINE__);
                }
                events("Reboot clamd daemon");
                $cmd = trim("{$GLOBALS["nohup"]} /etc/init.d/clamav-daemon restart >/dev/null 2>&1 &");
                shell_exec2($cmd);
            }
        }
    }
    return implode("\n", $l);
    return;
}
function UserAgentsStatisticsMemDump()
{
    $time = $GLOBALS["CACHEDUserAgentsStatistics"]["TIME"];
    $xtime = tool_time_sec($GLOBALS["CACHEDUserAgentsStatistics"]["TIME"]);
    if ($GLOBALS["DEBUG_MEM"]) {
        events("CACHEDUserAgentsStatistics: {$xtime}s/10 " . count($GLOBALS["CACHEDUserAgentsStatistics"]) . " elemnt(s)");
    }
    if ($xtime < 10) {
        return;
    }
    $MAIN = $GLOBALS["CACHEDUserAgentsStatistics"];
    $q = new influx();
    while (list($KEYMD5, $ARRAY) = each($MAIN)) {
        if (!isset($GLOBALS["CACHEDUserAgentsStatistics"][$KEYMD5]["USERAGENT"])) {
            continue;
        }
        $PROXYNAME = $GLOBALS["CACHEDUserAgentsStatistics"][$KEYMD5]["PROXYNAME"];
        $USERAGENT = $GLOBALS["CACHEDUserAgentsStatistics"][$KEYMD5]["USERAGENT"];
        $UID = $GLOBALS["CACHEDUserAgentsStatistics"][$KEYMD5]["UID"];
        $MAC = $GLOBALS["CACHEDUserAgentsStatistics"][$KEYMD5]["MAC"];
        $SIZE = intval($GLOBALS["CACHEDUserAgentsStatistics"][$KEYMD5]["SIZE"]);
        $RQS = intval($GLOBALS["CACHEDUserAgentsStatistics"][$KEYMD5]["RQS"]);
        $line = time() . ":::{$USERAGENT}:::{$UID}:::{$MAC}:::{$SIZE}:::{$RQS}:::{$PROXYNAME}";
        if ($GLOBALS["NoCompressStatisticsByHour"] == 0) {
            writeCompresslogs("{$GLOBALS["LogFileDeamonLogDir"]}/USERAGENTS", $line);
            unset($GLOBALS["CACHEDUserAgentsStatistics"][$KEYMD5]);
            continue;
        }
        $zArray = array();
        $zArray["tags"]["USERAGENT"] = $USERAGENT;
        $zArray["tags"]["UID"] = $UID;
        $zArray["tags"]["MAC"] = $MAC;
        $zArray["fields"]["SIZE"] = $SIZE;
        $zArray["fields"]["RQS"] = $RQS;
        $zArray["tags"]["proxyname"] = $PROXYNAME;
        $zArray["fields"]["ZDATE"] = time();
        if ($GLOBALS["DEBUG_MEM"]) {
            events("INSERT - {$zArray["tags"]["USERAGENT"]} {$zArray["fields"]["SIZE"]}Bytes {$zArray["fields"]["RQS"]}rqs [" . __LINE__ . "]");
        }
        if ($GLOBALS["EnableInfluxDB"] == 1) {
            $q->insert("useragents", $zArray);
        }
        unset($GLOBALS["CACHEDUserAgentsStatistics"][$KEYMD5]);
    }
    $GLOBALS["CACHEDUserAgentsStatistics"] = array();
    $GLOBALS["CACHEDUserAgentsStatistics"]["TIME"] = time();
}
function squid_memory_monitor()
{
    $unix = new unix();
    $EnableIntelCeleron = intval(file_get_contents("/etc/artica-postfix/settings/Daemons/EnableIntelCeleron"));
    if ($EnableIntelCeleron == 1) {
        return;
    }
    $pidfileTime = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    if ($GLOBALS["VERBOSE"]) {
        echo "{$pidfileTime}\n";
    }
    if (!$GLOBALS["VERBOSE"]) {
        if ($unix->file_time_min($pidfileTime) < 10) {
            return;
        }
    }
    $sock = new sockets();
    $MonitConfig = unserialize(base64_decode($sock->GET_INFO("SquidWatchdogMonitConfig")));
    $MonitConfig = watchdog_config_default($MonitConfig);
    if (!class_exists("influx")) {
        return;
    }
    $q = new influx();
    $total_memory = 0;
    $python = $unix->find_program("python");
    exec("{$python} /usr/share/artica-postfix/bin/ps_mem.py 2>&1", $results);
    while (list($index, $line) = each($results)) {
        $line = trim($line);
        if (!preg_match("#^[0-9\\.]+.*?=\\s+([0-9\\.]+)\\s+([a-zA-Z]+)\\s+squid#", $line, $re)) {
            if ($GLOBALS["VERBOSE"]) {
                echo "No match {$line}\n";
            }
            continue;
        }
        $total_memory = $re[1];
        $total_memory_unit = $re[2];
        if ($GLOBALS["VERBOSE"]) {
            echo "!!Match : {$total_memory} {$total_memory_unit}\n";
        }
        break;
    }
    if ($total_memory == 0) {
        return;
    }
    if ($total_memory_unit == "KiB") {
        $total_memory = round($total_memory * 1024);
    }
    if ($total_memory_unit == "MiB") {
        $total_memory = round($total_memory * 1024);
        $total_memory = $total_memory * 1024;
    }
    if ($total_memory_unit == "GiB") {
        $total_memory = round($total_memory * 1024);
        $total_memory = $total_memory * 1024;
        $total_memory = $total_memory * 1024;
    }
    $total_memory_MB = $total_memory / 1024;
    $total_memory_MB = $total_memory_MB / 1024;
    $MEMORY_SYSTEM_MB = $unix->SYSTEM_GET_MEMORY_MB();
    if ($GLOBALS["VERBOSE"]) {
        echo "MEMORY_SYSTEM_MB: {$MEMORY_SYSTEM_MB}MB\n";
    }
    $prc = $total_memory_MB / $MEMORY_SYSTEM_MB;
    $prc = round($prc * 100, 2);
    $total_memory_MB = round($total_memory_MB);
    if ($GLOBALS["VERBOSE"]) {
        echo "Bytes: {$total_memory} MB: {$total_memory_MB}/{$MEMORY_SYSTEM_MB} ({$prc}%) \n";
    }
    $date = date("Y-m-d H:i:s");
    $array["fields"]["memory"] = $total_memory_MB;
    $array["fields"]["prc"] = $prc;
    $array["tags"]["proxyname"] = $unix->hostname_g();
    if ($GLOBALS["VERBOSE"]) {
        print_r($array);
    }
    $q->insert("squidmem", $array);
    if ($MonitConfig["watchdog"] == 0) {
        return;
    }
    if ($MonitConfig["watchdogRestart"] > 99) {
        return;
    }
    if ($prc > $MonitConfig["watchdogRestart"]) {
        $GLOBALS["ALL_SCORES"]++;
        $GLOBALS["ALL_SCORES_WHY"][] = "Proxy service memory exceed % value ({$prc})";
        $GLOBALS["BY_WATCHDOG"] = true;
        restart_squid(true);
    }
}
function update_commit($ip, $mac, $hostname)
{
    $ipClass = new IP();
    if (!$ipClass->isValid($ip)) {
        return;
    }
    $macZ = explode(":", $mac);
    while (list($num, $ligne) = each($macZ)) {
        if (strlen($ligne) == 1) {
            $macZ[$num] = "0{$ligne}";
        }
    }
    $mac = @implode(":", $macZ);
    if (preg_match("#^(.+?)\\.#", $hostname, $re)) {
        $hostname = $re[1];
    }
    localsyslog("Commit: IP:{$ip},{$mac},{$hostname}");
    $influx = new influx();
    $array["tags"]["ACTION"] = "COMMIT";
    $array["tags"]["HOSTNAME"] = "{$hostname}";
    $array["tags"]["IPADDR"] = "{$ip}";
    $array["tags"]["MAC"] = "{$mac}";
    $array["fields"]["RQS"] = 1;
    $influx->insert("dhcpd", $array);
}
Beispiel #7
0
function RXTX()
{
    $unix = new unix();
    $Cache = unserialize(@file_get_contents("/etc/artica-postfix/RXTX.array"));
    $ifconfig = $unix->find_program("ifconfig");
    exec("{$ifconfig} -a 2>&1", $results);
    foreach ($results as $line) {
        $line = trim($line);
        if ($line == null) {
            continue;
        }
        if (preg_match("#^([a-z0-9]+)\\s+Link#", $line, $re)) {
            $Interface = $re[1];
            continue;
        }
        if (preg_match("#RX bytes:([0-9]+).*?TX bytes:([0-9]+)#", $line, $re)) {
            $ARRAY[$Interface]["RX"] = $re[1];
            $ARRAY[$Interface]["TX"] = $re[2];
            continue;
        }
    }
    $q = new influx();
    while (list($Interface, $array) = each($ARRAY)) {
        $RX = $array["RX"];
        $TX = $array["TX"];
        $OLD_RX = intval($Cache[$Interface]["RX"]);
        $OLD_TX = intval($Cache[$Interface]["TX"]);
        if ($OLD_RX > $RX) {
            continue;
        }
        if ($OLD_TX > $TX) {
            continue;
        }
        $RX_NEW = $RX - $OLD_RX;
        $TX_NEW = $TX - $OLD_TX;
        if ($GLOBALS["VERBOSE"]) {
            echo "{$Interface} Rec:" . xFormatBytes($RX_NEW / 1024) . " Trans:" . xFormatBytes($TX_NEW / 1024) . "\n";
        }
        $INFLX["fields"]["TX"] = $TX_NEW;
        $INFLX["fields"]["RX"] = $RX_NEW;
        $INFLX["tags"]["ETH"] = $Interface;
        $INFLX["tags"]["proxyname"] = $unix->hostname_g();
        $q->insert("ethrxtx", $INFLX);
        $INFLX = array();
    }
    @file_put_contents("/etc/artica-postfix/RXTX.array", serialize($ARRAY));
}
function build_last_hour()
{
    $sock = new sockets();
    $influx = new influx();
    $data = $influx->QUERY_SQL("SELECT MAX(ZDATE) as MAX from access_hour");
    $date_end = InfluxQueryFromUTC($data[0]->MAX);
    echo "Date Start: {$date_end}\n";
    $LastHour = InfluxQueryFromUTC(strtotime("-1 hour"));
    $ThisHour = strtotime(date("Y-m-d H:00:00", $LastHour));
    if ($date_end > 0) {
        $WHERE_TIME = "WHERE time > '" . date("Y-m-d H:i:s", $date_end) . "' AND time < '" . date("Y-m-d H:i:s", $ThisHour) . "'";
        $seconds = InfluxQueryFromUTC(time()) - strtotime(date("Y-m-d H:00:00", $date_end));
        $diff = abs($ThisHour - $date_end) / 60;
        if ($GLOBALS["VERBOSE"]) {
            echo "Last date saved is " . date("Y-m-d H:i:s", $date_end) . "({$date_end}) To " . date("Y-m-d H:i:s", $ThisHour) . " ({$ThisHour}) {$diff}mn..\n";
        }
        if ($GLOBALS["VERBOSE"]) {
            echo "{$WHERE_TIME}\n";
        }
        if ($diff < 60) {
            if ($GLOBALS["VERBOSE"]) {
                echo "{$diff}mn < 60 aborting...\n";
            }
            return;
        }
    } else {
        $WHERE_TIME = "WHERE time < '" . date("Y-m-d H:i:s", $ThisHour) . "'";
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "Query From " . date("Y-m-d H:i:s", $date_end) . " to " . date("Y-m-d H:i:s", $ThisHour) . "\n";
    }
    $sql = "SELECT SIZE,RQS,FAMILYSITE,USERID,IPADDR,MAC,CATEGORY FROM access_log {$WHERE_TIME}";
    if ($GLOBALS["VERBOSE"]) {
        echo "{$sql}\n";
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "*******************************************\n";
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "\n";
    }
    $main = $influx->QUERY_SQL($sql);
    $MAIN_ARRAY = array();
    $catz = new mysql_catz();
    if ($GLOBALS["VERBOSE"]) {
        echo count($main) . " elements";
    }
    $xtime = 0;
    foreach ($main as $row) {
        $time = InfluxToTime($row->time);
        $Time_hour = date("Y-m-d H:00:00", $time);
        $size = intval($row->SIZE);
        $RQS = intval($row->RQS);
        $FAMILYSITE = $row->FAMILYSITE;
        $CATEGORY = $row->CATEGORY;
        if ($CATEGORY == null) {
            $CATEGORY = $catz->GET_CATEGORIES($FAMILYSITE);
        }
        $USERID = $row->USERID;
        $IPADDR = $row->IPADDR;
        $MAC = $row->MAC;
        //if($GLOBALS["VERBOSE"]){echo "$row->time] [$Time_hour] $FAMILYSITE {$size}Bytes, $CATEGORY $USERID/$IPADDR/$MAC\n";}
        if ($size == 0) {
            continue;
        }
        if ($RQS == 0) {
            continue;
        }
        if ($time > $xtime) {
            $xtime = $time;
        }
        $MD5KEY = md5("{$CATEGORY}{$USERID}{$FAMILYSITE}{$IPADDR}{$MAC}");
        if (!isset($MAIN_ARRAY[$Time_hour][$MD5KEY])) {
            $MAIN_ARRAY[$Time_hour][$MD5KEY]["FAMILYSITE"] = $FAMILYSITE;
            $MAIN_ARRAY[$Time_hour][$MD5KEY]["CATEGORY"] = $CATEGORY;
            $MAIN_ARRAY[$Time_hour][$MD5KEY]["USERID"] = $USERID;
            $MAIN_ARRAY[$Time_hour][$MD5KEY]["IPADDR"] = $IPADDR;
            $MAIN_ARRAY[$Time_hour][$MD5KEY]["MAC"] = $MAC;
            $MAIN_ARRAY[$Time_hour][$MD5KEY]["size"] = $size;
            $MAIN_ARRAY[$Time_hour][$MD5KEY]["hits"] = $RQS;
            $MAIN_ARRAY[$Time_hour][$MD5KEY]["ZDATE"] = $time;
        } else {
            $MAIN_ARRAY[$Time_hour][$MD5KEY]["size"] = $MAIN_ARRAY[$Time_hour][$MD5KEY]["size"] + $size;
            $MAIN_ARRAY[$Time_hour][$MD5KEY]["hits"] = $MAIN_ARRAY[$Time_hour][$MD5KEY]["hits"] + $RQS;
            if ($time > $MAIN_ARRAY[$Time_hour][$MD5KEY]["ZDATE"]) {
                $MAIN_ARRAY[$Time_hour][$MD5KEY]["ZDATE"] = $time;
            }
        }
    }
    $sq = new influx();
    if (count($MAIN_ARRAY) == 0) {
        if ($GLOBALS["VERBOSE"]) {
            echo "No array....\n";
        }
    }
    while (list($ztime, $array) = each($MAIN_ARRAY)) {
        while (list($md5, $Tarray) = each($array)) {
            $sdate = $ztime;
            $USERID = $Tarray["USERID"];
            $IPADDR = $Tarray["IPADDR"];
            $MAC = $Tarray["MAC"];
            $FAMILYSITE = $Tarray["FAMILYSITE"];
            $CATEGORY = $Tarray["CATEGORY"];
            $size = $Tarray["size"];
            $RQS = $Tarray["hits"];
            $ZDATE = $Tarray["ZDATE"];
            if ($GLOBALS["VERBOSE"]) {
                echo date("Y-m-d H:i:s", $ZDATE) . " {$USERID}/{$IPADDR}/{$MAC} -> [{$FAMILYSITE}/{$CATEGORY}] {$size}/{$RQS}\n";
            }
            $zArray = array();
            $zArray["fields"]["time"] = $sdate;
            $zArray["tags"]["CATEGORY"] = $CATEGORY;
            $zArray["tags"]["USERID"] = $USERID;
            $zArray["tags"]["IPADDR"] = $IPADDR;
            $zArray["tags"]["MAC"] = $MAC;
            $zArray["fields"]["SIZE"] = intval($size);
            $zArray["tags"]["FAMILYSITE"] = $FAMILYSITE;
            $zArray["fields"]["RQS"] = intval($RQS);
            $zArray["fields"]["ZDATE"] = $ZDATE;
            $sq->insert("access_hour", $zArray);
        }
    }
    $data = $influx->QUERY_SQL("SELECT MAX(ZDATE) as MAX from access_hour");
    $date_end = $data[0]->MAX;
    if ($GLOBALS["VERBOSE"]) {
        echo "Query was {$WHERE_TIME}\n";
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "Last time : {$xtime} " . date("Y-m-d H:i:s", $xtime) . "\n";
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "Last date saved is " . date("Y-m-d H:i:s", $date_end) . "({$date_end}) To " . date("Y-m-d H:i:s", $ThisHour) . " ({$ThisHour}) {$diff}mn..\n";
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "Last final access_hour saved date {$date_end} - " . date("Y-m-d H:i:s", $date_end) . "\n";
    }
}
function xtstart()
{
    $unix = new unix();
    $sock = new sockets();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $weektime = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".week.time";
    // /etc/artica-postfix/pids/exec.squid.rotate.php.build.time
    $sock = new sockets();
    $unix = new unix();
    $pid = $unix->get_pid_from_file($pidfile);
    if ($unix->process_exists($pid)) {
        echo "Already PID {$pid} is running\n";
        die;
    }
    @file_put_contents($pidfile, getmypid());
    $influx = new influx();
    $UserAgentsStatistics = intval($sock->GET_INFO("UserAgentsStatistics"));
    $EnableKerbAuth = intval($sock->GET_INFO("EnableKerbAuth"));
    $USERMM = "MAC";
    if ($EnableKerbAuth == 1) {
        $USERMM = "USERID";
    }
    $time = strtotime('5 minutes ago');
    $time_week = strtotime("-7 days");
    if ($UserAgentsStatistics == 1) {
        $sql = "SELECT SUM(SIZE) as size,COUNT(RQS) as hits,USERAGENT,MAC,uid FROM useragents  WHERE time> {$time}s GROUP BY time(10m),USERAGENT,MAC,uid ORDER BY ASC";
        if ($GLOBALS["VERBOSE"]) {
            echo "{$sql}\n";
        }
        $main = $influx->QUERY_SQL($sql);
        foreach ($main as $row) {
            $USERAGENT = $row->USERAGENT;
            $MAC = $row->MAC;
            $uid = $row->uid;
            $size = intval($row->size);
            $hits = intval($row->hits);
            if ($USERAGENT == null) {
                continue;
            }
            if ($size == null) {
                continue;
            }
            if ($size == 0) {
                continue;
            }
            $USERAGENT = mysql_escape_string2($USERAGENT);
            if ($GLOBALS["VERBOSE"]) {
                echo "MAIN_AGT: {$USERAGENT} -> {$size}\n";
            }
            $uid = mysql_escape_string2($uid);
            $f[] = "('{$size}','{$hits}','{$USERAGENT}','{$MAC}','{$uid}')";
        }
        $q = new mysql_squid_builder();
        if (!$q->TABLE_EXISTS("current_useragnt10m")) {
            $q->QUERY_SQL("CREATE TABLE IF NOT EXISTS current_useragnt10m (\n\t\t\t\t`hits` BIGINT UNSIGNED,\n\t\t\t\t`size` BIGINT UNSIGNED,\n\t\t\t\t`USERAGENT` VARCHAR(256) NOT NULL,\n\t\t\t\t`MAC` VARCHAR(90) NOT NULL,\n\t\t\t\t`uid` VARCHAR(128) NOT NULL\n\t\t\t\t\t\n\t\t\t\t) ENGINE=MYISAM\n\t\t\t\t");
        }
        if (!$q->TABLE_EXISTS("week_useragnt")) {
            $q->QUERY_SQL("CREATE TABLE IF NOT EXISTS week_useragnt (\n\t\t\t\t`hits` BIGINT UNSIGNED,\n\t\t\t\t`size` BIGINT UNSIGNED,\n\t\t\t\t`USERAGENT` VARCHAR(256) NOT NULL,\n\t\t\t\t`MAC` VARCHAR(90) NOT NULL,\n\t\t\t\t`uid` VARCHAR(128) NOT NULL\n\t\t\t\n\t\t\t\t) ENGINE=MYISAM\n\t\t\t\t");
        }
        if (count($f) > 0) {
            $q->QUERY_SQL("TRUNCATE TABLE current_useragnt10m");
            $q->QUERY_SQL("INSERT IGNORE INTO current_useragnt10m (hits,size,USERAGENT,MAC,uid) VALUES " . @implode(",", $f));
            $f = array();
        }
        $xtime = $unix->file_time_min($weektime);
        if ($GLOBALS["VERBOSE"]) {
            $xtime = 10000000;
        }
        if ($xtime > 360) {
            $sql = "SELECT SUM(SIZE) as size,COUNT(RQS) as hits,USERAGENT,MAC,uid FROM useragents WHERE time> {$time_week}s GROUP BY time(7d),USERAGENT,MAC,uid ORDER BY ASC";
            if ($GLOBALS["VERBOSE"]) {
                echo "{$sql}\n";
            }
            $main = $influx->QUERY_SQL($sql);
            $f = array();
            foreach ($main as $row) {
                $USERAGENT = $row->USERAGENT;
                $MAC = $row->MAC;
                $uid = $row->uid;
                $size = intval($row->size);
                $hits = intval($row->hits);
                if ($USERAGENT == null) {
                    continue;
                }
                if ($size == null) {
                    continue;
                }
                if ($size == 0) {
                    continue;
                }
                $USERAGENT = mysql_escape_string2($USERAGENT);
                if ($GLOBALS["VERBOSE"]) {
                    echo "MAIN_AGT: {$USERAGENT} -> {$size}\n";
                }
                $uid = mysql_escape_string2($uid);
                $f[] = "('{$size}','{$hits}','{$USERAGENT}','{$MAC}','{$uid}')";
            }
            if (count($f) > 0) {
                $q->QUERY_SQL("TRUNCATE TABLE useragents");
                $q->QUERY_SQL("INSERT IGNORE INTO useragents (hits,size,USERAGENT,MAC,uid) VALUES " . @implode(",", $f));
                $f = array();
            }
            @unlink($weektime);
            @file_put_contents($weektime, time());
        }
    }
    $sql = "SELECT SIZE,RQS,{$USERMM} from access_log WHERE time> {$time}s";
    if ($GLOBALS["VERBOSE"]) {
        echo "{$sql}\n";
    }
    $MAIN = array();
    $xdata = array();
    $ydata = array();
    $f = array();
    $influx = new influx();
    $main = $influx->QUERY_SQL($sql);
    $MEMBERS_COUNT = 0;
    foreach ($main as $row) {
        $USER = $row->{$USERMM};
        $size = intval($row->SIZE);
        $hits = intval($row->RQS);
        if ($USER == null) {
            continue;
        }
        if ($size == 0) {
            continue;
        }
        if (!isset($XMAIN[$USER])) {
            $XMAIN[$USER]["SIZE"] = $size;
            $XMAIN[$USER]["HITS"] = $hits;
        } else {
            $XMAIN[$USER]["SIZE"] = $XMAIN[$USER]["SIZE"] + $size;
            $XMAIN[$USER]["HITS"] = $XMAIN[$USER]["HITS"] + $hits;
        }
    }
    while (list($USER, $ARRAY) = each($XMAIN)) {
        if ($GLOBALS["VERBOSE"]) {
            echo "{$USER} {$ARRAY["SIZE"]}/{$ARRAY["HITS"]}\n";
        }
        $f[] = "('{$USER}','{$ARRAY["SIZE"]}','{$ARRAY["HITS"]}')";
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "RESULTS=" . count($f) . " members...\n";
    }
    if (count($f) == 0) {
        return;
    }
    $q = new mysql_squid_builder();
    if (!$q->TABLE_EXISTS("current_members10m")) {
        $q->QUERY_SQL("CREATE TABLE IF NOT EXISTS current_members10m (\n\t\t\t\t`hits` BIGINT UNSIGNED,\n\t\t\t\t`size` BIGINT UNSIGNED,\n\t\t\t\t`member` VARCHAR(128) NOT NULL PRIMARY KEY) ENGINE=MYISAM\n\t\t\t\t");
    }
    if (count($f) > 0) {
        $q->QUERY_SQL("TRUNCATE TABLE current_members10m");
        $q->QUERY_SQL("INSERT IGNORE INTO current_members10m (member,hits,size) VALUES " . @implode(",", $f));
        $MEMBERS_COUNT = $q->COUNT_ROWS("current_members10m");
        @file_put_contents("{$GLOBALS["BASEDIR"]}/MEMBERS_COUNT10M", $MEMBERS_COUNT);
    }
    $array["tags"]["proxyname"] = $unix->hostname_g();
    $array["fields"]["members"] = $MEMBERS_COUNT;
    $influx->insert("members_count", $array);
    $f = array();
}
function CachedUserMemDump()
{
    $xtime = tool_time_sec($GLOBALS["LOGACCESS_TIME"]);
    if ($xtime < 10) {
        return;
    }
    $c = 0;
    $MAIN = $GLOBALS["CACHEDUSersMem"];
    $q = new influx();
    $xRQS = 0;
    while (list($KEYMD5, $ARRAY) = each($MAIN)) {
        $zArray = array();
        $zArray2 = array();
        if (!isset($GLOBALS["CACHEDUSersMem"][$KEYMD5]["SITE"])) {
            unset($GLOBALS["CACHEDUSersMem"][$KEYMD5]);
            continue;
        }
        $CATEGORY = $GLOBALS["CACHEDUSersMem"][$KEYMD5]["CATEGORY"];
        $USERID = $GLOBALS["CACHEDUSersMem"][$KEYMD5]["USERID"];
        $IPADDR = $GLOBALS["CACHEDUSersMem"][$KEYMD5]["IPADDR"];
        $MAC = $GLOBALS["CACHEDUSersMem"][$KEYMD5]["MAC"];
        $SIZE = intval($GLOBALS["CACHEDUSersMem"][$KEYMD5]["SIZE"]);
        $SITE = $GLOBALS["CACHEDUSersMem"][$KEYMD5]["SITE"];
        $FAM = $GLOBALS["CACHEDUSersMem"][$KEYMD5]["FAM"];
        $RQS = $GLOBALS["CACHEDUSersMem"][$KEYMD5]["RQS"];
        $PROXYNAME = $GLOBALS["CACHEDUSersMem"][$KEYMD5]["PROXYNAME"];
        $GROUP = $GLOBALS["CACHEDUSersMem"][$KEYMD5]["GROUP"];
        $ORGA = $GLOBALS["CACHEDUSersMem"][$KEYMD5]["ORGA"];
        if ($MAC == null) {
            $MAC = "00:00:00:00:00:00";
        }
        if ($USERID == null) {
            $USERID = "none";
        }
        $xRQS = $xRQS + $RQS;
        $line = time() . ":::{$CATEGORY}:::{$USERID}:::{$IPADDR}:::{$MAC}:::{$SIZE}:::{$SITE}:::{$FAM}:::{$RQS}:::{$PROXYNAME}:::{$GROUP}:::{$ORGA}";
        $c++;
        if ($GLOBALS["NoCompressStatisticsByHour"] == 0) {
            writeCompresslogs("{$GLOBALS["LogFileDeamonLogDir"]}/ACCESS_LOG", $line);
            unset($GLOBALS["CACHEDUSersMem"][$KEYMD5]);
            continue;
        }
        $zArray["tags"]["GROUP"] = $GROUP;
        $zArray["tags"]["ORGA"] = $ORGA;
        $zArray["tags"]["CATEGORY"] = $CATEGORY;
        $zArray["tags"]["USERID"] = $USERID;
        $zArray["tags"]["IPADDR"] = $IPADDR;
        $zArray["tags"]["MAC"] = $MAC;
        $zArray["fields"]["SIZE"] = $SIZE;
        $zArray["tags"]["SITE"] = $SITE;
        $zArray["tags"]["FAMILYSITE"] = $FAM;
        $zArray["fields"]["ZDATE"] = time();
        $zArray["fields"]["RQS"] = $RQS;
        $zArray["tags"]["proxyname"] = $PROXYNAME;
        if ($GLOBALS["DEBUG_MEM"]) {
            events("INSERT - [{$KEYMD5}] {$zArray["tags"]["IPADDR"]} - {$zArray["tags"]["FAMILYSITE"]} - {$zArray["fields"]["SIZE"]}bytes {$zArray["fields"]["RQS"]}rqs [" . __LINE__ . "]");
        }
        $q->insert("access_log", $zArray);
        unset($GLOBALS["CACHEDUSersMem"][$KEYMD5]);
    }
    if (count($GLOBALS["USERRTT"]) > 0) {
        while (list($KEYMD5, $ARRAY) = each($GLOBALS["USERRTT"])) {
            $USERID = $GLOBALS["USERRTT"][$KEYMD5]["USERID"];
            $IPADDR = $GLOBALS["USERRTT"][$KEYMD5]["IPADDR"];
            $MAC = $GLOBALS["USERRTT"][$KEYMD5]["MAC"];
            $SIZE = intval($GLOBALS["USERRTT"][$KEYMD5]["SIZE"]);
            $RQS = $GLOBALS["USERRTT"][$KEYMD5]["RQS"];
            $PROXYNAME = $GLOBALS["USERRTT"][$KEYMD5]["PROXYNAME"];
            $GROUP = $GLOBALS["USERRTT"][$KEYMD5]["GROUP"];
            $ORGA = $GLOBALS["USERRTT"][$KEYMD5]["ORGA"];
            $line = time() . ":::{$USERID}:::{$IPADDR}:::{$MAC}:::{$SIZE}:::{$RQS}:::{$PROXYNAME}::{$GROUP}:::{$ORGA}";
            writeCompresslogs("{$GLOBALS["LogFileDeamonLogDir"]}/USERS_LOG", $line);
            unset($GLOBALS["USERRTT"][$KEYMD5]);
        }
    }
    events("CachedUserMemDump:: Saving {$c}/{$xRQS} requests time={$xtime}s");
    $GLOBALS["CACHEDUSersMemTime"] = array();
    $GLOBALS["USERRTT"] = array();
    $GLOBALS["LOGACCESS_TIME"] = time();
}
Beispiel #11
0
if (!is_numeric($SQUIDEnable)) {
    $SQUIDEnable = 1;
}
if ($SQUIDEnable == 0) {
    die;
}
$cache_manager = new cache_manager();
$data = $cache_manager->makeQuery("5min", true);
$hostname = $unix->hostname_g();
foreach ($data as $ligne) {
    if (preg_match("#server\\.http\\.kbytes_in.*?([0-9\\.]+)#", $ligne, $re)) {
        $kbytes_in = $re[1];
        continue;
    }
    if (preg_match("#client_http\\.kbytes_out.*?([0-9\\.]+)#", $ligne, $re)) {
        $kbytes_out = $re[1];
        continue;
    }
    if (preg_match("#client_http\\.requests.*?([0-9\\.]+)#", $ligne, $re)) {
        $client_http_req = $re[1];
        continue;
    }
}
echo "Server download {$kbytes_in}/sec and sent to client {$kbytes_out}/sec {$client_http_req}/reqs\n";
$q = new influx();
$array["fields"]["REQS"] = $client_http_req;
$array["fields"]["KBIN"] = $kbytes_in;
$array["fields"]["KBOUT"] = $kbytes_out;
$array["tags"]["proxyname"] = $hostname;
$q->insert("httpreqs", $array);