blob: 6b951e0bd3c9afa65acadd6b44d8df147f35a0ed [file] [log] [blame]
Thomas Vachuskab1702072018-01-30 13:57:38 -08001/*
2 * Copyright 2015-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.rest.resources;
17
18import org.onosproject.rest.AbstractWebResource;
19import org.slf4j.Logger;
20import org.slf4j.LoggerFactory;
21
22import javax.ws.rs.GET;
23import javax.ws.rs.Path;
24import javax.ws.rs.Produces;
25import javax.ws.rs.core.MediaType;
26import javax.ws.rs.core.Response;
27import java.io.FileInputStream;
28import java.io.IOException;
29
30import static com.google.common.io.ByteStreams.toByteArray;
31
32/**
33 * Provides stream of diagnostic information.
34 */
35@Path("diagnostics")
36public class DiagnosticsWebResource extends AbstractWebResource {
37
38 private static final String COMMAND = "../bin/onos-node-diagnostics";
39 private static final String DIAGS = "/tmp/onos-node-diags.tar.gz";
40
41 private final Logger log = LoggerFactory.getLogger(getClass());
42
43 /**
44 * Get tar.gz stream of node diagnostic information.
45 *
46 * @return 200 OK with a tar.gz stream of diagnostic data
47 */
48 @GET
49 @Produces(MediaType.APPLICATION_OCTET_STREAM)
50 public Response getDiagnostics() {
51 try {
52 execute(COMMAND);
53 return ok(new FileInputStream(DIAGS)).build();
54 } catch (IOException e) {
55 return Response.serverError().build();
56 }
57 }
58
59 // Executes the given command arguments as a system command.
60 private void execute(String command) throws IOException {
61 try {
62 Process process = Runtime.getRuntime().exec(command);
63 byte[] output = toByteArray(process.getInputStream());
64 byte[] error = toByteArray(process.getErrorStream());
65 int code = process.waitFor();
66 if (code != 0) {
67 log.info("Command failed: status={}, output={}, error={}",
68 code, new String(output), new String(error));
69 }
70 } catch (InterruptedException e) {
71 log.error("Interrupted executing command {}", command, e);
72 }
73 }
74}