Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@MikeRogers0
Last active January 30, 2024 08:52
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save MikeRogers0/8492860 to your computer and use it in GitHub Desktop.
Save MikeRogers0/8492860 to your computer and use it in GitHub Desktop.
Uptime monitoring geeklet script
#!/usr/bin/php
<?php
# Array of the servers you want to ping.
$servers = array('mikerogers.io', 'google.com', 'downserver.come');
# PingDomain() from http://stackoverflow.com/a/9843251/445724
function pingDomain($domain){
$start_time = microtime(true);
$file = @fsockopen ($domain, 80, $errno, $errstr, 10);
$end_time = microtime(true);
if ($file){ # We connected ok.
fclose($file);
return floor(($end_time - $start_time) * 1000);
}
return false;
}
foreach ($servers as $server){
$pingTime = pingDomain($server);
if($pingTime){ # it's online
echo "\033[0;32m".$server." (".$pingTime."ms)\033[0;254;176;19m";
} else {
echo "\033[0;31m".$server." (Offline)\033[0;254;176;19m";
}
echo "\n";
}
?>
@LarkRiseMedia
Copy link

Not getting this to work, what am I missing? Saved as .sh file, in the command line posted to the .sh file?

@ImreBrassaiSR
Copy link

It seems to be a php script, not a bash script. Run it as $ php uptime-monitor.php, after you change it from .sh to .php.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment