use of org.opennms.core.utils.ExecRunner in project opennms by OpenNMS.
the class GpMonitor method poll.
// read()
/**
* {@inheritDoc}
*
* Poll the specified address for service availability.
*
* During the poll an attempt is made to call the specified external script
* or program. If the connection request is successful, the banner line
* returned as standard output by the script or program is parsed for a
* partial match with the banner string specified in the poller
* configuration. Provided that the script's response is valid we set the
* service status to SERVICE_AVAILABLE and return.
*
* The timeout is handled by ExecRunner and is also passed as a parameter to
* the script or program being called.
*/
@Override
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
LOG.warn("GpMonitor: This poller monitor is deprecated in favor of SystemExecuteMonitor. GpMonitor will be removed in a future release.");
//
// Process parameters
//
TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);
String hoption = ParameterMap.getKeyedString(parameters, "hoption", "--hostname");
String toption = ParameterMap.getKeyedString(parameters, "toption", "--timeout");
//
// convert timeout to seconds for ExecRunner
//
String args = ParameterMap.getKeyedString(parameters, "args", null);
// Script
//
String script = ParameterMap.getKeyedString(parameters, "script", null);
if (script == null) {
throw new RuntimeException("GpMonitor: required parameter 'script' is not present in supplied properties.");
}
// BannerMatch
//
String strBannerMatch = (String) parameters.get("banner");
// Script standard output
//
String scriptoutput = "";
// Script error output
//
String scripterror = "";
// Get the address instance.
//
InetAddress ipAddr = svc.getAddress();
final String hostAddress = InetAddressUtils.str(ipAddr);
LOG.debug("poll: address = {}, script = {}, arguments = {}, {}", hostAddress, script, args, tracker);
// Give it a whirl
//
PollStatus serviceStatus = PollStatus.unavailable();
for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) {
try {
tracker.startAttempt();
int exitStatus = 100;
// Some scripts, such as Nagios check scripts, look for -H and -t versus --hostname and
// --timeout. If the optional parameter option-type is set to short, then the former
// will be used.
int timeoutInSeconds = (int) tracker.getTimeoutInSeconds();
ExecRunner er = new ExecRunner();
er.setMaxRunTimeSecs(timeoutInSeconds);
if (args == null)
exitStatus = er.exec(script + " " + hoption + " " + hostAddress + " " + toption + " " + timeoutInSeconds);
else
exitStatus = er.exec(script + " " + hoption + " " + hostAddress + " " + toption + " " + timeoutInSeconds + " " + args);
double responseTime = tracker.elapsedTimeInMillis();
if (exitStatus != 0) {
scriptoutput = er.getOutString();
String reason = script + " failed with exit code " + exitStatus + ". Standard out: " + scriptoutput;
LOG.debug(reason);
serviceStatus = PollStatus.unavailable(reason);
}
if (er.isMaxRunTimeExceeded()) {
String reason = script + " failed. Timeout exceeded";
LOG.debug(reason);
serviceStatus = PollStatus.unavailable(reason);
} else {
if (exitStatus == 0) {
scriptoutput = er.getOutString();
scripterror = er.getErrString();
if (!scriptoutput.equals(""))
LOG.debug("{} output = {}", script, scriptoutput);
else
LOG.debug("{} returned no output", script);
if (!scripterror.equals(""))
LOG.debug("{} error = {}", script, scripterror);
if (strBannerMatch == null || strBannerMatch.equals("*")) {
serviceStatus = PollStatus.available(responseTime);
} else {
if (scriptoutput.indexOf(strBannerMatch) > -1) {
serviceStatus = PollStatus.available(responseTime);
} else {
serviceStatus = PollStatus.unavailable(script + "banner not contained in output banner='" + strBannerMatch + "' output='" + scriptoutput + "'");
}
}
}
}
} catch (ArrayIndexOutOfBoundsException e) {
String reason = script + " ArrayIndexOutOfBoundsException";
LOG.debug(reason, e);
serviceStatus = PollStatus.unavailable(reason);
} catch (IOException e) {
String reason = "IOException occurred. Check for proper operation of " + script;
LOG.debug(reason, e);
serviceStatus = PollStatus.unavailable(reason);
} catch (Throwable e) {
String reason = script + "Exception occurred";
LOG.debug(reason, e);
serviceStatus = PollStatus.unavailable(reason);
}
}
//
// return the status of the service
//
LOG.debug("poll: GP - serviceStatus= {} {}", serviceStatus, hostAddress);
return serviceStatus;
}
use of org.opennms.core.utils.ExecRunner in project opennms by OpenNMS.
the class GpClient method connect.
/**
* {@inheritDoc}
*/
@Override
public void connect(final InetAddress address, final int port, final int timeout) throws IOException, Exception {
setExitStatus(100);
ExecRunner execRunner = new ExecRunner();
execRunner.setMaxRunTimeSecs(convertToSeconds(timeout));
final String hostAddress = InetAddressUtils.str(address);
final String script = "" + getScript() + " " + getHoption() + " " + hostAddress + " " + getToption() + " " + convertToSeconds(timeout);
if (getArgs() == null)
setExitStatus(execRunner.exec(script));
else
setExitStatus(execRunner.exec(getScript() + " " + getHoption() + " " + hostAddress + " " + getToption() + " " + convertToSeconds(timeout) + " " + getArgs()));
if (execRunner.isMaxRunTimeExceeded()) {
} else {
if (getExitStatus() == 0) {
setResponse(execRunner.getOutString());
setError(execRunner.getErrString());
}
}
}
use of org.opennms.core.utils.ExecRunner in project opennms by OpenNMS.
the class SystemExecuteMonitor method poll.
/**
* {@inheritDoc}
*
* During the poll an attempt is made to call the specified external script
* or program. The banner line returned as standard output by the script or program is parsed for a
* partial match with the banner string specified in the poller configuration.
* Provided that the script's response is valid we set the service status to SERVICE_AVAILABLE and return.
* The parameter script
* The parameter args is handed over to the called script. The following variables will be replaced.
* ${timeout} will be replaced with the specified timeout in milliseconds.
* ${timeoutsec} will be replaced with the specified timeout in seconds not in milliseconds.
* ${retry} will be replaced with the amount off specified retires.
* ${ipaddr} will be replaced with the ip-address of the interface that holds the polled service at the node.
* ${nodeid} will be replaced with the nodeid of the node that holds the polled service.
* ${nodelabel} will be replaced with the nodelabel of the node that holds the polled service.
* ${svcname} will be replaced with the name of the polled service.
*
* The timeout is handled by ExecRunner and is also passed as a parameter to
* the script or program being called.
*/
@Override
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);
String script = ParameterMap.getKeyedString(parameters, "script", null);
String problem = checkScriptFile(script);
if (problem != null) {
LOGGER.error(problem);
return PollStatus.unknown(problem);
}
String args = ParameterMap.getKeyedString(parameters, "args", "");
args = enrichArguments(args, svc, tracker, parameters);
String strBannerMatch = (String) parameters.get("banner");
String scriptOutput = "";
String scriptError = "";
PollStatus serviceStatus = PollStatus.unavailable();
for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) {
try {
tracker.startAttempt();
int exitStatus = 100;
int timeoutInSeconds = (int) tracker.getTimeoutInSeconds();
ExecRunner execRunner = new ExecRunner();
execRunner.setMaxRunTimeSecs(timeoutInSeconds);
LOGGER.debug("calling: " + script + " " + args);
exitStatus = execRunner.exec(script + " " + args);
double responseTime = tracker.elapsedTimeInMillis();
if (exitStatus != 0) {
scriptOutput = execRunner.getOutString();
String reason = script + " failed with exit code " + exitStatus + ". Standard out: " + scriptOutput;
LOGGER.debug(reason);
serviceStatus = PollStatus.unavailable(reason);
}
if (execRunner.isMaxRunTimeExceeded()) {
String reason = script + " failed. Timeout exceeded";
LOGGER.debug(reason);
serviceStatus = PollStatus.unavailable(reason);
} else {
if (exitStatus == 0) {
scriptOutput = execRunner.getOutString();
scriptError = execRunner.getErrString();
if (!scriptOutput.equals(""))
LOGGER.debug("{} output = {}", script, scriptOutput);
else
LOGGER.debug("{} returned no output", script);
if (!scriptError.equals(""))
LOGGER.debug("{} error = {}", script, scriptError);
if (strBannerMatch == null || strBannerMatch.equals("*")) {
serviceStatus = PollStatus.available(responseTime);
} else {
if (scriptOutput.indexOf(strBannerMatch) > -1) {
serviceStatus = PollStatus.available(responseTime);
} else {
serviceStatus = PollStatus.unavailable(script + " banner not contained in output banner='" + strBannerMatch + "' output='" + scriptOutput + "'");
}
}
}
}
} catch (ArrayIndexOutOfBoundsException e) {
String reason = script + " ArrayIndexOutOfBoundsException";
LOGGER.debug(reason, e);
serviceStatus = PollStatus.unavailable(reason);
} catch (IOException e) {
String reason = "IOException occurred. Check for proper operation of " + script;
LOGGER.debug(reason, e);
serviceStatus = PollStatus.unavailable(reason);
} catch (InterruptedException e) {
LOGGER.debug("Interruption for script " + script, e);
serviceStatus = PollStatus.unavailable("Interruption for script " + script + " " + e.getMessage());
}
}
LOGGER.debug("Called: '" + script + " " + args + "' Result: " + serviceStatus + " ResponseTime: " + serviceStatus.getResponseTime());
return serviceStatus;
}
Aggregations