【问题标题】:PHP - Show message if all arrays are active, inactive or unavailablePHP - 如果所有数组都处于活动状态、不活动状态或不可用,则显示消息
【发布时间】:2017-03-15 20:25:14
【问题描述】:
<?php 
//Page Variables 
$online='<td style="background-color:#00FF00; padding:5px;">Operational</td>'; 
$offline='<td style="background-color:#FF0000; padding:5px;">Failed</td>'; 

//Functions 
function servercheck($server,$port){ 
    //Check that the port value is not empty 
    if(empty($port)){ 
        $port=80; 
    } 
    //Check that the server value is not empty 
    if(empty($server)){ 
        $server='domain.com'; 
    } 
    //Connection 
    $fp=@fsockopen($server, $port, $errno, $errstr, 1); 
        //Check if connection is present 
        if($fp){ 
            //Return Alive 
            return 1; 
        } else{ 
            //Return Dead 
            return 0; 
        } 
    //Close Connection 
    fclose($fp); 
} 
//Ports and Services to check 
$services=array( 
'Website Access' => array('domain.com' => 80), 
'Another Service' => array('domain.com' => 443), 
'Another Service' => array('domain.com' => 21), 
); 
?> 

<div class="infobox">
<?php 
//Check All Services 
foreach($services as $name => $server){ 
?> 
<tr> 
<td><?php echo $name; ?></td> 
<?php 
foreach($server as $host => $port){ 
    if(servercheck($host,$port)){ echo $online; }else{ echo $offline; } 
} 
?> 
</tr> 
<?php 
} 
?> 
</div>
<div class="overallmesssage">
<h3>
<!--Need this bit to show green and a message if all servers online if not     show orange for not all of them and red for none of them--> 
</div>

我想添加一个整体消息,该消息会根据在线的服务器而变化。类似于https://demo.cachethq.io/,它有一个整体的绿色 div,告诉访问者一切都已启动并正在运行,但如果其中一台服务器出现故障,它会变为橙色,如果所有服务器都停机,它会变为红色。

【问题讨论】:

    标签: php arrays server status


    【解决方案1】:

    在一个数组中收集你的服务器状态,然后检查in_array

    <?php 
    $status = array();
    foreach($server as $host => $port){ 
        $status[] = servercheck($host,$port);
    }
    
    if (in_array('0', $status)) {
        echo $offline;
    } else {
        echo $online;
    }
    

    ?>

    【讨论】:

      【解决方案2】:

      在您的情况下,我更喜欢另一种方法,然后直接询问页面。在您的位置上,我将构建一个 cronjob,它每分钟检查您的服务并将状态保存在数据库或其他东西中。然后,您可以使用该数据构建图表。

      但对于你的情况。为了实现这一点,我宁愿通过您的服务器循环运行,然后将每个失败的声明保存在数组中。之后,您可以使用该数组。所以数组中有一个条目是橙色的。如果您有多个条目,则您的状态为红色。或者更容易使用计数器变量。

      $errors = 0;
      foreach($server as $host => $port) { 
          if(servercheck($host, $port) == 0) {
              $errors++;
          } 
      }
      
      if ($errors == 0) {
          echo "green!";
      } elseif ($errors == 1) {
          echo "orange!";
      } else {
          echo "red!";
      }
      

      您的完整代码:

      <?php
      //Page Variables
      $online = '<td style="background-color:#00FF00; padding:5px;">Operational</td>';
      $offline = '<td style="background-color:#FF0000; padding:5px;">Failed</td>';
      
      //Functions
      function servercheck($server, $port) {
          //Check that the port value is not empty
          if (empty($port)) {
              $port = 80;
          }
          //Check that the server value is not empty
          if (empty($server)) {
              $server = 'domain.com';
          }
          //Connection
          $fp = @fsockopen($server, $port, $errno, $errstr, 1);
          //Check if connection is present
          if ($fp) {
              //Return Alive
              return 1;
          } else {
              //Return Dead
              return 0;
          }
          //Close Connection
          fclose($fp);
      }
      
      //Ports and Services to check
      $services = [
          'Website Access' => ['domain.com' => 80],
          'Another Service' => ['domain.com' => 443],
          'Another Service' => ['domain.com' => 21],
      ];
      
      $errors = 0;
      foreach($services as $host => $port) {
          if(servercheck($host, $port) == 0) {
              $errors++;
          }
      }
      ?>
      
      <div class="infobox">
          <?php
          if ($errors == 0) {
              echo $online;
          } elseif ($errors > 1) {
              echo $offline;
          }
          ?>
      </div>
      <div class="overallmesssage">
          <h3></h3>
      </div>
      

      这样想。我不知道它是否正确,但你可以测试它。

      【讨论】:

      • 我认为你的意思是if(servercheck($host, $port) == 0)
      • 哦,对不起,是的...... :) 我已经改变了。
      • 不客气,用 setInterval 代替 cron 怎么样?
      • @hassan 您可以通过这种方式做到这一点,但是当您没有一些信息时,您将无法构建图表并使用该数据。所以我会将这些信息存储在一个地方并使用这些数据,然后你就有更多的可能性。
      • @Stony 你能把你的新代码放在我的旧代码中吗,因为我不确定你给的代码应该放在哪里(我是 PHP 新手)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-03
      • 1970-01-01
      • 1970-01-01
      • 2017-01-19
      相关资源
      最近更新 更多