Skip to content

Commit

Permalink
check the stat thresholds and publish jmx notification
Browse files Browse the repository at this point in the history
  • Loading branch information
chenson42 committed Jul 17, 2008
1 parent 9154c77 commit c35f739
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 54 deletions.
@@ -1,29 +1,57 @@
/*
* SymmetricDS is an open source database synchronization solution.
*
* Copyright (C) Chris Henson <chenson42@users.sourceforge.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*/
package org.jumpmind.symmetric.model;

import java.math.BigDecimal;

import javax.management.Notification;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.jumpmind.symmetric.statistic.Statistic;

public class StatisticAlertThresholds {

String statisticName;
BigDecimal threshholdTotalMax;
Long threshholdCountMax;
BigDecimal threshholdTotalMin;
Long threshholdCountMin;
BigDecimal thresholdTotalMax;
Long thresholdCountMax;
BigDecimal thresholdTotalMin;
Long thresholdCountMin;
BigDecimal thresholdAvgMax;
BigDecimal thresholdAvgMin;
static long sequenceNumber = System.currentTimeMillis();

public StatisticAlertThresholds() {
}

public StatisticAlertThresholds(String statisticName, BigDecimal threshholdTotalMax, Long threshholdCountMax,
BigDecimal threshholdTotalMin, Long threshholdCountMin) {
BigDecimal threshholdTotalMin, Long threshholdCountMin, BigDecimal threshholdAvgMax,
BigDecimal threshholdAvgMin) {
super();
this.statisticName = statisticName;
this.threshholdTotalMax = threshholdTotalMax;
this.threshholdCountMax = threshholdCountMax;
this.threshholdTotalMin = threshholdTotalMin;
this.threshholdCountMin = threshholdCountMin;
this.thresholdTotalMax = threshholdTotalMax;
this.thresholdCountMax = threshholdCountMax;
this.thresholdTotalMin = threshholdTotalMin;
this.thresholdCountMin = threshholdCountMin;
this.thresholdAvgMax = threshholdAvgMax;
this.thresholdAvgMin = threshholdAvgMin;
}

public String getStatisticName() {
Expand All @@ -34,42 +62,100 @@ public void setStatisticName(String statisticName) {
this.statisticName = statisticName;
}

public BigDecimal getThreshholdTotalMax() {
return threshholdTotalMax;
public BigDecimal getThresholdTotalMax() {
return thresholdTotalMax == null ? BigDecimal.ZERO : thresholdTotalMax;
}

public void setThresholdTotalMax(BigDecimal threshholdTotalMax) {
this.thresholdTotalMax = threshholdTotalMax;
}

public Long getThresholdCountMax() {
return thresholdCountMax == null ? 0l : thresholdCountMax;
}

public void setThresholdCountMax(Long threshholdCountMax) {
this.thresholdCountMax = threshholdCountMax;
}

public void setThreshholdTotalMax(BigDecimal threshholdTotalMax) {
this.threshholdTotalMax = threshholdTotalMax;
public BigDecimal getThresholdTotalMin() {
return thresholdTotalMin == null ? BigDecimal.ZERO : thresholdTotalMin;
}

public Long getThreshholdCountMax() {
return threshholdCountMax;
public void setThresholdTotalMin(BigDecimal threshholdTotalMin) {
this.thresholdTotalMin = threshholdTotalMin;
}

public void setThreshholdCountMax(Long threshholdCountMax) {
this.threshholdCountMax = threshholdCountMax;
public Long getThresholdCountMin() {
return thresholdCountMin == null ? 0l : thresholdCountMin;
}

public void setThresholdCountMin(Long threshholdCountMin) {
this.thresholdCountMin = threshholdCountMin;
}

public Notification outsideOfBoundsNotification(Statistic stats) {
if (stats != null && stats.getName().name().equals(statisticName)) {
boolean createNotification = false;
StringBuilder msg = new StringBuilder(statisticName);
long count = stats.getCount();
BigDecimal total = stats.getTotal();
BigDecimal avg = stats.getAverageValue();
if ((thresholdCountMax != null && thresholdCountMax > 0 && count > thresholdCountMax)
|| (thresholdCountMin != null && thresholdCountMin > 0 && count < thresholdCountMin)) {
msg.append(":count=");
msg.append(count);
createNotification = true;
}

if ((thresholdTotalMax != null && thresholdTotalMax.compareTo(BigDecimal.ZERO) > 0 && total
.compareTo(thresholdTotalMax) > 0)
|| (thresholdTotalMin != null && thresholdTotalMin.compareTo(BigDecimal.ZERO) > 0 && total
.compareTo(thresholdTotalMin) < 0)) {
msg.append(":total=");
msg.append(total);
createNotification = true;
}

if ((thresholdAvgMax != null && thresholdAvgMax.compareTo(BigDecimal.ZERO) > 0 && avg
.compareTo(thresholdAvgMax) > 0)
|| (thresholdAvgMin != null && thresholdAvgMin.compareTo(BigDecimal.ZERO) > 0 && avg
.compareTo(thresholdAvgMin) < 0)) {
msg.append(":avg=");
msg.append(avg);
createNotification = true;
}

if (createNotification) {
return new Notification("SymmetricDS:Alert", stats, sequenceNumber++, System.currentTimeMillis(), msg
.toString());
}

}
return null;
}

public BigDecimal getThreshholdTotalMin() {
return threshholdTotalMin;
public BigDecimal getThresholdAvgMax() {
return thresholdAvgMax == null ? BigDecimal.ZERO : thresholdAvgMax;
}

public void setThreshholdTotalMin(BigDecimal threshholdTotalMin) {
this.threshholdTotalMin = threshholdTotalMin;
public void setThresholdAvgMax(BigDecimal threshholdAvgMax) {
this.thresholdAvgMax = threshholdAvgMax;
}

public Long getThreshholdCountMin() {
return threshholdCountMin;
public BigDecimal getThresholdAvgMin() {
return thresholdAvgMin == null ? BigDecimal.ZERO : thresholdAvgMin;
}

public void setThreshholdCountMin(Long threshholdCountMin) {
this.threshholdCountMin = threshholdCountMin;
public void setThresholdAvgMin(BigDecimal threshholdAvgMin) {
this.thresholdAvgMin = threshholdAvgMin;
}

@Override
public int hashCode() {
return new HashCodeBuilder(17, 37).append(statisticName).append(threshholdTotalMax).append(threshholdCountMax)
.append(threshholdTotalMin).append(threshholdCountMin).toHashCode();
return new HashCodeBuilder(17, 37).append(statisticName).append(getThresholdTotalMax()).append(
getThresholdCountMax()).append(getThresholdTotalMin()).append(getThresholdCountMin()).append(
getThresholdAvgMax()).append(getThresholdAvgMin()).toHashCode();

}

Expand All @@ -82,10 +168,11 @@ public boolean equals(Object obj) {
return true;
}
StatisticAlertThresholds rhs = (StatisticAlertThresholds) obj;
return new EqualsBuilder().append(statisticName, rhs.statisticName).append(
threshholdTotalMax, rhs.threshholdTotalMax).append(threshholdCountMax, rhs.threshholdCountMax).append(
threshholdTotalMin, rhs.threshholdTotalMin).append(threshholdCountMin, rhs.threshholdCountMin)
.isEquals();
return new EqualsBuilder().append(statisticName, rhs.statisticName).append(getThresholdTotalMax(),
rhs.getThresholdTotalMax()).append(getThresholdCountMax(), rhs.getThresholdCountMax()).append(
getThresholdTotalMin(), rhs.getThresholdTotalMin()).append(getThresholdCountMin(),
rhs.getThresholdCountMin()).append(getThresholdAvgMax(), rhs.getThresholdAvgMax()).append(
getThresholdAvgMin(), rhs.getThresholdAvgMin()).isEquals();
}

}
Expand Up @@ -49,25 +49,27 @@ public List<StatisticAlertThresholds> getAlertThresholds() {
new ParameterizedRowMapper<StatisticAlertThresholds>() {
public StatisticAlertThresholds mapRow(ResultSet rs, int rowNum) throws SQLException {
return new StatisticAlertThresholds(rs.getString("statistic_name"), rs
.getBigDecimal("threshhold_total_max"), rs.getLong("threshhold_count_max"), rs
.getBigDecimal("threshhold_total_min"), rs.getLong("threshhold_count_min"));
.getBigDecimal("threshold_total_max"), rs.getLong("threshold_count_max"), rs
.getBigDecimal("threshold_total_min"), rs.getLong("threshold_count_min"), rs
.getBigDecimal("threshold_avg_max"), rs.getBigDecimal("threshold_avg_min"));
}
});
}

public void saveStatisticAlertThresholds(StatisticAlertThresholds threshold) {
SimpleJdbcTemplate template = getSimpleTemplate();
int updated = template.update(getSql("updateAlertThresholdsSql"), threshold.getThreshholdTotalMax(), threshold
.getThreshholdCountMax(), threshold.getThreshholdTotalMin(), threshold.getThreshholdCountMin(),
threshold.getStatisticName());
int updated = template.update(getSql("updateAlertThresholdsSql"), threshold.getThresholdTotalMax(), threshold
.getThresholdCountMax(), threshold.getThresholdAvgMax(), threshold.getThresholdTotalMin(), threshold
.getThresholdCountMin(), threshold.getThresholdAvgMin(), threshold.getStatisticName());
if (updated == 0) {
template.update(getSql("insertAlertThresholdsSql"), threshold.getStatisticName(), threshold
.getThreshholdTotalMax(), threshold.getThreshholdCountMax(), threshold.getThreshholdTotalMin(),
threshold.getThreshholdCountMin());
.getThresholdTotalMax(), threshold.getThresholdCountMax(), threshold.getThresholdAvgMax(),
threshold.getThresholdTotalMin(), threshold.getThresholdCountMin(), threshold
.getThresholdAvgMin());
}
}

public boolean removeStatisticAlertThresholds(String statisticName) {
return 1 == getSimpleTemplate().update(getSql("deleteAlertThresholdsSql"), statisticName);
return 1 == getSimpleTemplate().update(getSql("deleteAlertThresholdsSql"), statisticName);
}
}
Expand Up @@ -21,10 +21,14 @@

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.management.Notification;

import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.model.Node;
import org.jumpmind.symmetric.model.StatisticAlertThresholds;
import org.jumpmind.symmetric.service.INodeService;
import org.jumpmind.symmetric.service.INotificationService;
import org.jumpmind.symmetric.service.IParameterService;
Expand Down Expand Up @@ -62,8 +66,19 @@ synchronized public void flush() {
* alert thresholds and publish alerts if we fall outside the range.
*/
synchronized protected void publishAlerts() {
if (parameterService.is(ParameterConstants.STATISTIC_THRESHOLD_ALERTS_ENABLED)) {
// TODO
if (parameterService.is(ParameterConstants.STATISTIC_THRESHOLD_ALERTS_ENABLED) && statistics != null) {
List<StatisticAlertThresholds> thresholds = statisticService.getAlertThresholds();
if (thresholds != null) {
for (StatisticAlertThresholds statisticAlertThresholds : thresholds) {
StatisticName name = StatisticName.valueOf(statisticAlertThresholds.getStatisticName());
if (name != null) {
Notification event = statisticAlertThresholds.outsideOfBoundsNotification(statistics.get(name));
if (event != null) {
notificationService.sendNotification(event);
}
}
}
}
}
}

Expand Down
10 changes: 6 additions & 4 deletions symmetric/src/main/resources/ddl-config.xml
Expand Up @@ -237,10 +237,12 @@
<!-- This table configures thresholds at which JMX notifications will be fired -->
<table name="statistic_alert">
<column name="statistic_name" type="VARCHAR" size="50" primaryKey="true"/>
<column name="threshhold_total_max" type="DECIMAL" />
<column name="threshhold_count_max" type="BIGINT" />
<column name="threshhold_total_min" type="DECIMAL" />
<column name="threshhold_count_min" type="BIGINT" />
<column name="threshold_total_max" type="DECIMAL" />
<column name="threshold_avg_max" type="DECIMAL" />
<column name="threshold_count_max" type="BIGINT" />
<column name="threshold_total_min" type="DECIMAL" />
<column name="threshold_count_min" type="BIGINT" />
<column name="threshold_avg_min" type="DECIMAL" />
</table>

<table name="parameter">
Expand Down
12 changes: 6 additions & 6 deletions symmetric/src/main/resources/sql/statistic-service-sql.xml
Expand Up @@ -19,24 +19,24 @@
<entry key="getAlertThresholdsSql">
<value><![CDATA[
select statistic_name,
threshhold_total_max, threshhold_count_max,
threshhold_total_min, threshhold_count_min
threshold_total_max, threshold_count_max, threshold_avg_max,
threshold_total_min, threshold_count_min, threshold_avg_min
from ${sync.table.prefix}_statistic_alert
]]></value>
</entry>
<entry key="updateAlertThresholdsSql">
<value><![CDATA[
update ${sync.table.prefix}_statistic_alert
set threshhold_total_max=?, threshhold_count_max=?,
threshhold_total_min=?, threshhold_count_min=?
set threshold_total_max=?, threshold_count_max=?, threshold_avg_max=?,
threshold_total_min=?, threshold_count_min=?, threshold_avg_min=?
where statistic_name=?
]]></value>
</entry>
<entry key="insertAlertThresholdsSql">
<value><![CDATA[
insert into ${sync.table.prefix}_statistic_alert
(statistic_name, threshhold_total_max, threshhold_count_max,
threshhold_total_min, threshhold_count_min) values (?,?,?,?,?)
(statistic_name, threshold_total_max, threshold_count_max, threshold_avg_max,
threshold_total_min, threshold_count_min, threshold_avg_min) values (?,?,?,?,?,?,?)
]]></value>
</entry>
<entry key="deleteAlertThresholdsSql">
Expand Down

0 comments on commit c35f739

Please sign in to comment.