| 5827 |
amar.kumar |
1 |
/*
|
|
|
2 |
* Copyright 2005 stat4j.org
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
package net.sourceforge.stat4j;
|
|
|
17 |
|
|
|
18 |
import java.util.Properties;
|
|
|
19 |
|
|
|
20 |
import net.sourceforge.stat4j.util.Log;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Name: Alert.java Date: Sep 6, 2004 Description:
|
|
|
24 |
*
|
|
|
25 |
* Simple threshold based alert.
|
|
|
26 |
*
|
|
|
27 |
* This class represents a pre-configured alert for a statistic.
|
|
|
28 |
*
|
|
|
29 |
* The alert will be evaluated every time the statistic is updated. (alert
|
|
|
30 |
* suppresion is not supported in this version)
|
|
|
31 |
*
|
|
|
32 |
* If the alert fires (either WARN or CRITICAL) then an alert message will be
|
|
|
33 |
* sent to the nominated log4j category (console,email,NT event log etc)
|
|
|
34 |
*
|
|
|
35 |
* @see Threashold
|
|
|
36 |
*
|
|
|
37 |
* @author Lara D'Abreo
|
|
|
38 |
* @author Jakub Holy
|
|
|
39 |
*/
|
|
|
40 |
public final class Alert {
|
|
|
41 |
|
|
|
42 |
protected String name;
|
|
|
43 |
protected String statisticName;
|
|
|
44 |
protected String category;
|
|
|
45 |
protected String description;
|
|
|
46 |
protected Threshold warn;
|
|
|
47 |
protected Threshold critical;
|
|
|
48 |
|
|
|
49 |
protected long lastAlertedMs;
|
|
|
50 |
private long quietPeriodMs = 1000 * 60 * 10; // 10 min
|
|
|
51 |
private boolean lastAlertCritical = false;
|
|
|
52 |
|
|
|
53 |
public void init(String name, Properties properties) {
|
|
|
54 |
this.name = name;
|
|
|
55 |
|
|
|
56 |
statisticName = properties.getProperty("statistic");
|
|
|
57 |
|
|
|
58 |
description = properties.getProperty("description");
|
|
|
59 |
if (description == null) {
|
|
|
60 |
description = name;
|
|
|
61 |
}
|
|
|
62 |
// init category
|
|
|
63 |
category = (String) properties.get("category");
|
|
|
64 |
if (category == null) {
|
|
|
65 |
category = "alerts";
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
// init warn
|
|
|
69 |
String str = (String) properties.get("warn");
|
|
|
70 |
if (str != null) {
|
|
|
71 |
warn = Threshold.toThreshold(str);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
// init critical
|
|
|
75 |
str = (String) properties.get("critical");
|
|
|
76 |
if (str != null) {
|
|
|
77 |
critical = Threshold.toThreshold(str);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
str = (String) properties.getProperty("quietperiod");
|
|
|
81 |
if (str != null) {
|
|
|
82 |
quietPeriodMs = Long.parseLong(str);
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
public boolean evaluateAlert(double value) {
|
|
|
88 |
boolean triggered = false;
|
|
|
89 |
if (isTriggered(critical, value, true)) {
|
|
|
90 |
triggered = true;
|
|
|
91 |
lastAlertCritical = true;
|
|
|
92 |
Log.error(
|
|
|
93 |
category,
|
|
|
94 |
"CRITICAL Alert "
|
|
|
95 |
+ description
|
|
|
96 |
+ " rule "
|
|
|
97 |
+ critical
|
|
|
98 |
+ " value "
|
|
|
99 |
+ value);
|
|
|
100 |
} else if (isTriggered(warn, value, false)) {
|
|
|
101 |
triggered = true;
|
|
|
102 |
lastAlertCritical = false;
|
|
|
103 |
Log.error(
|
|
|
104 |
category,
|
|
|
105 |
"WARN Alert "
|
|
|
106 |
+ description
|
|
|
107 |
+ " rule "
|
|
|
108 |
+ warn
|
|
|
109 |
+ " value "
|
|
|
110 |
+ value);
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
if (triggered) {
|
|
|
114 |
lastAlertedMs = System.currentTimeMillis();
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
return triggered;
|
|
|
118 |
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
boolean isTriggered(Threshold t, double value, boolean critical) {
|
|
|
122 |
if (t == null) return false;
|
|
|
123 |
|
|
|
124 |
boolean inQuietPeriod = (System.currentTimeMillis() - lastAlertedMs) <= quietPeriodMs;
|
|
|
125 |
boolean newCritical = critical && !lastAlertCritical;
|
|
|
126 |
boolean ignoreEvent = inQuietPeriod && !newCritical;
|
|
|
127 |
|
|
|
128 |
boolean result = !ignoreEvent && t.isTriggered(value);
|
|
|
129 |
|
|
|
130 |
//System.out.println("Alert.isTriggered: quiet=" + inQuietPeriod + ", result=" + result + ", last alert at " + lastAlertedMs);
|
|
|
131 |
|
|
|
132 |
return result;
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
/**
|
|
|
136 |
* @return
|
|
|
137 |
*/
|
|
|
138 |
public String getName() {
|
|
|
139 |
return name;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
/**
|
|
|
143 |
* @return
|
|
|
144 |
*/
|
|
|
145 |
public String getStatisticName() {
|
|
|
146 |
return statisticName;
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
}
|