Checking Server Status – Easily

I’m currently working on a simple backup script and I wanted to make sure my backup server was up before I sent stuff from my laptop over to the backup server (which is housed here locally on a nice software RAID setup). It’s nothing ground breaking, but I couldn’t find a simple utility to simply ping a server, timeout gracefully and return a valid exit code. So, being the geek I am I made one.

<?php

  $timeout = 5;
  if (!isset($argv[1])) {
      die("Usage: php -q server_status ipaddress:port [timeout]n");
  } else {
      list($server,$port) = explode(':',$argv[1]);
      if (isset($argv[2]) && is_numeric($argv[2])) {
          $timeout = $argv[2];
      }
  }

  $fp = @fsockopen($server,$port,$errno,$errstr,$timeout);
  if (!$fp) {
      exit(1);
  } else {
      fclose($fp);
      exit(0);
  }

?>

5 thoughts on “Checking Server Status – Easily

  1. If that script doesn’t work out because of php’s sometimes flaky socket handeling, you can try these:

    ping
    fping
    nmap
    ssh

    All of them return a true or not true value.

    Any of those would seem more reliable than PHP’s socket functions. That’s just off the top of my head since I don’t know if you actually mean ICMP when you say ping. You might like the ssh one, as I assume you will be doing your backup through ssh anyway – but if your ssh timeout is too long, you might like the nmap better.

    ex:
    ssh $hostname /bin/true && do_something

    ping -c 1 $hostname && do_something

    nmap -p 22 –host_timeout 5000000
    $hostname && do_something

    Jeremy

  2. Your new design seems to react weird whith your code snippets above. The code snippets wrap into your navigation on the right. (Browser: Mozilla Firefox 8, Linux)

    Jer

  3. Re: Footer Copyright statement. What I say is copyright Jeremy Brand, not Joe Stump, unless I give up my copyright to you.

    You might want to re-write your footer so it is not false.
    An example:

    “Site & Content copyright Joe Stump. Joe Stump claims no responsibility or copyright to replies and posts and is not associated to the respectful replier or poster.”

    You don’t want to get sued by Time Warner for someone posting quotes from Harry Potter, do you..?

    😛 (emoticons?)

    Jer

  4. Such is the way of the pre tag. Not sure there is anything constructive I can do with the code (I don’t, afterall, want it to wrap).

    Good point on the content. I need to make something up that will indemnify me from what you crazies post in the comments section.

    –Joe

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.