View Javadoc

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  import java.util.HashMap;
22  import java.util.Map;
23  
24  /**
25   * This object take responsability of checking a pair value-metric agains all
26   * the ranges defined for such metric and returns the right {@link Status}
27   * object.
28   *
29   * @author Massimiliano Ziccardi
30   */
31  public class ThresholdsEvaluator {
32  
33      /**
34       * Default constructor.
35       */
36      ThresholdsEvaluator() {
37  
38      }
39  
40      /**
41       * This map contains all the pair METRIC-THRESHOLD.
42       */
43      private Map<String, IThreshold> thresholdsMap =
44              new HashMap<String, IThreshold>();
45  
46      /**
47       * Adds a threshold to the list of handled thresholds.
48       *
49       * @param thr
50       *            The threshold to add
51       */
52      final void addThreshold(final IThreshold thr) {
53          thresholdsMap.put(thr.getMetric(), thr);
54      }
55  
56      /**
57       * Returns <code>true</code> if the requested metric is required.
58       *
59       * @param metricName
60       *            The name of the metric
61       * @return <code>true</code> if the metric is required.
62       */
63      final boolean isMetricRequired(final String metricName) {
64          return thresholdsMap.containsKey(metricName);
65      }
66  
67      /**
68       * Returns the requested threshold.
69       *
70       * @param metric
71       *            The metric name attached to the threshold.
72       * @return The threshold
73       */
74      final IThreshold getThreshold(final String metric) {
75          return thresholdsMap.get(metric);
76      }
77  
78      /**
79       * Evaluates the passed in value against the threshold configured
80       * inside this evaluator.
81       * If the threshold do not refer the passed in metric, than it is
82       * ignored and the next threshold is checked.
83       *
84       * @param metric The metric name
85       * @param value The value to be checked
86       * @return The status computed accordin to the rules specified for
87       * {@link IThreshold#evaluate(BigDecimal)}
88       */
89      final Status evaluate(final String metric, final int value) {
90          return evaluate(metric, new BigDecimal(value));
91      }
92  
93      /**
94       * Evaluates the passed in value against the threshold configured
95       * inside this evaluator.
96       * If the threshold do not refer the passed in metric, than it is
97       * ignored and the next threshold is checked.
98       *
99       * @param metric The metric name
100      * @param value The value to be checked
101      * @return The status computed accordin to the rules specified for
102      * {@link IThreshold#evaluate(BigDecimal)}
103      */
104     final Status evaluate(final String metric, final BigDecimal value) {
105 
106         if (value == null) {
107             throw new NullPointerException(
108                     "Value to evaluate can't be null");
109         }
110 
111         if (metric == null) {
112             throw new NullPointerException(
113                     "Metric name can't be null");
114         }
115 
116         IThreshold thr = thresholdsMap.get(metric);
117         if (thr == null) {
118             return Status.OK;
119         }
120 
121         return thr.evaluate(value);
122     }
123 }