1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */18package org.apache.hadoop.chukwa.inputtools.plugin.metrics;
192021import java.util.Timer;
22import java.util.TimerTask;
23import org.apache.commons.logging.Log;
24import org.apache.commons.logging.LogFactory;
25import org.apache.hadoop.chukwa.inputtools.plugin.IPlugin;
26import org.apache.hadoop.chukwa.util.DaemonWatcher;
27import org.json.simple.JSONObject;
2829publicclassExecextends TimerTask {
30privatestatic Log log = LogFactory.getLog(Exec.class);
31private String cmde = null;
32privateIPlugin plugin = null;
3334publicExec(String[] cmds) {
35 StringBuffer c = new StringBuffer();
36for (String cmd : cmds) {
37 c.append(cmd);
38 c.append(" ");
39 }
40 cmde = c.toString();
41 plugin = newExecHelper(cmds);
42 }
4344publicvoid run() {
45try {
46 JSONObject result = plugin.execute();
47int status = (Integer) result.get("status");
48if (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 }
5960public String getCmde() {
61return cmde;
62 }
6364publicstaticvoid main(String[] args) {
65 DaemonWatcher.createInstance(System.getProperty("RECORD_TYPE") + "-data-loader");
66int period = 60;
67try {
68if (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(newExec(args), 0, period * 1000);
80 }
81 }