Przeglądaj źródła

TCP PING 检测工具

admin 7 lat temu
rodzic
commit
01f3cf66b4

+ 12 - 8
app/Console/Commands/AutoCheckNodeStatus.php

@@ -75,15 +75,19 @@ class AutoCheckNodeStatus extends Command
 
                 // 异常才发通知消息
                 if ($tcpCheck > 0) {
-                    if ($times < self::$config['tcp_check_warning_times']) {
-                        Cache::increment('tcp_check_warning_times_' . $node->id);
-
+                    if (self::$config['tcp_check_warning_times'] > 0) {
+                        if ($times < self::$config['tcp_check_warning_times']) {
+                            Cache::increment('tcp_check_warning_times_' . $node->id);
+
+                            $this->notifyMaster($title, "节点**{$node->name}【{$node->ip}】**:**" . $text . "**", $node->name, $node->server);
+                        } elseif ($times >= self::$config['tcp_check_warning_times']) {
+                            Cache::forget('tcp_check_warning_times_' . $node->id);
+                            SsNode::query()->where('id', $node->id)->update(['status' => 0]);
+
+                            $this->notifyMaster($title, "节点**{$node->name}【{$node->ip}】**:**" . $text . "**,节点自动进入维护状态", $node->name, $node->server);
+                        }
+                    } else {
                         $this->notifyMaster($title, "节点**{$node->name}【{$node->ip}】**:**" . $text . "**", $node->name, $node->server);
-                    } elseif ($times >= self::$config['tcp_check_warning_times']) {
-                        Cache::forget('tcp_check_warning_times_' . $node->id);
-                        SsNode::query()->where('id', $node->id)->update(['status' => 0]);
-
-                        $this->notifyMaster($title, "节点**{$node->name}【{$node->ip}】**:**" . $text . "**,节点自动进入维护状态", $node->name, $node->server);
                     }
                 }
 

+ 65 - 0
app/Http/Controllers/Api/PingController.php

@@ -0,0 +1,65 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+use App\Http\Controllers\Controller;
+use Illuminate\Http\Request;
+use Log;
+
+class PingController extends Controller
+{
+    public function ping(Request $request)
+    {
+        $host = $request->input('host');
+        $port = $request->input('port', 22);
+        $transport = $request->input('transport', 'tcp');
+        $timeout = $request->input('timeout', 0.5);
+
+        if (empty($host)) {
+            echo "使用方法:";
+            echo "<br>";
+            echo "GET /api/ping?host=www.baidu.com&port=80&transport=tcp&timeout=0.5";
+            echo "<br>";
+            echo "host:检测地址,必传,可以是域名、IPv4、IPv6";
+            echo "<br>";
+            echo "port:检测端口,可不传,默认22";
+            echo "<br>";
+            echo "transport:检测协议,可不传,默认tcp,可以是tcp、udp";
+            echo "<br>";
+            echo "timeout:检测超时,单位秒,可不传,默认0.5秒,建议不超过3秒";
+            echo "<br>";
+            echo "成功返回:1,失败返回:0";
+            exit();
+        }
+
+        // 如果不是IPv4
+        if (false === filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
+            // 如果是IPv6
+            if (filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
+                $host = '[' . $host . ']';
+            } else {
+                $host = gethostbyname($host);
+            }
+        }
+
+        $transport = $transport . '://';
+
+        try {
+            $fp = stream_socket_client($transport . $host . ':' . $port, $errno, $errstr, $timeout);
+            if (!$fp) {
+                Log::info("$errstr ($errno)");
+                $ret = 0;
+            } else {
+                $ret = 1;
+            }
+
+            fclose($fp);
+
+            return $ret;
+        } catch (\Exception $e) {
+            Log::info($e);
+
+            return 0;
+        }
+    }
+}

+ 2 - 0
routes/api.php

@@ -7,4 +7,6 @@ Route::group(['namespace' => 'Api'], function () {
     // 定制客户端
     Route::get('login', 'LoginController@login');
 
+    // PING检测
+    Route::get('ping', 'PingController@ping');
 });