1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.http.log;
19
20 import com.google.common.annotations.VisibleForTesting;
21 import com.google.common.base.Charsets;
22
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.io.PrintWriter;
27 import java.net.URL;
28 import java.net.URLConnection;
29 import java.util.regex.Pattern;
30
31 import javax.net.ssl.HttpsURLConnection;
32 import javax.net.ssl.SSLSocketFactory;
33 import javax.servlet.ServletException;
34 import javax.servlet.http.HttpServlet;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.apache.commons.logging.impl.Jdk14Logger;
41 import org.apache.commons.logging.impl.Log4JLogger;
42 import org.apache.hadoop.HadoopIllegalArgumentException;
43 import org.apache.hadoop.conf.Configuration;
44 import org.apache.hadoop.conf.Configured;
45 import org.apache.hadoop.hbase.classification.InterfaceAudience;
46 import org.apache.hadoop.hbase.classification.InterfaceStability;
47 import org.apache.hadoop.hbase.http.HttpServer;
48 import org.apache.hadoop.security.authentication.client.AuthenticatedURL;
49 import org.apache.hadoop.security.authentication.client.KerberosAuthenticator;
50 import org.apache.hadoop.security.ssl.SSLFactory;
51 import org.apache.hadoop.util.ServletUtil;
52 import org.apache.hadoop.util.Tool;
53
54
55
56
57 @InterfaceStability.Evolving
58 public class LogLevel {
59 private static final String USAGES = "\nUsage: General options are:\n"
60 + "\t[-getlevel <host:port> <classname> [-protocol (http|https)]\n"
61 + "\t[-setlevel <host:port> <classname> <level> [-protocol (http|https)]";
62
63 public static final String PROTOCOL_HTTP = "http";
64 public static final String PROTOCOL_HTTPS = "https";
65
66
67
68
69 public static void main(String[] args) throws Exception {
70 CLI cli = new CLI(new Configuration());
71 System.exit(cli.run(args));
72 }
73
74
75
76
77 private enum Operations {
78 GETLEVEL,
79 SETLEVEL,
80 UNKNOWN
81 }
82
83 private static void printUsage() {
84 System.err.println(USAGES);
85 System.exit(-1);
86 }
87
88 public static boolean isValidProtocol(String protocol) {
89 return ((protocol.equals(PROTOCOL_HTTP) || protocol.equals(PROTOCOL_HTTPS)));
90 }
91
92 @VisibleForTesting
93 static class CLI extends Configured implements Tool {
94 private Operations operation = Operations.UNKNOWN;
95 private String protocol;
96 private String hostName;
97 private String className;
98 private String level;
99
100 CLI(Configuration conf) {
101 setConf(conf);
102 }
103
104 @Override
105 public int run(String[] args) throws Exception {
106 try {
107 parseArguments(args);
108 sendLogLevelRequest();
109 } catch (HadoopIllegalArgumentException e) {
110 printUsage();
111 }
112 return 0;
113 }
114
115
116
117
118
119
120 private void sendLogLevelRequest()
121 throws HadoopIllegalArgumentException, Exception {
122 switch (operation) {
123 case GETLEVEL:
124 doGetLevel();
125 break;
126 case SETLEVEL:
127 doSetLevel();
128 break;
129 default:
130 throw new HadoopIllegalArgumentException(
131 "Expect either -getlevel or -setlevel");
132 }
133 }
134
135 public void parseArguments(String[] args) throws
136 HadoopIllegalArgumentException {
137 if (args.length == 0) {
138 throw new HadoopIllegalArgumentException("No arguments specified");
139 }
140 int nextArgIndex = 0;
141 while (nextArgIndex < args.length) {
142 switch (args[nextArgIndex]) {
143 case "-getlevel":
144 nextArgIndex = parseGetLevelArgs(args, nextArgIndex);
145 break;
146 case "-setlevel":
147 nextArgIndex = parseSetLevelArgs(args, nextArgIndex);
148 break;
149 case "-protocol":
150 nextArgIndex = parseProtocolArgs(args, nextArgIndex);
151 break;
152 default:
153 throw new HadoopIllegalArgumentException(
154 "Unexpected argument " + args[nextArgIndex]);
155 }
156 }
157
158
159 if (operation == Operations.UNKNOWN) {
160 throw new HadoopIllegalArgumentException(
161 "Must specify either -getlevel or -setlevel");
162 }
163
164
165 if (protocol == null) {
166 protocol = PROTOCOL_HTTP;
167 }
168 }
169
170 private int parseGetLevelArgs(String[] args, int index) throws
171 HadoopIllegalArgumentException {
172
173 if (operation != Operations.UNKNOWN) {
174 throw new HadoopIllegalArgumentException("Redundant -getlevel command");
175 }
176
177 if (index + 2 >= args.length) {
178 throw new HadoopIllegalArgumentException("-getlevel needs two parameters");
179 }
180 operation = Operations.GETLEVEL;
181 hostName = args[index + 1];
182 className = args[index + 2];
183 return index + 3;
184 }
185
186 private int parseSetLevelArgs(String[] args, int index) throws
187 HadoopIllegalArgumentException {
188
189 if (operation != Operations.UNKNOWN) {
190 throw new HadoopIllegalArgumentException("Redundant -setlevel command");
191 }
192
193 if (index + 3 >= args.length) {
194 throw new HadoopIllegalArgumentException("-setlevel needs three parameters");
195 }
196 operation = Operations.SETLEVEL;
197 hostName = args[index + 1];
198 className = args[index + 2];
199 level = args[index + 3];
200 return index + 4;
201 }
202
203 private int parseProtocolArgs(String[] args, int index) throws
204 HadoopIllegalArgumentException {
205
206 if (protocol != null) {
207 throw new HadoopIllegalArgumentException("Redundant -protocol command");
208 }
209
210 if (index + 1 >= args.length) {
211 throw new HadoopIllegalArgumentException("-protocol needs one parameter");
212 }
213
214 protocol = args[index + 1];
215 if (!isValidProtocol(protocol)) {
216 throw new HadoopIllegalArgumentException("Invalid protocol: " + protocol);
217 }
218 return index + 2;
219 }
220
221
222
223
224
225
226
227 private void doGetLevel() throws Exception {
228 process(protocol + "://" + hostName + "/logLevel?log=" + className);
229 }
230
231
232
233
234
235
236
237 private void doSetLevel() throws Exception {
238 process(protocol + "://" + hostName + "/logLevel?log=" + className
239 + "&level=" + level);
240 }
241
242
243
244
245
246
247
248
249
250
251 private URLConnection connect(URL url) throws Exception {
252 AuthenticatedURL.Token token = new AuthenticatedURL.Token();
253 AuthenticatedURL aUrl;
254 SSLFactory clientSslFactory;
255 URLConnection connection;
256
257
258 if (PROTOCOL_HTTPS.equals(url.getProtocol())) {
259 clientSslFactory = new SSLFactory(SSLFactory.Mode.CLIENT, this.getConf());
260 clientSslFactory.init();
261 SSLSocketFactory sslSocketF = clientSslFactory.createSSLSocketFactory();
262
263 aUrl = new AuthenticatedURL(new KerberosAuthenticator(), clientSslFactory);
264 connection = aUrl.openConnection(url, token);
265 HttpsURLConnection httpsConn = (HttpsURLConnection) connection;
266 httpsConn.setSSLSocketFactory(sslSocketF);
267 } else {
268 aUrl = new AuthenticatedURL(new KerberosAuthenticator());
269 connection = aUrl.openConnection(url, token);
270 }
271 connection.connect();
272 return connection;
273 }
274
275
276
277
278
279
280
281 private void process(String urlString) throws Exception {
282 URL url = new URL(urlString);
283 System.out.println("Connecting to " + url);
284
285 URLConnection connection = connect(url);
286
287
288
289 try (InputStreamReader streamReader =
290 new InputStreamReader(connection.getInputStream(), Charsets.UTF_8);
291 BufferedReader bufferedReader = new BufferedReader(streamReader)) {
292 String line;
293 while((line = bufferedReader.readLine()) != null) {
294 if (line.startsWith(MARKER)) {
295 System.out.println(TAG.matcher(line).replaceAll(""));
296 }
297 }
298 } catch (IOException ioe) {
299 System.err.println("" + ioe);
300 }
301 }
302 }
303
304 private static final String MARKER = "<!-- OUTPUT -->";
305 private static final Pattern TAG = Pattern.compile("<[^>]*>");
306
307
308
309
310 @InterfaceAudience.LimitedPrivate({"HDFS", "MapReduce"})
311 @InterfaceStability.Unstable
312 public static class Servlet extends HttpServlet {
313 private static final long serialVersionUID = 1L;
314
315 @Override
316 public void doGet(HttpServletRequest request, HttpServletResponse response)
317 throws ServletException, IOException {
318
319
320 if (!HttpServer.hasAdministratorAccess(getServletContext(), request,
321 response)) {
322 return;
323 }
324
325 PrintWriter out = ServletUtil.initHTML(response, "Log Level");
326 String logName = ServletUtil.getParameter(request, "log");
327 String level = ServletUtil.getParameter(request, "level");
328
329 if (logName != null) {
330 out.println("<br /><hr /><h3>Results</h3>");
331 out.println(MARKER
332 + "Submitted Log Name: <b>" + logName + "</b><br />");
333
334 Log log = LogFactory.getLog(logName);
335 out.println(MARKER
336 + "Log Class: <b>" + log.getClass().getName() +"</b><br />");
337 if (level != null) {
338 out.println(MARKER + "Submitted Level: <b>" + level + "</b><br />");
339 }
340
341 if (log instanceof Log4JLogger) {
342 process(((Log4JLogger)log).getLogger(), level, out);
343 }
344 else if (log instanceof Jdk14Logger) {
345 process(((Jdk14Logger)log).getLogger(), level, out);
346 }
347 else {
348 out.println("Sorry, " + log.getClass() + " not supported.<br />");
349 }
350 }
351
352 out.println(FORMS);
353 out.println(ServletUtil.HTML_TAIL);
354 }
355
356 static final String FORMS = "\n<br /><hr /><h3>Get / Set</h3>"
357 + "\n<form>Log: <input type='text' size='50' name='log' /> "
358 + "<input type='submit' value='Get Log Level' />"
359 + "</form>"
360 + "\n<form>Log: <input type='text' size='50' name='log' /> "
361 + "Level: <input type='text' name='level' /> "
362 + "<input type='submit' value='Set Log Level' />"
363 + "</form>";
364
365 private static void process(org.apache.log4j.Logger log, String level, PrintWriter out) {
366 if (level != null) {
367 if (!level.equals(org.apache.log4j.Level.toLevel(level).toString())) {
368 out.println(MARKER + "Bad level : <b>" + level + "</b><br />");
369 } else {
370 log.setLevel(org.apache.log4j.Level.toLevel(level));
371 out.println(MARKER + "Setting Level to " + level + " ...<br />");
372 }
373 }
374 out.println(MARKER
375 + "Effective level: <b>" + log.getEffectiveLevel() + "</b><br />");
376 }
377
378 private static void process(java.util.logging.Logger log, String level,
379 PrintWriter out) {
380 if (level != null) {
381 log.setLevel(java.util.logging.Level.parse(level));
382 out.println(MARKER + "Setting Level to " + level + " ...<br />");
383 }
384
385 java.util.logging.Level lev;
386 while ((lev = log.getLevel()) == null) {
387 log = log.getParent();
388 }
389 out.println(MARKER + "Effective level: <b>" + lev + "</b><br />");
390 }
391 }
392 }