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.ReturnValue;
19 import it.jnrpe.Status;
20 import it.jnrpe.plugins.Metric;
21
22 import org.apache.commons.lang.StringUtils;
23
24
25
26
27
28
29
30
31
32 public final class ReturnValueBuilder {
33
34
35
36 private final ReturnValue retVal = new ReturnValue().withStatus(Status.OK);
37
38
39
40
41 private final ThresholdsEvaluator thresholds;
42
43
44
45
46 private Status status = Status.OK;
47
48
49
50
51 private Status forcedStatus = null;
52
53
54
55
56 private final String pluginName;
57
58
59
60
61 private String retValMessage = "";
62
63
64
65
66
67
68
69
70
71 private ReturnValueBuilder(final String name, final ThresholdsEvaluator thr) {
72 pluginName = name;
73 thresholds = thr;
74 }
75
76
77
78
79
80
81
82
83 public static ReturnValueBuilder forPlugin(final String name) {
84 return forPlugin(name, null);
85 }
86
87
88
89
90
91
92
93
94
95
96 public static ReturnValueBuilder forPlugin(final String name,
97 final ThresholdsEvaluator thr) {
98 if (thr != null) {
99 return new ReturnValueBuilder(name, thr);
100 }
101 return new ReturnValueBuilder(name,
102 new ThresholdsEvaluatorBuilder().create());
103 }
104
105
106
107
108
109
110
111
112
113 public ReturnValueBuilder withValue(final Metric pluginMetric) {
114 if (thresholds.isMetricRequired(pluginMetric.getMetricName())) {
115 Status newStatus = thresholds
116 .evaluate(pluginMetric.getMetricName(),
117 pluginMetric.getMetricValue());
118 if (newStatus.getSeverity() > status.getSeverity()) {
119 status = newStatus;
120 }
121
122 IThreshold thr = thresholds.getThreshold(pluginMetric
123 .getMetricName());
124
125 formatResultMessage(pluginMetric);
126
127 retVal.withPerformanceData(pluginMetric.getMetricName(),
128 pluginMetric.getMetricValue(), thr.getUnitString(),
129 thr.getRangesAsString(Status.WARNING),
130 thr.getRangesAsString(Status.CRITICAL),
131 pluginMetric.getMinValue(), pluginMetric.getMaxValue());
132 }
133 return this;
134 }
135
136
137
138
139
140
141
142
143 private void formatResultMessage(final Metric pluginMetric) {
144 if (StringUtils.isEmpty(pluginMetric.getMessage())) {
145 return;
146 }
147
148 if (StringUtils.isEmpty(retValMessage)) {
149 retValMessage = pluginMetric.getMessage();
150 return;
151 }
152 retValMessage += " " + pluginMetric.getMessage();
153 }
154
155
156
157
158
159
160
161
162
163
164 public ReturnValueBuilder withForcedMessage(final String message) {
165 retValMessage = message;
166 return this;
167 }
168
169
170
171
172
173
174
175
176
177
178 public ReturnValueBuilder withStatus(final Status forceStatus) {
179 forcedStatus = forceStatus;
180 return this;
181 }
182
183
184
185
186
187
188 public ReturnValue create() {
189 if (forcedStatus == null) {
190 retVal.withStatus(status);
191 } else {
192 retVal.withStatus(forcedStatus);
193 }
194
195 StringBuffer msg = new StringBuffer(pluginName + " : "
196 + retVal.getStatus().toString());
197 if (!StringUtils.isEmpty(retValMessage)) {
198 msg.append(" - " + retValMessage);
199 }
200
201 retVal.withMessage(msg.toString());
202
203 return retVal;
204 }
205 }