use of org.opennms.systemreport.system.PsParser in project opennms by OpenNMS.
the class AbstractSystemReportPlugin method getOpenNMSProcesses.
protected Set<Integer> getOpenNMSProcesses() {
LOG.trace("getOpenNMSProcesses()");
final Set<Integer> processes = new HashSet<Integer>();
final String jps = getResourceLocator().findBinary("jps");
LOG.trace("jps = {}", jps);
DataInputStream input = null;
PsParser parser = null;
PipedInputStream pis = null;
PipedOutputStream output = new PipedOutputStream();
DefaultExecutor executor = new DefaultExecutor();
executor.setWatchdog(new ExecuteWatchdog(5000));
if (jps != null) {
CommandLine command = CommandLine.parse(jps + " -v");
PumpStreamHandler streamHandler = new PumpStreamHandler(output, System.err);
try {
LOG.trace("executing '{}'", command);
pis = new PipedInputStream(output);
input = new DataInputStream(pis);
parser = new PsParser(input, "opennms_bootstrap.jar", "status", 0);
parser.start();
executor.setStreamHandler(streamHandler);
int exitValue = executor.execute(command);
IOUtils.closeQuietly(output);
parser.join();
processes.addAll(parser.getProcesses());
LOG.trace("finished '{}'", command);
if (exitValue != 0) {
LOG.debug("error running '{}': exit value was {}", command, exitValue);
}
} catch (final Exception e) {
LOG.debug("Failed to run '{}'", command, e);
} finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(pis);
IOUtils.closeQuietly(output);
}
}
LOG.trace("looking for ps");
final String ps = getResourceLocator().findBinary("ps");
if (ps != null) {
// try Linux/Mac style
CommandLine command = CommandLine.parse(ps + " aww -o pid -o args");
output = new PipedOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(output, System.err);
try {
LOG.trace("executing '{}'", command);
pis = new PipedInputStream(output);
input = new DataInputStream(pis);
parser = new PsParser(input, "opennms_bootstrap.jar", "status", 0);
parser.start();
executor.setStreamHandler(streamHandler);
int exitValue = executor.execute(command);
IOUtils.closeQuietly(output);
parser.join(MAX_PROCESS_WAIT);
processes.addAll(parser.getProcesses());
LOG.trace("finished '{}'", command);
if (exitValue != 0) {
LOG.debug("error running '{}': exit value was {}", command, exitValue);
}
} catch (final Exception e) {
LOG.debug("error running '{}'", command, e);
} finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(pis);
IOUtils.closeQuietly(output);
}
if (processes.size() == 0) {
// try Solaris style
command = CommandLine.parse(ps + " -ea -o pid -o args");
output = new PipedOutputStream();
streamHandler = new PumpStreamHandler(output, System.err);
try {
LOG.trace("executing '{}'", command);
pis = new PipedInputStream(output);
input = new DataInputStream(pis);
parser = new PsParser(input, "opennms_bootstrap.jar", "status", 0);
parser.start();
executor.setStreamHandler(streamHandler);
int exitValue = executor.execute(command);
IOUtils.closeQuietly(output);
parser.join(MAX_PROCESS_WAIT);
processes.addAll(parser.getProcesses());
LOG.trace("finished '{}'", command);
if (exitValue != 0) {
LOG.debug("error running '{}': exit value was {}", command, exitValue);
}
} catch (final Exception e) {
LOG.debug("error running '{}'", command, e);
} finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(pis);
IOUtils.closeQuietly(output);
}
}
}
if (processes.size() == 0) {
LOG.warn("Unable to find any OpenNMS processes.");
}
return processes;
}
Aggregations