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;
17
18 import it.jnrpe.ReturnValue.UnitOfMeasure;
19
20 import java.math.BigDecimal;
21 import java.text.DecimalFormat;
22
23 /**
24 * This class holds the performance data to be returned with the result of the
25 * plugin execution.
26 *
27 * Refer to http://nagiosplug.sourceforge.net/developer-guidelines.html#AEN201
28 *
29 * @author Massimiliano Ziccardi
30 */
31 class PerformanceData {
32
33 /**
34 * The label associated with this performance data. Can contain any
35 * characters except the equals sign or single quote (').
36 */
37 private final String label;
38
39 /**
40 * The value of this performance data.
41 */
42 private final BigDecimal performanceValue;
43
44 /**
45 * The Unit of Measure of {@link #performanceValue}, {@link #minimumValue}
46 * and {@link #maximumValue}.
47 * With the new threshold format this won't be used anymore.
48 */
49 private final UnitOfMeasure unitOfMeasure;
50
51 /**
52 * The unit of {@link #performanceValue}, {@link #minimumValue}
53 * and {@link #maximumValue}.
54 */
55 private final String unit;
56
57 /**
58 * The warning range passed to the plugin that produced this performance
59 * data. Can be <code>null</code>
60 */
61 private final String warningRange;
62
63 /**
64 * The critical range passed to the plugin that produced this performance
65 * data. Can be <code>null</code>
66 */
67 private final String criticalRange;
68
69 /**
70 * The minimum value that this performance data could reach. Can be
71 * <code>null</code>
72 */
73 private final BigDecimal minimumValue;
74
75 /**
76 * The maximum value that this performance data could reach. Can be
77 * <code>null</code>
78 */
79 private final BigDecimal maximumValue;
80
81 /**
82 * The performance data will be returned as string. This is the formatter
83 * for all the performance numbers.
84 */
85 private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat(
86 "0.000000");
87
88 /**
89 * Creates a performance data object.
90 *
91 * @param perfLabel
92 * The label identifying this performance data
93 * @param value
94 * The performance value
95 * @param uom
96 * The Unit of Measure of <code>value</code>,
97 * <code>minValue</code> and <code>maxValue</code>
98 * @param warnRange
99 * the warning range passed to the plugin that is generating this
100 * performance data. Can be null.
101 * @param critRange
102 * the critical range passed to the plugin that is generating
103 * this performance data. Can be null.
104 * @param minValue
105 * the minimum value that this performance data can reach. Can be
106 * null.
107 * @param maxValue
108 * the maximum value that this performance data can reach. Can be
109 * null.
110 */
111 public PerformanceData(final String perfLabel, final BigDecimal value,
112 final UnitOfMeasure uom, final String warnRange,
113 final String critRange, final BigDecimal minValue,
114 final BigDecimal maxValue) {
115 this.label = perfLabel;
116 this.performanceValue = value;
117 this.unitOfMeasure = uom;
118 this.warningRange = warnRange;
119 this.criticalRange = critRange;
120 this.minimumValue = minValue;
121 this.maximumValue = maxValue;
122 this.unit = null;
123 }
124
125 /**
126 * Creates a performance data object.
127 *
128 * @param perfLabel
129 * The label identifying this performance data
130 * @param value
131 * The performance value
132 * @param unitofMeasure
133 * The Unit of Measure of <code>value</code>,
134 * <code>minValue</code> and <code>maxValue</code>
135 * @param warnRange
136 * the warning range passed to the plugin that is generating this
137 * performance data. Can be null.
138 * @param critRange
139 * the critical range passed to the plugin that is generating
140 * this performance data. Can be null.
141 * @param minValue
142 * the minimum value that this performance data can reach. Can be
143 * null.
144 * @param maxValue
145 * the maximum value that this performance data can reach. Can be
146 * null.
147 */
148 public PerformanceData(final String perfLabel, final BigDecimal value,
149 final String unitofMeasure, final String warnRange,
150 final String critRange, final BigDecimal minValue,
151 final BigDecimal maxValue) {
152 this.label = perfLabel;
153 this.performanceValue = value;
154 this.unitOfMeasure = null;
155 this.warningRange = warnRange;
156 this.criticalRange = critRange;
157 this.minimumValue = minValue;
158 this.maximumValue = maxValue;
159 this.unit = unitofMeasure;
160 }
161
162
163 /**
164 * Produce a performance string accordin to Nagios specification based on
165 * the value of this performance data object.
166 *
167 * @return a string that can be returned to Nagios
168 */
169 public String toPerformanceString() {
170 StringBuffer res =
171 new StringBuffer().append(quote(label)).append("=")
172 .append(DECIMAL_FORMAT.format(performanceValue));
173
174 if (unitOfMeasure != null) {
175 switch (unitOfMeasure) {
176 case milliseconds:
177 res.append("ms");
178 break;
179 case microseconds:
180 res.append("us");
181 break;
182 case seconds:
183 res.append("s");
184 break;
185 case bytes:
186 res.append("B");
187 break;
188 case kilobytes:
189 res.append("KB");
190 break;
191 case megabytes:
192 res.append("MB");
193 break;
194 case gigabytes:
195 res.append("GB");
196 break;
197 case terabytes:
198 res.append("TB");
199 break;
200 case percentage:
201 res.append("%");
202 break;
203 case counter:
204 res.append("c");
205 break;
206 default:
207 }
208 }
209
210 if (unit != null) {
211 res.append(unit);
212 }
213
214 res.append(";");
215 if (warningRange != null) {
216 res.append(warningRange);
217 }
218 res.append(";");
219 if (criticalRange != null) {
220 res.append(criticalRange);
221 }
222 res.append(";");
223 if (minimumValue != null) {
224 res.append(DECIMAL_FORMAT.format(minimumValue));
225 }
226 res.append(";");
227 if (maximumValue != null) {
228 res.append(DECIMAL_FORMAT.format(maximumValue));
229 }
230
231 while (res.charAt(res.length() - 1) == ';') {
232 res.deleteCharAt(res.length() - 1);
233 }
234
235 return res.toString();
236 }
237
238 /**
239 * Quotes the label if required.
240 *
241 * @param lbl
242 * The label to be quoted
243 * @return The quoted label or the original label if quoting is not
244 * required.
245 */
246 private String quote(final String lbl) {
247 if (lbl.indexOf(' ') == -1) {
248 return lbl;
249 }
250
251 return new StringBuffer("'").append(lbl).append("'").toString();
252 }
253 }