diff options
author | Jonas Gunz <himself@jonasgunz.de> | 2022-11-21 00:41:12 +0100 |
---|---|---|
committer | Jonas Gunz <himself@jonasgunz.de> | 2022-11-21 00:41:12 +0100 |
commit | 8d288cf0833621aa9097bc08e49b6833616b64a1 (patch) | |
tree | b73451ee7e5854c062c4c8240a705b8c64789707 | |
parent | 1bc6d81651c5b5fe865515d420fc2772f4997059 (diff) | |
download | monitoring-plugins-8d288cf0833621aa9097bc08e49b6833616b64a1.tar.gz |
check_by_ssh: Add random wait option -r
Added option -r, --random-wait=MAXTIME to wait for a random amount
of seconds between 0 and MAXTIME.
This should help with deployments which check multiple services
over SSH. I found that adding a wrapper to wait randomly for max
10s reduced the amount of failed SSH connections dramatically,
and thus eliminated many false alarms.
-rw-r--r-- | plugins/check_by_ssh.c | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/plugins/check_by_ssh.c b/plugins/check_by_ssh.c index 1ad547ed..405f1c2b 100644 --- a/plugins/check_by_ssh.c +++ b/plugins/check_by_ssh.c @@ -35,6 +35,10 @@ const char *email = "devel@monitoring-plugins.org"; #include "netutils.h" #include "utils_cmd.h" +#include <stdint.h> +#include <sys/stat.h> +#include <fcntl.h> + #ifndef NP_MAXARGS #define NP_MAXARGS 1024 #endif @@ -60,6 +64,7 @@ char *host_shortname = NULL; char **service; int passive = FALSE; int verbose = FALSE; +unsigned int random_wait_max = 0; int main (int argc, char **argv) @@ -87,6 +92,16 @@ main (int argc, char **argv) if (process_arguments (argc, argv) == ERROR) usage_va(_("Could not parse arguments")); + /* Wait a random amount of time */ + if (random_wait_max) { + uint8_t random = 0; + int fd = open("/dev/urandom", O_RDONLY); + read(fd, &random, sizeof(uint8_t)); + close(fd); + + sleep( (random_wait_max * random) / 255 ); + } + /* Set signal handling and alarm timeout */ if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) { usage_va(_("Cannot catch SIGALRM")); @@ -210,6 +225,7 @@ process_arguments (int argc, char **argv) {"ssh-option", required_argument, 0, 'o'}, {"quiet", no_argument, 0, 'q'}, {"configfile", optional_argument, 0, 'F'}, + {"random-wait", optional_argument, 0, 'r'}, {0, 0, 0, 0} }; @@ -221,7 +237,7 @@ process_arguments (int argc, char **argv) strcpy (argv[c], "-t"); while (1) { - c = getopt_long (argc, argv, "Vvh1246fqt:UH:O:p:i:u:l:C:S::E::n:s:o:F:", longopts, + c = getopt_long (argc, argv, "Vvh1246fqt:UH:O:p:i:u:l:C:S::E::n:s:o:F:r:", longopts, &option); if (c == -1 || c == EOF) @@ -338,6 +354,11 @@ process_arguments (int argc, char **argv) comm_append("-F"); comm_append(optarg); break; + case 'r': + if (!is_integer (optarg)) + usage_va(_("Port must be a positive integer")); + random_wait_max = atoi( optarg ); + break; default: /* help */ usage5(); } @@ -455,6 +476,8 @@ print_help (void) printf (" %s\n", _("Tell ssh to use this configfile [optional]")); printf (" %s\n","-q, --quiet"); printf (" %s\n", _("Tell ssh to suppress warning and diagnostic messages [optional]")); + printf (" %s\n","-r, --random-wait=MAXTIME"); + printf (" %s\n", _("Wait a random amount of seconds between 0 and MAXTIME before connecting [optional]")); printf (UT_WARN_CRIT); printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); printf (" %s\n","-U, --unknown-timeout"); @@ -491,6 +514,6 @@ print_usage (void) printf (" %s -H <host> -C <command> [-fqvU] [-1|-2] [-4|-6]\n" " [-S [lines]] [-E [lines]] [-W] [-t timeout] [-i identity]\n" " [-l user] [-n name] [-s servicelist] [-O outputfile]\n" - " [-p port] [-o ssh-option] [-F configfile]\n", + " [-p port] [-o ssh-option] [-F configfile] [-r randomwait]\n", progname); } |