doushi9780 2016-09-14 18:11
浏览 25

PHP Ping服务器监控代码优化

I have picked up a PHP ping tool and with help from another member here I have managed to get it to display everything I need correctly. The problem I'm having is this has to ping 115 different IP Addresses and some times gets the "Fatal Error maximum execution time of 30 seconds"

I have copied the code below and have only added 1 host and 1 service as to save space but there is normally 115 hosts and 115 services.

$host1[1] = "8.8.8.8";//These addresses are generic for security reasons.
$host1[2] = "8.8.8.8";
$host1[3] = "8.8.8.8";

$services1[1] = "site 1";//These site names are generic for security reasons.
$services1[2] = "site 2";
$services1[3] = "site 3";

    $counter = "";
        $has_down = false;   // flag, no sites are down yet
        foreach ($host1 as $value1) 
        {
          $counter = $counter + 1;
          echo "<tr>"; 
          echo '<body bgcolor="#FFFFFF" text="#000000"></body>';       
          //check target IP or domain
          $pingreply = exec("ping -n $count $value1");
          if ( substr($pingreply, -2) == 'ms')
            {

            }
          else 
            {
                echo "<p><font size='6'><center><strong><div class='alert alert-danger'>" . $services1[$counter] . " - No Reply</div></strong></center></font></p>";
                // site down, set flag
                $has_down = true;
            }
        }

        // flag still false, all sites work
        if (!$has_down) {
            echo "<p><font size='6'><center><strong><div class='alert alert-success'>All sites responding to pings</div></strong></center></font></p>";
        }

Where there is $host1 and $services1 I have 115 of these but I have only put 3 on the code here so it doesn't take up loads of space on the question.

Is there any way I can improve this as it is taking a long time to load the page and it is sometimes getting a fatal error if it takes to long.

Also I believe it is only sending 1 ping per $host but i'm wondering if I can get this to send 4 ping packets to site and if it gets an average ping response then the site is up, as we have some sites on ADSL Lines which often drop packets when pinging so when the site refreshes it says they are down but they have only dropped 1 ping.

Can we optimise this and increase the amount of pings to site?

Thanks for your help in advanced.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
    • ¥50 需求一个up主付费课程
    • ¥20 模型在y分布之外的数据上预测能力不好如何解决
    • ¥15 processing提取音乐节奏
    • ¥15 gg加速器加速游戏时,提示不是x86架构
    • ¥15 python按要求编写程序
    • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
    • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
    • ¥15 opencv图像处理,需要四个处理结果图
    • ¥15 无线移动边缘计算系统中的系统模型