douzi8127 2015-10-28 16:54
浏览 75

通过PHP监控TCP / IP端口消耗

So I'm running PHP on an Azure Web App. Recently, I ran into an issue with my server going into TCP/IP Port Exhaustion, because I was not using persistent connections to my Redis Cache. I don't believe the issue should come up again, but since I don't have any metrics available for this (and since I can't just remote into an Azure Web App), I was wondering if there's any functions in PHP that would allow me to see information about all the TCP ports currently in use by the server, so I can log that for diagnostics.

Does anyone have any ideas on how this can be accomplished? My initial thinking is that I would need to use shell_exec.

  • 写回答

1条回答 默认 最新

  • drgc9632 2015-10-28 17:51
    关注

    Unfortunately, I can't get this to work on Azure Web App, however I think this would work in other scenarios, where you are running your own server.

    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("
    ", $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);
        }
    }
    

    The above object will be able to display the current number of TCP ports in use, and the total number of dynamic ports available for TCP on that machine. It also contains a "Ports" array, which contains information about each port in use, such as their local and foreign address, status and PID (process id).

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测