Skip to content

Commit

Permalink
0002793: Add count and is_resolved to monitor events
Browse files Browse the repository at this point in the history
  • Loading branch information
erilong committed Sep 13, 2016
1 parent 58463ad commit c2dc885
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 23 deletions.
Expand Up @@ -37,10 +37,53 @@ public class MonitorEvent {
protected long threshold;

protected long value;

protected int count;

protected int severityLevel;

protected boolean isResolved;

protected boolean isNotified;

protected Date lastUpdateTime;

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((eventTime == null) ? 0 : eventTime.hashCode());
result = prime * result + ((monitorId == null) ? 0 : monitorId.hashCode());
result = prime * result + ((nodeId == null) ? 0 : nodeId.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MonitorEvent other = (MonitorEvent) obj;
if (eventTime == null) {
if (other.eventTime != null)
return false;
} else if (!eventTime.equals(other.eventTime))
return false;
if (monitorId == null) {
if (other.monitorId != null)
return false;
} else if (!monitorId.equals(other.monitorId))
return false;
if (nodeId == null) {
if (other.nodeId != null)
return false;
} else if (!nodeId.equals(other.nodeId))
return false;
return true;
}

public String getMonitorId() {
return monitorId;
Expand Down Expand Up @@ -114,4 +157,28 @@ public void setNotified(boolean isNotified) {
this.isNotified = isNotified;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public boolean isResolved() {
return isResolved;
}

public void setResolved(boolean isResolved) {
this.isResolved = isResolved;
}

public Date getLastUpdateTime() {
return lastUpdateTime;
}

public void setLastUpdateTime(Date lastUpdateTime) {
this.lastUpdateTime = lastUpdateTime;
}

}
Expand Up @@ -40,7 +40,7 @@ public interface IMonitorService {

public List<MonitorEvent> getMonitorEvents();

public List<MonitorEvent> getMonitorEventsFiltered(int limit, String type, int severityLevel, String nodeId);
public List<MonitorEvent> getMonitorEventsFiltered(int limit, String type, int severityLevel, String nodeId, boolean isResolved);

public void saveMonitorEvent(MonitorEvent notificationEvent);

Expand Down
Expand Up @@ -107,6 +107,7 @@ 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());
Expand All @@ -116,7 +117,7 @@ public synchronized void update() {
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);
updateMonitor(monitor, monitorType, identity, unresolved);
}
}
} else {
Expand All @@ -133,7 +134,7 @@ public synchronized void update() {
IMonitorType monitorType = monitorTypes.get(monitor.getType());
if (monitorType != null && monitorType.requiresClusterLock() &&
(System.currentTimeMillis() - clusterLastCheckTime) / 1000 >= monitor.getRunPeriod()) {
updateMonitor(monitor, monitorType, identity);
updateMonitor(monitor, monitorType, identity, unresolved);
}
}

Expand Down Expand Up @@ -172,7 +173,7 @@ public synchronized void update() {
}
}

protected void updateMonitor(Monitor monitor, IMonitorType monitorType, Node identity) {
protected void updateMonitor(Monitor monitor, IMonitorType monitorType, Node identity, Map<String, MonitorEvent> unresolved) {
long value = monitorType.check(monitor);
boolean readyToCompare = true;

Expand All @@ -198,17 +199,36 @@ protected void updateMonitor(Monitor monitor, IMonitorType monitorType, Node ide
}
}

if (readyToCompare && value >= monitor.getThreshold()) {
MonitorEvent event = new MonitorEvent();
event.setMonitorId(monitor.getMonitorId());
event.setNodeId(identity.getNodeId());
event.setEventTime(new Date((System.currentTimeMillis() / 1000) * 1000));
event.setHostName(hostName);
event.setType(monitor.getType());
event.setValue(value);
event.setThreshold(monitor.getThreshold());
event.setSeverityLevel(monitor.getSeverityLevel());
saveMonitorEvent(event);
if (readyToCompare) {
MonitorEvent event = unresolved.get(monitor.getMonitorId());
Date now = new Date((System.currentTimeMillis() / 1000) * 1000);
if (event != null && value < monitor.getThreshold()) {
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);
}
}
}
}

Expand Down Expand Up @@ -251,11 +271,22 @@ public List<MonitorEvent> getMonitorEvents() {
return sqlTemplate.query(getSql("selectMonitorEventSql"), new MonitorEventRowMapper());
}

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;
}

@Override
public List<MonitorEvent> getMonitorEventsFiltered(int limit, String type, int severityLevel, String nodeId) {
public List<MonitorEvent> getMonitorEventsFiltered(int limit, String type, int severityLevel, String nodeId, boolean isResolved) {
String sql = getSql("selectMonitorEventSql", "whereMonitorEventFilteredSql");
ArrayList<Object> args = new ArrayList<Object>();
args.add(severityLevel);
args.add(isResolved ? 1 : 0);
if (type != null) {
sql += " and type = ?";
args.add(type);
Expand All @@ -275,9 +306,22 @@ protected List<MonitorEvent> getMonitorEventsForNotification(int severityLevel)

@Override
public void saveMonitorEvent(MonitorEvent event) {
if (!updateMonitorEvent(event)) {
insertMonitorEvent(event);
}
}

protected void insertMonitorEvent(MonitorEvent event) {
sqlTemplate.update(getSql("insertMonitorEventSql"), event.getMonitorId(), event.getNodeId(),
event.getEventTime(), event.getHostName(), event.getType(), event.getValue(), event.getThreshold(),
event.getSeverityLevel(), event.isNotified() ? 1 : 0);
event.getEventTime(), event.getHostName(), event.getType(), event.getValue(), event.getCount(), event.getThreshold(),
event.getSeverityLevel(), event.isResolved() ? 1 : 0, event.isNotified() ? 1 : 0, event.getLastUpdateTime());
}

protected boolean updateMonitorEvent(MonitorEvent event) {
int count = sqlTemplate.update(getSql("updateMonitorEventSql"), event.getHostName(), event.getType(), event.getValue(),
event.getCount(), event.getThreshold(), event.getSeverityLevel(), event.getLastUpdateTime(),
event.getMonitorId(), event.getNodeId(), event.getEventTime());
return count != 0;
}

@Override
Expand All @@ -295,6 +339,10 @@ protected void updateMonitorEventAsNotified(MonitorEvent event) {
sqlTemplate.update(getSql("updateMonitorEventNotifiedSql"), event.getMonitorId(), event.getNodeId(), event.getEventTime());
}

protected void updateMonitorEventAsResolved(MonitorEvent event) {
sqlTemplate.update(getSql("updateMonitorEventResolvedSql"), event.getMonitorId(), event.getNodeId(), event.getEventTime());
}

@Override
public List<Notification> getNotifications() {
return sqlTemplate.query(getSql("selectNotificationSql"), new NotificationRowMapper());
Expand Down Expand Up @@ -371,8 +419,11 @@ public MonitorEvent mapRow(Row row) {
m.setType(row.getString("type"));
m.setThreshold(row.getLong("threshold"));
m.setValue(row.getLong("event_value"));
m.setCount(row.getInt("event_count"));
m.setSeverityLevel(row.getInt("severity_level"));
m.setResolved(row.getBoolean("is_resolved"));
m.setNotified(row.getBoolean("is_notified"));
m.setLastUpdateTime(row.getDateTime("last_update_time"));
return m;
}
}
Expand Down
Expand Up @@ -54,19 +54,30 @@ public MonitorServiceSqlMap(IDatabasePlatform platform, Map<String, String> repl
// Monitor Events

putSql("selectMonitorEventSql",
"select monitor_id, node_id, event_time, type, event_value, threshold, severity_level, host_name, is_notified " +
"select monitor_id, node_id, event_time, type, event_value, event_count, threshold, severity_level, host_name, " +
"is_resolved, is_notified, last_update_time " +
"from $(monitor_event) ");

putSql("whereMonitorEventFilteredSql", "where severity_level >= ?");
putSql("whereMonitorEventNotResolvedSql", "where node_id = ? and is_resolved = 0");

putSql("whereMonitorEventFilteredSql", "where severity_level >= ? and is_resolved = ?");

putSql("whereMonitorEventForNotificationBySeveritySql",
"where is_notified = 0 and severity_level >= ?");

putSql("insertMonitorEventSql",
"insert into $(monitor_event) " +
"(monitor_id, node_id, event_time, host_name, type, event_value, threshold, severity_level, is_notified) " +
"values (?, ?, ?, ?, ?, ?, ?, ?, ?)");

"(monitor_id, node_id, event_time, host_name, type, event_value, event_count, threshold, severity_level, " +
"is_resolved, is_notified, last_update_time) " +
"values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

putSql("updateMonitorEventSql",
"update $(monitor_event) set host_name = ?, type = ?, event_value = ?, event_count = ?, threshold = ?, severity_level = ?, " +
"is_resolved = 0, last_update_time = ? where monitor_id = ? and node_id = ? and event_time = ?");

putSql("updateMonitorEventResolvedSql",
"update $(monitor_event) set is_resolved = 1, last_update_time = ? where monitor_id = ? and node_id = ? and event_time = ?");

putSql("updateMonitorEventNotifiedSql",
"update $(monitor_event) set is_notified = 1 where monitor_id = ? and node_id = ? and event_time = ?");

Expand Down
3 changes: 3 additions & 0 deletions symmetric-core/src/main/resources/symmetric-schema.xml
Expand Up @@ -546,8 +546,11 @@
<column name="type" type="VARCHAR" size="50" required="true" description="Monitor type that detected the value recorded." />
<column name="threshold" type="BIGINT" required="true" default="0" description="Minimum value for the monitor to cause an event." />
<column name="event_value" type="BIGINT" required="true" default="0" description="Actual value detected by the monitor." />
<column name="event_count" type="INTEGER" required="true" default="0" description="Number of times this event has occurred and been updated." />
<column name="severity_level" type="INTEGER" required="true" default="0" description="Severity level configured for the monitor." />
<column name="is_resolved" type="BOOLEANINT" required="true" default="0" description="Whether an event is resolved because its value dropped below the threshold." />
<column name="is_notified" type="BOOLEANINT" required="true" default="0" description="Whether a notification was run." />
<column name="last_update_time" type="TIMESTAMP" description="Timestamp when the event was last updated." />
</table>

<table name="notification" description="Defines what notification to send when a monitor detects a problem in the system.">
Expand Down

0 comments on commit c2dc885

Please sign in to comment.