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.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
39
40
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
59
60
61
62
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 }