【问题标题】:Monitor TCP/IP Port Consumption via PHP通过 PHP 监控 TCP/IP 端口消耗
【发布时间】:2016-01-28 13:52:39
【问题描述】:

所以我在 Azure Web App 上运行 PHP。最近,我的服务器遇到了 TCP/IP 端口耗尽的问题,因为我没有使用到我的 Redis 缓存的持久连接。我不相信这个问题应该再次出现,但由于我没有任何可用的指标(而且我不能只是远程进入 Azure Web 应用程序),我想知道 PHP 中是否有任何函数将允许我查看有关服务器当前使用的所有 TCP 端口的信息,因此我可以记录这些信息以进行诊断。

有人对如何实现这一点有任何想法吗?我最初的想法是我需要使用 shell_exec。

【问题讨论】:

    标签: php sockets tcp ports diagnostics


    【解决方案1】:

    很遗憾,我无法让它在 Azure Web App 上运行,但我认为这在您运行自己的服务器的其他场景中也可以使用。

    class netstat {
        public $PortsInUse = 0;
        public $AvailablePorts = 0;
        public $TotalPorts = 0;
        public $Ports = array();
    
        public function __construct () {
            $p = popen('netsh int ipv4 show dynamicport tcp', 'r');
            $this->TotalPorts = rtrim(trim(explode("Number of Ports : ", stream_get_contents($p))[1]));
            pclose($p);
    
            $netstat = popen('netstat -no', 'r');
            $log = stream_get_contents($netstat);
            pclose($netstat);
    
            $this->Ports = array_slice(explode("\n", $log), 4);
            array_pop($this->Ports);
    
            foreach($this->Ports as &$port) {
                $port = explode(" ", $port);
                foreach($port as $k => $p) {
                    if (empty($p))
                        unset($port[$k]);
                }
    
                $port = array_values($port);
                $port = (object)array(
                    "LocalAddress" => $port[0],
                    "ForeignAddress" => $port[1],
                    "Status" => $port[2],
                    "ProcessId" => $port[3]
                );
            }
    
            $this->PortsInUse = count($this->Ports);
            $this->AvailablePorts = $this->TotalPorts - count($this->Ports);
        }
    }
    

    上述对象将能够显示当前正在使用的 TCP 端口数,以及该机器上可用于 TCP 的动态端口总数。它还包含一个“Ports”数组,其中包含有关每个正在使用的端口的信息,例如它们的本地和外部地址、状态和 PID(进程 ID)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-12
      • 1970-01-01
      • 2017-03-23
      • 2019-02-19
      • 1970-01-01
      • 1970-01-01
      • 2012-06-03
      相关资源
      最近更新 更多