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;
}
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());
}
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());
}
}
}
}
}
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;
}
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);
}
}
Aggregations