Search in sources :

Example 1 with NtpMessage

use of org.opennms.netmgt.poller.monitors.support.NtpMessage in project opennms by OpenNMS.

the class NtpMonitor method poll.

/**
 * {@inheritDoc}
 *
 * <P>
 * Poll the specified address for NTP service availability.
 * </P>
 *
 * <P>
 * During the poll an NTP request query packet is generated. The query is
 * sent via UDP socket to the interface at the specified port (by default
 * UDP port 123). If a response is received, it is parsed and validated. If
 * the NTP was successful the service status is set to SERVICE_AVAILABLE and
 * the method returns.
 * </P>
 */
@Override
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
    // get the parameters
    // 
    TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);
    int port = ParameterMap.getKeyedInteger(parameters, "port", DEFAULT_PORT);
    // get the address and NTP address request
    // 
    InetAddress ipAddr = svc.getAddress();
    PollStatus serviceStatus = PollStatus.unavailable();
    DatagramSocket socket = null;
    double responseTime = -1.0;
    try {
        socket = new DatagramSocket();
        // will force the
        socket.setSoTimeout(tracker.getSoTimeout());
        for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) {
            try {
                // Send NTP request
                // 
                byte[] data = new NtpMessage().toByteArray();
                DatagramPacket outgoing = new DatagramPacket(data, data.length, ipAddr, port);
                tracker.startAttempt();
                socket.send(outgoing);
                // Get NTP Response
                // 
                // byte[] buffer = new byte[512];
                DatagramPacket incoming = new DatagramPacket(data, data.length);
                socket.receive(incoming);
                responseTime = tracker.elapsedTimeInMillis();
                double destinationTimestamp = (System.currentTimeMillis() / 1000.0) + 2208988800.0;
                // Validate NTP Response
                // IOException thrown if packet does not decode as expected.
                NtpMessage msg = new NtpMessage(incoming.getData());
                double localClockOffset = ((msg.receiveTimestamp - msg.originateTimestamp) + (msg.transmitTimestamp - destinationTimestamp)) / 2;
                LOG.debug("poll: valid NTP request received the local clock offset is {}, responseTime= {}ms", localClockOffset, responseTime);
                LOG.debug("poll: NTP message : {}", msg);
                serviceStatus = PollStatus.available(responseTime);
            } catch (InterruptedIOException ex) {
            // Ignore, no response received.
            }
        }
    } catch (NoRouteToHostException e) {
        String reason = "No route to host exception for address: " + ipAddr;
        LOG.debug(reason, e);
        serviceStatus = PollStatus.unavailable(reason);
    } catch (ConnectException e) {
        String reason = "Connection exception for address: " + ipAddr;
        LOG.debug(reason, e);
        serviceStatus = PollStatus.unavailable(reason);
    } catch (IOException ex) {
        String reason = "IOException while polling address: " + ipAddr;
        LOG.debug(reason, ex);
        serviceStatus = PollStatus.unavailable(reason);
    } finally {
        if (socket != null)
            socket.close();
    }
    // 
    return serviceStatus;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) PollStatus(org.opennms.netmgt.poller.PollStatus) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) NoRouteToHostException(java.net.NoRouteToHostException) NtpMessage(org.opennms.netmgt.poller.monitors.support.NtpMessage) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) InetAddress(java.net.InetAddress) ConnectException(java.net.ConnectException)

Aggregations

IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 ConnectException (java.net.ConnectException)1 DatagramPacket (java.net.DatagramPacket)1 DatagramSocket (java.net.DatagramSocket)1 InetAddress (java.net.InetAddress)1 NoRouteToHostException (java.net.NoRouteToHostException)1 TimeoutTracker (org.opennms.core.utils.TimeoutTracker)1 PollStatus (org.opennms.netmgt.poller.PollStatus)1 NtpMessage (org.opennms.netmgt.poller.monitors.support.NtpMessage)1