怎样使用Twitter来远程监控服务器的运行情况(转)_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 怎样使用Twitter来远程监控服务器的运行情况(转)

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

 2012/1/10 12:45:40  wdmsyf  程序员俱乐部  我要评论(0)
  • 摘要:DownloadSourceCode+BinaryThisisapieceofcodethatIjustdevelopedoutoffun,butithelpedtoresolveoneofmyrecentproblem.IwasrunningabatchjobinoneoftheUnixserver.Itwasalready10p.m.Ihadnottakenmydinneryet.Thebatchwasgoingtotakelongtimetofinish,maybe5to7hours
  • 标签:使用 情况 运行 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>

发表评论
用户名: 匿名