1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
33
34
35
36
37 public class CheckDisk extends PluginBase {
38
39
40
41
42 private static final long KB = 1024;
43
44
45
46
47 private static final long MB = KB << 10;
48
49
50
51
52
53
54
55
56
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
122
123
124
125
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 }