Search in sources :

Example 1 with OnmsMinion

use of org.opennms.netmgt.model.minion.OnmsMinion in project opennms by OpenNMS.

the class RestClient method getAllMinions.

public List<OnmsMinion> getAllMinions() {
    GenericType<List<OnmsMinion>> minions = new GenericType<List<OnmsMinion>>() {
    };
    final WebTarget target = getTargetV2().path("minions");
    return getBuilder(target).accept(MediaType.APPLICATION_XML).get(minions);
}
Also used : GenericType(javax.ws.rs.core.GenericType) OnmsMinion(org.opennms.netmgt.model.minion.OnmsMinion) List(java.util.List) WebTarget(javax.ws.rs.client.WebTarget)

Example 2 with OnmsMinion

use of org.opennms.netmgt.model.minion.OnmsMinion in project opennms by OpenNMS.

the class MinionDaoIT method testQueryByLocation.

@Test
public void testQueryByLocation() throws Exception {
    final Date now = new Date();
    m_minionDao.save(new OnmsMinion(UUID.randomUUID().toString(), "TestLocation", "Started", now));
    m_minionDao.save(new OnmsMinion(UUID.randomUUID().toString(), "TestLocation", "Stopped", now));
    m_minionDao.save(new OnmsMinion(UUID.randomUUID().toString(), "OtherLocation", "Stopped", now));
    final Collection<OnmsMinion> testMinions = m_minionDao.findByLocation("TestLocation");
    assertEquals(2, testMinions.size());
}
Also used : OnmsMinion(org.opennms.netmgt.model.minion.OnmsMinion) Date(java.util.Date) Test(org.junit.Test)

Example 3 with OnmsMinion

use of org.opennms.netmgt.model.minion.OnmsMinion in project opennms by OpenNMS.

the class MinionHeartbeatMonitor method poll.

@Override
public PollStatus poll(final MonitoredService svc, final Map<String, Object> parameters) {
    // Minions send heartbeat every 30 seconds - we check that we can skip not more than one beat
    final int period = 2 * ParameterMap.getKeyedInteger(parameters, "period", 30 * 1000);
    // Get the minion to test whereas the minion ID is the nodes foreign ID by convention
    final OnmsNode node = nodeDao.get().get(svc.getNodeId());
    final OnmsMinion minion = minionDao.get().findById(node.getForeignId());
    // Calculate the time since the last heartbeat was received
    final long lastSeen = System.currentTimeMillis() - minion.getLastUpdated().getTime();
    final PollStatus status;
    if (lastSeen <= period) {
        status = PollStatus.available();
    } else if (ManagementFactory.getRuntimeMXBean().getUptime() < period) {
        status = PollStatus.unknown("JVM has not been started long enough to process a heartbeat.");
    } else {
        status = PollStatus.unavailable(String.format("Last heartbeat was %.2f seconds ago", lastSeen / 1000.0));
    }
    return status;
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) PollStatus(org.opennms.netmgt.poller.PollStatus) OnmsMinion(org.opennms.netmgt.model.minion.OnmsMinion)

Example 4 with OnmsMinion

use of org.opennms.netmgt.model.minion.OnmsMinion in project opennms by OpenNMS.

the class HeartbeatConsumer method handleMessage.

@Override
@Transactional
public void handleMessage(MinionIdentityDTO minionHandle) {
    LOG.info("Received heartbeat for Minion with id: {} at location: {}", minionHandle.getId(), minionHandle.getLocation());
    OnmsMinion minion = minionDao.findById(minionHandle.getId());
    if (minion == null) {
        minion = new OnmsMinion();
        minion.setId(minionHandle.getId());
        // The real location is filled in below, but we set this to null
        // for now to detect requisition changes
        minion.setLocation(null);
    }
    final String prevLocation = minion.getLocation();
    final String nextLocation = minionHandle.getLocation();
    minion.setLocation(minionHandle.getLocation());
    // Provision the minions node before we alter the location
    this.provision(minion, prevLocation, nextLocation);
    if (minionHandle.getTimestamp() == null) {
        // The heartbeat does not contain a timestamp - use the current time
        minion.setLastUpdated(new Date());
        LOG.info("Received heartbeat without a timestamp: {}", minionHandle);
    } else if (minion.getLastUpdated() == null) {
        // The heartbeat does contain a timestamp, and we don't have
        // one set yet, so use whatever we've been given
        minion.setLastUpdated(minionHandle.getTimestamp());
    } else if (minionHandle.getTimestamp().after(minion.getLastUpdated())) {
        // The timestamp in the heartbeat is more recent than the one we
        // have stored, so update it
        minion.setLastUpdated(minionHandle.getTimestamp());
    } else {
        // The timestamp in the heartbeat is earlier than the
        // timestamp we have stored, so ignore it
        LOG.info("Ignoring stale timestamp from heartbeat: {}", minionHandle);
    }
    minionDao.saveOrUpdate(minion);
    if (prevLocation == null) {
        final EventBuilder eventBuilder = new EventBuilder(EventConstants.MONITORING_SYSTEM_ADDED_UEI, "OpenNMS.Minion.Heartbeat");
        eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_TYPE, OnmsMonitoringSystem.TYPE_MINION);
        eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_ID, minionHandle.getId());
        eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_LOCATION, nextLocation);
        try {
            eventProxy.send(eventBuilder.getEvent());
        } catch (final EventProxyException e) {
            throw new DataAccessResourceFailureException("Unable to send event", e);
        }
    } else if (!prevLocation.equals(nextLocation)) {
        final EventBuilder eventBuilder = new EventBuilder(EventConstants.MONITORING_SYSTEM_LOCATION_CHANGED_UEI, "OpenNMS.Minion.Heartbeat");
        eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_TYPE, OnmsMonitoringSystem.TYPE_MINION);
        eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_ID, minionHandle.getId());
        eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_PREV_LOCATION, prevLocation);
        eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_LOCATION, nextLocation);
        try {
            eventProxy.send(eventBuilder.getEvent());
        } catch (final EventProxyException e) {
            throw new DataAccessResourceFailureException("Unable to send event", e);
        }
    }
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) OnmsMinion(org.opennms.netmgt.model.minion.OnmsMinion) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) EventProxyException(org.opennms.netmgt.events.api.EventProxyException) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with OnmsMinion

use of org.opennms.netmgt.model.minion.OnmsMinion in project opennms by OpenNMS.

the class MinionDaoIT method testProperties.

@Test
@Transactional
public void testProperties() throws Exception {
    final Date now = new Date();
    final OnmsMinion a = new OnmsMinion(UUID.randomUUID().toString(), "TestLocation", "Started", now);
    final OnmsMinion b = new OnmsMinion(UUID.randomUUID().toString(), "OtherLocation", "Started", now);
    a.getProperties().put("Yes", "No");
    a.setProperty("Up", "Down");
    b.setProperty("Left", "Right");
    b.setProperty("Wrong", "Right");
    m_minionDao.save(a);
    m_minionDao.save(b);
    m_minionDao.flush();
    assertEquals(Integer.valueOf(4), m_jdbcTemplate.queryForObject("select count(*) from monitoringsystemsproperties", Integer.class));
    assertEquals(Integer.valueOf(2), m_jdbcTemplate.queryForObject("select count(*) from monitoringsystemsproperties where monitoringsystemid = ?", new Object[] { a.getId() }, Integer.class));
    assertEquals(Integer.valueOf(2), m_jdbcTemplate.queryForObject("select count(*) from monitoringsystemsproperties where monitoringsystemid = ?", new Object[] { b.getId() }, Integer.class));
    String prop = m_minionDao.findById(a.getId()).getProperties().get("Left");
    // a doesn't have that property, b does
    assertNull(prop);
    prop = m_minionDao.findById(b.getId()).getProperties().get("Left");
    assertNotNull(prop);
    assertEquals("Right", prop);
}
Also used : OnmsMinion(org.opennms.netmgt.model.minion.OnmsMinion) Date(java.util.Date) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

OnmsMinion (org.opennms.netmgt.model.minion.OnmsMinion)11 Date (java.util.Date)4 Test (org.junit.Test)3 Transactional (org.springframework.transaction.annotation.Transactional)3 Path (javax.ws.rs.Path)2 Response (javax.ws.rs.core.Response)2 EventProxyException (org.opennms.netmgt.events.api.EventProxyException)2 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)2 InetSocketAddress (java.net.InetSocketAddress)1 List (java.util.List)1 DELETE (javax.ws.rs.DELETE)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 WebTarget (javax.ws.rs.client.WebTarget)1 GenericType (javax.ws.rs.core.GenericType)1 Before (org.junit.Before)1 Ignore (org.junit.Ignore)1 OnmsNode (org.opennms.netmgt.model.OnmsNode)1 PollStatus (org.opennms.netmgt.poller.PollStatus)1 RestClient (org.opennms.smoketest.utils.RestClient)1