PHP check if server is alive

I need to check if a group of servers, routers and switches is alive. I was looking for something reliable that will work with IP addresses and ports for more than an hour, can anyone help?

Finished with

function ping($addr, $port='') { if(empty($port)) { ob_start(); system('ping -c1 -w1 '.$addr, $return); ob_end_clean(); if($return == 0) { return true; } else { return false; } } else { $fp = fsockopen("udp://{$addr}", $port, $errno, $errstr); if (!$fp) { return false; } else { return true; } } } 
+6
source share
3 answers

Servers, routers and switches ... one common community with which they share is the ability to accept SNMP requests when the SNMP service is running. It seems like what you are trying to do is implement a funny workaround for a monitoring system (nagios, etc.).

According to: http://php.net/manual/en/book.snmp.php

  <?php $endpoints = array('10.0.0.1','10.0.0.2','10.0.0.3','10.0.0.4','10.0.0.5'); foreach ($endpoints as $endpoint) { $session = new SNMP(SNMP::VERSION_2c, $endpoint, 'boguscommunity'); var_dump($session->getError()); // do something with the $session->getError() if it exists else, endpoint is up } ?> 

This will show you if the endpoint exists and the SNMP service is running. Specifically, if the port is accessible / open, you can use fsockopen() :

http://php.net/manual/en/function.fsockopen.php

  <?php $fp = fsockopen("udp://127.0.0.1", 13, $errno, $errstr); if (!$fp) { echo "ERROR: $errno - $errstr<br />\n"; } ?> 
+8
source
 $ip = "123.456.789.0"; $ping = exec("ping -c 1 -s 64 -t 64 ".$ip); var_dump($ping); // and so forth. 
0
source

If you are checking mysql you can use something like

  if (mysqli_ping($ip)) echo "sweet!"; else echo "oh dam"; 

I would be a little worried about using the ping option, as this might be blocked, and from nothing ...

0
source

Source: https://habr.com/ru/post/899445/


All Articles