1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package it.jnrpe.plugins;
17
18 import it.jnrpe.ICommandLine;
19 import it.jnrpe.ReturnValue;
20 import it.jnrpe.events.EventParam;
21 import it.jnrpe.events.EventsUtil;
22 import it.jnrpe.events.IJNRPEEventListener;
23 import it.jnrpe.events.LogEvent;
24 import it.jnrpe.utils.BadThresholdException;
25 import it.jnrpe.utils.thresholds.ReturnValueBuilder;
26 import it.jnrpe.utils.thresholds.ThresholdsEvaluatorBuilder;
27
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.HashSet;
31 import java.util.Set;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public abstract class PluginBase implements IPluginInterfaceEx {
53
54
55
56 private final Set<IJNRPEEventListener> listenersSet = new HashSet<IJNRPEEventListener>();
57
58
59
60
61 protected final Logger log = new Logger();
62
63
64
65
66
67
68
69
70 protected class Logger {
71
72
73
74
75
76
77
78 public final void trace(final String message) {
79 EventsUtil.sendEvent(listenersSet, PluginBase.this, LogEvent.TRACE,
80 message);
81 }
82
83
84
85
86
87
88
89
90
91 public final void trace(final String message, final Throwable exc) {
92 EventsUtil.sendEvent(listenersSet, PluginBase.this, LogEvent.TRACE,
93 message, exc);
94 }
95
96
97
98
99
100
101
102 public final void debug(final String message) {
103 EventsUtil.sendEvent(listenersSet, PluginBase.this, LogEvent.DEBUG,
104 message);
105 }
106
107
108
109
110
111
112
113
114
115 public final void debug(final String message, final Throwable exc) {
116 EventsUtil.sendEvent(listenersSet, PluginBase.this, LogEvent.DEBUG,
117 message, exc);
118 }
119
120
121
122
123
124
125
126 public final void info(final String message) {
127 EventsUtil.sendEvent(listenersSet, PluginBase.this, LogEvent.INFO,
128 message);
129 }
130
131
132
133
134
135
136
137
138
139 public final void info(final String message, final Throwable exc) {
140 EventsUtil.sendEvent(listenersSet, PluginBase.this, LogEvent.INFO,
141 message, exc);
142 }
143
144
145
146
147
148
149
150 public final void warn(final String message) {
151 EventsUtil.sendEvent(listenersSet, PluginBase.this,
152 LogEvent.WARNING, message);
153 }
154
155
156
157
158
159
160
161
162
163 public final void warn(final String message, final Throwable exc) {
164 EventsUtil.sendEvent(listenersSet, PluginBase.this,
165 LogEvent.WARNING, message, exc);
166 }
167
168
169
170
171
172
173
174 public final void error(final String message) {
175 EventsUtil.sendEvent(listenersSet, PluginBase.this, LogEvent.ERROR,
176 message);
177 }
178
179
180
181
182
183
184
185
186
187 public final void error(final String message, final Throwable exc) {
188 EventsUtil.sendEvent(listenersSet, PluginBase.this, LogEvent.ERROR,
189 message, exc);
190 }
191
192
193
194
195
196
197
198 public final void fatal(final String message) {
199 EventsUtil.sendEvent(listenersSet, PluginBase.this, LogEvent.FATAL,
200 message);
201 }
202
203
204
205
206
207
208
209
210
211 public final void fatal(final String message, final Throwable exc) {
212 EventsUtil.sendEvent(listenersSet, PluginBase.this, LogEvent.FATAL,
213 message, exc);
214 }
215 }
216
217
218
219
220
221
222
223
224 public final void addListener(final IJNRPEEventListener listener) {
225 listenersSet.add(listener);
226 }
227
228
229
230
231
232
233
234 public final void addListeners(
235 final Collection<IJNRPEEventListener> listeners) {
236 if (listeners == null) {
237 return;
238 }
239
240 listenersSet.addAll(listeners);
241 }
242
243
244
245
246
247
248
249
250
251 public final void sendEvent(final LogEvent evt, final String message) {
252 EventsUtil.sendEvent(listenersSet, this, evt, message);
253 }
254
255
256
257
258
259
260
261
262
263
264
265 public final void sendEvent(final LogEvent evt, final String message,
266 final Exception exc) {
267 EventsUtil.sendEvent(listenersSet, this, evt, message, exc);
268 }
269
270
271
272
273
274
275
276
277
278 public final void sendEvent(final String customEventName,
279 final EventParam... paramsAry) {
280 EventsUtil.sendEvent(listenersSet, this, customEventName, paramsAry);
281 }
282
283
284
285
286 protected abstract String getPluginName();
287
288
289
290
291
292
293 protected final Set<IJNRPEEventListener> getListeners() {
294 return listenersSet;
295 }
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310 public void configureThresholdEvaluatorBuilder(
311 final ThresholdsEvaluatorBuilder thrb, final ICommandLine cl)
312 throws BadThresholdException {
313 if (cl.hasOption("th")) {
314 for (Object obj : cl.getOptionValues("th")) {
315 thrb.withThreshold(obj.toString());
316 }
317 }
318 }
319
320
321
322
323
324
325
326
327
328
329
330
331 public Collection<Metric> gatherMetrics(final ICommandLine cl)
332 throws MetricGatheringException {
333 return Collections.emptyList();
334 }
335
336
337
338
339
340
341
342
343
344
345
346
347 public ReturnValue execute(final ICommandLine cl)
348 throws BadThresholdException {
349 ThresholdsEvaluatorBuilder thrb = new ThresholdsEvaluatorBuilder();
350 configureThresholdEvaluatorBuilder(thrb, cl);
351 ReturnValueBuilder builder = ReturnValueBuilder.forPlugin(
352 getPluginName(), thrb.create());
353
354 try {
355 Collection<Metric> metrics = gatherMetrics(cl);
356
357 for (Metric m : metrics) {
358 builder.withValue(m);
359 }
360
361 return builder.create();
362 } catch (MetricGatheringException mge) {
363 return ReturnValueBuilder.forPlugin(getPluginName())
364 .withForcedMessage(mge.getMessage())
365 .withStatus(mge.getStatus()).create();
366 }
367
368 }
369 }