Search in sources :

Example 1 with RTCCategory

use of org.opennms.netmgt.rtc.datablock.RTCCategory in project opennms by OpenNMS.

the class AvailabilityServiceIT method categoryIsFullyAvailableWhenNoServicesArePresent.

@Test
public void categoryIsFullyAvailableWhenNoServicesArePresent() throws Exception {
    final RTCCategory rtcCat = EasyMock.createNiceMock(RTCCategory.class);
    EasyMock.expect(rtcCat.getLabel()).andReturn("Routers");
    // This nodeid should not exist in the database
    EasyMock.expect(rtcCat.getNodes()).andReturn(Lists.newArrayList(99999));
    EasyMock.replay(rtcCat);
    final EuiLevel euiLevel = m_availabilityService.getEuiLevel(rtcCat);
    assertEquals(1, euiLevel.getCategory().size());
    final Category category = euiLevel.getCategory().get(0);
    assertEquals(100.0, category.getCatvalue(), 0.001);
    assertEquals(1, category.getNode().size());
    final Node node = category.getNode().get(0);
    assertEquals(100.0, node.getNodevalue(), 0.001);
    assertEquals(0, node.getNodesvccount());
    assertEquals(0, node.getNodesvcdowncount());
}
Also used : RTCCategory(org.opennms.netmgt.rtc.datablock.RTCCategory) Category(org.opennms.netmgt.xml.rtc.Category) Node(org.opennms.netmgt.xml.rtc.Node) EuiLevel(org.opennms.netmgt.xml.rtc.EuiLevel) RTCCategory(org.opennms.netmgt.rtc.datablock.RTCCategory) Test(org.junit.Test)

Example 2 with RTCCategory

use of org.opennms.netmgt.rtc.datablock.RTCCategory in project opennms by OpenNMS.

the class DataManager method rtcNodeRescan.

/**
 * Update the categories for a node. This method will update the categories
 * for all interfaces on a node.
 *
 * @param nodeid
 *            the nodeid on which SNMP service was added
 * @throws java.sql.SQLException
 *             if the database read fails due to an SQL error
 * @throws org.opennms.netmgt.filter.api.FilterParseException
 *             if filtering the data against the category rule fails due to
 *             the rule being incorrect
 * @throws org.opennms.netmgt.rtc.RTCException
 *             if the database read or filtering the data against the
 *             category rule fails for some reason
 */
public synchronized void rtcNodeRescan(int nodeid) throws SQLException, FilterParseException, RTCException {
    for (RTCCategory cat : m_categories.values()) {
        cat.deleteNode(nodeid);
    }
    m_map.deleteNode(nodeid);
    populateNodesFromDB("ifsvc.nodeid = ?", new Object[] { Long.valueOf(nodeid) });
}
Also used : RTCCategory(org.opennms.netmgt.rtc.datablock.RTCCategory)

Example 3 with RTCCategory

use of org.opennms.netmgt.rtc.datablock.RTCCategory in project opennms by OpenNMS.

the class DataManager method serviceDeleted.

/**
 * Remove node from the map and the categories on a 'serviceDeleted' event.
 *
 * @param nodeid
 *            the nodeid on which service was deleted
 * @param ip
 *            the ip on which service was deleted
 * @param svcName
 *            the service that was deleted
 */
public synchronized void serviceDeleted(int nodeid, InetAddress ip, String svcName) {
    // create lookup key
    RTCNodeKey key = new RTCNodeKey(nodeid, ip, svcName);
    // lookup the node
    RTCNode rtcN = m_map.getRTCNode(key);
    if (rtcN == null) {
        LOG.warn("Received a {} event for an unknown node: {}", EventConstants.SERVICE_DELETED_EVENT_UEI, key.toString());
        return;
    }
    // 
    // Go through from all the categories this node belongs to
    // and delete the service
    // 
    List<String> categories = rtcN.getCategories();
    ListIterator<String> catIter = categories.listIterator();
    while (catIter.hasNext()) {
        String catlabel = (String) catIter.next();
        RTCCategory cat = (RTCCategory) m_categories.get(catlabel);
        // get nodes in this category
        List<Integer> catNodes = cat.getNodes();
        // check if the category contains this node
        int nIndex = catNodes.indexOf(rtcN.getNodeID());
        if (nIndex != -1) {
            // remove from the category if it is the only service left.
            if (m_map.getServiceCount(nodeid, catlabel) == 1) {
                catNodes.remove(nIndex);
                LOG.info("Removing node from category: {}", catlabel);
            }
            // let the node know that this category is out
            catIter.remove();
        }
    }
    // finally remove from map
    m_map.delete(rtcN);
}
Also used : RTCNodeKey(org.opennms.netmgt.rtc.datablock.RTCNodeKey) RTCNode(org.opennms.netmgt.rtc.datablock.RTCNode) RTCCategory(org.opennms.netmgt.rtc.datablock.RTCCategory)

Example 4 with RTCCategory

use of org.opennms.netmgt.rtc.datablock.RTCCategory in project opennms by OpenNMS.

the class DataSender method sendData.

/**
 * Loop through the categories and send out data for all categories that
 * have changed
 */
public synchronized void sendData() {
    LOG.debug("In DataSender sendData()");
    // loop through and send info
    for (final RTCCategory cat : m_dataMgr.getCategories().values()) {
        // get label
        final String catlabel = cat.getLabel();
        LOG.debug("DataSender:sendData(): Category '{}'", catlabel);
        // get the post info for this category
        final Set<HttpPostInfo> urlList = m_catUrlMap.get(catlabel);
        if (urlList == null || urlList.size() <= 0) {
            // a category that no one is listening for?
            LOG.debug("DataSender: category '{}' has no listeners", catlabel);
            continue;
        }
        LOG.debug("DataSender: category '{}' has listeners - converting to xml...", catlabel);
        final EuiLevel euidata;
        try {
            euidata = m_dataMgr.getEuiLevel(cat);
        } catch (final Throwable t) {
            LOG.warn("DataSender: unable to convert data to xml for category: '{}'", catlabel, t);
            continue;
        }
        // do a HTTP POST if subscribed
        if (urlList != null && urlList.size() > 0) {
            final Iterator<HttpPostInfo> urlIter = urlList.iterator();
            while (urlIter.hasNext()) {
                final HttpPostInfo postInfo = urlIter.next();
                InputStream inp = null;
                try {
                    LOG.debug("DataSender: posting data to: {}", postInfo.getURLString());
                    final String marshaledUeiData = JaxbUtils.marshal(euidata);
                    try (final StringReader inr = new StringReader(marshaledUeiData)) {
                        inp = HttpUtils.post(postInfo.getURL(), inr, postInfo.getUser(), postInfo.getPassword(), 8 * HttpUtils.DEFAULT_POST_BUFFER_SIZE, HttpUtils.DEFAULT_CONNECT_TIMEOUT);
                    }
                    LOG.debug("DataSender: posted data for category: {}", catlabel);
                    final byte[] tmp = new byte[1024];
                    int bytesRead;
                    while ((bytesRead = inp.read(tmp)) != -1) {
                        if (LOG.isDebugEnabled()) {
                            if (bytesRead > 0) {
                                LOG.debug("DataSender: post response: {}", new String(tmp, 0, bytesRead));
                            }
                        }
                    }
                    postInfo.clearErrors();
                } catch (final Throwable t) {
                    LOG.warn("DataSender: unable to send data for category: {} due to {}: {}", catlabel, t.getClass().getName(), t.getMessage(), t);
                    postInfo.incrementErrors();
                } finally {
                    IOUtils.closeQuietly(inp);
                }
                // check to see if URL had too many errors
                if (POST_ERROR_LIMIT > 0 && postInfo.getErrors() >= POST_ERROR_LIMIT) {
                    // unsubscribe the URL
                    urlIter.remove();
                    LOG.warn("URL {} UNSUBSCRIBED due to reaching error limit {}", postInfo.getURLString(), postInfo.getErrors());
                }
            }
        }
    }
}
Also used : HttpPostInfo(org.opennms.netmgt.rtc.datablock.HttpPostInfo) InputStream(java.io.InputStream) StringReader(java.io.StringReader) EuiLevel(org.opennms.netmgt.xml.rtc.EuiLevel) RTCCategory(org.opennms.netmgt.rtc.datablock.RTCCategory)

Example 5 with RTCCategory

use of org.opennms.netmgt.rtc.datablock.RTCCategory in project opennms by OpenNMS.

the class RTCUtils method createCategoriesMap.

/**
 * Creates the categories map. Reads the categories from the categories.xml
 * and creates the 'RTCCategory's map
 */
public static HashMap<String, RTCCategory> createCategoriesMap() {
    CatFactory cFactory = null;
    try {
        CategoryFactory.init();
        cFactory = CategoryFactory.getInstance();
    } catch (IOException ex) {
        LOG.error("Failed to load categories information", ex);
        throw new UndeclaredThrowableException(ex);
    }
    HashMap<String, RTCCategory> retval = new HashMap<String, RTCCategory>();
    cFactory.getReadLock().lock();
    try {
        for (CategoryGroup cg : cFactory.getConfig().getCategoryGroups()) {
            final String commonRule = cg.getCommon().getRule();
            for (final org.opennms.netmgt.config.categories.Category cat : cg.getCategories()) {
                RTCCategory rtcCat = new RTCCategory(cat, commonRule);
                retval.put(rtcCat.getLabel(), rtcCat);
            }
        }
    } finally {
        cFactory.getReadLock().unlock();
    }
    return retval;
}
Also used : HashMap(java.util.HashMap) CatFactory(org.opennms.netmgt.config.api.CatFactory) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) IOException(java.io.IOException) RTCCategory(org.opennms.netmgt.rtc.datablock.RTCCategory) CategoryGroup(org.opennms.netmgt.config.categories.CategoryGroup)

Aggregations

RTCCategory (org.opennms.netmgt.rtc.datablock.RTCCategory)11 EuiLevel (org.opennms.netmgt.xml.rtc.EuiLevel)6 InputStream (java.io.InputStream)3 Date (java.util.Date)3 Test (org.junit.Test)3 Category (org.opennms.netmgt.xml.rtc.Category)3 Node (org.opennms.netmgt.xml.rtc.Node)3 StringReader (java.io.StringReader)2 HashMap (java.util.HashMap)2 OnmsMonitoredService (org.opennms.netmgt.model.OnmsMonitoredService)2 OnmsOutage (org.opennms.netmgt.model.OnmsOutage)2 HttpPostInfo (org.opennms.netmgt.rtc.datablock.HttpPostInfo)2 RTCNode (org.opennms.netmgt.rtc.datablock.RTCNode)2 Preconditions (com.google.common.base.Preconditions)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 IOException (java.io.IOException)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 ConnectException (java.net.ConnectException)1 MalformedURLException (java.net.MalformedURLException)1