function CHECK_DNS_SYSTEMS()
{
    $unix = new unix();
    $pidtime = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    $pidFile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $BigTime = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".week.time";
    $pid = $unix->get_pid_from_file($pidFile);
    if ($unix->process_exists($pid)) {
        if ($unix->PROCCESS_TIME_MIN($pid, 10) < 2) {
            return;
        }
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "pidtime ={$pidtime}\n";
    }
    @file_put_contents($pidFile, getmypid());
    $time = $unix->file_time_min($pidtime);
    if (!$GLOBALS["FORCE"]) {
        if ($time < 5) {
            Events("{$time}mn < 5mn ( use --force to bypass)");
            if ($GLOBALS["VERBOSE"]) {
                echo "{$time}mn < 5mn ( use --force to bypass)\n";
            }
            return;
        }
    }
    @unlink($pidtime);
    @file_put_contents($pidtime, time());
    $BigTimeEx = $unix->file_time_min($BigTime);
    if ($GLOBALS["VERBOSE"]) {
        echo "{$BigTimeEx}M for {$BigTime}\n";
    }
    if ($BigTimeEx > 4320) {
        $q = new mysql();
        @unlink($BigTimeEx);
        @file_put_contents($BigTimeEx, time());
        Events("DELETE FROM dnsperfs_week WHERE zDate<DATE_SUB(NOW(),INTERVAL 8 DAY)");
        $q->QUERY_SQL("DELETE QUICK FROM dnsperfs_week WHERE zDate<DATE_SUB(NOW(),INTERVAL 8 DAY)", "artica_events");
        $q->QUERY_SQL("OPTIMIZE TABLE dnsperfs_week", "artica_events");
    }
    $resolv = new resolv_conf();
    $q = new mysql_squid_builder();
    $sock = new sockets();
    include_once dirname(__FILE__) . "/ressources/externals/Net_DNS2/DNS2.php";
    include_once dirname(__FILE__) . "/ressources/class.resolv.conf.inc";
    $sql = "SELECT *  FROM dns_servers ORDER BY zOrder";
    $results = $q->QUERY_SQL($sql);
    if (!$q->ok) {
        ToSyslog("{$q->mysql_error}");
        return;
    }
    while ($ligne = mysql_fetch_assoc($results)) {
        $DNS[] = $ligne["dnsserver"];
    }
    $sock = new sockets();
    if ($sock->dnsmasq_enabled() == 1) {
        $DNS[] = "127.0.0.1";
    }
    $q = new mysql();
    $sql = "CREATE TABLE IF NOT EXISTS `dnsperfs` (\n\t\t\t\t`dnsserver` VARCHAR(128) PRIMARY KEY,\n\t\t\t\t`performance` FLOAT(5),\n\t\t\t\t `percent` smallint(2),\n\t\t\t\t `zDate` datetime\n\t\t\t\t ) ENGINE=MyISAM;";
    $q->QUERY_SQL($sql, 'artica_events');
    $sql = "CREATE TABLE IF NOT EXISTS `dnsperfs_week` (\n\t\t\t\t`dnsserver` VARCHAR(128),\n\t\t\t\t`performance` FLOAT(5),\n\t\t\t\t `percent` smallint(2),\n\t\t\t\t `zDate` datetime,\n\t\t\t\t KEY `zDate` (`zDate`),\n\t\t\t     KEY `dnsserver` (`dnsserver`)\n\t\t\t\t ) ENGINE=MyISAM;";
    $q->QUERY_SQL($sql, 'artica_events');
    $nameserver = explode("\n", @file_get_contents("/etc/resolv.conf"));
    while (list($index, $line) = each($nameserver)) {
        $line = trim($line);
        if ($line == null) {
            continue;
        }
        if (!preg_match("#^nameserver\\s+(.+)#i", $line, $re)) {
            continue;
        }
        $COMP[trim($re[1])] = true;
    }
    $q->QUERY_SQL("TRUNCATE TABLE `dnsperfs`", 'artica_events');
    //ref=518379
    if ($resolv->MainArray["DNS1"] != null) {
        $DNS[] = $resolv->MainArray["DNS1"];
    }
    if ($resolv->MainArray["DNS2"] != null) {
        $DNS[] = $resolv->MainArray["DNS2"];
    }
    if ($resolv->MainArray["DNS3"] != null) {
        $DNS[] = $resolv->MainArray["DNS3"];
    }
    $type = "A";
    $ipClass = new IP();
    $minperf = $sock->GET_INFO("DNSPerfsPointer");
    if (!is_numeric($minperf)) {
        $minperf = 301450;
    }
    while (list($index, $dnsA) = each($DNS)) {
        $COMP[$dnsA] = true;
    }
    while (list($dnsA, $none) = each($COMP)) {
        Events("Checks DNS {$dnsA}");
        if (!$ipClass->isIPAddress($dnsA)) {
            continue;
        }
        if ($GLOBALS["VERBOSE"]) {
            echo "{$dnsA}\n";
        }
        $t['start'] = microtime(true);
        $rs = new Net_DNS2_Resolver(array('nameservers' => array($dnsA)));
        try {
            $date = date("Y-m-d H:i:s");
            $tA = rand("10000", "208460");
            $result = $rs->query("p4-cpsk2owex6nby-dwvedtapjla4ebei-{$tA}-i2-v6exp3-v4.metric.gstatic.com", "A");
            $t[$dnsA] = microtime(true);
            $time = mini_bench_to($t);
            $timeC = $time * 10000;
        } catch (Net_DNS2_Exception $e) {
            $error = $e->getMessage();
            $q->QUERY_SQL("INSERT IGNORE INTO `dnsperfs` (dnsserver,performance,percent,zDate) VALUES ('{$dnsA}','0','0','{$date}')", "artica_events");
            $q->QUERY_SQL("INSERT IGNORE INTO `dnsperfs_week` (dnsserver,performance,percent,zDate) VALUES ('{$dnsA}','0','0','{$date}')", "artica_events");
            continue;
        }
        $perc = $minperf / $timeC;
        $perc = round($perc * 100);
        Events("{$dnsA} Response Time:{$time} = {$timeC}/{$minperf} {$perc}%");
        foreach ($result->answer as $record) {
            if ($ipClass->isIPAddress($record->address)) {
                if ($perc > 100) {
                    $perc = 100;
                }
                $q->QUERY_SQL("INSERT IGNORE INTO `dnsperfs` (dnsserver,performance,percent,zDate) VALUES ('{$dnsA}','{$time}','{$perc}','{$date}')", "artica_events");
                if ($GLOBALS["VERBOSE"]) {
                    echo "INSERT IGNORE INTO `dnsperfs_week` (dnsserver,performance,percent,zDate) VALUES ('{$dnsA}','{$time}','{$perc}','{$date}')\n";
                }
                $q->QUERY_SQL("INSERT IGNORE INTO `dnsperfs_week` (dnsserver,performance,percent,zDate) VALUES ('{$dnsA}','{$time}','{$perc}','{$date}')", "artica_events");
                break;
            }
        }
    }
    $cacheFile = "/usr/share/artica-postfix/ressources/logs/web/dnsperformances.cache";
    $sql = "SELECT * FROM dnsperfs ORDER BY percent DESC";
    $results = $q->QUERY_SQL($sql, "artica_events");
    while ($ligne = mysql_fetch_assoc($results)) {
        $percentage = $ligne["percent"];
        $dnsserver = $ligne["dnsserver"];
        if ($percentage > 100) {
            $percentage = 100;
        }
        $TTR[] = "\n\t\t<tr>\n\t\t\t<td style='font-weight:bold;font-size:12px' align='right'>{$dnsserver}:</td>\n\t\t\t<td style='font-weight:bold;font-size:12px'><td>" . pourcentage($percentage, 10, "green") . "</td>\n\t\t</tr>\n\t\t";
    }
    if (count($TTR) > 0) {
        $data = RoundedLightGreen("<div style='min-height:147px'>\n\t\t<table style='width:100%'>\n\t\t\t\t<tr><td colspan=3><span style='font-weight:bold;font-size:12px'>{dns_performance}</span></td></tr>\n\t\t\t\t" . @implode($TTR, "\n") . "\n\t\t\t\t\n\t\t<tr>\n\t\t\t<td colspan=3 align='right'>\n\t\t\t\t<i><a href=\"javascript:blur();\" OnClick=\"javascript:Loadjs('dnsperfs.php',true)\" style='font-weight:normal;font-size:12px;text-decoration:underline'>{more_infos}</a>&nbsp;|&nbsp;\n\t\t\t\t<a href=\"javascript:blur();\" OnClick=\"javascript:Loadjs('squid.popups.php?script=dns');\" style='font-weight:normal;font-size:12px;text-decoration:underline'>{settings}</a>\n\t\t\t\t</i>\n\t\t\t</td>\n\t\t</tr>\t\t\t\t\n\t\t\t\t\n\t\t</table></div>") . "<br>";
        @file_put_contents($cacheFile, $data);
    }
}
Beispiel #2
0
function CHECK_DNS_SYSTEMS()
{
    $unix = new unix();
    include_once dirname(__FILE__) . "/ressources/class.influx.inc";
    $pidtime = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    $pidFile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $BigTime = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".week.time";
    if (!is_file("/etc/artica-postfix/settings/Daemons/EnableDNSPerfs")) {
        @file_put_contents("/etc/artica-postfix/settings/Daemons/EnableDNSPerfs", 1);
    }
    $EnableDNSPerfs = intval(@file_get_contents("/etc/artica-postfix/settings/Daemons/EnableDNSPerfs"));
    if ($EnableDNSPerfs == 0) {
        echo "EnableDNSPerfs -> Disabled\n";
        die;
    }
    $unix = new unix();
    $GLOBALS["MYHOSTNAME"] = $unix->hostname_g();
    $pid = $unix->get_pid_from_file($pidFile);
    if ($unix->process_exists($pid)) {
        if ($unix->PROCCESS_TIME_MIN($pid, 10) < 2) {
            return;
        }
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "pidtime ={$pidtime}\n";
    }
    @file_put_contents($pidFile, getmypid());
    $time = $unix->file_time_min($pidtime);
    if (!$GLOBALS["FORCE"]) {
        if ($time < 5) {
            Events("{$time}mn < 5mn ( use --force to bypass)");
            if ($GLOBALS["VERBOSE"]) {
                echo "{$time}mn < 5mn ( use --force to bypass)\n";
            }
            return;
        }
    }
    @unlink($pidtime);
    @file_put_contents($pidtime, time());
    $BigTimeEx = $unix->file_time_min($BigTime);
    $resolv = new resolv_conf();
    $q = new mysql_squid_builder();
    $sock = new sockets();
    if ($resolv->MainArray["DNS1"] != null) {
        $DNS[] = $resolv->MainArray["DNS1"];
    }
    if ($resolv->MainArray["DNS2"] != null) {
        $DNS[] = $resolv->MainArray["DNS2"];
    }
    if (isset($resolv->MainArray["DNS3"])) {
        if ($resolv->MainArray["DNS3"] != null) {
            $DNS[] = $resolv->MainArray["DNS3"];
        }
    }
    $sql = "SELECT * FROM dns_servers";
    $q = new mysql_squid_builder();
    $results = $q->QUERY_SQL($sql);
    while ($ligne = mysql_fetch_assoc($results)) {
        $DNS[] = $ligne["dnsserver"];
    }
    $type = "A";
    $ipClass = new IP();
    $minperf = $sock->GET_INFO("DNSPerfsPointer");
    if (!is_numeric($minperf)) {
        $minperf = 301450;
    }
    while (list($index, $dnsA) = each($DNS)) {
        $COMP[$dnsA] = true;
    }
    while (list($dnsA, $none) = each($COMP)) {
        Events("Checks DNS {$dnsA}");
        if (!$ipClass->isIPAddress($dnsA)) {
            continue;
        }
        if ($GLOBALS["VERBOSE"]) {
            echo "{$dnsA}\n";
        }
        $t['start'] = microtime(true);
        $rs = new Net_DNS2_Resolver(array('nameservers' => array($dnsA)));
        $rs->timeout = 20;
        try {
            $date = date("Y-m-d H:i:s");
            $tA = rand("10000", "208460");
            $result = $rs->query("p4-cpsk2owex6nby-dwvedtapjla4ebei-{$tA}-i2-v6exp3-v4.metric.gstatic.com", "A");
            $t[$dnsA] = microtime(true);
            $time = mini_bench_to($t);
            $timeC = $time * 10000;
        } catch (Net_DNS2_Exception $e) {
            $error = $e->getMessage();
            squid_admin_mysql(1, "DNS benchmark failed on {$dnsA} {$error}", $error, __FILE__, __LINE__);
            continue;
        }
        $perc = $minperf / $timeC;
        $perc = round($perc * 100);
        Events("{$dnsA} Response Time:{$time} = {$timeC}/{$minperf} {$perc}%");
        foreach ($result->answer as $record) {
            if ($ipClass->isIPAddress($record->address)) {
                if ($perc > 100) {
                    $perc = 100;
                }
                $zDate = date("Y-m-d H:i:s");
                $q = new postgres_sql();
                $q->QUERY_SQL("INSERT INTO dnsperfs (zDate,PROXYNAME,DNS,PERCENT,RESPONSE)\n\t\t\t\tVALUES ('{$zDate}','{$GLOBALS["MYHOSTNAME"]}','{$dnsA}','{$perc}','{$time}');");
                break;
            }
        }
    }
    GenerateGraph(true);
}