-1

I have been trying to get a working server status script to work but without any success. There's a community who runs one themself which looks really cool and i was wondering if anyone could help me out?

I've looked over StackOverflow website but seems like most of them doesn't work.

Aberidius
  • 21
  • 6
  • 1
    Can you show us what have you tried so far ? – Alexis May 09 '16 at 08:21
  • made a pastebin since i can't post the whole code in a reply http://pastebin.com/Htsh0FkD – Aberidius May 09 '16 at 08:46
  • By server status you mean to check if it is online or not? or any other status, like overloaded and so on? – api55 May 09 '16 at 08:53
  • Yes, to check if it's online or offline. – Aberidius May 09 '16 at 08:55
  • And how exactly will you show the server to be online and offline??? – Umair Shah May 09 '16 at 08:57
  • It's more practical to setup some sort of RCON pinger that runs on a cronjob and then your website front end polls that data instead. I'm looking at this and it shouts "garrys mod" so you can sourceQuery it. But the answer to your question depends on what game server it is – Barry Carlyon May 09 '16 at 08:58
  • Hi Barry. My purpose for this is for Garry's Mod. I'm going to try out the source query and i will return with the results. – Aberidius May 09 '16 at 09:04
  • 1
    @Aberidius You might want to use [xpaw/php-source-query-class](https://github.com/xPaw/PHP-Source-Query) for this as you can't be certain the server responds to ping. This library is made specifically for source servers. [There's an example here](https://github.com/xPaw/PHP-Source-Query/blob/master/Examples/Example.php). – h2ooooooo May 09 '16 at 09:49
  • @h2ooooooo Unfortunatly, the SourceQuery only returned socket errors. – Aberidius May 10 '16 at 18:07

2 Answers2

1

With PHP, you could use the shell_exec command to effectively ping a specific IP and then determine if it's online. If the backend is run on Linux, this should work:

$offline = shell_exec("ping -c 1 8.8.8.8 | grep -c '0 received'");

The above will return 1 if offline, or if the first packet is lost, and 0 if online or the first packet is received. It does not account for packetloss.

Another alternative is to sign up for an actual reliable and redundant service for monitoring, that has an API, and fetch status via that to your site. Depending on your actual needs, this may prove far more reliable.

CmdrSharp
  • 1,085
  • 13
  • 30
  • [No need to use `shell_exec` if you can avoid it](http://stackoverflow.com/questions/20467432/php-how-to-ping-a-server-without-system-or-exec). There's also a composer package doing ping with either `exec` or sockets: [`geerlingguy/Ping`](https://github.com/geerlingguy/Ping) – h2ooooooo May 09 '16 at 09:16
  • @h2ooooooo I see no reason to prefer `exec` over `shell_exec`. For actual monitoring of network, rather than specific servers, ICMP requests are also far more reliable than sockets. – CmdrSharp May 09 '16 at 09:39
  • 1
    There's absolutely no reason to prefer `exec` over `shell_exec` but for all you know OP is trying to copy in an IP and it only takes a single omitted `escapeshellarg` to open up your server to anyone who wishes. The socket part also sends an _actual_ ICMP packet, so you can't say they're more reliable than sockets because it _is_ just a socket. If in doubt I'm sure the [wiki page on ICMP](https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol) will show you how it's constructed. – h2ooooooo May 09 '16 at 09:41
  • @h2ooooooo I'd assume the OP controls the input to `shell_exec` - but assumptions are of course dangerous. As for sockets, I had it confused thinking you were referring to `fsockopen` which would be more advantageous in terms of service monitoring than network monitoring. Good stuff, thanks for the clarification! – CmdrSharp May 09 '16 at 09:50
  • Ah, that makes perfect sense then. The library *also* supports `fsockopen`, but as you mentioned yourself, it's not ICMP. OP would of course have to use `->ping('socket')` to avoid `exec` and `fsockopen`. – h2ooooooo May 09 '16 at 09:51
1

Polling the state of servers from the script from an on-demand script is usually going to be a very bad idea. Where there is high demand for your script you will be endlessly probing the servers (we'll come back to 'ping' later) which is very inefficient at best and could easily be exploited to launch a reflected denial of service attack against the server. Regardless of the traffic volume, response times will be very slow as each instance polls each server. If a server is unavailable then the situation becomes much worse as you need to wait for a timeout to determine its status (browser use adaptive timeouts for this rather than the OS default).

A better solution is to scheduled probes for checking the status - this is how tools like BMC Patrol and Nagios work for service monitoring.

Regarding the method of polling the service, while its usually true that if you can't ping a node, then you won't be able to access the services it should be providing, that does not mean that if you can ping a server you can access its services. Carrying out a TCP handshake on the service socket will give a much more accurate description of whether the service is available. However if you are repeatedly connecting to someone else's server without exchanging any data, this might be perceived as a denial of service attack by the admins and hence your IP will get black listed. i.e. you should probably be talking to whoever runs the servers (if it's not yourself).

Probing from javacript is only going to work if the server is talking HTTP. It is possible to infer the state of a port from the time it takes for an error to come back (have a google for 'portscan javascript' for some examples) but again the results will not be definitive unless you know how the network and servers are configured.

symcbean
  • 47,736
  • 6
  • 59
  • 94