Thomas Vachuska | 15370d2 | 2018-01-30 13:57:38 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.rest.resources; |
| 17 | |
| 18 | import org.onosproject.rest.AbstractWebResource; |
| 19 | import org.slf4j.Logger; |
| 20 | import org.slf4j.LoggerFactory; |
| 21 | |
| 22 | import javax.ws.rs.GET; |
| 23 | import javax.ws.rs.Path; |
| 24 | import javax.ws.rs.Produces; |
| 25 | import javax.ws.rs.core.MediaType; |
| 26 | import javax.ws.rs.core.Response; |
| 27 | import java.io.FileInputStream; |
| 28 | import java.io.IOException; |
| 29 | |
| 30 | import static com.google.common.io.ByteStreams.toByteArray; |
| 31 | |
| 32 | /** |
| 33 | * Provides stream of diagnostic information. |
| 34 | */ |
| 35 | @Path("diagnostics") |
| 36 | public 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); |
Ray Milkey | 5c7d488 | 2018-02-05 14:50:39 -0800 | [diff] [blame] | 72 | Thread.currentThread().interrupt(); |
Thomas Vachuska | 15370d2 | 2018-01-30 13:57:38 -0800 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | } |