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.plugin;
17  
18  import it.jnrpe.ICommandLine;
19  import it.jnrpe.plugins.Metric;
20  import it.jnrpe.plugins.MetricGatheringException;
21  import it.jnrpe.plugins.PluginBase;
22  import it.jnrpe.utils.BadThresholdException;
23  import it.jnrpe.utils.thresholds.ThresholdsEvaluatorBuilder;
24  
25  import java.io.File;
26  import java.math.BigDecimal;
27  import java.util.ArrayList;
28  import java.util.Collection;
29  import java.util.List;
30  
31  /**
32   * Checks the disk space.
33   *
34   * @author Massimiliano Ziccardi
35   *
36   */
37  public class CheckDisk extends PluginBase {
38  
39      /**
40       * Number of bytes in a Kilobyte.
41       */
42      private static final long KB = 1024;
43  
44      /**
45       * Number of kilobytes in a Megabyte.
46       */
47      private static final long MB = KB << 10;
48  
49      /**
50       * Compute the percent values.
51       *
52       * @param val
53       *            The value to be represented in percent
54       * @param total
55       *            The total value
56       * @return The percent of val/total
57       */
58      private int percent(final long val, final long total) {
59          if (total == 0) {
60              return 100;
61          }
62  
63          if (val == 0) {
64              return 0;
65          }
66  
67          double dVal = (double) val;
68          double dTotal = (double) total;
69  
70          return (int) (dVal / dTotal * 100);
71      }
72  
73      @Override
74      public final void configureThresholdEvaluatorBuilder(
75              final ThresholdsEvaluatorBuilder thrb,
76              final ICommandLine cl)
77              throws BadThresholdException {
78          if (cl.hasOption("th")) {
79              super.configureThresholdEvaluatorBuilder(thrb, cl);
80          } else {
81              thrb.withLegacyThreshold("freepct", null,
82                      cl.getOptionValue("warning"),
83                      cl.getOptionValue("critical"));
84          }
85      }
86  
87      @Override
88      public final Collection<Metric> gatherMetrics(final ICommandLine cl)
89              throws MetricGatheringException {
90          String sPath = cl.getOptionValue("path");
91  
92          File f = new File(sPath);
93  
94          long lBytes = f.getFreeSpace();
95          long lTotalSpace = f.getTotalSpace();
96  
97          String sFreeSpace = format(lBytes);
98          String sUsedSpace = format(lTotalSpace - lBytes);
99  
100         int iFreePercent = percent(lBytes, lTotalSpace);
101 
102         String sFreePercent = "" + iFreePercent + "%";
103         String sUsedPercent =
104                 "" + percent(lTotalSpace - lBytes, lTotalSpace) + "%";
105 
106         List<Metric> res = new ArrayList<Metric>();
107 
108         String msg = "Used: " + sUsedSpace + "("
109                 + sUsedPercent + ") Free: " + sFreeSpace + "("
110                 + sFreePercent + ")";
111 
112         res.add(new Metric("freepct", msg, new BigDecimal(iFreePercent),
113                 new BigDecimal(0), new BigDecimal(100)));
114         res.add(new Metric("freespace", msg, new BigDecimal(iFreePercent),
115                 new BigDecimal(0), new BigDecimal(lTotalSpace)));
116 
117         return res;
118     }
119 
120     /**
121      * Format the size returning it as MB or KB.
122      *
123      * @param bytes
124      *            The size to be formatted
125      * @return The formatted size
126      */
127     private String format(final long bytes) {
128         if (bytes > MB) {
129             return "" + (bytes / MB) + " MB";
130         }
131         return "" + (bytes / KB) + " KB";
132     }
133 
134     @Override
135     protected String getPluginName() {
136         return "CHECK_DISK";
137     }
138 
139 }