1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package it.jnrpe.utils.thresholds;
17
18 import it.jnrpe.Status;
19 import it.jnrpe.utils.BadThresholdException;
20
21 import java.math.BigDecimal;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.commons.lang.StringUtils;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 class Threshold implements IThreshold {
47
48
49
50
51 private String metricName = null;
52
53
54
55
56 private List<Range> okThresholdList =
57 new ArrayList<Range>();
58
59
60
61 private List<Range> warningThresholdList =
62 new ArrayList<Range>();
63
64
65
66 private List<Range> criticalThresholdList =
67 new ArrayList<Range>();
68
69
70
71
72
73
74 private String unit = null;
75
76
77
78
79
80 private Prefixes prefix = null;
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 Threshold(final String definition)
114 throws BadThresholdException {
115 parse(definition);
116 }
117
118
119
120
121
122
123
124
125
126 private void parse(final String definition)
127 throws BadThresholdException {
128 String[] thresholdComponentAry = definition.split(",");
129
130 for (String thresholdComponent : thresholdComponentAry) {
131 String[] nameValuePair = thresholdComponent.split("=");
132
133 if (nameValuePair == null || nameValuePair.length != 2
134 || StringUtils.isEmpty(nameValuePair[0])
135 || StringUtils.isEmpty(nameValuePair[1])) {
136 throw new BadThresholdException("Invalid threshold syntax : "
137 + definition);
138 }
139
140 if (nameValuePair[0].equalsIgnoreCase("metric")) {
141 metricName = nameValuePair[1];
142 continue;
143 }
144 if (nameValuePair[0].equalsIgnoreCase("ok")) {
145
146 Range thr = new Range(nameValuePair[1]);
147
148 okThresholdList.add(thr);
149 continue;
150 }
151 if (nameValuePair[0].equalsIgnoreCase("warning")
152 || nameValuePair[0].equalsIgnoreCase("warn")
153 || nameValuePair[0].equalsIgnoreCase("w")) {
154 Range thr = new Range(nameValuePair[1]);
155 warningThresholdList.add(thr);
156 continue;
157 }
158 if (nameValuePair[0].equalsIgnoreCase("critical")
159 || nameValuePair[0].equalsIgnoreCase("crit")
160 || nameValuePair[0].equalsIgnoreCase("c")) {
161 Range thr = new Range(nameValuePair[1]);
162 criticalThresholdList.add(thr);
163 continue;
164 }
165 if (nameValuePair[0].equalsIgnoreCase("unit")) {
166 unit = nameValuePair[1];
167 continue;
168 }
169 if (nameValuePair[0].equalsIgnoreCase("prefix")) {
170 prefix = Prefixes.fromString(nameValuePair[1].toLowerCase());
171 continue;
172 }
173
174
175 }
176 }
177
178
179
180
181 public final String getMetric() {
182 return metricName;
183 }
184
185
186
187
188
189 public final String getUnitString() {
190 StringBuffer res = new StringBuffer();
191 if (prefix != null) {
192 res.append(prefix.toString());
193 }
194 if (unit != null) {
195 res.append(unit);
196 }
197
198 if (res.length() != 0) {
199 return res.toString();
200 }
201
202 return null;
203 }
204
205
206
207
208
209
210
211
212 public final String getRangesAsString(final Status status) {
213 List<String> ranges = new ArrayList<String>();
214
215 List<Range> rangeList = null;
216
217 switch (status) {
218 case OK:
219 rangeList = okThresholdList;
220 break;
221 case WARNING:
222 rangeList = warningThresholdList;
223 break;
224 case CRITICAL:
225 default:
226 rangeList = criticalThresholdList;
227 break;
228 }
229
230 for (Range r : rangeList) {
231 ranges.add(r.getRangeString());
232 }
233
234 if (ranges.isEmpty()) {
235 return null;
236 }
237
238 return StringUtils.join(ranges, ",");
239 }
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258 public final Status evaluate(final BigDecimal value) {
259 if (okThresholdList.isEmpty() && warningThresholdList.isEmpty()
260 && criticalThresholdList.isEmpty()) {
261 return Status.OK;
262 }
263
264 BigDecimal convertedValue = value;
265
266
267 for (Range range : okThresholdList) {
268 if (range.isValueInside(convertedValue, prefix)) {
269 return Status.OK;
270 }
271 }
272
273 for (Range range : criticalThresholdList) {
274 if (range.isValueInside(convertedValue, prefix)) {
275 return Status.CRITICAL;
276 }
277 }
278
279 for (Range range : warningThresholdList) {
280 if (range.isValueInside(convertedValue, prefix)) {
281 return Status.WARNING;
282 }
283 }
284
285 if (!okThresholdList.isEmpty()) {
286 return Status.CRITICAL;
287 }
288
289 return Status.OK;
290 }
291
292
293
294
295
296
297 public final boolean isAboutMetric(final String metric) {
298 return metric.equalsIgnoreCase(metricName);
299 }
300 }