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.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   * This plugin checks the number of users currently logged in on the local
40   * system and generates a critical or an error status according to the passed-in
41   * thresholds.
42   *
43   * @author Frederico Campos
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      * Get list of logged in users for linux.
133      *
134      * @return The number of logged in users
135      * @throws IOException
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      * Get list of logged in users for windows by counting the number of
156      * explorer.exe processes.
157      *
158      * @return The number of logged in windows users
159      * @throws IOException
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 }