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.plugin.utils.SshUtils;
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.math.BigDecimal;
30  import java.util.ArrayList;
31  import java.util.Collection;
32  import java.util.List;
33  
34  import com.jcraft.jsch.Channel;
35  import com.jcraft.jsch.Session;
36  
37  /**
38   * Plugin to check ssh connections on a remote host.
39   * 
40   * @author Frederico Campos
41   * 
42   */
43  
44  @Plugin(name = "CHECK_SSH", description = "Try to connect to an SSH server at specified server and port.\n"
45  		+ "EXAMPLES:\n"
46  		+ "The example will be based upon the following command definition (ini file)\n\n"
47  		+ "check_ssh : CHECK_SSH --hostname $ARG1$ --port $ARG2$ --password $ARG3$ \n"
48  		+ "check_nrpe -H myjnrpeserver -c check_ssh -a myhostname 22 password")
49  @PluginOptions({
50  		@Option(shortName = "h", longName = "hostname", description = "IP or hostname", required = true, hasArgs = true, argName = "hostname", optionalArgs = false, option = "hostname"),
51  		@Option(shortName = "p", longName = "port", description = "Port number. Default is 22.", required = false, hasArgs = true, argName = "port", optionalArgs = false, option = "port"),
52  		@Option(shortName = "u", longName = "username", description = "Username.", required = false, hasArgs = true, argName = "username", optionalArgs = false, option = "username"),
53  		@Option(shortName = "P", longName = "password", description = "Password.", required = false, hasArgs = true, argName = "hostname", optionalArgs = false, option = "password"),
54  		@Option(shortName = "t", longName = "timeout", description = "Seconds before connection times out (default: 10)", required = false, hasArgs = true, argName = "timeout", optionalArgs = false, option = "timeout") })
55  public class CheckSsh extends PluginBase {
56  
57  	/**
58  	 * @TODO
59  	 * 
60  	 *       - ssh key authentication
61  	 * 
62  	 *       - remote-version check option
63  	 * 
64  	 */
65  
66  	@Override
67  	protected String getPluginName() {
68  		return "CHECK_SSH";
69  	}
70  
71  	@Override
72  	public final void configureThresholdEvaluatorBuilder(
73  			final ThresholdsEvaluatorBuilder thrb, final ICommandLine cl)
74  			throws BadThresholdException {
75  		thrb.withLegacyThreshold("connected", "1:", null, "0");
76  	}
77  
78  	@Override
79  	public final Collection<Metric> gatherMetrics(final ICommandLine cl)
80  			throws MetricGatheringException {
81  		List<Metric> metrics = new ArrayList<Metric>();
82  		Session session = null;
83  		try {
84  			session = SshUtils.getSession(cl);
85  			Channel channel = session.openChannel("shell");
86  			channel.setInputStream(System.in);
87  
88  			channel.setOutputStream(System.out);
89  			channel.connect();
90  			metrics.add(new Metric("connected", "", new BigDecimal(1), null,
91  					null));
92  			channel.disconnect();
93  			session.disconnect();
94  
95  		} catch (Exception e) {
96  			metrics.add(new Metric("connected", e.getMessage(), new BigDecimal(
97  					0), null, null));
98  			log.debug(e.getMessage(), e);
99  		}
100 		return metrics;
101 	}
102 
103 }