use of org.opennms.netmgt.model.OnmsMonitoredService in project opennms by OpenNMS.
the class DefaultAdminApplicationService method findServiceApplications.
/**
* {@inheritDoc}
*/
@Override
public ServiceEditModel findServiceApplications(String ifServiceIdString) {
if (ifServiceIdString == null) {
throw new IllegalArgumentException("ifServiceIdString must not be null");
}
OnmsMonitoredService service = findService(ifServiceIdString);
List<OnmsApplication> applications = findAllApplications();
m_monitoredServiceDao.initialize(service.getIpInterface());
m_monitoredServiceDao.initialize(service.getIpInterface().getNode());
return new ServiceEditModel(service, applications);
}
use of org.opennms.netmgt.model.OnmsMonitoredService in project opennms by OpenNMS.
the class DefaultDistributedStatusService method calculatePercentageUptime.
/**
* Calculate the percentage of time that all services are up for this
* application on this remote monitor.
*
* @param applicationServices services to report on
* @param statuses status entries to use for report
* @param startDate start date. The report starts on this date.
* @param endDate end date. The report ends the last millisecond prior
* this date.
* @return representation of the percentage uptime out to three decimal
* places. Null is returned if there is no data.
*/
public String calculatePercentageUptime(Collection<OnmsMonitoredService> applicationServices, Collection<OnmsLocationSpecificStatus> statuses, Date startDate, Date endDate) {
/*
* The methodology is as such:
* 1) Sort the status entries by their timestamp;
* 2) Create a Map of each monitored service with a default
* PollStatus of unknown.
* 3) Iterate through the sorted list of status entries until
* we hit a timestamp that is not within our time range or
* run out of entries.
* a) Along the way, update the status Map with the current
* entry's status, and calculate the current status.
* b) If the current timestamp is before the start time, store
* the current status so we can use it once we cross over
* into our time range and then continue.
* c) If the previous status is normal, then count up the number
* of milliseconds since the previous state change entry in
* the time range (or the beginning of the range if this is
* the first entry in within the time range), and add that
* a counter of "normal" millseconds.
* d) Finally, save the current date and status for later use.
* 4) Perform the same computation in 3c, except count the number
* of milliseconds since the last state change entry (or the
* start time if there were no entries) and the end time, and add
* that to the counter of "normal" milliseconds.
* 5) Divide the "normal" milliseconds counter by the total number
* of milliseconds in our time range and compute and return a
* percentage.
*/
List<OnmsLocationSpecificStatus> sortedStatuses = new LinkedList<OnmsLocationSpecificStatus>(statuses);
Collections.sort(sortedStatuses, new Comparator<OnmsLocationSpecificStatus>() {
@Override
public int compare(OnmsLocationSpecificStatus o1, OnmsLocationSpecificStatus o2) {
return o1.getPollResult().getTimestamp().compareTo(o2.getPollResult().getTimestamp());
}
});
HashMap<OnmsMonitoredService, PollStatus> serviceStatus = new HashMap<OnmsMonitoredService, PollStatus>();
for (OnmsMonitoredService service : applicationServices) {
serviceStatus.put(service, PollStatus.unknown("No history for this service from this location"));
}
float normalMilliseconds = 0f;
Date lastDate = startDate;
Severity lastStatus = Severity.CRITICAL;
for (OnmsLocationSpecificStatus status : sortedStatuses) {
Date currentDate = status.getPollResult().getTimestamp();
if (!currentDate.before(endDate)) {
// We're at or past the end date, so we're done processing
break;
}
serviceStatus.put(status.getMonitoredService(), status.getPollResult());
Severity currentStatus = calculateStatus(serviceStatus.values());
if (currentDate.before(startDate)) {
/*
* We're not yet to a date that is inside our time period, so
* we don't need to check the status and adjust the
* normalMilliseconds variable, but we do need to save the
* status so we have an up-to-date status when we cross the
* start date.
*/
lastStatus = currentStatus;
continue;
}
/*
* Because we *just* had a state change, we want to look at the
* value of the *last* status.
*/
if (lastStatus == Severity.NORMAL) {
long milliseconds = currentDate.getTime() - lastDate.getTime();
normalMilliseconds += milliseconds;
}
lastDate = currentDate;
lastStatus = currentStatus;
}
if (lastStatus == Severity.NORMAL) {
long milliseconds = endDate.getTime() - lastDate.getTime();
normalMilliseconds += milliseconds;
}
float percentage = normalMilliseconds / (endDate.getTime() - startDate.getTime()) * 100;
return new DecimalFormat("0.000").format((double) percentage) + "%";
}
use of org.opennms.netmgt.model.OnmsMonitoredService in project opennms by OpenNMS.
the class DefaultDistributedStatusService method findLocationSpecificStatus.
/**
* <p>findLocationSpecificStatus</p>
*
* @param command a {@link org.opennms.web.command.DistributedStatusDetailsCommand} object.
* @param errors a {@link org.springframework.validation.Errors} object.
* @return a {@link java.util.List} object or null if no location monitors are registered for the specified location and application tuple
*/
protected List<OnmsLocationSpecificStatus> findLocationSpecificStatus(DistributedStatusDetailsCommand command, Errors errors) throws IllegalArgumentException {
String locationName = command.getLocation();
String applicationName = command.getApplication();
Assert.notNull(locationName, "location cannot be null");
Assert.notNull(applicationName, "application cannot be null");
OnmsMonitoringLocation location = m_monitoringLocationDao.get(locationName);
if (location == null) {
throw new IllegalArgumentException("Could not find location for " + "location name \"" + locationName + "\"");
}
OnmsApplication application = m_applicationDao.findByName(applicationName);
if (application == null) {
throw new IllegalArgumentException("Could not find application " + "for application name \"" + applicationName + "\"");
}
Collection<OnmsLocationMonitor> locationMonitors = m_locationMonitorDao.findByLocationDefinition(location);
if (locationMonitors.size() == 0) {
errors.reject("location.no-monitors", new Object[] { applicationName, locationName }, "No remote pollers have registered for this " + "application and location");
return null;
}
List<OnmsLocationMonitor> sortedLocationMonitors = new ArrayList<OnmsLocationMonitor>(locationMonitors);
Collections.sort(sortedLocationMonitors);
Collection<OnmsMonitoredService> services = m_monitoredServiceDao.findByApplication(application);
List<OnmsMonitoredService> sortedServices = new ArrayList<OnmsMonitoredService>(services);
Collections.sort(sortedServices);
List<OnmsLocationSpecificStatus> status = new LinkedList<>();
for (OnmsMonitoredService service : sortedServices) {
for (OnmsLocationMonitor locationMonitor : sortedLocationMonitors) {
OnmsLocationSpecificStatus currentStatus = m_locationMonitorDao.getMostRecentStatusChange(locationMonitor, service);
if (currentStatus == null) {
status.add(new OnmsLocationSpecificStatus(locationMonitor, service, NO_RECORDED_STATUS));
} else {
status.add(currentStatus);
}
}
}
return status;
}
use of org.opennms.netmgt.model.OnmsMonitoredService in project opennms by OpenNMS.
the class DefaultDistributedStatusService method createHistoryModel.
/**
* {@inheritDoc}
*/
@Override
public DistributedStatusHistoryModel createHistoryModel(String locationName, String monitorId, String applicationName, String timeSpan, String previousLocationName) {
List<String> errors = new LinkedList<>();
List<OnmsMonitoringLocation> locationDefinitions = m_monitoringLocationDao.findAll();
List<RelativeTimePeriod> periods = Arrays.asList(RelativeTimePeriod.getDefaultPeriods());
Collection<OnmsApplication> applications = m_applicationDao.findAll();
List<OnmsApplication> sortedApplications = new ArrayList<OnmsApplication>(applications);
Collections.sort(sortedApplications);
OnmsMonitoringLocation location = new OnmsMonitoringLocation();
if (locationName == null) {
if (!locationDefinitions.isEmpty()) {
location = locationDefinitions.get(0);
}
} else {
location = m_monitoringLocationDao.get(locationName);
if (location == null) {
errors.add("Could not find location definition '" + locationName + "'");
if (!locationDefinitions.isEmpty()) {
location = locationDefinitions.get(0);
}
}
}
OnmsApplication application = new OnmsApplication();
if (applicationName == null) {
if (!sortedApplications.isEmpty()) {
application = sortedApplications.get(0);
}
} else {
application = m_applicationDao.findByName(applicationName);
if (application == null) {
errors.add("Could not find application '" + applicationName + "'");
if (!sortedApplications.isEmpty()) {
application = sortedApplications.get(0);
}
}
}
Collection<OnmsLocationMonitor> monitors = m_locationMonitorDao.findByLocationDefinition(location);
List<OnmsLocationMonitor> sortedMonitors = new LinkedList<OnmsLocationMonitor>(monitors);
Collections.sort(sortedMonitors);
OnmsLocationMonitor monitor = null;
if (monitorId != null && !"".equals(monitorId.trim()) && location.getLocationName().equals(previousLocationName)) {
for (OnmsLocationMonitor m : sortedMonitors) {
if (m.getId().equals(monitorId)) {
monitor = m;
break;
}
}
}
if (monitor == null && !sortedMonitors.isEmpty()) {
monitor = sortedMonitors.get(0);
}
RelativeTimePeriod period = RelativeTimePeriod.getPeriodByIdOrDefault(timeSpan);
/*
* Initialize the hierarchy under the service so that we don't get
* a LazyInitializationException later when the JSP page is pulling
* data out of the model object.
*/
Collection<OnmsMonitoredService> memberServices = m_monitoredServiceDao.findByApplication(application);
for (OnmsMonitoredService service : memberServices) {
m_locationMonitorDao.initialize(service.getIpInterface());
m_locationMonitorDao.initialize(service.getIpInterface().getNode());
}
Collection<OnmsMonitoredService> applicationMemberServices = m_monitoredServiceDao.findByApplication(application);
if (applicationMemberServices.isEmpty()) {
errors.add("There are no services in the application '" + applicationName + "'");
}
DistributedStatusHistoryModel model = new DistributedStatusHistoryModel(locationDefinitions, sortedApplications, sortedMonitors, periods, location, application, applicationMemberServices, monitor, period, errors);
initializeGraphUrls(model);
return model;
}
use of org.opennms.netmgt.model.OnmsMonitoredService in project opennms by OpenNMS.
the class DefaultDistributedStatusService method createFacilityStatusTable.
/**
* {@inheritDoc}
*/
@Override
public SimpleWebTable createFacilityStatusTable(Date start, Date end) {
Assert.notNull(start, "argument start cannot be null");
Assert.notNull(end, "argument end cannot be null");
if (!start.before(end)) {
throw new IllegalArgumentException("start date (" + start + ") must be older than end date (" + end + ")");
}
SimpleWebTable table = new SimpleWebTable();
List<OnmsMonitoringLocation> locationDefinitions = m_monitoringLocationDao.findAll();
Collection<OnmsApplication> applications = m_applicationDao.findAll();
if (applications.size() == 0) {
throw new IllegalArgumentException("there are no applications");
}
List<OnmsApplication> sortedApplications = new ArrayList<OnmsApplication>(applications);
Collections.sort(sortedApplications);
Collection<OnmsLocationSpecificStatus> mostRecentStatuses = m_locationMonitorDao.getAllMostRecentStatusChanges();
Collection<OnmsLocationSpecificStatus> statusesPeriod = new HashSet<>();
statusesPeriod.addAll(m_locationMonitorDao.getAllStatusChangesAt(start));
statusesPeriod.addAll(m_locationMonitorDao.getStatusChangesBetween(start, end));
table.setTitle("Distributed Status Summary");
table.addColumn("Area", "");
table.addColumn("Location", "");
for (OnmsApplication application : sortedApplications) {
table.addColumn(application.getName(), "");
}
for (OnmsMonitoringLocation locationDefinition : locationDefinitions) {
Collection<OnmsLocationMonitor> monitors = m_locationMonitorDao.findByLocationDefinition(locationDefinition);
table.newRow();
table.addCell(locationDefinition.getMonitoringArea(), "");
table.addCell(locationDefinition.getLocationName(), "");
for (OnmsApplication application : sortedApplications) {
Collection<OnmsMonitoredService> memberServices = m_monitoredServiceDao.findByApplication(application);
Severity status = calculateCurrentStatus(monitors, memberServices, mostRecentStatuses);
Set<OnmsLocationSpecificStatus> selectedStatuses = filterStatus(statusesPeriod, monitors, memberServices);
if (selectedStatuses.size() > 0) {
String percentage = calculatePercentageUptime(memberServices, selectedStatuses, start, end);
table.addCell(percentage, status.getStyle(), createHistoryPageUrl(locationDefinition, application));
} else {
table.addCell("No data", status.getStyle());
}
}
}
if (isLayoutApplicationsVertically()) {
SimpleWebTable newTable = new SimpleWebTable();
newTable.setErrors(table.getErrors());
newTable.setTitle(table.getTitle());
newTable.addColumn("Application");
for (List<Cell> row : table.getRows()) {
// The location is in the second row
newTable.addColumn(row.get(1).getContent(), row.get(1).getStyleClass());
}
for (Cell columnHeader : table.getColumnHeaders().subList(2, table.getColumnHeaders().size())) {
// This is the index into collumn list of the old table to get the data for the current application
int rowColumnIndex = newTable.getRows().size() + 2;
newTable.newRow();
newTable.addCell(columnHeader.getContent(), columnHeader.getStyleClass());
for (List<Cell> row : table.getRows()) {
newTable.addCell(row.get(rowColumnIndex).getContent(), row.get(rowColumnIndex).getStyleClass(), row.get(rowColumnIndex).getLink());
}
}
return newTable;
}
return table;
}
Aggregations