Skip to content

Instantly share code, notes, and snippets.

@Zobber
Created April 13, 2020 02:20
Show Gist options
  • Select an option

  • Save Zobber/a09f0e22cf4ca616a64f67793367140d to your computer and use it in GitHub Desktop.

Select an option

Save Zobber/a09f0e22cf4ca616a64f67793367140d to your computer and use it in GitHub Desktop.
<?php
// This script is run every 5 minutes
// A ping failure will generate an email
// We ping critical servers that serve to identify both a server failure and/or a circuit or VPN failure.
// Define descriptions and ip addresses below to test on
$hosts = array(
array("description" => "Google.com", "hostip" => "www.google.com"),
array("description" => "Office1", "hostip" => "192.168.1.2")
);
// Setup the email parameters and the ping count (one should suffice)
$mailto = "you@yourdomain.com";
$mailfrom = "you@yourdomain.com";
$pingcount =2;
// Loop on each host and ping it
// check the return code from exec via status.
// cmdoutput holds the full ping result as an array but we don't really need it.
foreach ($hosts as $host){
$description = $host["description"];
$hostip = $host["hostip"];
unset($cmdoutput);
exec("ping -n $pingcount $hostip", $cmdoutput, $status);
$finalout = "";
foreach ($cmdoutput as $value) {
$finalout = $finalout.$value."\n";
}
echo $finalout;
if($status == 0){
echo("<BR>$description ($hostip) is Up<BR>");
}
else {
// echo("$description ($hostip) is Down<br>");
mail($mailto,"Network Issue with $description!",$finalout,"From: $mailfrom");
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment