Skip to content

Commit

Permalink
Merge pull request #1 from Appdynamics/master
Browse files Browse the repository at this point in the history
https://singularity.jira.com/browse/ACE-775 Revamp Linux Monitoring E…
  • Loading branch information
akshayAppd19 authored Oct 25, 2017
2 parents c7ec0d3 + 49b6818 commit 447a3aa
Show file tree
Hide file tree
Showing 15 changed files with 1,260 additions and 336 deletions.
33 changes: 29 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.appdynamics.extensions</groupId>
<artifactId>linux-monitoring-extension</artifactId>
<version>2.0.4</version>
<version>2.1.0</version>
<packaging>jar</packaging>

<name>linux-monitoring-extension</name>
Expand Down Expand Up @@ -40,6 +40,28 @@
<version>4.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
Expand Down Expand Up @@ -88,11 +110,14 @@
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Implementation-Title>Linux Monitor v${project.version} Build Date ${maven.build.timestamp}
</Implementation-Title>
<Main-Class>com.appdynamics.extensions.workbench.WorkbenchServerLauncher</Main-Class>
<Implementation-Title>Linux Monitor v${project.version} Build Date ${maven.build.timestamp}</Implementation-Title>
</manifestEntries>
</transformer>
</transformers>
Expand Down
125 changes: 125 additions & 0 deletions src/main/java/com/appdynamics/extensions/linux/CommandExecutor.java
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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ public class FileParser {
private String countMetric;
private List<StatParser> parserList = new ArrayList<StatParser>();

private Logger logger;
private Logger logger = Logger.getLogger(FileParser.class);;

public FileParser(BufferedReader reader, String description, Logger logger, String countMetric) {
public FileParser(BufferedReader reader, String description, String countMetric) {
this.reader = reader;
this.description = description;
this.logger = logger;
this.countMetric = countMetric;
}

Expand Down
Loading

0 comments on commit 447a3aa

Please sign in to comment.