怎样使用Twitter来远程监控服务器的运行情况(转)

 

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

 

<!--adsense-->

By sending the command starting with “RUN@”, 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());
}

<!--adsense-->

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

<script type="text/javascript"></script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值