Raised This Month: $32 Target: $400
 8% 

PHP script to notify via email when game server(s) are down


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
MoggieX
Veteran Member
Join Date: Aug 2007
Location: n00bville
Old 12-09-2011 , 07:19   PHP script to notify via email when game server(s) are down
Reply With Quote #1

Howdy,

I've had a nose about the internet and don't seem to be able to find such a script, but surely its got to be possible?

What I'm looking for is a php script that I can poll one or more source game servers and if they're down for it to email one or more email addresses.

Have you seen such a script before?

Matt
__________________
MoggieX is offline
Send a message via Skype™ to MoggieX
Adjo
Member
Join Date: Dec 2009
Location: UK
Old 12-09-2011 , 08:51   Re: PHP script to notify via email when game server(s) are down
Reply With Quote #2

Here's something that does what you want, stick it on a cron so it runs say... every 30 minutes or so.

PHP Code:
<?php
    
    
//Server array
    //IP => PORT
    
$servers = array(
        
'1'                 =>     '2',
        
'server.google.de'    =>    '27015',
        
'vsh.dcsrvdls.com'    =>    '27015'
        
    
);
    
    
//Email array
    
$emails = array(
        
'[email protected]',
        
'[email protected]'
    
);
    
    
$onlineCheckTimeout 5;    //Timeout time (default = 5)
    
$onlineCheckAttempts 3;    //How many times to check the server(s) (default = 3)
    
$onlineCheckInterval 5;    //Interval time (in seconds) between making those (default 3) checks (default = 5)
    
    
$message 'Hello!'."\r\n\r\n".'It appears one or more servers are offline, as they failed to respond to the online check. Those servers are as follows:'."\r\n\r\n";
    
    
    
    
//Our function that does the check
    
function serverOnline($ip,$port){
        global 
$onlineCheckTimeout$onlineCheckAttempts$onlineCheckInterval;
        for(
$i=0,$l=$onlineCheckAttempts;$i<$l;++$i){
            
$online    = @fsockopen($ip,$port,$errno,$errstr,$onlineCheckTimeout);
            if(
$online)return true;
            if(
$i!==$l-1)sleep($onlineCheckInterval);
        }
        return 
false;
    }
    
    
ini_set('max_execution_time'300);
    
    foreach(
$servers AS $ip => $port){
        
        if(!
serverOnline($ip$port)){
            echo 
'Server: ',$ip,':',$port,' is offline, emailing administrators...<br />',"\r\n";
            foreach(
$emails AS $emailAddr){
                
mail($emailAddr'Server Checker Script: Server: '.$ip.':'.$port.' is offline!'$message.$ip.':'.$port."\r\n"'X-Mailer: PHP/'.phpversion());
            }
        }
        
    }
    
?>
It could be improved, but it does what you want. If a server is offline, it'll email the admins who are specified in the array. It'll do several checks first though.
Adjo is offline
Zephyrus
Cool Pig B)
Join Date: Jun 2010
Location: Hungary
Old 12-09-2011 , 13:01   Re: PHP script to notify via email when game server(s) are down
Reply With Quote #3

i would recommend a plugin that requests a php page using sockets extension, it would send a mail everytime the server crashed

@Adjo: server ports are udp ports, that means your script wouldnt work, as its a connection-less protocol, so you would have to send a packet to the server, for example A2S_PING and if it replies then its online. Also, fsockopen fn needs to be modified as well like this:

@fsockopen("udp://".$ip, ...)
__________________
Taking private C++/PHP/SourcePawn requests, PM me.

Last edited by Zephyrus; 12-09-2011 at 13:05.
Zephyrus is offline
Adjo
Member
Join Date: Dec 2009
Location: UK
Old 12-09-2011 , 13:20   Re: PHP script to notify via email when game server(s) are down
Reply With Quote #4

Quote:
Originally Posted by Zephyrus View Post
@Adjo: server ports are udp ports, that means your script wouldnt work, as its a connection-less protocol, so you would have to send a packet to the server, for example A2S_PING and if it replies then its online. Also, fsockopen fn needs to be modified as well like this:

@fsockopen("udp://".$ip, ...)
Weird, the code I posted worked fine for my test server, when tested on my webserver... when it was online, no email was sent, and when it was offline, an email was sent. Could you test it to see if you get any different results?
Adjo is offline
Zephyrus
Cool Pig B)
Join Date: Jun 2010
Location: Hungary
Old 12-09-2011 , 13:23   Re: PHP script to notify via email when game server(s) are down
Reply With Quote #5

Quote:
Originally Posted by Adjo View Post
Weird, the code I posted worked fine for my test server, when tested on my webserver... when it was online, no email was sent, and when it was offline, an email was sent. Could you test it to see if you get any different results?
hmm, interesting, i thought srcds only listens on udp, then excuse me and keep up the good work
__________________
Taking private C++/PHP/SourcePawn requests, PM me.
Zephyrus is offline
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 12-09-2011 , 16:48   Re: PHP script to notify via email when game server(s) are down
Reply With Quote #6

SRCDS listens on udp for gameconnections and tcp for rcon. both usually on the same port.
__________________
Peace-Maker is offline
Zephyrus
Cool Pig B)
Join Date: Jun 2010
Location: Hungary
Old 12-10-2011 , 03:59   Re: PHP script to notify via email when game server(s) are down
Reply With Quote #7

Quote:
Originally Posted by Peace-Maker View Post
SRCDS listens on udp for gameconnections and tcp for rcon. both usually on the same port.
oh i see, i forgot about rcon
__________________
Taking private C++/PHP/SourcePawn requests, PM me.
Zephyrus is offline
Nomarky
SourceMod Donor
Join Date: Sep 2007
Old 12-10-2011 , 08:08   Re: PHP script to notify via email when game server(s) are down
Reply With Quote #8

This looks great, pretty much what I was looking for. However I would like the script to execute a restart script as well as send an email if a server is down. I can figure that much out by adding
PHP Code:
 (exec('c:\scripts\restart_server_1.bat'
but how could it work for multiple servers, so it ran the correct .bat for the ip:port that was not responding?
Nomarky is offline
Adjo
Member
Join Date: Dec 2009
Location: UK
Old 12-10-2011 , 09:18   Re: PHP script to notify via email when game server(s) are down
Reply With Quote #9

Quote:
Originally Posted by Nomarky View Post
This looks great, pretty much what I was looking for. However I would like the script to execute a restart script as well as send an email if a server is down. I can figure that much out by adding
PHP Code:
 (exec('c:\scripts\restart_server_1.bat'
but how could it work for multiple servers, so it ran the correct .bat for the ip:port that was not responding?
Try this out, haven't been able to test if the exec(); part works - because I don't have any restart script to test it with, but it should, hopefully.

PHP Code:
<?php
    
    
//Server array
    //SERVER:PORT => RESTART SCRIPT
    
$servers = array(
        
'1:2'                        =>    'some-path-to-the-restart-script',
        
'server.google.de:27015'    =>    'some-path-to-the-restart-script',
        
'vsh.dcsrvdls.com:27015'    =>    'some-path-to-the-restart-script',
        
'127.0.0.1:27017'            =>    'some-path-to-the-restart-script'
    
);
    
    
//Email array
    
$emails = array(
        
'[email protected]',
        
'[email protected]',
        
'hihi@bye'
    
);
    
    
$onlineCheckTimeout 5;    //Timeout time (default = 5)
    
$onlineCheckAttempts 3;    //How many times to check the server(s) (default = 3)
    
$onlineCheckInterval 5;    //Interval time (in seconds) between making those (default 3) checks (default = 5)
    
    
$message 'Hello!'."\r\n\r\n".'It appears one or more servers are offline, as they failed to respond to the online check. Those servers are as follows:'."\r\n\r\n";
    
    
    
    
//Our function that does the check
    
function serverOnline($ip,$port){
        global 
$onlineCheckTimeout$onlineCheckAttempts$onlineCheckInterval;
        for(
$i=0,$l=$onlineCheckAttempts;$i<$l;++$i){
            
$online    = @fsockopen($ip,$port,$errno,$errstr,$onlineCheckTimeout);
            if(
$online)return true;
            if(
$i!==$l-1)sleep($onlineCheckInterval);
        }
        return 
false;
    }
    
    
ini_set('max_execution_time'300);
    
    foreach(
$servers AS $host => $script){
        
        
$boom explode(':'$host);
        
        if(!
serverOnline($boom[0], $boom[1])){
            echo 
'Server: ',$boom[0],':',$boom[1],' is offline, attempting to restart server, and email the administrators...<br />',"\r\n";
            
exec($script);
            foreach(
$emails AS $emailAddr){
                
mail($emailAddr'Server Checker Script: Server: '.$boom[0].':'.$boom[1].' is offline!'$message.$boom[0].':'.$boom[1]."\r\n"'X-Mailer: PHP/'.phpversion());
            }
        }
      
    }
    
?>
Adjo is offline
Nomarky
SourceMod Donor
Join Date: Sep 2007
Old 12-10-2011 , 09:54   Re: PHP script to notify via email when game server(s) are down
Reply With Quote #10

Thanks Adjo that works great!

One php question though (only just starting to get to grips with php...). Where it says
PHP Code:
 $servers = array(
        
'1:2'                        =>    'some-path-to-the-restart-script',
        
'server.google.de:27015'    =>    'some-path-to-the-restart-script',
        
'vsh.dcsrvdls.com:27015'    =>    'some-path-to-the-restart-script',
        
'127.0.0.1:27017'            =>    'some-path-to-the-restart-script'
    
); 
is there a reason why you can't put
PHP Code:
 '1'                        =>  '2'  =>  'some-path-to-the-restart-script'
Nomarky is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:01.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode