Mike Rogers on a Snowmobile

Geeklet for monitoring server ping

Posted on

I've been using Geektool for a while now, it's a super handy way to display useful information on my desktop. One geeklet I've found to be useful displays the status and ping of my VPS.

Server name and its ping

Geeklet code

Here is the code you need to stick in the command box.

# 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.
        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";
View on Gist Github

To modify the websites being pinged, just edit the $servers array on line 4.

This was written by Mike Rogers, a freelance Ruby on Rails developer based in London.

Share the ♥ by sharing this!

If you want to discuss this post, feel free to tweet me (@MikeRogers0) or drop me an email. Any code samples unless stated otherwise are licensed under the The MIT License (MIT). Spotted a mistake? Send me a pull request :)