0

I suppose this may be an odd question, but I have a small EC2-instance that costs quite a large sum of money every month. It's charged hourly though, so I only turn on this particular instance when I need it, and power it off when I'm done.

The purpose of this instance is for hosting a Counter-Strike: Global Offensive dedicated server which I only power on when I have a scrim to play.

Instead of forgetting to turn it off and being charged a lot, or having an unintelligent start-up script that asks the instance to power-off after 3 hours, I was thinking of a more intelligent design.

Here's my idea; that the instance intelligently powers itself off when it senses it is no longer in use, by determaining if a certain amount of network activity on UDP 27015 has not been recorded over the last 10 minutes, trying 3 times before powering off.

That way I can power-on, play the match, and not worry about powering off the server :-)

It sounds cool in my head. The question is how I go about solving the task. I imagine a bash-script executed every 10 minutes with the help of cron.

If I'm not being entirely crazy here, could a bash-script suggestion possibly be offered? Or maybe a better solution how I solve this quest I'm on, to save $$ by having the server power itself off when sensing it is no longer in use!

Amivit
  • 189
  • 2
  • 4
  • 15
  • http://www.linuxquestions.org/questions/programming-9/port-monitoring-bash-script-214779/ – FlavorScape Nov 09 '12 at 19:55
  • Unfortunately nothing within that thread is of help, as the udp port 27015 will constantly be listening, regardless of whether or not people are connected to and playing on the server. I need to be able to measure the amount of data every 10 minutes and have the bash script determine whether it is in use or not (at least I imagine, to solve my idea). – Amivit Nov 09 '12 at 20:22

1 Answers1

1

I'm not too familiar with EC2 instances, but if they are running some form of linux... Under Fedora I can use ifconfig to see how much data has been received/transmitted across the network interface. It's not just the single port but all ports on that interface... Would that number suffice for you? Ought to be pretty trivial to monitor it every few minutes and see when the load drops off...

Possibly a simple script to start with that is started when the EC2 instance is brought up and just logs the data. An hour after your game you can grab the log, manually shut down, and review it at your leisure to see if this will work. (It's amazing how many things use the network sometimes...)

Afterthought: Perhaps tcpdump would be better? Will it work with UDP port 27015? You might need some way to time it out, like running it as a background process, possibly with the -c option, sleeping for a while, and then killing the tcpdump process if it's still running. You may need to pipe through wc -l or just grep the final packets grabbed line. Caveat: tcpdump may need to be run as root.

E.g. /usr/sbin/tcpdump -n -nn -q -c 100 -i eth0 port 27015

Further afterthought:

#!/bin/bash --norc                             

/usr/sbin/tcpdump -n -nn -q -i eth0 port 27015 2>./logfile 1>/dev/null &
TCPDUMP_PID=$!

echo "sleeping...  pid=$TCPDUMP_PID"
sleep 30
echo "wake up"
kill $TCPDUMP_PID
sleep 2
cat ./logfile
TooLazyToLogIn
  • 284
  • 2
  • 3
  • It should be a simple matter to write an iptables rule for which the only function is to count the traffic on UDP port 27015. Then your scripts could just check the totals reported by iptables -L in a similar way that you suggest using ifconfig – Adrian Pronk Nov 10 '12 at 00:24