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.Status;
20 import it.jnrpe.plugins.Metric;
21 import it.jnrpe.plugins.MetricGatheringException;
22 import it.jnrpe.plugins.PluginBase;
23 import it.jnrpe.plugins.annotations.Option;
24 import it.jnrpe.plugins.annotations.Plugin;
25 import it.jnrpe.plugins.annotations.PluginOptions;
26 import it.jnrpe.utils.BadThresholdException;
27 import it.jnrpe.utils.thresholds.ThresholdsEvaluatorBuilder;
28
29 import java.io.BufferedReader;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.math.BigDecimal;
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.List;
37
38
39
40
41
42
43
44
45
46 @Plugin(
47 name = "CHECK_USERS",
48 description = "This plugin checks the number of users currently logged in on the\n" +
49 "local system and generates an error if the number exceeds the thresholds specified.\n" +
50 "EXAMPLES\n" +
51 "The example will be based upon the following command definition (ini file)\n\n" +
52
53 "check_user : CHECK_USER --warning $ARG1$ --critical $ARG2$\n\n"+
54
55 "* Example 1 (Windows and Unix)\n"+
56 "The following example will give a WARNING if the number of logged in users exceeds 10 and a CRITICAL message if the number of users exceeds 20\n\n"+
57
58 "check_nrpe -H myjnrpeserver -c check_users -a '10:!20:'")
59
60 @PluginOptions({
61
62 @Option(shortName="w",
63 longName="warning",
64 description="Set WARNING status if more than INTEGER users are logged in",
65 required=false,
66 hasArgs=true,
67 argName="warning",
68 optionalArgs=false,
69 option="warning"
70 ),
71
72 @Option(shortName="c",
73 longName="critical",
74 description="Set CRITICAL status if more than INTEGER users are logged in",
75 required=false,
76 hasArgs=true,
77 argName="critical",
78 optionalArgs=false,
79 option="critical"),
80
81 @Option(shortName="T",
82 longName="th",
83 description="Configure a threshold. Format : metric={metric},ok={range},warn={range},crit={range},unit={unit},prefix={SI prefix}",
84 required=false,
85 hasArgs=true,
86 argName="critical",
87 optionalArgs=false,
88 option="th")
89 })
90 public class CheckUsers extends PluginBase {
91
92 @Override
93 public void configureThresholdEvaluatorBuilder(
94 ThresholdsEvaluatorBuilder thrb, ICommandLine cl)
95 throws BadThresholdException {
96
97 if (cl.hasOption("th")) {
98 super.configureThresholdEvaluatorBuilder(thrb, cl);
99 } else {
100 thrb.withLegacyThreshold("users", null,
101 cl.getOptionValue("warning"), cl.getOptionValue("critical"));
102 }
103
104 }
105
106 @Override
107 public Collection<Metric> gatherMetrics(ICommandLine cl)
108 throws MetricGatheringException {
109
110 List<Metric> metricList = new ArrayList<Metric>();
111 String os = System.getProperty("os.name").toLowerCase();
112 try {
113 if (os.contains("linux")) {
114 metricList.add(new Metric("users", "", new BigDecimal(
115 getLinuxLoggedInUsers()), null, null));
116 } else if (os.contains("windows")) {
117 metricList.add(new Metric("users", "", new BigDecimal(
118 getWindowsLoggedInUsers()), null, null));
119 }
120
121 return metricList;
122 } catch (IOException e) {
123 log.warn("CheckUser plugin execution error: "
124 + e.getMessage(), e);
125
126 throw new MetricGatheringException("An error has occurred : "
127 + e.getMessage(), Status.UNKNOWN, e);
128 }
129 }
130
131
132
133
134
135
136
137
138 private int getLinuxLoggedInUsers() throws IOException {
139 String command = "/usr/bin/users";
140 List<String> users = new ArrayList<String>();
141 ProcessBuilder builder = new ProcessBuilder();
142 Process proc = null;
143 proc = builder.command(command).start();
144 InputStream stdin = proc.getInputStream();
145 InputStreamReader isr = new InputStreamReader(stdin, "UTF-8");
146 BufferedReader br = new BufferedReader(isr);
147 String line = null;
148 while ((line = br.readLine()) != null) {
149 users.add(line);
150 }
151 return users.size();
152 }
153
154
155
156
157
158
159
160
161
162 private int getWindowsLoggedInUsers() throws IOException {
163 String command =
164 System.getenv("windir") + "\\system32\\" + "tasklist.exe";
165 int userCount = 0;
166 ProcessBuilder builder = new ProcessBuilder();
167 Process proc = null;
168 proc = builder.command(command).start();
169 InputStream stdin = proc.getInputStream();
170 InputStreamReader isr = new InputStreamReader(stdin);
171 BufferedReader br = new BufferedReader(isr);
172 String line = null;
173 while ((line = br.readLine()) != null) {
174 if (line.contains("explorer.exe")) {
175 userCount++;
176 }
177 }
178 return userCount;
179 }
180
181 @Override
182 protected String getPluginName() {
183 return "CHECK_USERS";
184 }
185 }