1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.chukwa.inputtools.plugin.metrics;
19
20
21 import java.util.Timer;
22 import java.util.TimerTask;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.chukwa.inputtools.plugin.IPlugin;
26 import org.apache.hadoop.chukwa.util.DaemonWatcher;
27 import org.json.simple.JSONObject;
28
29 public class Exec extends TimerTask {
30 private static Log log = LogFactory.getLog(Exec.class);
31 private String cmde = null;
32 private IPlugin plugin = null;
33
34 public Exec(String[] cmds) {
35 StringBuffer c = new StringBuffer();
36 for (String cmd : cmds) {
37 c.append(cmd);
38 c.append(" ");
39 }
40 cmde = c.toString();
41 plugin = new ExecHelper(cmds);
42 }
43
44 public void run() {
45 try {
46 JSONObject result = plugin.execute();
47 int status = (Integer) result.get("status");
48 if (status < 0) {
49 System.out.println("Error");
50 log.warn("[ChukwaError]:" + Exec.class + ", "
51 + result.get("stderr"));
52 } else {
53 log.info(result.get("stdout"));
54 }
55 } catch (Exception e) {
56 log.error("Exec output unparsable:" + this.cmde);
57 }
58 }
59
60 public String getCmde() {
61 return cmde;
62 }
63
64 public static void main(String[] args) {
65 DaemonWatcher.createInstance(System.getProperty("RECORD_TYPE") + "-data-loader");
66 int period = 60;
67 try {
68 if (System.getProperty("PERIOD") != null) {
69 period = Integer.parseInt(System.getProperty("PERIOD"));
70 }
71 } catch (NumberFormatException ex) {
72 ex.printStackTrace();
73 System.out
74 .println("Usage: java -DPERIOD=nn -DRECORD_TYPE=recordType Exec [cmd]");
75 System.out.println("PERIOD should be numeric format of seconds.");
76 System.exit(0);
77 }
78 Timer timer = new Timer();
79 timer.schedule(new Exec(args), 0, period * 1000);
80 }
81 }