This repository has been archived by the owner on May 12, 2021. It is now read-only.
/
ExecAdaptor.java
201 lines (172 loc) · 5.7 KB
/
ExecAdaptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.chukwa.datacollection.adaptor;
import org.apache.hadoop.chukwa.ChunkImpl;
import org.apache.hadoop.chukwa.datacollection.ChunkReceiver;
import org.apache.hadoop.chukwa.inputtools.plugin.ExecPlugin;
import org.apache.log4j.Logger;
import org.apache.log4j.helpers.ISO8601DateFormat;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.*;
/**
* Runs a command inside chukwa. Takes as params the interval in seconds at
* which to run the command, and the path and args to execute.
*
* Interval is optional, and defaults to 5 seconds.
*
* Example usage: add
* org.apache.hadoop.chukwa.datacollection.adaptor.ExecAdaptor Ps 2 /bin/ps aux
* 0
*
*/
public class ExecAdaptor extends AbstractAdaptor {
static class EmbeddedExec extends ExecPlugin {
String cmd;
public EmbeddedExec(String c) {
cmd = c;
}
@Override
public String getCmde() {
return cmd;
}
}
EmbeddedExec exec;
static final boolean FAKE_LOG4J_HEADER = true;
static final boolean SPLIT_LINES = false;
static Logger log = Logger.getLogger(ExecAdaptor.class);
class RunToolTask extends TimerTask {
public void run() {
JSONObject o = exec.execute();
try {
if (o.getInt("status") == exec.statusKO)
hardStop();
// FIXME: downstream customers would like timestamps here.
// Doing that efficiently probably means cutting out all the
// excess buffer copies here, and appending into an OutputBuffer.
byte[] data;
if (FAKE_LOG4J_HEADER) {
StringBuilder result = new StringBuilder();
ISO8601DateFormat dateFormat = new org.apache.log4j.helpers.ISO8601DateFormat();
result.append(dateFormat.format(new java.util.Date()));
result.append(" INFO org.apache.hadoop.chukwa.");
result.append(type);
result.append(": ");
result.append(o.getString("stdout"));
data = result.toString().getBytes();
} else {
String stdout = o.getString("stdout");
data = stdout.getBytes();
}
sendOffset += data.length;
ChunkImpl c = new ChunkImpl(ExecAdaptor.this.type, "results from "
+ cmd, sendOffset, data, ExecAdaptor.this);
if (SPLIT_LINES) {
ArrayList<Integer> carriageReturns = new ArrayList<Integer>();
for (int i = 0; i < data.length; ++i)
if (data[i] == '\n')
carriageReturns.add(i);
c.setRecordOffsets(carriageReturns);
} // else we get default one record
dest.add(c);
} catch (JSONException e) {
// FIXME: log this somewhere
} catch (InterruptedException e) {
// TODO Auto-generated catch block
} catch (AdaptorException e) {
// FIXME: log this somewhere
}
}
};
String cmd;
final java.util.Timer timer;
long period = 5 * 1000;
volatile long sendOffset = 0;
public ExecAdaptor() {
timer = new java.util.Timer();
}
@Override
public String getCurrentStatus() throws AdaptorException {
return type + " " + period + " " + cmd + " " + sendOffset;
}
public String getStreamName() {
return cmd;
}
@Override
@Deprecated
/**
* use shutdown(AdaptorShutdownPolicy shutdownPolicy)
*/
public void hardStop() throws AdaptorException {
shutdown(AdaptorShutdownPolicy.HARD_STOP);
}
@Override
@Deprecated
/**
* use shutdown(AdaptorShutdownPolicy shutdownPolicy)
*/
public long shutdown() throws AdaptorException {
return shutdown(AdaptorShutdownPolicy.GRACEFULLY);
}
@Override
public long shutdown(AdaptorShutdownPolicy shutdownPolicy)
throws AdaptorException {
log.info("Enter Shutdown:" + shutdownPolicy.name()+ " - ObjectId:" + this);
switch(shutdownPolicy) {
case HARD_STOP :
timer.cancel();
exec.stop();
break;
case GRACEFULLY :
try {
timer.cancel();
exec.waitFor();
} catch (InterruptedException e) {
}
break;
case WAIT_TILL_FINISHED :
try {
timer.cancel();
exec.waitFor();
} catch (InterruptedException e) {
}
break;
}
log.info("Exist Shutdown:" + shutdownPolicy.name()+ " - ObjectId:" + this);
return sendOffset;
}
@Override
public void start(String status, long offset) throws AdaptorException {
int spOffset = status.indexOf(' ');
if (spOffset > 0) {
try {
period = Integer.parseInt(status.substring(0, spOffset));
cmd = status.substring(spOffset + 1);
} catch (NumberFormatException e) {
log.warn("ExecAdaptor: sample interval "
+ status.substring(0, spOffset) + " can't be parsed");
cmd = status;
}
} else
cmd = status;
this.sendOffset = offset;
this.exec = new EmbeddedExec(cmd);
TimerTask execTimer = new RunToolTask();
timer.schedule(execTimer, 0L, period);
}
}