check_procs: Assume we have stat()
[monitoring-plugins.git] / plugins / check_ssh.c
blob6e8a5fc5d65ef45adf2abb8a42247c2125f18c78
1 /*****************************************************************************
2 *
3 * Nagios check_ssh plugin
4 *
5 * License: GPL
6 * Copyright (c) 2000-2007 Nagios Plugins Development Team
7 *
8 * Description:
9 *
10 * This file contains the check_ssh plugin
12 * Try to connect to an SSH server at specified server and port
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 *****************************************************************************/
31 const char *progname = "check_ssh";
32 const char *copyright = "2000-2007";
33 const char *email = "nagiosplug-devel@lists.sourceforge.net";
35 #include "common.h"
36 #include "netutils.h"
37 #include "utils.h"
39 #ifndef MSG_DONTWAIT
40 #define MSG_DONTWAIT 0
41 #endif
43 #define SSH_DFL_PORT 22
44 #define BUFF_SZ 256
46 int port = -1;
47 char *server_name = NULL;
48 char *remote_version = NULL;
49 int verbose = FALSE;
51 int process_arguments (int, char **);
52 int validate_arguments (void);
53 void print_help (void);
54 void print_usage (void);
56 int ssh_connect (char *haddr, int hport, char *remote_version);
60 int
61 main (int argc, char **argv)
63 int result = STATE_UNKNOWN;
65 setlocale (LC_ALL, "");
66 bindtextdomain (PACKAGE, LOCALEDIR);
67 textdomain (PACKAGE);
69 /* Parse extra opts if any */
70 argv=np_extra_opts (&argc, argv, progname);
72 if (process_arguments (argc, argv) == ERROR)
73 usage4 (_("Could not parse arguments"));
75 /* initialize alarm signal handling */
76 signal (SIGALRM, socket_timeout_alarm_handler);
78 alarm (socket_timeout);
80 /* ssh_connect exits if error is found */
81 result = ssh_connect (server_name, port, remote_version);
83 alarm (0);
85 return (result);
90 /* process command-line arguments */
91 int
92 process_arguments (int argc, char **argv)
94 int c;
96 int option = 0;
97 static struct option longopts[] = {
98 {"help", no_argument, 0, 'h'},
99 {"version", no_argument, 0, 'V'},
100 {"host", required_argument, 0, 'H'}, /* backward compatibility */
101 {"hostname", required_argument, 0, 'H'},
102 {"port", required_argument, 0, 'p'},
103 {"use-ipv4", no_argument, 0, '4'},
104 {"use-ipv6", no_argument, 0, '6'},
105 {"timeout", required_argument, 0, 't'},
106 {"verbose", no_argument, 0, 'v'},
107 {"remote-version", required_argument, 0, 'r'},
108 {0, 0, 0, 0}
111 if (argc < 2)
112 return ERROR;
114 for (c = 1; c < argc; c++)
115 if (strcmp ("-to", argv[c]) == 0)
116 strcpy (argv[c], "-t");
118 while (1) {
119 c = getopt_long (argc, argv, "+Vhv46t:r:H:p:", longopts, &option);
121 if (c == -1 || c == EOF)
122 break;
124 switch (c) {
125 case '?': /* help */
126 usage5 ();
127 case 'V': /* version */
128 print_revision (progname, NP_VERSION);
129 exit (STATE_OK);
130 case 'h': /* help */
131 print_help ();
132 exit (STATE_OK);
133 case 'v': /* verbose */
134 verbose = TRUE;
135 break;
136 case 't': /* timeout period */
137 if (!is_integer (optarg))
138 usage2 (_("Timeout interval must be a positive integer"), optarg);
139 else
140 socket_timeout = atoi (optarg);
141 break;
142 case '4':
143 address_family = AF_INET;
144 break;
145 case '6':
146 #ifdef USE_IPV6
147 address_family = AF_INET6;
148 #else
149 usage4 (_("IPv6 support not available"));
150 #endif
151 break;
152 case 'r': /* remote version */
153 remote_version = optarg;
154 break;
155 case 'H': /* host */
156 if (is_host (optarg) == FALSE)
157 usage2 (_("Invalid hostname/address"), optarg);
158 server_name = optarg;
159 break;
160 case 'p': /* port */
161 if (is_intpos (optarg)) {
162 port = atoi (optarg);
164 else {
165 usage2 (_("Port number must be a positive integer"), optarg);
170 c = optind;
171 if (server_name == NULL && c < argc) {
172 if (is_host (argv[c])) {
173 server_name = argv[c++];
177 if (port == -1 && c < argc) {
178 if (is_intpos (argv[c])) {
179 port = atoi (argv[c++]);
181 else {
182 print_usage ();
183 exit (STATE_UNKNOWN);
187 return validate_arguments ();
191 validate_arguments (void)
193 if (server_name == NULL)
194 return ERROR;
195 if (port == -1) /* funky, but allows -p to override stray integer in args */
196 port = SSH_DFL_PORT;
197 return OK;
201 /************************************************************************
203 * Try to connect to SSH server at specified server and port
205 *-----------------------------------------------------------------------*/
209 ssh_connect (char *haddr, int hport, char *remote_version)
211 int sd;
212 int result;
213 char *output = NULL;
214 char *buffer = NULL;
215 char *ssh_proto = NULL;
216 char *ssh_server = NULL;
217 static char *rev_no = VERSION;
218 struct timeval tv;
219 double elapsed_time;
221 gettimeofday(&tv, NULL);
223 result = my_tcp_connect (haddr, hport, &sd);
225 if (result != STATE_OK)
226 return result;
228 output = (char *) malloc (BUFF_SZ + 1);
229 memset (output, 0, BUFF_SZ + 1);
230 recv (sd, output, BUFF_SZ, 0);
231 if (strncmp (output, "SSH", 3)) {
232 printf (_("Server answer: %s"), output);
233 close(sd);
234 exit (STATE_CRITICAL);
236 else {
237 strip (output);
238 if (verbose)
239 printf ("%s\n", output);
240 ssh_proto = output + 4;
241 ssh_server = ssh_proto + strspn (ssh_proto, "-0123456789. ");
242 ssh_proto[strspn (ssh_proto, "0123456789. ")] = 0;
244 xasprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, rev_no);
245 send (sd, buffer, strlen (buffer), MSG_DONTWAIT);
246 if (verbose)
247 printf ("%s\n", buffer);
249 if (remote_version && strcmp(remote_version, ssh_server)) {
250 printf
251 (_("SSH WARNING - %s (protocol %s) version mismatch, expected '%s'\n"),
252 ssh_server, ssh_proto, remote_version);
253 close(sd);
254 exit (STATE_WARNING);
257 elapsed_time = (double)deltime(tv) / 1.0e6;
259 printf
260 (_("SSH OK - %s (protocol %s) | %s\n"),
261 ssh_server, ssh_proto, fperfdata("time", elapsed_time, "s",
262 FALSE, 0, FALSE, 0, TRUE, 0, TRUE, (int)socket_timeout));
263 close(sd);
264 exit (STATE_OK);
270 void
271 print_help (void)
273 char *myport;
274 xasprintf (&myport, "%d", SSH_DFL_PORT);
276 print_revision (progname, NP_VERSION);
278 printf ("Copyright (c) 1999 Remi Paulmier <remi@sinfomic.fr>\n");
279 printf (COPYRIGHT, copyright, email);
281 printf ("%s\n", _("Try to connect to an SSH server at specified server and port"));
283 printf ("\n\n");
285 print_usage ();
287 printf (UT_HELP_VRSN);
288 printf (UT_EXTRA_OPTS);
290 printf (UT_HOST_PORT, 'p', myport);
292 printf (UT_IPv46);
294 printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
296 printf (" %s\n", "-r, --remote-version=STRING");
297 printf (" %s\n", _("Warn if string doesn't match expected server version (ex: OpenSSH_3.9p1)"));
299 printf (UT_VERBOSE);
301 printf (UT_SUPPORT);
306 void
307 print_usage (void)
309 printf ("%s\n", _("Usage:"));
310 printf ("%s [-4|-6] [-t <timeout>] [-r <remote version>] [-p <port>] <host>\n", progname);