Explorar o código

improved the function of checking the availability of the host in the web interface

root hai 5 meses
pai
achega
57e9b87d1a
Modificáronse 3 ficheiros con 36 adicións e 5 borrados
  1. 4 1
      Readme.en.md
  2. 3 0
      Readme.ru.md
  3. 29 4
      html/inc/snmp.php

+ 4 - 1
Readme.en.md

@@ -77,7 +77,7 @@ set password for root
 #mysql_secure_installation
 
 Create database
-#cat docs/mysql/create_db.sql | mysql -u root -p stat
+#cat docs/mysql/create_db.sql | mysql -u root -p
 
 Import default tables
 #cat docs/mysql/latest-mysql.sql | mysql -u root -p stat
@@ -170,6 +170,9 @@ set enabled=yes  interfaces=WAN
 /ip traffic-flow target
 add dst-address=[IP-NETFLOW-SERVER] port=2055
 
+#cp docs/systemd/eye-statd.service /etc/systemd/system
+#systemctl enable eye-statd
+
 ######################################### Remote System Log ###############################################################
 
 If you need to write logs from devices:

+ 3 - 0
Readme.ru.md

@@ -167,6 +167,9 @@ set enabled=yes  interfaces=WAN
 /ip traffic-flow target
 add dst-address=[IP-SERVER] port=2055
 
+#cp docs/systemd/eye-statd.service /etc/systemd/system
+#systemctl enable eye-statd
+
 ######################################### Remote syslog ###############################################################
 
 Если нужно писать логи с устройств:

+ 29 - 4
html/inc/snmp.php

@@ -199,16 +199,41 @@ function get_fdb_table($ip, $snmp)
     return $fdb_table;
 }
 
+function is_host_up(string $ip, int $maxLoss = 5, int $count = 3): bool
+{
+    $cmd = sprintf(
+        'ping -W 1 -i 1 -c %d %s 2>&1',
+        $count,
+        escapeshellarg($ip)
+    );
+
+    exec($cmd, $out, $ret);
+
+    // Ищем строку вида: "3 packets transmitted, 3 received, 0% packet loss"
+    foreach ($out as $line) {
+        if (preg_match(
+            '/(\d+)\s+packets transmitted,\s+(\d+)\s+received.*?(\d+)% packet loss/i',
+            $line,
+            $m
+        )) {
+            $loss = (int)$m[3];
+            return $loss <= $maxLoss;
+        }
+    }
+
+    // Если статистики нет вообще — считаем хост недоступным
+    return false;
+}
+
 function check_snmp_access($ip, $snmp)
 {
     if (!isset($ip)) {
         return;
     }
+
     //check host up
-    $status = exec(escapeshellcmd("ping -W 1 -i 1 -c 3 " . $ip));
-    if (empty($status)) {
-        return;
-    }
+    if (!is_host_up($ip)) { return; }
+
     //check snmp
     $result = get_snmp($ip, $snmp, SYS_DESCR_MIB);
     if (empty($result)) {