Search in sources :

Example 1 with EuiLevel

use of org.opennms.netmgt.xml.rtc.EuiLevel in project opennms by OpenNMS.

the class LegacyEuiLevelMapper method convertToEuiLevelXML.

/**
     * Convert the 'RTCCategory' object to a 'EuiLevel' object.
     *
     * @param rtcCat
     *            the RTCCategory to be converted
     * @return a {@link org.opennms.netmgt.xml.rtc.EuiLevel} object.
     */
public EuiLevel convertToEuiLevelXML(RTCCategory rtcCat) {
    // current time
    Date curDate = new Date();
    long curTime = curDate.getTime();
    // get the rolling window
    long rWindow = 24L * 60L * 60L * 1000L;
    LOG.debug("curdate: {}", curDate);
    // create the data
    EuiLevel level = new EuiLevel();
    // set created in m_header and add to level
    m_header.setCreated(EventConstants.formatToString(curDate));
    level.setHeader(m_header);
    org.opennms.netmgt.xml.rtc.Category levelCat = new org.opennms.netmgt.xml.rtc.Category();
    // get a handle to data
    synchronized (m_dataMgr) {
        // category label
        levelCat.setCatlabel(rtcCat.getLabel());
        // availability value for this category
        levelCat.setCatvalue(m_dataMgr.getValue(rtcCat, curTime, rWindow));
        // nodes in this category
        for (int nodeID : m_dataMgr.getNodes(rtcCat)) {
            Node levelNode = new Node();
            levelNode.setNodeid(nodeID);
            // value for this node for this category
            levelNode.setNodevalue(m_dataMgr.getValue(nodeID, rtcCat, curTime, rWindow));
            // node service count
            levelNode.setNodesvccount(m_dataMgr.getServiceCount(nodeID, rtcCat));
            // node service down count
            levelNode.setNodesvcdowncount(m_dataMgr.getServiceDownCount(nodeID, rtcCat));
            // add the node
            levelCat.getNode().add(levelNode);
        }
    }
    // add category
    level.getCategory().add(levelCat);
    return level;
}
Also used : RTCCategory(org.opennms.netmgt.rtc.datablock.RTCCategory) Node(org.opennms.netmgt.xml.rtc.Node) Date(java.util.Date) EuiLevel(org.opennms.netmgt.xml.rtc.EuiLevel)

Example 2 with EuiLevel

use of org.opennms.netmgt.xml.rtc.EuiLevel 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 3 with EuiLevel

use of org.opennms.netmgt.xml.rtc.EuiLevel 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 4 with EuiLevel

use of org.opennms.netmgt.xml.rtc.EuiLevel in project opennms by OpenNMS.

the class AvailabilityServiceHibernateImpl method getEuiLevel.

/**
     * Optimized method for calculating the category statistics.
     *
     * We start off by retrieving outages affecting the nodes and services
     * in the given category, and group these by node id.
     *
     * Using the outages, we calculate node-level statistics
     * and tally the values to calculate the category statistics.
     */
@Override
@Transactional(readOnly = true)
public synchronized EuiLevel getEuiLevel(RTCCategory category) {
    final Header header = new Header();
    header.setVer("1.9a");
    header.setMstation("");
    // current time
    final Date curDate = new Date();
    final long curTime = curDate.getTime();
    // get the rolling window
    final long rWindow = 24L * 60L * 60L * 1000L;
    LOG.debug("Retrieving availability statistics for {} with current date: {} and rolling window: {}", category.getLabel(), curDate, rWindow);
    // create the data
    final EuiLevel level = new EuiLevel();
    // set created in m_header and add to level
    header.setCreated(EventConstants.formatToString(curDate));
    level.setHeader(header);
    final Category levelCat = new Category();
    // category label
    levelCat.setCatlabel(category.getLabel());
    double outageTimeInCategory = 0.0;
    int numServicesInCategory = 0;
    // window bounds
    final Date windowStart = new Date(curTime - rWindow);
    final Date windowEnd = new Date(curTime);
    // category specifics
    final List<Integer> nodeIds = getNodes(category);
    final List<String> serviceNames = category.getServices();
    // retrieve the outages associated with the given nodes, only retrieving those that affect our window
    final Map<Integer, List<OnmsOutage>> outagesByNode = getOutages(nodeIds, serviceNames, windowStart, windowEnd);
    // calculate the node level statistics
    for (final int nodeId : nodeIds) {
        List<OnmsOutage> outages = outagesByNode.get(nodeId);
        if (outages == null) {
            outages = Lists.newArrayList();
        }
        // sum the outage time
        final double outageTime = getOutageTimeInWindow(outages, windowStart, windowEnd);
        // determine the number of services
        final int numServices = getNumServices(nodeId, serviceNames);
        // count the number of outstanding outages
        final long numServicesDown = outages.stream().filter(outage -> outage.getIfRegainedService() == null).count();
        final Node levelNode = new Node();
        levelNode.setNodeid(nodeId);
        // value for this node for this category
        levelNode.setNodevalue(RTCUtils.getOutagePercentage(outageTime, rWindow, numServices));
        // node service count
        levelNode.setNodesvccount(numServices);
        // node service down count
        levelNode.setNodesvcdowncount(numServicesDown);
        // add the node
        levelCat.getNode().add(levelNode);
        // update the category statistics
        numServicesInCategory += numServices;
        outageTimeInCategory += outageTime;
    }
    // calculate the outage percentage using tallied values
    levelCat.setCatvalue(RTCUtils.getOutagePercentage(outageTimeInCategory, rWindow, numServicesInCategory));
    // add category
    level.getCategory().add(levelCat);
    LOG.debug("Done retrieving availability statistics for {} with {} services.", category.getLabel(), numServicesInCategory);
    return level;
}
Also used : NullRestriction(org.opennms.core.criteria.restrictions.NullRestriction) AllRestriction(org.opennms.core.criteria.restrictions.AllRestriction) OnmsMonitoredService(org.opennms.netmgt.model.OnmsMonitoredService) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) EventConstants(org.opennms.netmgt.events.api.EventConstants) MonitoredServiceDao(org.opennms.netmgt.dao.api.MonitoredServiceDao) RTCCategory(org.opennms.netmgt.rtc.datablock.RTCCategory) Lists(com.google.common.collect.Lists) Map(java.util.Map) EuiLevel(org.opennms.netmgt.xml.rtc.EuiLevel) Node(org.opennms.netmgt.xml.rtc.Node) Header(org.opennms.netmgt.xml.rtc.Header) GtRestriction(org.opennms.core.criteria.restrictions.GtRestriction) Logger(org.slf4j.Logger) OutageDao(org.opennms.netmgt.dao.api.OutageDao) FilterDao(org.opennms.netmgt.filter.api.FilterDao) Category(org.opennms.netmgt.xml.rtc.Category) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) LeRestriction(org.opennms.core.criteria.restrictions.LeRestriction) List(java.util.List) OnmsOutage(org.opennms.netmgt.model.OnmsOutage) Preconditions(com.google.common.base.Preconditions) CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) Transactional(org.springframework.transaction.annotation.Transactional) OnmsOutage(org.opennms.netmgt.model.OnmsOutage) RTCCategory(org.opennms.netmgt.rtc.datablock.RTCCategory) Category(org.opennms.netmgt.xml.rtc.Category) Node(org.opennms.netmgt.xml.rtc.Node) Date(java.util.Date) Header(org.opennms.netmgt.xml.rtc.Header) List(java.util.List) EuiLevel(org.opennms.netmgt.xml.rtc.EuiLevel) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with EuiLevel

use of org.opennms.netmgt.xml.rtc.EuiLevel in project opennms by OpenNMS.

the class DataSender method subscribe.

/**
     * Subscribe - Add the received URL and related info to the category->URLs map
     * so the sendData() can send out to appropriate URLs for each category.
     * Also send the latest info for the category
     *
     * @param url a {@link java.lang.String} object.
     * @param catlabel a {@link java.lang.String} object.
     * @param user a {@link java.lang.String} object.
     * @param passwd a {@link java.lang.String} object.
     */
public synchronized void subscribe(final String url, final String catlabel, final String user, final String passwd) {
    // send category data to the newly subscribed URL
    // look up info for this category
    final RTCCategory cat = m_dataMgr.getCategories().get(catlabel);
    if (cat == null) {
        // oops! category for which we have no info!
        LOG.warn("RTC: No information available for category: {}", catlabel);
        return;
    }
    // create new HttpPostInfo
    final HttpPostInfo postInfo;
    try {
        postInfo = new HttpPostInfo(url, catlabel, user, passwd);
    } catch (final MalformedURLException mue) {
        LOG.warn("ERROR subscribing: Invalid URL '{}' - Data WILL NOT be SENT to the specified url", url);
        return;
    }
    // Add the URL to the list for the specified category
    Set<HttpPostInfo> urlList = m_catUrlMap.get(catlabel);
    if (urlList == null) {
        urlList = new HashSet<HttpPostInfo>();
        m_catUrlMap.put(catlabel, urlList);
    }
    if (!urlList.add(postInfo)) {
        LOG.debug("Already subscribed to URL: {}\tcatlabel: {}\tuser: {} - IGNORING LATEST subscribe event", url, catlabel, user);
    } else {
        LOG.debug("Subscribed to URL: {}\tcatlabel: {}\tuser:{}", url, catlabel, user);
    }
    try {
        m_dsrPool.execute(new Runnable() {

            @Override
            public void run() {
                // send data
                InputStream inp = null;
                try {
                    LOG.debug("DataSender: posting data to: {}", url);
                    final EuiLevel euidata = m_dataMgr.getEuiLevel(cat);
                    // Connect with a fairly long timeout to allow the web UI time to register the
                    // {@link RTCPostServlet}. Actually, this doesn't seem to work because the POST
                    // will immediately throw a {@link ConnectException} if the web UI isn't ready
                    // yet. Oh well.
                    final String marshaledUeiData = JaxbUtils.marshal(euidata);
                    try (final StringReader inr = new StringReader(marshaledUeiData)) {
                        inp = HttpUtils.post(postInfo.getURL(), inr, user, passwd, 8 * HttpUtils.DEFAULT_POST_BUFFER_SIZE, 60000);
                    }
                    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));
                            }
                        }
                    }
                    LOG.debug("DataSender: posted data for category: {}", catlabel);
                } catch (final ConnectException e) {
                    // These exceptions will be thrown if we try to POST RTC data before the web UI is available.
                    // Don't log a large stack trace for this because it will happen during startup before the
                    // RTCPostServlet is ready to handle requests.
                    LOG.warn("DataSender:  Unable to send category '{}' to URL '{}': {}", catlabel, url, e.getMessage());
                } catch (final Throwable t) {
                    LOG.warn("DataSender:  Unable to send category '{}' to URL '{}'", catlabel, url, t);
                } finally {
                    IOUtils.closeQuietly(inp);
                }
            }
        });
    } catch (RejectedExecutionException e) {
        LOG.warn("Unable to queue datasender. The task was rejected by the pool. Current queue size: {}.", m_queue.size(), e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) InputStream(java.io.InputStream) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) HttpPostInfo(org.opennms.netmgt.rtc.datablock.HttpPostInfo) StringReader(java.io.StringReader) EuiLevel(org.opennms.netmgt.xml.rtc.EuiLevel) RTCCategory(org.opennms.netmgt.rtc.datablock.RTCCategory) ConnectException(java.net.ConnectException)

Aggregations

RTCCategory (org.opennms.netmgt.rtc.datablock.RTCCategory)6 EuiLevel (org.opennms.netmgt.xml.rtc.EuiLevel)6 Date (java.util.Date)3 Category (org.opennms.netmgt.xml.rtc.Category)3 Node (org.opennms.netmgt.xml.rtc.Node)3 InputStream (java.io.InputStream)2 StringReader (java.io.StringReader)2 Test (org.junit.Test)2 OnmsMonitoredService (org.opennms.netmgt.model.OnmsMonitoredService)2 OnmsOutage (org.opennms.netmgt.model.OnmsOutage)2 HttpPostInfo (org.opennms.netmgt.rtc.datablock.HttpPostInfo)2 Preconditions (com.google.common.base.Preconditions)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 ConnectException (java.net.ConnectException)1 MalformedURLException (java.net.MalformedURLException)1 List (java.util.List)1 Map (java.util.Map)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 Collectors (java.util.stream.Collectors)1