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 java.math.BigDecimal; 19 import java.util.ArrayList; 20 import java.util.List; 21 22 /** 23 * This class is just a container for the plugin result. 24 * 25 * @author Massimiliano Ziccardi 26 */ 27 public final class ReturnValue { 28 /** 29 * An enumeration defining all supported unit of measures. 30 * 31 * @author Massimiliano Ziccardi 32 */ 33 public enum UnitOfMeasure { 34 /** 35 * microseconds. 36 */ 37 microseconds, 38 /** 39 * milliseconds. 40 */ 41 milliseconds, 42 /** 43 * seconds. 44 */ 45 seconds, 46 /** 47 * percentage. 48 */ 49 percentage, 50 /** 51 * bytes. 52 */ 53 bytes, 54 /** 55 * kilobytes. 56 */ 57 kilobytes, 58 /** 59 * megabytes. 60 */ 61 megabytes, 62 /** 63 * gigabytes. 64 */ 65 gigabytes, 66 /** 67 * terabytes. 68 */ 69 terabytes, 70 /** 71 * counter. 72 */ 73 counter 74 }; 75 76 /** 77 * The performance data to attach to the result string. 78 */ 79 private final List<PerformanceData> performanceDataList = new ArrayList<PerformanceData>(); 80 81 /** 82 * The raw return code. 83 */ 84 private Status statusCode; 85 86 /** 87 * The message. 88 */ 89 private String messageString; 90 91 /** 92 * Initializes an empty return value. 93 */ 94 public ReturnValue() { 95 96 } 97 98 /** 99 * Initializes the return value object with the given message and with the 100 * {@link Status.OK} state. 101 * 102 * @param message 103 * The message 104 */ 105 public ReturnValue(final String message) { 106 statusCode = Status.OK; 107 messageString = message; 108 } 109 110 /** 111 * Initializes the return value object with the given state and the given 112 * message. 113 * 114 * @param returnCode 115 * The state 116 * @param message 117 * The message 118 * @deprecated Use {@link #ReturnValue(Status, String)} instead 119 */ 120 @Deprecated 121 public ReturnValue(final int returnCode, final String message) { 122 statusCode = Status.fromIntValue(returnCode); 123 messageString = message; 124 } 125 126 /** 127 * Initializes the return value object with the given state and the given 128 * message. 129 * 130 * @param status 131 * The status to be returned 132 * @param message 133 * The message to be returned 134 */ 135 public ReturnValue(final Status status, final String message) { 136 statusCode = status; 137 messageString = message; 138 } 139 140 /** 141 * Sets the return code and returns 'this' so that the calls can be 142 * cascaded. 143 * 144 * @param returnCode 145 * The return code 146 * @return this 147 * @deprecated Use {@link #withStatus(Status)} instead. 148 */ 149 @Deprecated 150 public ReturnValue withReturnCode(final int returnCode) { 151 statusCode = Status.fromIntValue(returnCode); 152 return this; 153 } 154 155 /** 156 * Sets the return code and returns 'this' so that the calls can be 157 * cascaded. 158 * 159 * @param status 160 * The status to be returned to Nagios 161 * @return this 162 */ 163 public ReturnValue withStatus(final Status status) { 164 if (status == null) { 165 throw new IllegalArgumentException("Status cannot be null"); 166 } 167 168 statusCode = status; 169 return this; 170 } 171 172 /** 173 * Sets the message and returns 'this' so that the calls can be cascaded. 174 * 175 * @param message 176 * The message to be returned 177 * @return this 178 */ 179 public ReturnValue withMessage(final String message) { 180 messageString = message; 181 return this; 182 } 183 184 /** 185 * Returns the status. 186 * 187 * @return The state 188 * @deprecated Use {@link #getStatus()} instead. 189 */ 190 @Deprecated 191 public int getReturnCode() { 192 return statusCode.intValue(); 193 } 194 195 /** 196 * Returns the status. 197 * 198 * @return The status 199 */ 200 public Status getStatus() { 201 return statusCode; 202 } 203 204 /** 205 * Returns the message. If the performance data has been passed in, they are 206 * attached at the end of the message accordingly to the Nagios 207 * specifications 208 * 209 * @return The message and optionally the performance data 210 */ 211 public String getMessage() { 212 if (performanceDataList.isEmpty()) { 213 return messageString; 214 } 215 StringBuffer res = new StringBuffer(messageString).append("|"); 216 for (PerformanceData pd : performanceDataList) { 217 res.append(pd.toPerformanceString()).append(' '); 218 } 219 return res.toString(); 220 } 221 222 /** 223 * Adds performance data to the plugin result. Thos data will be added to 224 * the output formatted as specified in Nagios specifications 225 * (http://nagiosplug.sourceforge.net/developer-guidelines.html#AEN201) 226 * 227 * @param label 228 * The label of the performance data we are adding 229 * @param value 230 * The performance data value 231 * @param uom 232 * The Unit Of Measure 233 * @param warningRange 234 * The warning threshold used to check this metric (can be null) 235 * @param criticalRange 236 * The critical threshold used to check this value (can be null) 237 * @param minimumValue 238 * The minimum value for this metric (can be null if not 239 * applicable) 240 * @param maximumValue 241 * The maximum value for this metric (can be null if not 242 * applicable) 243 * @return this 244 */ 245 public ReturnValue withPerformanceData(final String label, 246 final Long value, final UnitOfMeasure uom, 247 final String warningRange, final String criticalRange, 248 final Long minimumValue, final Long maximumValue) { 249 BigDecimal bdValue = null; 250 BigDecimal bdMin = null; 251 BigDecimal bdMax = null; 252 253 if (value != null) { 254 bdValue = new BigDecimal(value); 255 } 256 if (minimumValue != null) { 257 bdMin = new BigDecimal(minimumValue); 258 } 259 if (maximumValue != null) { 260 bdMax = new BigDecimal(maximumValue); 261 } 262 263 performanceDataList.add(new PerformanceData(label, bdValue, uom, 264 warningRange, criticalRange, bdMin, bdMax)); 265 return this; 266 } 267 268 /** 269 * Adds performance data to the plugin result. Thos data will be added to 270 * the output formatted as specified in Nagios specifications 271 * (http://nagiosplug.sourceforge.net/developer-guidelines.html#AEN201) 272 * 273 * @param label 274 * The label of the performance data we are adding 275 * @param value 276 * The performance data value 277 * @param uom 278 * The Unit Of Measure 279 * @param warningRange 280 * The warning threshold used to check this metric (can be null) 281 * @param criticalRange 282 * The critical threshold used to check this value (can be null) 283 * @param minimumValue 284 * The minimum value for this metric (can be null if not 285 * applicable) 286 * @param maximumValue 287 * The maximum value for this metric (can be null if not 288 * applicable) 289 * @return this 290 */ 291 public ReturnValue withPerformanceData(final String label, 292 final BigDecimal value, final UnitOfMeasure uom, 293 final String warningRange, final String criticalRange, 294 final BigDecimal minimumValue, final BigDecimal maximumValue) { 295 performanceDataList.add(new PerformanceData(label, value, uom, 296 warningRange, criticalRange, minimumValue, maximumValue)); 297 return this; 298 } 299 300 /** 301 * Adds performance data to the plugin result. Thos data will be added to 302 * the output formatted as specified in Nagios specifications 303 * (http://nagiosplug.sourceforge.net/developer-guidelines.html#AEN201) 304 * 305 * @param label 306 * The label of the performance data we are adding 307 * @param value 308 * The performance data value 309 * @param unit 310 * The Unit Of Measure 311 * @param warningRange 312 * The warning threshold used to check this metric (can be null) 313 * @param criticalRange 314 * The critical threshold used to check this value (can be null) 315 * @param minimumValue 316 * The minimum value for this metric (can be null if not 317 * applicable) 318 * @param maximumValue 319 * The maximum value for this metric (can be null if not 320 * applicable) 321 * @return this 322 */ 323 public ReturnValue withPerformanceData(final String label, 324 final BigDecimal value, final String unit, 325 final String warningRange, final String criticalRange, 326 final BigDecimal minimumValue, final BigDecimal maximumValue) { 327 328 performanceDataList.add(new PerformanceData(label, value, unit, 329 warningRange, criticalRange, minimumValue, maximumValue)); 330 return this; 331 } 332 }