use of org.opennms.netmgt.poller.monitors.nrpe.NrpePacket in project opennms by OpenNMS.
the class NrpeMonitor method poll.
/**
* {@inheritDoc}
*
* Poll the specified address for service availability.
*
* During the poll an attempt is made to connect on the specified port. If
* the connection request is successful, the banner line generated by the
* interface is parsed and if the banner text indicates that we are talking
* to Provided that the interface's response is valid we set the service
* status to SERVICE_AVAILABLE and return.
*/
@Override
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
String reason = null;
TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);
String command = ParameterMap.getKeyedString(parameters, "command", NrpePacket.HELLO_COMMAND);
int port = ParameterMap.getKeyedInteger(parameters, "port", CheckNrpe.DEFAULT_PORT);
int padding = ParameterMap.getKeyedInteger(parameters, "padding", NrpePacket.DEFAULT_PADDING);
boolean useSsl = ParameterMap.getKeyedBoolean(parameters, "usessl", DEFAULT_USE_SSL);
/*
// Port
//
int port = ParameterMap.getKeyedInteger(parameters, "port", DEFAULT_PORT);
if (port == DEFAULT_PORT) {
throw new RuntimeException("NrpeMonitor: required parameter 'port' is not present in supplied properties.");
}
*/
// BannerMatch
//
// Commented out because it is not currently referenced in this monitor
// String strBannerMatch = (String) parameters.get("banner");
// Get the address instance.
//
InetAddress ipv4Addr = svc.getAddress();
final String hostAddress = InetAddressUtils.str(ipv4Addr);
LOG.debug("poll: address = {}, port = {}, {}", hostAddress, port, tracker);
// Give it a whirl
//
int serviceStatus = PollStatus.SERVICE_UNAVAILABLE;
Double responseTime = null;
for (tracker.reset(); tracker.shouldRetry() && serviceStatus != PollStatus.SERVICE_AVAILABLE; tracker.nextAttempt()) {
Socket socket = null;
try {
//
// create a connected socket
//
tracker.startAttempt();
socket = new Socket();
socket.connect(new InetSocketAddress(ipv4Addr, port), tracker.getConnectionTimeout());
socket.setSoTimeout(tracker.getSoTimeout());
LOG.debug("NrpeMonitor: connected to host: {} on port: {}", ipv4Addr, port);
reason = "Perhaps check the value of 'usessl' for this monitor against the NRPE daemon configuration";
socket = wrapSocket(socket, useSsl);
// We're connected, so upgrade status to unresponsive
serviceStatus = PollStatus.SERVICE_UNRESPONSIVE;
reason = "Connected successfully, but no response received";
NrpePacket p = new NrpePacket(NrpePacket.QUERY_PACKET, (short) 0, command);
byte[] b = p.buildPacket(padding);
OutputStream o = socket.getOutputStream();
o.write(b);
/*
if (strBannerMatch == null || strBannerMatch.length() == 0 || strBannerMatch.equals("*")) {
if (true) {
serviceStatus = SERVICE_AVAILABLE;
// Store response time in RRD
if (responseTime >= 0 && rrdPath != null) {
try {
this.updateRRD(rrdPath, ipv4Addr, dsName, responseTime, pkg);
} catch (RuntimeException rex) {
LOG.debug("There was a problem writing the RRD: {}", rex);
}
}
break;
}
BufferedReader rdr = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//
// Tokenize the Banner Line, and check the first
// line for a valid return.
//
String response = rdr.readLine();
responseTime = System.currentTimeMillis() - sentTime;
if (response == null)
continue;
LOG.debug("poll: banner = {}", response);
LOG.debug("poll: responseTime= {}ms", responseTime);
if (response.indexOf(strBannerMatch) > -1) {
*/
NrpePacket response = NrpePacket.receivePacket(socket.getInputStream(), padding);
responseTime = tracker.elapsedTimeInMillis();
if (response.getResultCode() == 0) {
serviceStatus = PollStatus.SERVICE_AVAILABLE;
reason = null;
} else {
serviceStatus = PollStatus.SERVICE_UNAVAILABLE;
reason = "NRPE command returned code " + response.getResultCode() + " and message: " + response.getBuffer();
}
} catch (NoRouteToHostException e) {
reason = "No route to host exception for address " + hostAddress;
LOG.warn("poll: {}", reason, e);
} catch (InterruptedIOException e) {
reason = "did not connect to host within " + tracker;
LOG.debug("NrpeMonitor: did not connect to host within {}", tracker);
} catch (ConnectException e) {
reason = "Connection exception for address: " + ipv4Addr;
//
if (LOG.isDebugEnabled()) {
e.fillInStackTrace();
LOG.debug("poll: {}", reason, e);
}
} catch (NrpeException e) {
reason = "NrpeException while polling address: " + ipv4Addr;
if (LOG.isDebugEnabled()) {
e.fillInStackTrace();
LOG.debug("poll: {}", reason, e);
}
} catch (IOException e) {
// Ignore
reason = "IOException while polling address: " + ipv4Addr;
if (LOG.isDebugEnabled()) {
e.fillInStackTrace();
LOG.debug("poll: {}", reason, e);
}
} finally {
try {
// Close the socket
if (socket != null) {
socket.close();
}
} catch (IOException e) {
if (LOG.isDebugEnabled()) {
e.fillInStackTrace();
LOG.debug("poll: Error closing socket.", e);
}
}
}
}
//
if (reason == null) {
return PollStatus.get(serviceStatus, responseTime);
} else {
return PollStatus.get(serviceStatus, reason);
}
}
Aggregations