RSS Feed

Minecraft Server Alerts to iOS

4

September 7, 2012 by brian

This guide explains how to send iOS push notifications from your Minecraft server. This configuration will alert when someone connects to or disconnects from my server, but it could be expanded to alert on any activity displayed in the server log.

I’m running Ubuntu Server, but this should work on other operating systems with little modification. I originally configured this for an unmodded Minecraft server, but I’ve since converted to CraftBukkit and this all still works fine.

Requirements

  1. Prowl (on your iOS device)
  2. Minecraft server
  3. swatch
  4. Python
  5. Pyrowl (a python module for the Prowl API)

I’ll assume you have installed all of the software above. Shoot me a comment if you have any questions.

Step 1: Create the Script

Copy the following script into your home directory. I’ll use ~/minecraft_prowl.py.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#! /usr/bin/env python
 
"""minecraft_prowl.py: Sends Minecraft alerts to Prowl clients"""
 
import sys
import re
import pyrowl
 
# setup objects
api_keys = dict()
p = pyrowl.Pyrowl()
 
################ Customize your Players / API Keys #################
 
api_keys['Notch'] = "eface9676e29cca82fe827cb58dc1a0cfb8d3301"
api_keys['jeb_'] = "732d4f2446cb921a0b366893247843e30dfaed63"
 
####################################################################
 
# acquire raw input in log format
raw_input = sys.stdin.readline()
 
# get player name
result = re.search("\[INFO\].(\w+)", raw_input)
player = result.group(1)
 
# get datetime
result = re.search("(.+) \[INFO\]", raw_input)
datetime = result.group(1)
 
# start base of description field
description = player
 
# determine if this is a login or logoff
if raw_input.find("disconnect") < 0:
	event = "Player Connected"
	description += " connected at " + datetime
else:
	event = "Player Disconnected"
	description += " disconnected ("
 
	# get disconnect reason
	result = re.search("disconnect.(\w+)", raw_input)
	reason = result.group(1)
	description += reason + ") at " + datetime
 
# add each API key unless that player is in the event log
for key, value in api_keys.iteritems():
	if raw_input.find(key) < 0:
		p.addkey(value)
 
# send prowl notification
p.push("Minecraft Server", event, description)

Then just give it execute permission with this command:

chmod +x ~/minecraft_prowl.py

We’ll come back later and customize it.

Step 2: Create the Swatch Configuration

Create a file called minecraft.swatch in your home directory. This tells swatch to pipe login and disconnect logs to our Python script. Copy the following text into this file, but modify the second line to match the full path of the Python script:

1
2
watchfor /INFO.+(logged\sin|disconnect)/
 pipe /home/minecraft/minecraft_prowl.py

Step 3: Setup Swatch to Run on Boot

I have to admit I took the easy route on this one. I since found that there have been init scripts written for swatch, but my method works so I haven’t taken the time to change it.

First, you’ll want to open your crontab:

crontab -e

Then add the following line, replacing the config and log file paths if necessary:

@reboot swatch --daemon --config-file=/home/minecraft/minecraft.swatch --tail-file=/home/minecraft/server.log

After you have saved the file, you can start swatch manually just this once by running that command without the “@reboot”. You shouldn’t have to again since we’ve instructed cron to launch it at boot.

Step 4: Modify the Script

Now you need to replace the Minecraft usernames and Prowl API keys in the script. Go out to Prowl’s API Keys page and create a new key. Name it whatever you want. Then, copy the API key.

You need to open the minecraft_prowl.py script and replace lines 15 and 16 with the Minecraft usernames and Prowl API keys that you are alerting to. Make sure that the API key and minecraft username in a line are for the same person. In my example, “Notch” is the first Minecraft user that I’m sending alerts to. Notch’s Prowl API key will only receive an alert when someone else causes a login/logoff event. If you don’t have a Minecraft username or just want to receive alerts for all users, type something into the username field that is unlikely to be a used on your server.

Add as many username/API key entries as you want.

Step 5: Test it!

Connect to your server in Minecraft and see if you get a push notification. If you don’t, you can troubleshoot it by running this command:

echo "2012-09-07 23:47:56 [INFO] Notch lost connection: disconnect.quitting" | ~/minecraft_prowl.py

One likely scenario for this not working would be if Pyrowl wasn’t properly installed. After an Ubuntu upgrade I had Pyrowl disappear from my server and this debugging helped me figure that out.

That’s it…

Enjoy! Let me know if you’ve found this helpful or if you have any questions.


4 comments »

  1. Mike says:

    Ha, That’s crazy, I was looking for this the other day. iPhone > Android!

  2. Ryan says:

    Great post! Thanks much!

  3. Brad Green says:

    thanks for this! I keep getting a syntax error when running the script, however. It errors out on this line:

    if raw_input.find(“disconnect”) < 0:

    Any ideas? There’s an arrow pointing at the semicolon after the &lt.

  4. brian says:

    @Brad

    I think I know what’s going on. Somehow my post got the left angle brackets (<) replaced with their HTML entities. Python would definitely not understand them.

    I've updated the page. Try copying it again or just fix lines 35 and 49. Let me know if this does the trick.

Leave a Reply

Your email address will not be published. Required fields are marked *