Diaries of a Madman Diaries of a Madman

Make your servers tweet harder

A couple of years ago, Peter Gyongyosi wrote a piece titled Make your servers tweet, wherein he demonstrated a syslog-ng destination that can send logs to twitter. The code has been bit-rotting since, yet, the crazy value of the idea remained, so today, driven by an urge to do something silly, I assembled a tool to make my servers tweet, harder.

Unlike Peter's solution, mine is not a syslog-ng patch, nor a module, but a tiny python script, and a carefully crafted config. While both are available on GitHub, I will present both the code and the configuration here.

To be able to tweet, we need three things: we need a program, a twitter account, and permissons for the program to post on our behalf. Registering a twitter account is left as an exercise for the reader, but registering an application, and giving it permissions is a bit more involved, so we'll go through that. But first, lets code!

#! /usr/bin/python
import twitter
import json
import os
import sys

def tweet(message):
	tweet = ""
	if len(message) > 125:
		tweet = "%s... #syslog_ng" % message[:125]
		tweet = "%s #syslog_ng" % message

	cfg = json.load(open("/etc/syslog-ng/tweeter.credentials.json", 'r'))
	t = twitter.Api(
		consumer_key = cfg['consumer_key'],
		consumer_secret = cfg['consumer_secret'],
		access_token_key = cfg['access_token_key'],
		access_token_secret = cfg['access_token_secret']

while True:
	message = sys.stdin.readline().strip()
	if message:

This simple program will read credentials from /etc/syslog-ng/tweeter.credentials.json, and post whatever it receives on its standard input to Twitter. That is all. Well, not exactly: it also hashtags everything with #syslog_ng and trims long messages, but you get the idea!

Now, on to the harder part: granting access to this application. First of all, visit the twitter development center, and log in, then create a new application. Here, fill out the values as you feel appropriate. I suggest using yourname-log-tweeter as the application name, https://github.com/algernon/log-tweeter as the website, and the description is up to you.

Once this is done, visit your application, and click on the Settings tab, and change the application type to Read and Write. When done, switch back to the Details page, and press the "Create access tokens"" button at the bottom.

With these, you have all the information needed, so lets create a configuration file for the log-tweeter application! All we need to do, is paste the following JSON into /etc/syslog-ng/tweeter.credentials.json, and fill in the appropriate values from the page you still have open:

 "consumer_key": "",
 "consumer_secret": "",
 "access_token_key": "",
 "access_token_secret": ""

You can test the application by running the echo test | /usr/local/bin/tweeter command (obviously substituting the path to wherever you placed the script). If all goes well, this should post a tweet on your behalf.

All that is left to do, is to wire it up with syslog-ng. We don't want all messages to end up on twitter, that would be bad. So I present a snippet that posts on twitter whenever I unlock my screen at work:

filter f_unlock {
	program("gnome-screensaver-dialog") and match("authenticated as") and match("pam_krb5");
rewrite r_unlock {
	set("$MESSAGE", value("LOGIN_MSG"));
	subst("pam_krb5.*: ", "", value("LOGIN_MSG"));
destination d_tweeter {
	program("/usr/local/bin/tweeter" template("${LOGIN_MSG}\n"));
log {

Assuming a default Debian configuration, putting this into a file named - say - 99-twitter.conf under /etc/syslog-ng/conf.d/ and restarting syslog-ng will do the right thing.

As an example, see @a_syslog on twitter!