-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Appdynamics/master
https://singularity.jira.com/browse/ACE-775 Revamp Linux Monitoring E…
- Loading branch information
Showing
15 changed files
with
1,260 additions
and
336 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
src/main/java/com/appdynamics/extensions/linux/CommandExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/** | ||
* Copyright 2015 AppDynamics | ||
* | ||
* Licensed 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 com.appdynamics.extensions.linux; | ||
|
||
import org.apache.log4j.Logger; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
public class CommandExecutor { | ||
private static Logger logger = Logger.getLogger(CommandExecutor.class); | ||
|
||
public static List<String> execute(String command) { | ||
return init(command); | ||
} | ||
|
||
public static List<String> init(String command) { | ||
Process process; | ||
try { | ||
logger.debug("Executing the command " + command); | ||
process = Runtime.getRuntime().exec(command); | ||
|
||
new ErrorReader(process.getErrorStream()).start(); | ||
ResponseParser responseParser = new ResponseParser(process, command); | ||
responseParser.start(); | ||
process.waitFor(); | ||
responseParser.join(); | ||
List<String> commandOutput = responseParser.getData(); | ||
logger.trace("Command Output: " + commandOutput); | ||
return commandOutput; | ||
} catch (Exception e) { | ||
logger.error("Error while executing the process " + command, e); | ||
return null; | ||
} | ||
} | ||
|
||
public static class ResponseParser extends Thread { | ||
|
||
private Process process; | ||
private String command; | ||
private List<String> data = new ArrayList<String>(); | ||
|
||
public ResponseParser(Process process, String command) { | ||
this.process = process; | ||
this.command = command; | ||
} | ||
|
||
public void run() { | ||
InputStream inputStream = process.getInputStream(); | ||
BufferedReader input = null; | ||
try { | ||
input = new BufferedReader(new InputStreamReader(inputStream)); | ||
String line; | ||
while ((line = input.readLine()) != null) { | ||
data.add(line); | ||
} | ||
} catch (Exception e) { | ||
logger.error("Error while reading the input stream from the command " + command, e); | ||
} finally { | ||
try { | ||
input.close(); | ||
} catch (IOException e) { | ||
} | ||
if (process != null) { | ||
logger.trace("Destroying the process " + command); | ||
process.destroy(); | ||
} | ||
} | ||
} | ||
|
||
public List<String> getData() { | ||
return data; | ||
} | ||
|
||
} | ||
|
||
|
||
public static class ErrorReader extends Thread { | ||
public static final Logger logger = Logger.getLogger(ErrorReader.class); | ||
|
||
|
||
private final InputStream in; | ||
|
||
public ErrorReader(InputStream in) { | ||
this.in = in; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(in)); | ||
String temp; | ||
try { | ||
while ((temp = reader.readLine()) != null) { | ||
logger.error("Process Error - " + temp); | ||
} | ||
} catch (IOException e) { | ||
logger.error("Error while reading the contents of the the error stream", e); | ||
} finally { | ||
try { | ||
reader.close(); | ||
} catch (IOException e) { | ||
} | ||
} | ||
logger.trace("Closing the Error Reader " + Thread.currentThread().getName()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.