summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-02-25 11:26:36 +0100
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-07 23:38:50 +0100
commited06df7f34ad72439b2a0ebb0c0e527d2435050a (patch)
tree4b04a076e511b471a61417d7dbf12a8a27b93b1f
parentc87bc7eee4b83571199ffd14b70bfca5418ec101 (diff)
downloadmonitoring-plugins-ed06df7f34ad72439b2a0ebb0c0e527d2435050a.tar.gz
check_ssh: Migrate to new output infrastructure
-rw-r--r--plugins/check_ssh.c99
-rw-r--r--plugins/check_ssh.d/config.h6
2 files changed, 84 insertions, 21 deletions
diff --git a/plugins/check_ssh.c b/plugins/check_ssh.c
index 3745f799..62c8b891 100644
--- a/plugins/check_ssh.c
+++ b/plugins/check_ssh.c
@@ -28,6 +28,9 @@
28 * 28 *
29 *****************************************************************************/ 29 *****************************************************************************/
30 30
31#include "output.h"
32#include "perfdata.h"
33#include "states.h"
31const char *progname = "check_ssh"; 34const char *progname = "check_ssh";
32const char *copyright = "2000-2024"; 35const char *copyright = "2000-2024";
33const char *email = "devel@monitoring-plugins.org"; 36const char *email = "devel@monitoring-plugins.org";
@@ -55,7 +58,7 @@ static process_arguments_wrapper process_arguments(int /*argc*/, char ** /*argv*
55static void print_help(void); 58static void print_help(void);
56void print_usage(void); 59void print_usage(void);
57 60
58static int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_protocol); 61static int ssh_connect(mp_check *overall, char *haddr, int hport, char *remote_version, char *remote_protocol);
59 62
60int main(int argc, char **argv) { 63int main(int argc, char **argv) {
61 setlocale(LC_ALL, ""); 64 setlocale(LC_ALL, "");
@@ -78,14 +81,21 @@ int main(int argc, char **argv) {
78 81
79 alarm(socket_timeout); 82 alarm(socket_timeout);
80 83
84 mp_check overall = mp_check_init();
85 if (config.output_format_is_set) {
86 overall.format = config.output_format;
87 }
88
81 /* ssh_connect exits if error is found */ 89 /* ssh_connect exits if error is found */
82 int result = ssh_connect(config.server_name, config.port, config.remote_version, config.remote_protocol); 90 ssh_connect(&overall, config.server_name, config.port, config.remote_version, config.remote_protocol);
83 91
84 alarm(0); 92 alarm(0);
85 93
86 return (result); 94 mp_exit(overall);
87} 95}
88 96
97#define output_format_index CHAR_MAX + 1
98
89/* process command-line arguments */ 99/* process command-line arguments */
90process_arguments_wrapper process_arguments(int argc, char **argv) { 100process_arguments_wrapper process_arguments(int argc, char **argv) {
91 static struct option longopts[] = {{"help", no_argument, 0, 'h'}, 101 static struct option longopts[] = {{"help", no_argument, 0, 'h'},
@@ -99,6 +109,7 @@ process_arguments_wrapper process_arguments(int argc, char **argv) {
99 {"verbose", no_argument, 0, 'v'}, 109 {"verbose", no_argument, 0, 'v'},
100 {"remote-version", required_argument, 0, 'r'}, 110 {"remote-version", required_argument, 0, 'r'},
101 {"remote-protocol", required_argument, 0, 'P'}, 111 {"remote-protocol", required_argument, 0, 'P'},
112 {"output-format", required_argument, 0, output_format_index},
102 {0, 0, 0, 0}}; 113 {0, 0, 0, 0}};
103 114
104 process_arguments_wrapper result = { 115 process_arguments_wrapper result = {
@@ -173,6 +184,18 @@ process_arguments_wrapper process_arguments(int argc, char **argv) {
173 } else { 184 } else {
174 usage2(_("Port number must be a positive integer"), optarg); 185 usage2(_("Port number must be a positive integer"), optarg);
175 } 186 }
187 case output_format_index: {
188 parsed_output_format parser = mp_parse_output_format(optarg);
189 if (!parser.parsing_success) {
190 // TODO List all available formats here, maybe add anothoer usage function
191 printf("Invalid output format: %s\n", optarg);
192 exit(STATE_UNKNOWN);
193 }
194
195 result.config.output_format_is_set = true;
196 result.config.output_format = parser.output_format;
197 break;
198 }
176 } 199 }
177 } 200 }
178 201
@@ -208,7 +231,7 @@ process_arguments_wrapper process_arguments(int argc, char **argv) {
208 * 231 *
209 *-----------------------------------------------------------------------*/ 232 *-----------------------------------------------------------------------*/
210 233
211int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_protocol) { 234int ssh_connect(mp_check *overall, char *haddr, int hport, char *desired_remote_version, char *desired_remote_protocol) {
212 struct timeval tv; 235 struct timeval tv;
213 gettimeofday(&tv, NULL); 236 gettimeofday(&tv, NULL);
214 237
@@ -260,15 +283,25 @@ int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_proto
260 } 283 }
261 } 284 }
262 285
286 mp_subcheck connection_sc = mp_subcheck_init();
263 if (recv_ret < 0) { 287 if (recv_ret < 0) {
264 printf("SSH CRITICAL - %s", strerror(errno)); 288 connection_sc = mp_set_subcheck_state(connection_sc, STATE_CRITICAL);
265 exit(STATE_CRITICAL); 289 xasprintf(&connection_sc.output, "%s", "SSH CRITICAL - %s", strerror(errno));
290 mp_add_subcheck_to_check(overall, connection_sc);
291 return OK;
266 } 292 }
267 293
268 if (version_control_string == NULL) { 294 if (version_control_string == NULL) {
269 printf("SSH CRITICAL - No version control string received"); 295 connection_sc = mp_set_subcheck_state(connection_sc, STATE_CRITICAL);
270 exit(STATE_CRITICAL); 296 xasprintf(&connection_sc.output, "%s", "SSH CRITICAL - No version control string received");
297 mp_add_subcheck_to_check(overall, connection_sc);
298 return OK;
271 } 299 }
300
301 connection_sc = mp_set_subcheck_state(connection_sc, STATE_OK);
302 xasprintf(&connection_sc.output, "%s", "Initial connection succeded");
303 mp_add_subcheck_to_check(overall, connection_sc);
304
272 /* 305 /*
273 * "When the connection has been established, both sides MUST send an 306 * "When the connection has been established, both sides MUST send an
274 * identification string. This identification string MUST be 307 * identification string. This identification string MUST be
@@ -307,10 +340,19 @@ int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_proto
307 if (tmp) { 340 if (tmp) {
308 ssh_server[tmp - ssh_server] = '\0'; 341 ssh_server[tmp - ssh_server] = '\0';
309 } 342 }
343
344 mp_subcheck protocol_validity_sc = mp_subcheck_init();
310 if (strlen(ssh_proto) == 0 || strlen(ssh_server) == 0) { 345 if (strlen(ssh_proto) == 0 || strlen(ssh_server) == 0) {
311 printf(_("SSH CRITICAL - Invalid protocol version control string %s\n"), version_control_string); 346 protocol_validity_sc = mp_set_subcheck_state(protocol_validity_sc, STATE_CRITICAL);
312 exit(STATE_CRITICAL); 347 xasprintf(&protocol_validity_sc.output, "Invalid protocol version control string %s", version_control_string);
348 mp_add_subcheck_to_check(overall, protocol_validity_sc);
349 return OK;
313 } 350 }
351
352 protocol_validity_sc = mp_set_subcheck_state(protocol_validity_sc, STATE_OK);
353 xasprintf(&protocol_validity_sc.output, "Valid protocol version control string %s", version_control_string);
354 mp_add_subcheck_to_check(overall, protocol_validity_sc);
355
314 ssh_proto[strspn(ssh_proto, "0123456789. ")] = 0; 356 ssh_proto[strspn(ssh_proto, "0123456789. ")] = 0;
315 357
316 static char *rev_no = VERSION; 358 static char *rev_no = VERSION;
@@ -320,24 +362,38 @@ int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_proto
320 printf("%s\n", buffer); 362 printf("%s\n", buffer);
321 } 363 }
322 364
323 if (remote_version && strcmp(remote_version, ssh_server)) { 365 if (desired_remote_version && strcmp(desired_remote_version, ssh_server)) {
324 printf(_("SSH CRITICAL - %s (protocol %s) version mismatch, expected '%s'\n"), ssh_server, ssh_proto, remote_version); 366 mp_subcheck remote_version_sc = mp_subcheck_init();
367 remote_version_sc = mp_set_subcheck_state(remote_version_sc, STATE_CRITICAL);
368 xasprintf(&remote_version_sc.output, _("%s (protocol %s) version mismatch, expected '%s'"), ssh_server, ssh_proto,
369 desired_remote_version);
325 close(socket); 370 close(socket);
326 exit(STATE_CRITICAL); 371 mp_add_subcheck_to_check(overall, remote_version_sc);
372 return OK;
327 } 373 }
328 374
329 double elapsed_time = (double)deltime(tv) / 1.0e6; 375 double elapsed_time = (double)deltime(tv) / 1.0e6;
330 if (remote_protocol && strcmp(remote_protocol, ssh_proto)) { 376 mp_perfdata time_pd = perfdata_init();
331 printf(_("SSH CRITICAL - %s (protocol %s) protocol version mismatch, expected '%s' | %s\n"), ssh_server, ssh_proto, remote_protocol, 377 time_pd.value = mp_create_pd_value(elapsed_time);
332 fperfdata("time", elapsed_time, "s", false, 0, false, 0, true, 0, true, (int)socket_timeout)); 378 time_pd.label = "time";
333 close(socket); 379 time_pd.max_present = true;
334 exit(STATE_CRITICAL); 380 time_pd.max = mp_create_pd_value(socket_timeout);
381
382 mp_subcheck protocol_version_sc = mp_subcheck_init();
383 mp_add_perfdata_to_subcheck(&protocol_version_sc, time_pd);
384
385 if (desired_remote_protocol && strcmp(desired_remote_protocol, ssh_proto)) {
386 protocol_version_sc = mp_set_subcheck_state(protocol_version_sc, STATE_CRITICAL);
387 xasprintf(&protocol_version_sc.output, _("%s (protocol %s) protocol version mismatch, expected '%s'"), ssh_server, ssh_proto,
388 desired_remote_protocol);
389 } else {
390 protocol_version_sc = mp_set_subcheck_state(protocol_version_sc, STATE_OK);
391 xasprintf(&protocol_version_sc.output, "SSH server verison: %s (protocol version: %s)", ssh_server, ssh_proto);
335 } 392 }
336 393
337 printf(_("SSH OK - %s (protocol %s) | %s\n"), ssh_server, ssh_proto, 394 mp_add_subcheck_to_check(overall, protocol_version_sc);
338 fperfdata("time", elapsed_time, "s", false, 0, false, 0, true, 0, true, (int)socket_timeout));
339 close(socket); 395 close(socket);
340 exit(STATE_OK); 396 return OK;
341} 397}
342 398
343void print_help(void) { 399void print_help(void) {
@@ -369,6 +425,7 @@ void print_help(void) {
369 425
370 printf(" %s\n", "-P, --remote-protocol=STRING"); 426 printf(" %s\n", "-P, --remote-protocol=STRING");
371 printf(" %s\n", _("Alert if protocol doesn't match expected protocol version (ex: 2.0)")); 427 printf(" %s\n", _("Alert if protocol doesn't match expected protocol version (ex: 2.0)"));
428 printf(UT_OUTPUT_FORMAT);
372 429
373 printf(UT_VERBOSE); 430 printf(UT_VERBOSE);
374 431
diff --git a/plugins/check_ssh.d/config.h b/plugins/check_ssh.d/config.h
index 05698d83..d739c57c 100644
--- a/plugins/check_ssh.d/config.h
+++ b/plugins/check_ssh.d/config.h
@@ -1,12 +1,16 @@
1#pragma once 1#pragma once
2 2
3#include <stddef.h> 3#include <stddef.h>
4#include "../../lib/monitoringplug.h"
4 5
5typedef struct check_ssh_config { 6typedef struct check_ssh_config {
6 int port; 7 int port;
7 char *server_name; 8 char *server_name;
8 char *remote_version; 9 char *remote_version;
9 char *remote_protocol; 10 char *remote_protocol;
11
12 bool output_format_is_set;
13 mp_output_format output_format;
10} check_ssh_config; 14} check_ssh_config;
11 15
12check_ssh_config check_ssh_config_init(void) { 16check_ssh_config check_ssh_config_init(void) {
@@ -15,6 +19,8 @@ check_ssh_config check_ssh_config_init(void) {
15 .server_name = NULL, 19 .server_name = NULL,
16 .remote_version = NULL, 20 .remote_version = NULL,
17 .remote_protocol = NULL, 21 .remote_protocol = NULL,
22
23 .output_format_is_set = false,
18 }; 24 };
19 25
20 return tmp; 26 return tmp;