1 /*******************************************************************************
2 * Copyright (c) 2007, 2014 Massimiliano Ziccardi
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 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 * This object takes the responsability to build and configure the return value
26 * object and the performance data. The plugin has only the responsability to
27 * gain the metrics and pass them to the builder: both status and performance
28 * data will be generated.
29 *
30 * @author Massimiliano Ziccardi
31 */
32 public final class ReturnValueBuilder {
33 /**
34 * The return value that we are configuring.
35 */
36 private final ReturnValue retVal = new ReturnValue().withStatus(Status.OK);
37
38 /**
39 * The thresholds that must be used to compute the Status result.
40 */
41 private final ThresholdsEvaluator thresholds;
42
43 /**
44 * The status.
45 */
46 private Status status = Status.OK;
47
48 /**
49 * The status to force.
50 */
51 private Status forcedStatus = null;
52
53 /**
54 * The name of the plugin that is generating this result.
55 */
56 private final String pluginName;
57
58 /**
59 * The value of the message to return to Nagios.
60 */
61 private String retValMessage = "";
62
63 /**
64 * Constructs the object passing the thresholds evaluator.
65 *
66 * @param name
67 * The name of the plugin that is creating this result
68 * @param thr
69 * The threshold evaluator.
70 */
71 private ReturnValueBuilder(final String name, final ThresholdsEvaluator thr) {
72 pluginName = name;
73 thresholds = thr;
74 }
75
76 /**
77 * Constructs the object with an empty threshold evaluator.
78 *
79 * @param name
80 * The name of the plugin that is creating this result
81 * @return the newly created instance.
82 */
83 public static ReturnValueBuilder forPlugin(final String name) {
84 return forPlugin(name, null);
85 }
86
87 /**
88 * Constructs the object with the given threshold evaluator.
89 *
90 * @param name
91 * The name of the plugin that is creating this result
92 * @param thr
93 * The threshold evaluator.
94 * @return the newly created instance.
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 * Configure the {@link ReturnValue} we are building with the specified
107 * value.
108 *
109 * @param pluginMetric
110 * The metric for wich we want to compute the result Status.
111 * @return this
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 * Formats the message to return to Nagios according to the specifications
138 * contained inside the pluginMetric object.
139 *
140 * @param pluginMetric
141 * The metric.
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 * Force the message to return to Nagios. Instead of computing the message
157 * using the {@link Metric} object received in the
158 * {@link #withValue(Metric)} methods, this value will be returned.
159 *
160 * @param message
161 * The message to return.
162 * @return this
163 */
164 public ReturnValueBuilder withForcedMessage(final String message) {
165 retValMessage = message;
166 return this;
167 }
168
169 /**
170 * Use this method if you want to force the status to be returned. This can
171 * be useful if, for example, your check got an error and has no metric to
172 * be evaluated to automatically compute the status.
173 *
174 * @param forceStatus
175 * The status to be forced
176 * @return this
177 */
178 public ReturnValueBuilder withStatus(final Status forceStatus) {
179 forcedStatus = forceStatus;
180 return this;
181 }
182
183 /**
184 * Builds the configured {@link ReturnValue} object.
185 *
186 * @return The {@link ReturnValue} object
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 }