| CODE |
#!/usr/bin/perl $| ++; use strict; use IO::Socket; use Fcntl; ## Create new socket object. my $socket = new IO::Socket::INET ( PeerAddr => '207.44.156.107', PeerPort => 27015, Proto => 'udp', ); die "Could not create socket!\n" if (! $socket); ## Make reads non-blocking. my $flags = ''; fcntl ($socket, F_GETFL, $flags) or die ("Couldn't get flags for HANDLE: $!\n"); $flags |= O_NONBLOCK; fcntl ($socket, F_SETFL, $flags) or die ("Couldn't set flags for HANDLE: $!\n"); $socket->send ("\xFF\xFF\xFF\xFFinfo"); my ($buffer, @parts); while (1) { sysread ($socket, $buffer, 1024); if ($buffer) { @parts = split (//, $buffer); print ord($parts[-3]); print "\n"; print ord($parts[-2]); print "\n"; exit; } } |
(Obviously, replace my server IP with yours.
And here's the MRTG config block to create the graph:
| CODE |
Target[nsplayers]: `/usr/local/mrtg-2/bin/nsplayers.pl` MaxBytes[nsplayers]: 100 Title[nsplayers]: # of Players - Server Name Here YLegend[nsplayers]: Players Legend1[nsplayers]: Current Players Legend2[nsplayers]: Maximum Players LegendI[nsplayers]: Curr: LegendO[nsplayers]: Max: Options[nsplayers]: integer,gauge,growright,nobanner,nopercent ShortLegend[nsplayers]: |