[cod] Server status

Kendokan kendokan at amateur-hour.net
Mon Jan 5 16:08:20 EST 2004


No, you can't query a TCP port, it must be the UDP port your server is
running on. Below is a Perl script I use for updating my server status to
my web site. I'm sure there are other query scripts around written in PHP
if you prefer to use that instead, and my script is customized to my site
so you will have to tweak the output section to suit your needs. You can
see it in action at http://amateur-hour.net near the top of the screen. I
have it run from a cron entry like:

*/1 * * * * /path/to/this/script.pl -s your.server.ip > /path/to/status.html

The script is:

#!/usr/bin/env perl

use strict;
use IO::Socket;
use Getopt::Long;

# Initialize settings and results hashes.
my(%Settings,%Info);

# Get command-line options
GetOptions (
        'server|s=s'            => \$Settings{ServerIP},
        'port|p=i'              => \$Settings{ServerPort},
        'link|l=s'              => \$Settings{Link},
        'help|h'                => \$Settings{Help}
);

# How many seconds before we timeout the query?
$Settings{Timeout} = 2;

# How many times should we retry the query before giving up?
$Settings{RetryCount} = 3;

# This status command if for Call of Duty servers.
$Settings{StatusCmd} = "ÿÿÿÿgetinfo\x00";

# Default to port 28960 if a port is not specified.
$Settings{ServerPort} = "28960" unless $Settings{ServerPort};

# If user asks for help, show the help text.
if ($Settings{Help}) { &PrintHelp }

# If we got an IP address and port from the command-line, query.
Otherwise, print the help text.
if ($Settings{ServerIP} && $Settings{ServerPort}) {
        &GetStatus;
        &ParseStatus;
} else {
        &PrintHelp;
}

# This section sends the status command and returns the raw result.
sub GetStatus {
        ++$Settings{LoopCount};

        my $Socket = new IO::Socket::INET (
                Proto           => "udp",
                PeerAddr        => $Settings{ServerIP},
                PeerPort        => $Settings{ServerPort},
        );

        if ($Socket) {
                open (ERROR, ">/dev/null") or die $!;
                STDERR->fdopen(\*ERROR, "w") or die $!;
                $Socket->send($Settings{StatusCmd}) or next;
                $SIG{ALRM} = \&Timeout;

                eval {
                        alarm ($Settings{Timeout});
                        $Socket->recv($Info{Data},1024) or next;
                        alarm (0);
                        close $Socket;
                }

        } else {

                close $Socket;

        }
}

# This section parses the raw results from GetStatus and turns it into HTML.
sub ParseStatus {
        if ($Info{Data} =~ s/^ÿÿÿÿinfoResponse//) {

                my(@DataLeft, at DataRight);

                foreach (split(/\\/,$Info{Data})) {
                        push @DataLeft,$_;
                        push @DataRight,$_;
                }

                shift @DataRight;

                foreach (@DataLeft) {
                        $Info{$_} = shift @DataRight;
                }

                $Info{IP} = "$Settings{ServerIP}:$Settings{ServerPort}";
                $Info{UsedSlots} = $Info{clients};
                $Info{EmptySlots} = ($Info{sv_maxclients} - $Info{clients});
                $Info{Now} = localtime;

                # This is the HTML result.
                print "<!-- Begin codsmallstat.pl content - $Info{Now}
loop $Settings{LoopCount} -->\n";
                print "     <nobr><a class=\"servername\"
href=\"$Settings{Link}\"
target=\"main\">$Info{hostname}</a><br>\n";
                print "     <input class=\"serverip\" type=\"text\"
value=\"$Settings{ServerIP}:$Settings{ServerPort}\"
readonly onFocus=\"this.select()\"
onDblClick=\"this.value='cod.amateur-hour.net';this.select()\"><br>\n";
                print "     Current map: <span
class=\"servermap\">$Info{mapname}
(".uc($Info{gametype}).")</span></nobr>\n";
                print "     <table class=\"serverplayer\"
cellspacing=\"0\" cellpadding=\"0\" border=\"0\"
width=\"100%\" title=\"$Info{clients}/$Info{sv_maxclients}
players currently active\">\n";
                print "      <tr>\n";

                while ($Info{UsedSlots} > 0) {
                        --$Info{UsedSlots};
                        print "       <td
class=\"player_lit\">&nbsp;</td>\n";
                }

                while ($Info{EmptySlots} > 0) {
                        --$Info{EmptySlots};
                        print "       <td
class=\"player_dim\">&nbsp;</td>\n";
                }

                print "      </tr>\n";
                print "     </table>\n";
                print "     <!-- End codsmallstat.pl content -->";

        } else {

                if ($Settings{LoopCount} < $Settings{RetryCount}) {

                        &GetStatus;
                        &ParseStatus;

                } else {

                        print "<!-- Begin codsmallstat.pl content -
$Info{Now} loop $Settings{LoopCount} of
$Settings{RetryCount} -->\n";
                        print "     <b><font size=\"2\">Error getting
status.</font></b><br>\n";
                        print "     <!-- End codsmallstat.pl content -->";

                }
        }
}

sub Timeout {
          die "How long can this go on?!";
}

sub PrintHelp {
        print "Usage: codsmallstat.pl -s [IP] -p [PORT] -l [LINK]\n";
        print "Server status script for Call of Duty servers.\n\n";
        print "  -h, --help                     display this help and
exit\n";
        print "  -s, --server                   IP address of game server\n";
        print "  -p, --port                     IP port of game server\n\n";
        print "Long options can be abbreviated, where such abbreviation is
not ambiguous.\n";
        print "Default values for options are indicated in square brackets
[...].\n\n";
        print "codsmallstat: http://amateur-hour.net\n\n";
        exit;
}

> Hiya,
> I want to display my server status on the website, when its on or offline
> i have a PHP script for it but it only queries tcp ports
> Does any1 knows if COD uses a tcp port and if yes what is the tcp port
> then
>
> thnx allot
> 4MOTION [GHQ]
> www.gaming-hq.com



More information about the Cod mailing list