use of org.jumpmind.symmetric.model.MonitorEvent in project symmetric-ds by JumpMind.
the class MonitorService method getMonitorEventsNotResolvedForNode.
protected Map<String, MonitorEvent> getMonitorEventsNotResolvedForNode(String nodeId) {
List<MonitorEvent> list = sqlTemplate.query(getSql("selectMonitorEventSql", "whereMonitorEventNotResolvedSql"), new MonitorEventRowMapper(), nodeId);
Map<String, MonitorEvent> map = new HashMap<String, MonitorEvent>();
for (MonitorEvent monitorEvent : list) {
map.put(monitorEvent.getMonitorId(), monitorEvent);
}
return map;
}
use of org.jumpmind.symmetric.model.MonitorEvent in project symmetric-ds by JumpMind.
the class MonitorService method update.
@Override
public synchronized void update() {
Map<String, IMonitorType> monitorTypes = extensionService.getExtensionPointMap(IMonitorType.class);
Node identity = nodeService.findIdentity();
List<Monitor> activeMonitors = getActiveMonitorsForNode(identity.getNodeGroupId(), identity.getExternalId());
Map<String, MonitorEvent> unresolved = getMonitorEventsNotResolvedForNode(identity.getNodeId());
for (Monitor monitor : activeMonitors) {
IMonitorType monitorType = monitorTypes.get(monitor.getType());
if (monitorType != null) {
if (!monitorType.requiresClusterLock()) {
Long lastCheckTimeLong = checkTimesByType.get(monitor.getMonitorId());
long lastCheckTime = lastCheckTimeLong != null ? lastCheckTimeLong : 0;
if (lastCheckTime == 0 || (System.currentTimeMillis() - lastCheckTime) / 1000 >= monitor.getRunPeriod()) {
checkTimesByType.put(monitor.getMonitorId(), System.currentTimeMillis());
updateMonitor(monitor, monitorType, identity, unresolved);
}
}
} else {
log.warn("Could not find monitor of type '" + monitor.getType() + "'");
}
}
if (clusterService.lock(ClusterConstants.MONITOR)) {
Lock lock = clusterService.findLocks().get(ClusterConstants.MONITOR);
long clusterLastCheckTime = lock.getLastLockTime() != null ? lock.getLastLockTime().getTime() : 0;
try {
for (Monitor monitor : activeMonitors) {
IMonitorType monitorType = monitorTypes.get(monitor.getType());
if (monitorType != null && monitorType.requiresClusterLock() && (System.currentTimeMillis() - clusterLastCheckTime) / 1000 >= monitor.getRunPeriod()) {
updateMonitor(monitor, monitorType, identity, unresolved);
}
}
int minSeverityLevel = Integer.MAX_VALUE;
List<Notification> notifications = getActiveNotificationsForNode(identity.getNodeGroupId(), identity.getExternalId());
if (notifications.size() > 0) {
for (Notification notification : notifications) {
if (notification.getSeverityLevel() < minSeverityLevel) {
minSeverityLevel = notification.getSeverityLevel();
}
}
Map<String, INotificationType> notificationTypes = extensionService.getExtensionPointMap(INotificationType.class);
List<MonitorEvent> allMonitorEvents = getMonitorEventsForNotification(minSeverityLevel);
for (Notification notification : notifications) {
List<MonitorEvent> monitorEvents = new ArrayList<MonitorEvent>();
for (MonitorEvent monitorEvent : allMonitorEvents) {
if (monitorEvent.getSeverityLevel() >= notification.getSeverityLevel()) {
monitorEvents.add(monitorEvent);
}
}
if (monitorEvents.size() > 0) {
INotificationType notificationType = notificationTypes.get(notification.getType());
if (notificationType != null) {
notificationType.notify(notification, monitorEvents);
updateMonitorEventAsNotified(monitorEvents);
} else {
log.warn("Could not find notification of type '" + notification.getType() + "'");
}
}
}
}
} finally {
clusterService.unlock(ClusterConstants.MONITOR);
}
}
}
use of org.jumpmind.symmetric.model.MonitorEvent in project symmetric-ds by JumpMind.
the class NotificationTypeEmail method notify.
public void notify(Notification notification, List<MonitorEvent> monitorEvents) {
String subject = null;
if (monitorEvents.size() == 1) {
MonitorEvent event = monitorEvents.get(0);
subject = "Monitor event for " + event.getType() + " from node " + event.getNodeId();
} else {
Set<String> nodeIds = new HashSet<String>();
Set<String> types = new HashSet<String>();
for (MonitorEvent event : monitorEvents) {
nodeIds.add(event.getNodeId());
types.add(event.getType());
}
StringBuilder typesString = new StringBuilder();
Iterator<String> iter = types.iterator();
while (iter.hasNext()) {
typesString.append(iter.next());
if (iter.hasNext()) {
typesString.append(", ");
}
}
subject = "Monitor events for " + typesString + " from " + nodeIds.size() + " nodes";
}
Map<String, Node> nodes = engine.getNodeService().findAllNodesAsMap();
StringBuilder text = new StringBuilder();
for (MonitorEvent event : monitorEvents) {
Node node = nodes.get(event.getNodeId());
String nodeString = node != null ? node.toString() : event.getNodeId();
text.append(DATE_FORMATTER.format(event.getEventTime())).append(" [");
text.append(Monitor.getSeverityLevelNames().get(event.getSeverityLevel())).append("] [");
text.append(nodeString).append("] [");
text.append(event.getHostName()).append("] ");
text.append("Monitor event for ").append(event.getType());
text.append(" reached threshold of ").append(event.getThreshold());
text.append(" with a value of ").append(event.getValue()).append("\n");
}
String recipients = notification.getExpression();
if (recipients != null) {
log.info("Sending email with subject '" + subject + "' to " + recipients);
engine.getMailService().sendEmail(subject, text.toString(), recipients);
} else {
log.warn("Notification " + notification.getNotificationId() + " has no email recipients configured.");
}
}
use of org.jumpmind.symmetric.model.MonitorEvent in project symmetric-ds by JumpMind.
the class NotificationTypeLog method notify.
public void notify(Notification notification, List<MonitorEvent> monitorEvents) {
Map<String, Node> nodes = engine.getNodeService().findAllNodesAsMap();
for (MonitorEvent monitorEvent : monitorEvents) {
Node node = nodes.get(monitorEvent.getNodeId());
String nodeString = node != null ? node.toString() : monitorEvent.getNodeId();
String message = "Monitor " + monitorEvent.getType() + " on " + nodeString + " reached threshold of " + monitorEvent.getThreshold() + " with a value of " + monitorEvent.getValue();
if (monitorEvent.getSeverityLevel() >= Monitor.SEVERE) {
log.error(message);
} else if (monitorEvent.getSeverityLevel() >= Monitor.WARNING) {
log.warn(message);
} else {
log.info(message);
}
}
}
use of org.jumpmind.symmetric.model.MonitorEvent in project symmetric-ds by JumpMind.
the class MonitorService method updateMonitor.
protected void updateMonitor(Monitor monitor, IMonitorType monitorType, Node identity, Map<String, MonitorEvent> unresolved) {
long value = monitorType.check(monitor);
boolean readyToCompare = true;
if (!monitorType.requiresClusterLock() && monitor.getRunCount() > 0) {
List<Long> averages = averagesByType.get(monitor.getType());
if (averages == null) {
averages = new ArrayList<Long>();
averagesByType.put(monitor.getType(), averages);
}
averages.add(value);
while (averages.size() > monitor.getRunCount()) {
averages.remove(0);
}
if (averages.size() == monitor.getRunCount()) {
long accumValue = 0;
for (Long oneValue : averages) {
accumValue += oneValue;
}
value = accumValue / monitor.getRunCount();
} else {
readyToCompare = false;
}
}
if (readyToCompare) {
MonitorEvent event = unresolved.get(monitor.getMonitorId());
Date now = new Date((System.currentTimeMillis() / 1000) * 1000);
if (event != null && value < monitor.getThreshold()) {
event.setLastUpdateTime(now);
updateMonitorEventAsResolved(event);
} else if (value >= monitor.getThreshold()) {
if (event == null) {
event = new MonitorEvent();
event.setMonitorId(monitor.getMonitorId());
event.setNodeId(identity.getNodeId());
event.setEventTime(now);
event.setHostName(hostName);
event.setType(monitor.getType());
event.setValue(value);
event.setCount(1);
event.setThreshold(monitor.getThreshold());
event.setSeverityLevel(monitor.getSeverityLevel());
event.setLastUpdateTime(now);
insertMonitorEvent(event);
} else {
event.setHostName(hostName);
event.setType(monitor.getType());
event.setValue(value);
event.setCount(event.getCount() + 1);
event.setThreshold(monitor.getThreshold());
event.setSeverityLevel(monitor.getSeverityLevel());
event.setLastUpdateTime(now);
saveMonitorEvent(event);
}
}
}
}
Aggregations