#! /usr/bin/perl # # Copyright (C) 2007, RO Status Project , # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in # the documentation and/or other materials provided with the # distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR (RO STATUS PROJECT) "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. use strict; use warnings; use LWP::UserAgent; use XML::LibXML; ## If you do not have XML::LibXML (libxml2), I recommend you to use ## XML::DOM::Lite, which has only one dependency. # use XML::DOM::Lite qw[Parser :constants]; my $URL_FMT = "http://rostatus.net/status/%s.xml"; MAIN: { my $world = shift || "chaos"; # Show Chaos status if you give no options. my $xml = getStatusXml($world) || die "Could not get XML file."; my %status = parseStatusXml($xml); print "\U$world\E WORLD SERVER STATUS:\n"; foreach my $h (sort keys %status) { print "$h = $status{$h}\n"; } } sub getStatusXml { my $world = shift || die; my $ua = new LWP::UserAgent; $ua->timeout(3); my $res = $ua->get(sprintf $URL_FMT, $world); if($res->is_success) { return $res->content; } else { return undef; } } sub parseStatusXml { my $xml = shift || die; ## XML::LibXML my $parser = new XML::LibXML; my $doc = $parser->parse_string($xml) or die; ## XML::DOM::Lite # my $doc = Parser->parse($xml, whitespace => "strip"); my %status; my $hosts = $doc->getElementsByTagName("host"); foreach my $h (@$hosts) { my $n = $h->getAttribute("id"); my $s = $h->getElementsByTagName("status")->[0]->firstChild->nodeValue; $status{$n} = $s; } %status; }