AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Source Servers (SRCDS) (https://forums.alliedmods.net/forumdisplay.php?f=130)
-   -   Shell script to check if server's empty (https://forums.alliedmods.net/showthread.php?t=311307)

dustinandband 10-12-2018 16:55

Shell script to check if server's empty
 
1)
Is it possible for a bash shell script to query a game server and determine if it’s empty or has players? If so, how is this done? (Even if you'd need a c++ extension installed to do so, I'd be willing to do the research with an idea of how it would work.)

2)
Is it possible for a shell script to monitor a screen session for a key word? The sourcemod equivalent of adding a command listener.
"MasterRequestRestart" is the word I'm wanting to pick up.

nosoop 10-13-2018 02:12

Re: Shell script to check if server's empty
 
  1. There's a couple of ways. You can either send the A2S_INFO (Source Server) query via some application, use a standalone rcon tool to read the status output, or write some plugin using the socket extension to respond with your desired info
  2. Don't think so. Not sure if the logging facilities would catch that.
Any particular reason why a SteamWorks-based plugin that gets forwarded an event on restart request won't work for you?

Maxximou5 10-13-2018 02:45

Re: Shell script to check if server's empty
 
2) Log the output of the console and use a cronjob to grep for whatever phrase you wish for.
Example

dustinandband 10-13-2018 18:06

Re: Shell script to check if server's empty
 
Quote:

Originally Posted by nosoop (Post 2619457)
  1. There's a couple of ways. You can either send the A2S_INFO (Source Server) query via some application, use a standalone rcon tool to read the status output, or write some plugin using the socket extension to respond with your desired info
  2. Don't think so. Not sure if the logging facilities would catch that.
Any particular reason why a SteamWorks-based plugin that gets forwarded an event on restart request won't work for you?

Thanks.

I've compiled the rcon tool, but I'm not sure if I'm using it correctly.
In this example, the rcon password is just a made up password.

Code:

root@testsvrchi:~# ./rcon --help
Usage: <programname> --rcon="<ip> <port> <rconpw> <cmd>"
Example: --rcon="127.0.0.1 27960 myrconpw devmap oasis"

root@testsvrchi:~# ./rcon --rcon="74.91.124.177 27015 rconpassword101 status"
Sending:
¦¦¦¦rcon rconpassword101 status

Could not receive.

Not sure if there's a more specific rcon command to query "players - humans" count, or if it's something that would have to be parsed from the status command.
Code:

] rcon status
hostname: Left 4 Dead 2
version : 2.1.5.2 7149 secure  (unknown)
udp/ip  : 74.91.124.177:27015 [ public same ]
os      : Linux Dedicated
map    : c2m1_highway
players : 1 humans, 0 bots (4 max) (not hibernating) (reserved 186000000a383ab)

Edit:
Haven't worked with c++ / c before, but it looks like the error is stemming from this returning -1:
Code:

    n = recvfrom(sockfd, srline, MAX_SR_LINE, 0, NULL, NULL);
    if(n < 0) {
        close(sockfd);
        return RC_RECV;
    }

http://pubs.opengroup.org/onlinepubs.../recvfrom.html

Edit#2
Forgot to mention I tried the local IP as well
Code:

./rcon --rcon="127.0.0.1 27015 rconpassword101 status"

hustl4 10-14-2018 18:09

Re: Shell script to check if server's empty
 
why not use python?

PHP Code:

pip install python-valve 

PHP Code:

import valve.rcon
import re

ip 
"10.0.0.1"
port 27015
password 
"rconpassword"


def isEmpty(addresspassword):
    
with valve.rcon.RCON(addresspassword) as rcon:
        
response rcon.execute("status")
        
response_text response.body.decode('ascii''ignore')
        for 
line in response_text.splitlines():
            if 
"players :" in line:
                for 
words in ['0 humans']:
                    if 
re.search(r'\b' words r'\b'line):
                        return 
True
                    
else:
                        return 
False
            
else:
                
pass


if __name__ == "__main__":
    if 
isEmpty((ipport), password):
        print(
"server is empty")
    else:
        print(
"server is NOT empty"


dustinandband 10-14-2018 19:28

Re: Shell script to check if server's empty
 
@ hustl4
Thanks for the idea. The script appears to be getting hung up. If you do happen to test it out - would you mind uploading it to github so I can include a direct download for the script I'm currently writing? Would be best if there was safeguards against situations where it might get hung up - maybe there could be a timeout feature if the server doesn't respond in x amount of time there's a certain return value. (Since this script is designed to get launched automatically from a cron job task.)

In regards to the auto-updating:
- I can use screen to log output to a file and successfully added update functionality there, but the problem is when you have multiple servers on the same machine and need to make a unique log file name that's the same as the screen session (socket) name.
- Some users say to use tmux instead of screen, so I spent the better part of a day writing out a script to handle auto-launching X amount of servers:
(I still have to make the tmux log.)
Spoiler

- Come to find out after all that work, the latest version of tmux may not be compatible with creating dynamic log names (if i revert to an older version of tmux and ever 'apt-get update', it may break the script):
https://github.com/GameServerManager..._start.sh#L113

Anyway, if that's the case I'll just use something similar to what hustl4 posted. If all game servers are all detected as empty on some night, they'll all get an update then the physical server will do a reboot. The cron job task will relaunch the script ^ and the servers will be back online.

hustl4 10-15-2018 04:46

Re: Shell script to check if server's empty
 
Quote:

Originally Posted by dustinandband (Post 2619752)
@ hustl4
Thanks for the idea. The script appears to be getting hung up. If you do happen to test it out - would you mind uploading it to github so I can include a direct download for the script I'm currently writing? Would be best if there was safeguards against situations where it might get hung up - maybe there could be a timeout feature if the server doesn't respond in x amount of time there's a certain return value. (Since this script is designed to get launched automatically from a cron job task.)

sorry, should have mentioned only tested on python3.6 on a mix of centos 7 and ubuntu 16.04 (csgo servers)
I have it running in cron every minute (cpu mining crypto when servers are empty lmao)
I added a timeout exception and uploaded to github
https://gist.github.com/wh1te909/82f...7fed2b8fd7dcf8


All times are GMT -4. The time now is 23:42.

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