http://www.perlmonks.org?node_id=1019678

dr.jekyllandme has asked for the wisdom of the Perl Monks concerning the following question:

Hello, Hoping someone can enlighten me on this. I would like to check the status of a given server. For example, I want to know whether the server is up or down, pretty much determine the health of a server. Can someone point me to the right module? Thank you.
  • Comment on How do you check the status of a given server?

Replies are listed 'Best First'.
Re: How do you check the status of a given server?
by roboticus (Chancellor) on Feb 20, 2013 at 02:42 UTC

    dr.jeyllandme:

    Try accessing the service it provides and see if it provides reasonably good service within your minimal limits.

    Not a very helpful answer, is it? But you don't provide any information, so it's hard to point you to anything. You might try adding a bit to your question. Some things like: Operating system? What services does it provide? What do you mean by "health". How many and which services need to be dysfunctional to call the server "down"?

    If you just want to ping it to see if it crashed, perhaps Net::Ping::External. Beyond that, you'll need to provide some details.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: How do you check the status of a given server?
by techgrrl (Sexton) on Feb 20, 2013 at 04:51 UTC

    I think roboticus said it fairly well, check the services it provides.

    Without knowing more about the server it is hard to get more specific.

    To add a little more detail. You can ping most servers to see if they have a healthy network stack, either by using system calls or backticks (not my first choice) or by using Net::ICMP. To use the `backtick` method enclose any valid system command in backticks and the output of the command will be assigned to the variable. It is messy, and you often lose return codes from the commands, but it is simple.

    #!/usr/bin/perl $result = `/sbin/ping -q -c 5 www.google.com | grep loss`; if ($result =~ /(\d+\.\d+\%) packet loss/) { print "We lost $1 of the traffic\n"; }

    To use any of the ICMP modules such as Net::Ping let your friend CPAN explain everything, but feel free to ask again when you have a more specific question.

    You can also monitor a server via SNMP. Most servers respond to the OID system.sysuptime.0.

    If you don't know what an OID is, read the Net::SNMP documentation. In order to use SNMP, you'll need to find a SNMP package and read the docs, something like Net::SNMP or SNMP::Simple. From a Unix host you can also call the Net-SNMP using the system call or `snmpget $args` like in the above code example, but again, that is probably not the best way to do it. Generally I prefer to use modules like Net::SNMP.

    If the server has a web server running, using LWP::Simple to get a test document is pretty easy.

    If you have CGI enabled on the web server, you can write a CGI to run some health checks, and then use LWP::Simple to get the web page.

    For example if you have a perl script called health.cgi, you can run any commands you want to check the servers health and print out valid html, and then from another machine run a perl script using LWP::Simple to get http://your-server.com/health.cgi

    Think about those things to get started, and if you have more specific questions, feel free to ask again.

    I hope this helps you get started.

Re: How do you check the status of a given server?
by duelafn (Parson) on Feb 20, 2013 at 14:19 UTC

    Non-perl answer: use Nagios

    Good Day,
        Dean

Re: How do you check the status of a given server?
by karlgoethebier (Abbot) on Feb 20, 2013 at 15:03 UTC

    Second non-perl answer: or use icinga. It's a nagios fork that looks much better (clearly arranged web interface, nagios web interface is terrific - that really matters if you have many hosts/services) and has a lot of cool features (filters, cronks etc.). But unfortunately it isn't so easy to setup as nagios.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»