RSS Feed for This PostCurrent Article

How I Use Twitter to Monitor Server Remotely

Download Source Code + Binary

This is a piece of code that I just developed out of fun, but it helped to resolve one of my recent problem.

I was running a batch job in one of the Unix server. It was already 10 p.m. I had not taken my dinner yet. The batch was going to take long time to finish, maybe 5 to 7 hours. I had to monitor it to make sure it is completed by tomorrow so that someone else could proceed with his work. Going back home and coming back was troublesome. I wished that I could monitor the job remotely, but the server is protected by firewall. The only port opened is port 80.

I was wondering if there is anything that I can do so that I can monitor the Unix server remotely, bypassing the firewall. Then I stumbled upon the idea – why not use Twitter to achieve it ??

I came out with the code to do that in roughly 1 hour. It may not be perfect, but at least it achieved what I wanted to do.

Using the program, I can monitor my server remotely. Below is a screenshot of the output using WinTwit

By sending the command starting with “[email protected]”, it will be executed by a Java process running on the server. The output will be sent back to Twitter and get refreshed at WinTwit.

To start with, you need to configure the proxy server, proxy port, and your Twitter settings in the properties file.

twitter.user.name=
twitter.user.password=

twitter.check.interval.seconds=5

http.proxyHost=
http.proxyPort=

TwitterPoller loads the properties from the configuration file, and starts TwitterExecutor, which is a Java TimerTask.

public static void main(String args[]) {
    try {
        Properties systemSettings = = System.getProperties();
        systemSettings.load(
        TwitterExecutor.class.getResourceAsStream(
        CONFIG_FILE));
        System.setProperties(systemSettings);
        long interval =
          Long.parseLong(System.getProperty(CHECK_INTERVAL));
        Timer timer = new Timer();
        timer.schedule(new TwitterExecutor(), 0, interval * 1000);
    } catch (IOException e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    }
}

In TwitterExecutor, I poll Twitter, run the command, and send back the results to Twitter.

Twitter twitter = new Twitter(userName, password);
Twitter.Status status = twitter.getStatus(userName);
if (status.getText().startsWith(EXEC_PATTERN)) {
    String cmds[] = status.getText().split(EXEC_PATTERN);
    String cmd = "";
    for (String str : cmds) {
        cmd += str;
    }
    System.out.println("Running command: " + cmd);
    ExecutorResult result = execute(cmd);
    System.out.println("Output: " + result.getOutput());
    System.out.println("Exit: " + result.getExitStatus());
    if (result.getOutput() != null) {
        String output = result.getOutput();
        if (output.length() > 130) {
            int count = output.length() % 130;
            for (int i = 0; i < count; i++) {
                int startIndex = i * 130;
                int endIndex = (i * 130) + 130;
                if (endIndex > output.length())
                   endIndex = output.length() - 1;
                String text =
                   output.substring(startIndex, endIndex);
                twitter.updateStatus(text);
            }
        } else {
            twitter.updateStatus(output);
        }
    }
} else {
    System.out.println("Not a command: " + status.getText());
}

The Java program size is very small. It takes very little memory and has no other dependencies. All you need is JDK 1.5.

The jar file twittersh.jar and config.properties are all you need. To run it,

java -cp <path to config.properties folder>:<path>/twittersh.jar
           com.twt.twitter.TwitterPoller

Note:

  1. This code is developed in a short time frame, and may not be bug free.
  2. The program is actually a backdoor which violates company security policies. I would advise that you use it wisely.
  3. Last, do not abuse Twitter

Popularity: 2% [?]


Trackback URL


1 Trackback(s)

  1. From Java - Writing An Automated Telnet Client | twit88.com | Dec 22, 2007

RSS Feed for This PostPost a Comment