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.Status; 19 20 import java.math.BigDecimal; 21 22 /** 23 * This class represent a parser/evaluator for the old threshold syntax. 24 * 25 * @author Massimiliano Ziccardi 26 */ 27 public class LegacyThreshold implements IThreshold { 28 /** 29 * The range for an OK status. 30 */ 31 private final LegacyRange okRange; 32 /** 33 * The range for a Warning status. 34 */ 35 private final LegacyRange warnRange; 36 /** 37 * The range for a critical status. 38 */ 39 private final LegacyRange critRange; 40 /** 41 * The metric associated with this threshold specification. 42 */ 43 private final String metricName; 44 45 /** 46 * Builds and initializes the threshold object. 47 * 48 * @param metric 49 * The metric associated with this threshold. 50 * @param ok 51 * The ok range. 52 * @param warn 53 * The warning range. 54 * @param crit 55 * The critical range. 56 */ 57 public LegacyThreshold(final String metric, final LegacyRange ok, 58 final LegacyRange warn, final LegacyRange crit) { 59 okRange = ok; 60 warnRange = warn; 61 critRange = crit; 62 metricName = metric; 63 } 64 65 /** 66 * Evaluates the value agains the specified ranges. 67 * The followed flow is: 68 * <ol> 69 * <li>If a critical range is defined and the value falls inside the 70 * specified range, a {@link Status#CRITICAL} is returned. 71 * <li>If a warning range is defined and the value falls inside the 72 * specified range, a {@link Status#WARNING} is returned. 73 * <li>If a OK range is defined and the value falls inside the 74 * specified range, a {@link Status#OK} is returned. 75 * <li>If the OK range is not specified, a {@link Status#CRITICAL} is 76 * returned 77 * <li>{@link Status#OK} is returned 78 * </ol> 79 * @param value The value to be evaluated. 80 * @return the evaluated status. 81 */ 82 public final Status evaluate(final BigDecimal value) { 83 if (critRange != null && critRange.isValueInside(value)) { 84 return Status.CRITICAL; 85 } 86 if (warnRange != null && warnRange.isValueInside(value)) { 87 return Status.WARNING; 88 } 89 if (okRange != null) { 90 if (okRange.isValueInside(value)) { 91 return Status.OK; 92 } else { 93 return Status.CRITICAL; 94 } 95 } 96 97 return Status.OK; 98 } 99 100 /** 101 * @param metric The metric we want to evaluate. 102 * @return wether this threshold is about the passed in metric. 103 */ 104 public final boolean isAboutMetric(final String metric) { 105 return metricName.equalsIgnoreCase(metric); 106 } 107 108 /** 109 * The metric referred by this threshold. 110 * @return the metric name. 111 */ 112 public final String getMetric() { 113 return metricName; 114 } 115 116 /** 117 * @param status the range we are interested in. 118 * @return the requested unparsed range string. 119 */ 120 public final String getRangesAsString(final Status status) { 121 switch (status) { 122 case OK: 123 if (okRange != null) { 124 return okRange.getThresholdString(); 125 } 126 break; 127 case WARNING: 128 if (warnRange != null) { 129 return warnRange.getThresholdString(); 130 } 131 break; 132 case CRITICAL: 133 if (critRange != null) { 134 return critRange.getThresholdString(); 135 } 136 break; 137 default: 138 } 139 140 return null; 141 } 142 143 /** 144 * @return the unit of measure as string. 145 */ 146 public final String getUnitString() { 147 return null; 148 } 149 }