diff options
| -rw-r--r-- | .github/NPTest.cache | 4 | ||||
| -rwxr-xr-x | .github/prepare_debian.sh | 1 | ||||
| -rw-r--r-- | NPTest.pm | 4 | ||||
| -rw-r--r-- | lib/output.c | 12 | ||||
| -rw-r--r-- | lib/output.h | 7 | ||||
| -rw-r--r-- | plugins/Makefile.am | 4 | ||||
| -rw-r--r-- | plugins/check_dbi.c | 445 | ||||
| -rw-r--r-- | plugins/check_dbi.d/config.h | 63 | ||||
| -rw-r--r-- | plugins/check_mysql.c | 689 | ||||
| -rw-r--r-- | plugins/check_ssh.c | 210 | ||||
| -rw-r--r-- | plugins/check_ssh.d/config.h | 29 | ||||
| -rw-r--r-- | plugins/check_swap.c | 2 | ||||
| -rw-r--r-- | plugins/netutils.c | 93 | ||||
| -rw-r--r-- | plugins/t/check_http.t | 2 | ||||
| -rw-r--r-- | plugins/t/check_jabber.t | 2 | ||||
| -rw-r--r-- | plugins/t/check_ldap.t | 2 | ||||
| -rw-r--r-- | plugins/t/check_mysql.t | 32 | ||||
| -rw-r--r-- | plugins/t/check_ntp.t | 2 | ||||
| -rw-r--r-- | plugins/t/check_smtp.t | 3 | ||||
| -rw-r--r-- | plugins/t/check_ssh.t | 114 | ||||
| -rw-r--r-- | plugins/t/check_swap.t | 35 |
21 files changed, 1035 insertions, 720 deletions
diff --git a/.github/NPTest.cache b/.github/NPTest.cache index d488d1b9..6b463e74 100644 --- a/.github/NPTest.cache +++ b/.github/NPTest.cache | |||
| @@ -38,8 +38,8 @@ | |||
| 38 | 'NP_MYSQL_LOGIN_DETAILS' => '-u root -d test', | 38 | 'NP_MYSQL_LOGIN_DETAILS' => '-u root -d test', |
| 39 | 'NP_MYSQL_SERVER' => 'localhost', | 39 | 'NP_MYSQL_SERVER' => 'localhost', |
| 40 | 'NP_MYSQL_SOCKET' => '/var/run/mysqld/mysqld.sock', | 40 | 'NP_MYSQL_SOCKET' => '/var/run/mysqld/mysqld.sock', |
| 41 | 'NP_MYSQL_WITH_SLAVE' => '', | 41 | 'NP_MYSQL_WITH_REPLICA' => '', |
| 42 | 'NP_MYSQL_WITH_SLAVE_LOGIN' => '', | 42 | 'NP_MYSQL_WITH_REPLICA_LOGIN' => '', |
| 43 | 'NP_NO_NTP_SERVICE' => 'localhost', | 43 | 'NP_NO_NTP_SERVICE' => 'localhost', |
| 44 | 'NP_PORT_TCP_PROXY' => '3128', | 44 | 'NP_PORT_TCP_PROXY' => '3128', |
| 45 | 'NP_SMB_SHARE' => '', | 45 | 'NP_SMB_SHARE' => '', |
diff --git a/.github/prepare_debian.sh b/.github/prepare_debian.sh index c79211cd..f7b6cf9f 100755 --- a/.github/prepare_debian.sh +++ b/.github/prepare_debian.sh | |||
| @@ -59,6 +59,7 @@ apt-get -y install perl \ | |||
| 59 | mariadb-server \ | 59 | mariadb-server \ |
| 60 | mariadb-client \ | 60 | mariadb-client \ |
| 61 | libmariadb-dev \ | 61 | libmariadb-dev \ |
| 62 | libmariadb-dev-compat \ | ||
| 62 | cron \ | 63 | cron \ |
| 63 | iputils-ping \ | 64 | iputils-ping \ |
| 64 | iproute2 \ | 65 | iproute2 \ |
| @@ -15,6 +15,8 @@ use warnings; | |||
| 15 | use Cwd; | 15 | use Cwd; |
| 16 | use File::Basename; | 16 | use File::Basename; |
| 17 | 17 | ||
| 18 | use JSON; | ||
| 19 | |||
| 18 | use IO::File; | 20 | use IO::File; |
| 19 | use Data::Dumper; | 21 | use Data::Dumper; |
| 20 | 22 | ||
| @@ -617,6 +619,8 @@ sub testCmd { | |||
| 617 | chomp $output; | 619 | chomp $output; |
| 618 | $object->output($output); | 620 | $object->output($output); |
| 619 | 621 | ||
| 622 | eval { $object->{'mp_test_result'} = decode_json($output) }; | ||
| 623 | |||
| 620 | alarm(0); | 624 | alarm(0); |
| 621 | 625 | ||
| 622 | my ($pkg, $file, $line) = caller(0); | 626 | my ($pkg, $file, $line) = caller(0); |
diff --git a/lib/output.c b/lib/output.c index 17919afc..61fbf832 100644 --- a/lib/output.c +++ b/lib/output.c | |||
| @@ -11,6 +11,9 @@ | |||
| 11 | #include "perfdata.h" | 11 | #include "perfdata.h" |
| 12 | #include "states.h" | 12 | #include "states.h" |
| 13 | 13 | ||
| 14 | // == Global variables | ||
| 15 | static mp_output_format output_format = MP_FORMAT_DEFAULT; | ||
| 16 | |||
| 14 | // == Prototypes == | 17 | // == Prototypes == |
| 15 | static char *fmt_subcheck_output(mp_output_format output_format, mp_subcheck check, unsigned int indentation); | 18 | static char *fmt_subcheck_output(mp_output_format output_format, mp_subcheck check, unsigned int indentation); |
| 16 | static inline cJSON *json_serialize_subcheck(mp_subcheck subcheck); | 19 | static inline cJSON *json_serialize_subcheck(mp_subcheck subcheck); |
| @@ -55,7 +58,6 @@ static inline char *fmt_subcheck_perfdata(mp_subcheck check) { | |||
| 55 | */ | 58 | */ |
| 56 | mp_check mp_check_init(void) { | 59 | mp_check mp_check_init(void) { |
| 57 | mp_check check = {0}; | 60 | mp_check check = {0}; |
| 58 | check.format = MP_FORMAT_DEFAULT; | ||
| 59 | return check; | 61 | return check; |
| 60 | } | 62 | } |
| 61 | 63 | ||
| @@ -234,7 +236,7 @@ mp_state_enum mp_compute_check_state(const mp_check check) { | |||
| 234 | char *mp_fmt_output(mp_check check) { | 236 | char *mp_fmt_output(mp_check check) { |
| 235 | char *result = NULL; | 237 | char *result = NULL; |
| 236 | 238 | ||
| 237 | switch (check.format) { | 239 | switch (output_format) { |
| 238 | case MP_FORMAT_MULTI_LINE: { | 240 | case MP_FORMAT_MULTI_LINE: { |
| 239 | if (check.summary == NULL) { | 241 | if (check.summary == NULL) { |
| 240 | check.summary = get_subcheck_summary(check); | 242 | check.summary = get_subcheck_summary(check); |
| @@ -482,7 +484,7 @@ void mp_print_output(mp_check check) { puts(mp_fmt_output(check)); } | |||
| 482 | */ | 484 | */ |
| 483 | void mp_exit(mp_check check) { | 485 | void mp_exit(mp_check check) { |
| 484 | mp_print_output(check); | 486 | mp_print_output(check); |
| 485 | if (check.format == MP_FORMAT_TEST_JSON) { | 487 | if (output_format == MP_FORMAT_TEST_JSON) { |
| 486 | exit(0); | 488 | exit(0); |
| 487 | } | 489 | } |
| 488 | 490 | ||
| @@ -533,3 +535,7 @@ parsed_output_format mp_parse_output_format(char *format_string) { | |||
| 533 | 535 | ||
| 534 | return result; | 536 | return result; |
| 535 | } | 537 | } |
| 538 | |||
| 539 | void mp_set_format(mp_output_format format) { output_format = format; } | ||
| 540 | |||
| 541 | mp_output_format mp_get_format(void) { return output_format; } | ||
diff --git a/lib/output.h b/lib/output.h index ffc36f53..2bdfa074 100644 --- a/lib/output.h +++ b/lib/output.h | |||
| @@ -36,13 +36,18 @@ typedef enum output_format { | |||
| 36 | #define MP_FORMAT_DEFAULT MP_FORMAT_MULTI_LINE | 36 | #define MP_FORMAT_DEFAULT MP_FORMAT_MULTI_LINE |
| 37 | 37 | ||
| 38 | /* | 38 | /* |
| 39 | * Format related functions | ||
| 40 | */ | ||
| 41 | void mp_set_format(mp_output_format format); | ||
| 42 | mp_output_format mp_get_format(void); | ||
| 43 | |||
| 44 | /* | ||
| 39 | * The main state object of a plugin. Exists only ONCE per plugin. | 45 | * The main state object of a plugin. Exists only ONCE per plugin. |
| 40 | * This is the "root" of a tree of singular checks. | 46 | * This is the "root" of a tree of singular checks. |
| 41 | * The final result is always derived from the children and the "worst" state | 47 | * The final result is always derived from the children and the "worst" state |
| 42 | * in the first layer of subchecks | 48 | * in the first layer of subchecks |
| 43 | */ | 49 | */ |
| 44 | typedef struct { | 50 | typedef struct { |
| 45 | mp_output_format format; // The output format | ||
| 46 | char *summary; // Overall summary, if not set a summary will be automatically generated | 51 | char *summary; // Overall summary, if not set a summary will be automatically generated |
| 47 | mp_subcheck_list *subchecks; | 52 | mp_subcheck_list *subchecks; |
| 48 | } mp_check; | 53 | } mp_check; |
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index fb689f87..8e35bc61 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am | |||
| @@ -50,7 +50,9 @@ EXTRA_DIST = t \ | |||
| 50 | tests \ | 50 | tests \ |
| 51 | $(np_test_scripts) \ | 51 | $(np_test_scripts) \ |
| 52 | check_swap.d \ | 52 | check_swap.d \ |
| 53 | check_game.d | 53 | check_game.d \ |
| 54 | check_dbi.d \ | ||
| 55 | check_ssh.d | ||
| 54 | 56 | ||
| 55 | PLUGINHDRS = common.h | 57 | PLUGINHDRS = common.h |
| 56 | 58 | ||
diff --git a/plugins/check_dbi.c b/plugins/check_dbi.c index 96575672..9efcd1cb 100644 --- a/plugins/check_dbi.c +++ b/plugins/check_dbi.c | |||
| @@ -33,6 +33,8 @@ const char *progname = "check_dbi"; | |||
| 33 | const char *copyright = "2011-2024"; | 33 | const char *copyright = "2011-2024"; |
| 34 | const char *email = "devel@monitoring-plugins.org"; | 34 | const char *email = "devel@monitoring-plugins.org"; |
| 35 | 35 | ||
| 36 | #include "../lib/monitoringplug.h" | ||
| 37 | #include "check_dbi.d/config.h" | ||
| 36 | #include "common.h" | 38 | #include "common.h" |
| 37 | #include "utils.h" | 39 | #include "utils.h" |
| 38 | #include "utils_cmd.h" | 40 | #include "utils_cmd.h" |
| @@ -53,55 +55,24 @@ const char *email = "devel@monitoring-plugins.org"; | |||
| 53 | 55 | ||
| 54 | #include <stdarg.h> | 56 | #include <stdarg.h> |
| 55 | 57 | ||
| 56 | typedef enum { | ||
| 57 | METRIC_CONN_TIME, | ||
| 58 | METRIC_SERVER_VERSION, | ||
| 59 | METRIC_QUERY_RESULT, | ||
| 60 | METRIC_QUERY_TIME, | ||
| 61 | } np_dbi_metric_t; | ||
| 62 | |||
| 63 | typedef enum { | ||
| 64 | TYPE_NUMERIC, | ||
| 65 | TYPE_STRING, | ||
| 66 | } np_dbi_type_t; | ||
| 67 | |||
| 68 | typedef struct { | ||
| 69 | char *key; | ||
| 70 | char *value; | ||
| 71 | } driver_option_t; | ||
| 72 | |||
| 73 | static char *host = NULL; | ||
| 74 | static int verbose = 0; | 58 | static int verbose = 0; |
| 75 | 59 | ||
| 76 | static char *warning_range = NULL; | 60 | typedef struct { |
| 77 | static char *critical_range = NULL; | 61 | int errorcode; |
| 78 | static thresholds *dbi_thresholds = NULL; | 62 | check_dbi_config config; |
| 79 | 63 | } check_dbi_config_wrapper; | |
| 80 | static char *expect = NULL; | ||
| 81 | |||
| 82 | static regex_t expect_re; | ||
| 83 | static char *expect_re_str = NULL; | ||
| 84 | static int expect_re_cflags = 0; | ||
| 85 | |||
| 86 | static np_dbi_metric_t metric = METRIC_QUERY_RESULT; | ||
| 87 | static np_dbi_type_t type = TYPE_NUMERIC; | ||
| 88 | |||
| 89 | static char *np_dbi_driver = NULL; | ||
| 90 | static driver_option_t *np_dbi_options = NULL; | ||
| 91 | static int np_dbi_options_num = 0; | ||
| 92 | static char *np_dbi_database = NULL; | ||
| 93 | static char *np_dbi_query = NULL; | ||
| 94 | 64 | ||
| 95 | static int process_arguments(int, char **); | 65 | static check_dbi_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); |
| 96 | static int validate_arguments(void); | 66 | static check_dbi_config_wrapper validate_arguments(check_dbi_config_wrapper /*config_wrapper*/); |
| 97 | void print_usage(void); | 67 | void print_usage(void); |
| 98 | static void print_help(void); | 68 | static void print_help(void); |
| 99 | 69 | ||
| 100 | static double timediff(struct timeval, struct timeval); | 70 | static double timediff(struct timeval /*start*/, struct timeval /*end*/); |
| 101 | 71 | ||
| 102 | static void np_dbi_print_error(dbi_conn, char *, ...); | 72 | static void np_dbi_print_error(dbi_conn /*conn*/, char * /*fmt*/, ...); |
| 103 | 73 | ||
| 104 | static int do_query(dbi_conn, const char **, double *, double *); | 74 | static mp_state_enum do_query(dbi_conn /*conn*/, const char ** /*res_val_str*/, double * /*res_val*/, double * /*res_time*/, mp_dbi_metric /*metric*/, |
| 75 | mp_dbi_type /*type*/, char * /*np_dbi_query*/); | ||
| 105 | 76 | ||
| 106 | int main(int argc, char **argv) { | 77 | int main(int argc, char **argv) { |
| 107 | int status = STATE_UNKNOWN; | 78 | int status = STATE_UNKNOWN; |
| @@ -119,8 +90,6 @@ int main(int argc, char **argv) { | |||
| 119 | const char *query_val_str = NULL; | 90 | const char *query_val_str = NULL; |
| 120 | double query_val = 0.0; | 91 | double query_val = 0.0; |
| 121 | 92 | ||
| 122 | int i; | ||
| 123 | |||
| 124 | setlocale(LC_ALL, ""); | 93 | setlocale(LC_ALL, ""); |
| 125 | bindtextdomain(PACKAGE, LOCALEDIR); | 94 | bindtextdomain(PACKAGE, LOCALEDIR); |
| 126 | textdomain(PACKAGE); | 95 | textdomain(PACKAGE); |
| @@ -128,8 +97,13 @@ int main(int argc, char **argv) { | |||
| 128 | /* Parse extra opts if any */ | 97 | /* Parse extra opts if any */ |
| 129 | argv = np_extra_opts(&argc, argv, progname); | 98 | argv = np_extra_opts(&argc, argv, progname); |
| 130 | 99 | ||
| 131 | if (process_arguments(argc, argv) == ERROR) | 100 | check_dbi_config_wrapper tmp = process_arguments(argc, argv); |
| 101 | |||
| 102 | if (tmp.errorcode == ERROR) { | ||
| 132 | usage4(_("Could not parse arguments")); | 103 | usage4(_("Could not parse arguments")); |
| 104 | } | ||
| 105 | |||
| 106 | const check_dbi_config config = tmp.config; | ||
| 133 | 107 | ||
| 134 | /* Set signal handling and alarm */ | 108 | /* Set signal handling and alarm */ |
| 135 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { | 109 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { |
| @@ -137,8 +111,9 @@ int main(int argc, char **argv) { | |||
| 137 | } | 111 | } |
| 138 | alarm(timeout_interval); | 112 | alarm(timeout_interval); |
| 139 | 113 | ||
| 140 | if (verbose > 2) | 114 | if (verbose > 2) { |
| 141 | printf("Initializing DBI\n"); | 115 | printf("Initializing DBI\n"); |
| 116 | } | ||
| 142 | 117 | ||
| 143 | dbi_inst *instance_p = {0}; | 118 | dbi_inst *instance_p = {0}; |
| 144 | 119 | ||
| @@ -152,12 +127,13 @@ int main(int argc, char **argv) { | |||
| 152 | return STATE_UNKNOWN; | 127 | return STATE_UNKNOWN; |
| 153 | } | 128 | } |
| 154 | 129 | ||
| 155 | if (verbose) | 130 | if (verbose) { |
| 156 | printf("Opening DBI driver '%s'\n", np_dbi_driver); | 131 | printf("Opening DBI driver '%s'\n", config.dbi_driver); |
| 132 | } | ||
| 157 | 133 | ||
| 158 | driver = dbi_driver_open_r(np_dbi_driver, instance_p); | 134 | driver = dbi_driver_open_r(config.dbi_driver, instance_p); |
| 159 | if (!driver) { | 135 | if (!driver) { |
| 160 | printf("UNKNOWN - failed to open DBI driver '%s'; possibly it's not installed.\n", np_dbi_driver); | 136 | printf("UNKNOWN - failed to open DBI driver '%s'; possibly it's not installed.\n", config.dbi_driver); |
| 161 | 137 | ||
| 162 | printf("Known drivers:\n"); | 138 | printf("Known drivers:\n"); |
| 163 | for (driver = dbi_driver_list_r(NULL, instance_p); driver; driver = dbi_driver_list_r(driver, instance_p)) { | 139 | for (driver = dbi_driver_list_r(NULL, instance_p); driver; driver = dbi_driver_list_r(driver, instance_p)) { |
| @@ -176,17 +152,19 @@ int main(int argc, char **argv) { | |||
| 176 | return STATE_UNKNOWN; | 152 | return STATE_UNKNOWN; |
| 177 | } | 153 | } |
| 178 | 154 | ||
| 179 | for (i = 0; i < np_dbi_options_num; ++i) { | 155 | for (size_t i = 0; i < config.dbi_options_num; ++i) { |
| 180 | const char *opt; | 156 | const char *opt; |
| 181 | 157 | ||
| 182 | if (verbose > 1) | 158 | if (verbose > 1) { |
| 183 | printf("Setting DBI driver option '%s' to '%s'\n", np_dbi_options[i].key, np_dbi_options[i].value); | 159 | printf("Setting DBI driver option '%s' to '%s'\n", config.dbi_options[i].key, config.dbi_options[i].value); |
| 160 | } | ||
| 184 | 161 | ||
| 185 | if (!dbi_conn_set_option(conn, np_dbi_options[i].key, np_dbi_options[i].value)) | 162 | if (!dbi_conn_set_option(conn, config.dbi_options[i].key, config.dbi_options[i].value)) { |
| 186 | continue; | 163 | continue; |
| 164 | } | ||
| 187 | /* else: status != 0 */ | 165 | /* else: status != 0 */ |
| 188 | 166 | ||
| 189 | np_dbi_print_error(conn, "UNKNOWN - failed to set option '%s' to '%s'", np_dbi_options[i].key, np_dbi_options[i].value); | 167 | np_dbi_print_error(conn, "UNKNOWN - failed to set option '%s' to '%s'", config.dbi_options[i].key, config.dbi_options[i].value); |
| 190 | printf("Known driver options:\n"); | 168 | printf("Known driver options:\n"); |
| 191 | 169 | ||
| 192 | for (opt = dbi_conn_get_option_list(conn, NULL); opt; opt = dbi_conn_get_option_list(conn, opt)) { | 170 | for (opt = dbi_conn_get_option_list(conn, NULL); opt; opt = dbi_conn_get_option_list(conn, opt)) { |
| @@ -196,10 +174,11 @@ int main(int argc, char **argv) { | |||
| 196 | return STATE_UNKNOWN; | 174 | return STATE_UNKNOWN; |
| 197 | } | 175 | } |
| 198 | 176 | ||
| 199 | if (host) { | 177 | if (config.host) { |
| 200 | if (verbose > 1) | 178 | if (verbose > 1) { |
| 201 | printf("Setting DBI driver option 'host' to '%s'\n", host); | 179 | printf("Setting DBI driver option 'host' to '%s'\n", config.host); |
| 202 | dbi_conn_set_option(conn, "host", host); | 180 | } |
| 181 | dbi_conn_set_option(conn, "host", config.host); | ||
| 203 | } | 182 | } |
| 204 | 183 | ||
| 205 | if (verbose) { | 184 | if (verbose) { |
| @@ -209,10 +188,12 @@ int main(int argc, char **argv) { | |||
| 209 | dbname = dbi_conn_get_option(conn, "dbname"); | 188 | dbname = dbi_conn_get_option(conn, "dbname"); |
| 210 | host = dbi_conn_get_option(conn, "host"); | 189 | host = dbi_conn_get_option(conn, "host"); |
| 211 | 190 | ||
| 212 | if (!dbname) | 191 | if (!dbname) { |
| 213 | dbname = "<unspecified>"; | 192 | dbname = "<unspecified>"; |
| 214 | if (!host) | 193 | } |
| 194 | if (!host) { | ||
| 215 | host = "<unspecified>"; | 195 | host = "<unspecified>"; |
| 196 | } | ||
| 216 | 197 | ||
| 217 | printf("Connecting to database '%s' at host '%s'\n", dbname, host); | 198 | printf("Connecting to database '%s' at host '%s'\n", dbname, host); |
| 218 | } | 199 | } |
| @@ -226,109 +207,122 @@ int main(int argc, char **argv) { | |||
| 226 | conn_time = timediff(start_timeval, end_timeval); | 207 | conn_time = timediff(start_timeval, end_timeval); |
| 227 | 208 | ||
| 228 | server_version = dbi_conn_get_engine_version(conn); | 209 | server_version = dbi_conn_get_engine_version(conn); |
| 229 | if (verbose) | 210 | if (verbose) { |
| 230 | printf("Connected to server version %u\n", server_version); | 211 | printf("Connected to server version %u\n", server_version); |
| 212 | } | ||
| 231 | 213 | ||
| 232 | if (metric == METRIC_SERVER_VERSION) | 214 | if (config.metric == METRIC_SERVER_VERSION) { |
| 233 | status = get_status(server_version, dbi_thresholds); | 215 | status = get_status(server_version, config.dbi_thresholds); |
| 216 | } | ||
| 234 | 217 | ||
| 235 | if (verbose) | 218 | if (verbose) { |
| 236 | printf("Time elapsed: %f\n", conn_time); | 219 | printf("Time elapsed: %f\n", conn_time); |
| 220 | } | ||
| 237 | 221 | ||
| 238 | if (metric == METRIC_CONN_TIME) | 222 | if (config.metric == METRIC_CONN_TIME) { |
| 239 | status = get_status(conn_time, dbi_thresholds); | 223 | status = get_status(conn_time, config.dbi_thresholds); |
| 224 | } | ||
| 240 | 225 | ||
| 241 | /* select a database */ | 226 | /* select a database */ |
| 242 | if (np_dbi_database) { | 227 | if (config.dbi_database) { |
| 243 | if (verbose > 1) | 228 | if (verbose > 1) { |
| 244 | printf("Selecting database '%s'\n", np_dbi_database); | 229 | printf("Selecting database '%s'\n", config.dbi_database); |
| 230 | } | ||
| 245 | 231 | ||
| 246 | if (dbi_conn_select_db(conn, np_dbi_database)) { | 232 | if (dbi_conn_select_db(conn, config.dbi_database)) { |
| 247 | np_dbi_print_error(conn, "UNKNOWN - failed to select database '%s'", np_dbi_database); | 233 | np_dbi_print_error(conn, "UNKNOWN - failed to select database '%s'", config.dbi_database); |
| 248 | return STATE_UNKNOWN; | 234 | return STATE_UNKNOWN; |
| 249 | } | 235 | } |
| 250 | } | 236 | } |
| 251 | 237 | ||
| 252 | if (np_dbi_query) { | 238 | if (config.dbi_query) { |
| 253 | /* execute query */ | 239 | /* execute query */ |
| 254 | status = do_query(conn, &query_val_str, &query_val, &query_time); | 240 | status = do_query(conn, &query_val_str, &query_val, &query_time, config.metric, config.type, config.dbi_query); |
| 255 | if (status != STATE_OK) | 241 | if (status != STATE_OK) { |
| 256 | /* do_query prints an error message in this case */ | 242 | /* do_query prints an error message in this case */ |
| 257 | return status; | 243 | return status; |
| 244 | } | ||
| 258 | 245 | ||
| 259 | if (metric == METRIC_QUERY_RESULT) { | 246 | if (config.metric == METRIC_QUERY_RESULT) { |
| 260 | if (expect) { | 247 | if (config.expect) { |
| 261 | if ((!query_val_str) || strcmp(query_val_str, expect)) | 248 | if ((!query_val_str) || strcmp(query_val_str, config.expect)) { |
| 262 | status = STATE_CRITICAL; | 249 | status = STATE_CRITICAL; |
| 263 | else | 250 | } else { |
| 264 | status = STATE_OK; | 251 | status = STATE_OK; |
| 265 | } else if (expect_re_str) { | 252 | } |
| 253 | } else if (config.expect_re_str) { | ||
| 266 | int err; | 254 | int err; |
| 267 | 255 | ||
| 256 | regex_t expect_re = {}; | ||
| 268 | err = regexec(&expect_re, query_val_str, 0, NULL, /* flags = */ 0); | 257 | err = regexec(&expect_re, query_val_str, 0, NULL, /* flags = */ 0); |
| 269 | if (!err) | 258 | if (!err) { |
| 270 | status = STATE_OK; | 259 | status = STATE_OK; |
| 271 | else if (err == REG_NOMATCH) | 260 | } else if (err == REG_NOMATCH) { |
| 272 | status = STATE_CRITICAL; | 261 | status = STATE_CRITICAL; |
| 273 | else { | 262 | } else { |
| 274 | char errmsg[1024]; | 263 | char errmsg[1024]; |
| 275 | regerror(err, &expect_re, errmsg, sizeof(errmsg)); | 264 | regerror(err, &expect_re, errmsg, sizeof(errmsg)); |
| 276 | printf("ERROR - failed to execute regular expression: %s\n", errmsg); | 265 | printf("ERROR - failed to execute regular expression: %s\n", errmsg); |
| 277 | status = STATE_CRITICAL; | 266 | status = STATE_CRITICAL; |
| 278 | } | 267 | } |
| 279 | } else | 268 | } else { |
| 280 | status = get_status(query_val, dbi_thresholds); | 269 | status = get_status(query_val, config.dbi_thresholds); |
| 281 | } else if (metric == METRIC_QUERY_TIME) | 270 | } |
| 282 | status = get_status(query_time, dbi_thresholds); | 271 | } else if (config.metric == METRIC_QUERY_TIME) { |
| 272 | status = get_status(query_time, config.dbi_thresholds); | ||
| 273 | } | ||
| 283 | } | 274 | } |
| 284 | 275 | ||
| 285 | if (verbose) | 276 | if (verbose) { |
| 286 | printf("Closing connection\n"); | 277 | printf("Closing connection\n"); |
| 278 | } | ||
| 287 | dbi_conn_close(conn); | 279 | dbi_conn_close(conn); |
| 288 | 280 | ||
| 289 | /* In case of METRIC_QUERY_RESULT, isnan(query_val) indicates an error | 281 | /* In case of METRIC_QUERY_RESULT, isnan(query_val) indicates an error |
| 290 | * which should have been reported and handled (abort) before | 282 | * which should have been reported and handled (abort) before |
| 291 | * ... unless we expected a string to be returned */ | 283 | * ... unless we expected a string to be returned */ |
| 292 | assert((metric != METRIC_QUERY_RESULT) || (!isnan(query_val)) || (type == TYPE_STRING)); | 284 | assert((config.metric != METRIC_QUERY_RESULT) || (!isnan(query_val)) || (config.type == TYPE_STRING)); |
| 293 | 285 | ||
| 294 | assert((type != TYPE_STRING) || (expect || expect_re_str)); | 286 | assert((config.type != TYPE_STRING) || (config.expect || config.expect_re_str)); |
| 295 | 287 | ||
| 296 | printf("%s - connection time: %fs", state_text(status), conn_time); | 288 | printf("%s - connection time: %fs", state_text(status), conn_time); |
| 297 | if (np_dbi_query) { | 289 | if (config.dbi_query) { |
| 298 | if (type == TYPE_STRING) { | 290 | if (config.type == TYPE_STRING) { |
| 299 | assert(expect || expect_re_str); | 291 | assert(config.expect || config.expect_re_str); |
| 300 | printf(", '%s' returned '%s' in %fs", np_dbi_query, query_val_str ? query_val_str : "<nothing>", query_time); | 292 | printf(", '%s' returned '%s' in %fs", config.dbi_query, query_val_str ? query_val_str : "<nothing>", query_time); |
| 301 | if (status != STATE_OK) { | 293 | if (status != STATE_OK) { |
| 302 | if (expect) | 294 | if (config.expect) { |
| 303 | printf(" (expected '%s')", expect); | 295 | printf(" (expected '%s')", config.expect); |
| 304 | else if (expect_re_str) | 296 | } else if (config.expect_re_str) { |
| 305 | printf(" (expected regex /%s/%s)", expect_re_str, ((expect_re_cflags & REG_ICASE) ? "i" : "")); | 297 | printf(" (expected regex /%s/%s)", config.expect_re_str, ((config.expect_re_cflags & REG_ICASE) ? "i" : "")); |
| 298 | } | ||
| 306 | } | 299 | } |
| 307 | } else if (isnan(query_val)) | 300 | } else if (isnan(query_val)) { |
| 308 | printf(", '%s' query execution time: %fs", np_dbi_query, query_time); | 301 | printf(", '%s' query execution time: %fs", config.dbi_query, query_time); |
| 309 | else | 302 | } else { |
| 310 | printf(", '%s' returned %f in %fs", np_dbi_query, query_val, query_time); | 303 | printf(", '%s' returned %f in %fs", config.dbi_query, query_val, query_time); |
| 304 | } | ||
| 311 | } | 305 | } |
| 312 | 306 | ||
| 313 | printf(" | conntime=%fs;%s;%s;0; server_version=%u;%s;%s;0;", conn_time, | 307 | printf(" | conntime=%fs;%s;%s;0; server_version=%u;%s;%s;0;", conn_time, |
| 314 | ((metric == METRIC_CONN_TIME) && warning_range) ? warning_range : "", | 308 | ((config.metric == METRIC_CONN_TIME) && config.warning_range) ? config.warning_range : "", |
| 315 | ((metric == METRIC_CONN_TIME) && critical_range) ? critical_range : "", server_version, | 309 | ((config.metric == METRIC_CONN_TIME) && config.critical_range) ? config.critical_range : "", server_version, |
| 316 | ((metric == METRIC_SERVER_VERSION) && warning_range) ? warning_range : "", | 310 | ((config.metric == METRIC_SERVER_VERSION) && config.warning_range) ? config.warning_range : "", |
| 317 | ((metric == METRIC_SERVER_VERSION) && critical_range) ? critical_range : ""); | 311 | ((config.metric == METRIC_SERVER_VERSION) && config.critical_range) ? config.critical_range : ""); |
| 318 | if (np_dbi_query) { | 312 | if (config.dbi_query) { |
| 319 | if (!isnan(query_val)) /* this is also true when -e is used */ | 313 | if (!isnan(query_val)) { /* this is also true when -e is used */ |
| 320 | printf(" query=%f;%s;%s;;", query_val, ((metric == METRIC_QUERY_RESULT) && warning_range) ? warning_range : "", | 314 | printf(" query=%f;%s;%s;;", query_val, ((config.metric == METRIC_QUERY_RESULT) && config.warning_range) ? config.warning_range : "", |
| 321 | ((metric == METRIC_QUERY_RESULT) && critical_range) ? critical_range : ""); | 315 | ((config.metric == METRIC_QUERY_RESULT) && config.critical_range) ? config.critical_range : ""); |
| 322 | printf(" querytime=%fs;%s;%s;0;", query_time, ((metric == METRIC_QUERY_TIME) && warning_range) ? warning_range : "", | 316 | } |
| 323 | ((metric == METRIC_QUERY_TIME) && critical_range) ? critical_range : ""); | 317 | printf(" querytime=%fs;%s;%s;0;", query_time, ((config.metric == METRIC_QUERY_TIME) && config.warning_range) ? config.warning_range : "", |
| 318 | ((config.metric == METRIC_QUERY_TIME) && config.critical_range) ? config.critical_range : ""); | ||
| 324 | } | 319 | } |
| 325 | printf("\n"); | 320 | printf("\n"); |
| 326 | return status; | 321 | return status; |
| 327 | } | 322 | } |
| 328 | 323 | ||
| 329 | /* process command-line arguments */ | 324 | /* process command-line arguments */ |
| 330 | int process_arguments(int argc, char **argv) { | 325 | check_dbi_config_wrapper process_arguments(int argc, char **argv) { |
| 331 | int c; | ||
| 332 | 326 | ||
| 333 | int option = 0; | 327 | int option = 0; |
| 334 | static struct option longopts[] = {STD_LONG_OPTS, | 328 | static struct option longopts[] = {STD_LONG_OPTS, |
| @@ -343,13 +337,19 @@ int process_arguments(int argc, char **argv) { | |||
| 343 | {"database", required_argument, 0, 'D'}, | 337 | {"database", required_argument, 0, 'D'}, |
| 344 | {0, 0, 0, 0}}; | 338 | {0, 0, 0, 0}}; |
| 345 | 339 | ||
| 346 | while (1) { | 340 | check_dbi_config_wrapper result = { |
| 347 | c = getopt_long(argc, argv, "Vvht:c:w:e:r:R:m:H:d:o:q:D:", longopts, &option); | 341 | .config = check_dbi_config_init(), |
| 342 | .errorcode = OK, | ||
| 343 | }; | ||
| 344 | int option_char; | ||
| 345 | while (true) { | ||
| 346 | option_char = getopt_long(argc, argv, "Vvht:c:w:e:r:R:m:H:d:o:q:D:", longopts, &option); | ||
| 348 | 347 | ||
| 349 | if (c == EOF) | 348 | if (option_char == EOF) { |
| 350 | break; | 349 | break; |
| 350 | } | ||
| 351 | 351 | ||
| 352 | switch (c) { | 352 | switch (option_char) { |
| 353 | case '?': /* usage */ | 353 | case '?': /* usage */ |
| 354 | usage5(); | 354 | usage5(); |
| 355 | case 'h': /* help */ | 355 | case 'h': /* help */ |
| @@ -360,135 +360,148 @@ int process_arguments(int argc, char **argv) { | |||
| 360 | exit(STATE_UNKNOWN); | 360 | exit(STATE_UNKNOWN); |
| 361 | 361 | ||
| 362 | case 'c': /* critical range */ | 362 | case 'c': /* critical range */ |
| 363 | critical_range = optarg; | 363 | result.config.critical_range = optarg; |
| 364 | type = TYPE_NUMERIC; | 364 | result.config.type = TYPE_NUMERIC; |
| 365 | break; | 365 | break; |
| 366 | case 'w': /* warning range */ | 366 | case 'w': /* warning range */ |
| 367 | warning_range = optarg; | 367 | result.config.warning_range = optarg; |
| 368 | type = TYPE_NUMERIC; | 368 | result.config.type = TYPE_NUMERIC; |
| 369 | break; | 369 | break; |
| 370 | case 'e': | 370 | case 'e': |
| 371 | expect = optarg; | 371 | result.config.expect = optarg; |
| 372 | type = TYPE_STRING; | 372 | result.config.type = TYPE_STRING; |
| 373 | break; | 373 | break; |
| 374 | case 'R': | 374 | case 'R': |
| 375 | expect_re_cflags = REG_ICASE; | 375 | result.config.expect_re_cflags = REG_ICASE; |
| 376 | /* fall through */ | 376 | /* fall through */ |
| 377 | case 'r': { | 377 | case 'r': { |
| 378 | int err; | 378 | int err; |
| 379 | 379 | ||
| 380 | expect_re_cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE; | 380 | result.config.expect_re_cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE; |
| 381 | expect_re_str = optarg; | 381 | result.config.expect_re_str = optarg; |
| 382 | type = TYPE_STRING; | 382 | result.config.type = TYPE_STRING; |
| 383 | 383 | ||
| 384 | err = regcomp(&expect_re, expect_re_str, expect_re_cflags); | 384 | regex_t expect_re = {}; |
| 385 | err = regcomp(&expect_re, result.config.expect_re_str, result.config.expect_re_cflags); | ||
| 385 | if (err) { | 386 | if (err) { |
| 386 | char errmsg[1024]; | 387 | char errmsg[1024]; |
| 387 | regerror(err, &expect_re, errmsg, sizeof(errmsg)); | 388 | regerror(err, &expect_re, errmsg, sizeof(errmsg)); |
| 388 | printf("ERROR - failed to compile regular expression: %s\n", errmsg); | 389 | printf("ERROR - failed to compile regular expression: %s\n", errmsg); |
| 389 | return ERROR; | 390 | |
| 391 | result.errorcode = ERROR; | ||
| 392 | return result; | ||
| 390 | } | 393 | } |
| 391 | break; | 394 | break; |
| 392 | } | 395 | } |
| 393 | 396 | ||
| 394 | case 'm': | 397 | case 'm': |
| 395 | if (!strcasecmp(optarg, "CONN_TIME")) | 398 | if (!strcasecmp(optarg, "CONN_TIME")) { |
| 396 | metric = METRIC_CONN_TIME; | 399 | result.config.metric = METRIC_CONN_TIME; |
| 397 | else if (!strcasecmp(optarg, "SERVER_VERSION")) | 400 | } else if (!strcasecmp(optarg, "SERVER_VERSION")) { |
| 398 | metric = METRIC_SERVER_VERSION; | 401 | result.config.metric = METRIC_SERVER_VERSION; |
| 399 | else if (!strcasecmp(optarg, "QUERY_RESULT")) | 402 | } else if (!strcasecmp(optarg, "QUERY_RESULT")) { |
| 400 | metric = METRIC_QUERY_RESULT; | 403 | result.config.metric = METRIC_QUERY_RESULT; |
| 401 | else if (!strcasecmp(optarg, "QUERY_TIME")) | 404 | } else if (!strcasecmp(optarg, "QUERY_TIME")) { |
| 402 | metric = METRIC_QUERY_TIME; | 405 | result.config.metric = METRIC_QUERY_TIME; |
| 403 | else | 406 | } else { |
| 404 | usage2(_("Invalid metric"), optarg); | 407 | usage2(_("Invalid metric"), optarg); |
| 408 | } | ||
| 405 | break; | 409 | break; |
| 406 | case 't': /* timeout */ | 410 | case 't': /* timeout */ |
| 407 | if (!is_intnonneg(optarg)) | 411 | if (!is_intnonneg(optarg)) { |
| 408 | usage2(_("Timeout interval must be a positive integer"), optarg); | 412 | usage2(_("Timeout interval must be a positive integer"), optarg); |
| 409 | else | 413 | } else { |
| 410 | timeout_interval = atoi(optarg); | 414 | timeout_interval = atoi(optarg); |
| 415 | } | ||
| 411 | 416 | ||
| 412 | break; | 417 | break; |
| 413 | case 'H': /* host */ | 418 | case 'H': /* host */ |
| 414 | if (!is_host(optarg)) | 419 | if (!is_host(optarg)) { |
| 415 | usage2(_("Invalid hostname/address"), optarg); | 420 | usage2(_("Invalid hostname/address"), optarg); |
| 416 | else | 421 | } else { |
| 417 | host = optarg; | 422 | result.config.host = optarg; |
| 423 | } | ||
| 418 | break; | 424 | break; |
| 419 | case 'v': | 425 | case 'v': |
| 420 | verbose++; | 426 | verbose++; |
| 421 | break; | 427 | break; |
| 422 | 428 | ||
| 423 | case 'd': | 429 | case 'd': |
| 424 | np_dbi_driver = optarg; | 430 | result.config.dbi_driver = optarg; |
| 425 | break; | 431 | break; |
| 426 | case 'o': { | 432 | case 'o': { |
| 427 | driver_option_t *new; | 433 | driver_option_t *new = NULL; |
| 428 | |||
| 429 | char *k; | ||
| 430 | char *v; | ||
| 431 | 434 | ||
| 432 | k = optarg; | 435 | char *key = optarg; |
| 433 | v = strchr(k, (int)'='); | 436 | char *value = strchr(key, '='); |
| 434 | 437 | ||
| 435 | if (!v) | 438 | if (!value) { |
| 436 | usage2(_("Option must be '<key>=<value>'"), optarg); | 439 | usage2(_("Option must be '<key>=<value>'"), optarg); |
| 440 | } | ||
| 437 | 441 | ||
| 438 | *v = '\0'; | 442 | *value = '\0'; |
| 439 | ++v; | 443 | ++value; |
| 440 | 444 | ||
| 441 | new = realloc(np_dbi_options, (np_dbi_options_num + 1) * sizeof(*new)); | 445 | new = realloc(result.config.dbi_options, (result.config.dbi_options_num + 1) * sizeof(*new)); |
| 442 | if (!new) { | 446 | if (!new) { |
| 443 | printf("UNKNOWN - failed to reallocate memory\n"); | 447 | printf("UNKNOWN - failed to reallocate memory\n"); |
| 444 | exit(STATE_UNKNOWN); | 448 | exit(STATE_UNKNOWN); |
| 445 | } | 449 | } |
| 446 | 450 | ||
| 447 | np_dbi_options = new; | 451 | result.config.dbi_options = new; |
| 448 | new = np_dbi_options + np_dbi_options_num; | 452 | new = result.config.dbi_options + result.config.dbi_options_num; |
| 449 | ++np_dbi_options_num; | 453 | result.config.dbi_options_num++; |
| 450 | 454 | ||
| 451 | new->key = k; | 455 | new->key = key; |
| 452 | new->value = v; | 456 | new->value = value; |
| 453 | } break; | 457 | } break; |
| 454 | case 'q': | 458 | case 'q': |
| 455 | np_dbi_query = optarg; | 459 | result.config.dbi_query = optarg; |
| 456 | break; | 460 | break; |
| 457 | case 'D': | 461 | case 'D': |
| 458 | np_dbi_database = optarg; | 462 | result.config.dbi_database = optarg; |
| 459 | break; | 463 | break; |
| 460 | } | 464 | } |
| 461 | } | 465 | } |
| 462 | 466 | ||
| 463 | set_thresholds(&dbi_thresholds, warning_range, critical_range); | 467 | set_thresholds(&result.config.dbi_thresholds, result.config.warning_range, result.config.critical_range); |
| 464 | 468 | ||
| 465 | return validate_arguments(); | 469 | return validate_arguments(result); |
| 466 | } | 470 | } |
| 467 | 471 | ||
| 468 | int validate_arguments(void) { | 472 | check_dbi_config_wrapper validate_arguments(check_dbi_config_wrapper config_wrapper) { |
| 469 | if (!np_dbi_driver) | 473 | if (!config_wrapper.config.dbi_driver) { |
| 470 | usage("Must specify a DBI driver"); | 474 | usage("Must specify a DBI driver"); |
| 475 | } | ||
| 471 | 476 | ||
| 472 | if (((metric == METRIC_QUERY_RESULT) || (metric == METRIC_QUERY_TIME)) && (!np_dbi_query)) | 477 | if (((config_wrapper.config.metric == METRIC_QUERY_RESULT) || (config_wrapper.config.metric == METRIC_QUERY_TIME)) && |
| 478 | (!config_wrapper.config.dbi_query)) { | ||
| 473 | usage("Must specify a query to execute (metric == QUERY_RESULT)"); | 479 | usage("Must specify a query to execute (metric == QUERY_RESULT)"); |
| 480 | } | ||
| 474 | 481 | ||
| 475 | if ((metric != METRIC_CONN_TIME) && (metric != METRIC_SERVER_VERSION) && (metric != METRIC_QUERY_RESULT) && | 482 | if ((config_wrapper.config.metric != METRIC_CONN_TIME) && (config_wrapper.config.metric != METRIC_SERVER_VERSION) && |
| 476 | (metric != METRIC_QUERY_TIME)) | 483 | (config_wrapper.config.metric != METRIC_QUERY_RESULT) && (config_wrapper.config.metric != METRIC_QUERY_TIME)) { |
| 477 | usage("Invalid metric specified"); | 484 | usage("Invalid metric specified"); |
| 485 | } | ||
| 478 | 486 | ||
| 479 | if (expect && (warning_range || critical_range || expect_re_str)) | 487 | if (config_wrapper.config.expect && (config_wrapper.config.warning_range || config_wrapper.config.critical_range || config_wrapper.config.expect_re_str)) { |
| 480 | usage("Do not mix -e and -w/-c/-r/-R"); | 488 | usage("Do not mix -e and -w/-c/-r/-R"); |
| 489 | } | ||
| 481 | 490 | ||
| 482 | if (expect_re_str && (warning_range || critical_range || expect)) | 491 | if (config_wrapper.config.expect_re_str && (config_wrapper.config.warning_range || config_wrapper.config.critical_range || config_wrapper.config.expect)) { |
| 483 | usage("Do not mix -r/-R and -w/-c/-e"); | 492 | usage("Do not mix -r/-R and -w/-c/-e"); |
| 493 | } | ||
| 484 | 494 | ||
| 485 | if (expect && (metric != METRIC_QUERY_RESULT)) | 495 | if (config_wrapper.config.expect && (config_wrapper.config.metric != METRIC_QUERY_RESULT)) { |
| 486 | usage("Option -e requires metric QUERY_RESULT"); | 496 | usage("Option -e requires metric QUERY_RESULT"); |
| 497 | } | ||
| 487 | 498 | ||
| 488 | if (expect_re_str && (metric != METRIC_QUERY_RESULT)) | 499 | if (config_wrapper.config.expect_re_str && (config_wrapper.config.metric != METRIC_QUERY_RESULT)) { |
| 489 | usage("Options -r/-R require metric QUERY_RESULT"); | 500 | usage("Options -r/-R require metric QUERY_RESULT"); |
| 501 | } | ||
| 490 | 502 | ||
| 491 | return OK; | 503 | config_wrapper.errorcode = OK; |
| 504 | return config_wrapper; | ||
| 492 | } | 505 | } |
| 493 | 506 | ||
| 494 | void print_help(void) { | 507 | void print_help(void) { |
| @@ -518,6 +531,8 @@ void print_help(void) { | |||
| 518 | printf(" %s\n", _("DBI driver options")); | 531 | printf(" %s\n", _("DBI driver options")); |
| 519 | printf(" %s\n", "-q, --query=STRING"); | 532 | printf(" %s\n", "-q, --query=STRING"); |
| 520 | printf(" %s\n", _("query to execute")); | 533 | printf(" %s\n", _("query to execute")); |
| 534 | printf(" %s\n", "-H STRING"); | ||
| 535 | printf(" %s\n", _("target database host")); | ||
| 521 | printf("\n"); | 536 | printf("\n"); |
| 522 | 537 | ||
| 523 | printf(UT_WARN_CRIT_RANGE); | 538 | printf(UT_WARN_CRIT_RANGE); |
| @@ -592,13 +607,7 @@ void print_usage(void) { | |||
| 592 | printf(" [-e <string>] [-r|-R <regex>]\n"); | 607 | printf(" [-e <string>] [-r|-R <regex>]\n"); |
| 593 | } | 608 | } |
| 594 | 609 | ||
| 595 | #define CHECK_IGNORE_ERROR(s) \ | 610 | const char *get_field_str(dbi_conn conn, dbi_result res, unsigned short field_type, mp_dbi_metric metric, mp_dbi_type type) { |
| 596 | do { \ | ||
| 597 | if (metric != METRIC_QUERY_RESULT) \ | ||
| 598 | return (s); \ | ||
| 599 | } while (0) | ||
| 600 | |||
| 601 | const char *get_field_str(dbi_conn conn, dbi_result res, unsigned short field_type) { | ||
| 602 | const char *str; | 611 | const char *str; |
| 603 | 612 | ||
| 604 | if (field_type != DBI_TYPE_STRING) { | 613 | if (field_type != DBI_TYPE_STRING) { |
| @@ -608,17 +617,20 @@ const char *get_field_str(dbi_conn conn, dbi_result res, unsigned short field_ty | |||
| 608 | 617 | ||
| 609 | str = dbi_result_get_string_idx(res, 1); | 618 | str = dbi_result_get_string_idx(res, 1); |
| 610 | if ((!str) || (strcmp(str, "ERROR") == 0)) { | 619 | if ((!str) || (strcmp(str, "ERROR") == 0)) { |
| 611 | CHECK_IGNORE_ERROR(NULL); | 620 | if (metric != METRIC_QUERY_RESULT) { |
| 621 | return NULL; | ||
| 622 | } | ||
| 612 | np_dbi_print_error(conn, "CRITICAL - failed to fetch string value"); | 623 | np_dbi_print_error(conn, "CRITICAL - failed to fetch string value"); |
| 613 | return NULL; | 624 | return NULL; |
| 614 | } | 625 | } |
| 615 | 626 | ||
| 616 | if ((verbose && (type == TYPE_STRING)) || (verbose > 2)) | 627 | if ((verbose && (type == TYPE_STRING)) || (verbose > 2)) { |
| 617 | printf("Query returned string '%s'\n", str); | 628 | printf("Query returned string '%s'\n", str); |
| 629 | } | ||
| 618 | return str; | 630 | return str; |
| 619 | } | 631 | } |
| 620 | 632 | ||
| 621 | double get_field(dbi_conn conn, dbi_result res, unsigned short *field_type) { | 633 | double get_field(dbi_conn conn, dbi_result res, unsigned short *field_type, mp_dbi_metric metric, mp_dbi_type type) { |
| 622 | double val = NAN; | 634 | double val = NAN; |
| 623 | 635 | ||
| 624 | if (*field_type == DBI_TYPE_INTEGER) { | 636 | if (*field_type == DBI_TYPE_INTEGER) { |
| @@ -629,26 +641,33 @@ double get_field(dbi_conn conn, dbi_result res, unsigned short *field_type) { | |||
| 629 | const char *val_str; | 641 | const char *val_str; |
| 630 | char *endptr = NULL; | 642 | char *endptr = NULL; |
| 631 | 643 | ||
| 632 | val_str = get_field_str(conn, res, *field_type); | 644 | val_str = get_field_str(conn, res, *field_type, metric, type); |
| 633 | if (!val_str) { | 645 | if (!val_str) { |
| 634 | CHECK_IGNORE_ERROR(NAN); | 646 | if (metric != METRIC_QUERY_RESULT) { |
| 647 | return NAN; | ||
| 648 | } | ||
| 635 | *field_type = DBI_TYPE_ERROR; | 649 | *field_type = DBI_TYPE_ERROR; |
| 636 | return NAN; | 650 | return NAN; |
| 637 | } | 651 | } |
| 638 | 652 | ||
| 639 | val = strtod(val_str, &endptr); | 653 | val = strtod(val_str, &endptr); |
| 640 | if (endptr == val_str) { | 654 | if (endptr == val_str) { |
| 641 | CHECK_IGNORE_ERROR(NAN); | 655 | if (metric != METRIC_QUERY_RESULT) { |
| 656 | return NAN; | ||
| 657 | } | ||
| 642 | printf("CRITICAL - result value is not a numeric: %s\n", val_str); | 658 | printf("CRITICAL - result value is not a numeric: %s\n", val_str); |
| 643 | *field_type = DBI_TYPE_ERROR; | 659 | *field_type = DBI_TYPE_ERROR; |
| 644 | return NAN; | 660 | return NAN; |
| 645 | } | 661 | } |
| 646 | if ((endptr != NULL) && (*endptr != '\0')) { | 662 | if ((endptr != NULL) && (*endptr != '\0')) { |
| 647 | if (verbose) | 663 | if (verbose) { |
| 648 | printf("Garbage after value: %s\n", endptr); | 664 | printf("Garbage after value: %s\n", endptr); |
| 665 | } | ||
| 649 | } | 666 | } |
| 650 | } else { | 667 | } else { |
| 651 | CHECK_IGNORE_ERROR(NAN); | 668 | if (metric != METRIC_QUERY_RESULT) { |
| 669 | return NAN; | ||
| 670 | } | ||
| 652 | printf("CRITICAL - cannot parse value of type %s (%i)\n", | 671 | printf("CRITICAL - cannot parse value of type %s (%i)\n", |
| 653 | (*field_type == DBI_TYPE_BINARY) ? "BINARY" | 672 | (*field_type == DBI_TYPE_BINARY) ? "BINARY" |
| 654 | : (*field_type == DBI_TYPE_DATETIME) ? "DATETIME" | 673 | : (*field_type == DBI_TYPE_DATETIME) ? "DATETIME" |
| @@ -660,53 +679,66 @@ double get_field(dbi_conn conn, dbi_result res, unsigned short *field_type) { | |||
| 660 | return val; | 679 | return val; |
| 661 | } | 680 | } |
| 662 | 681 | ||
| 663 | double get_query_result(dbi_conn conn, dbi_result res, const char **res_val_str, double *res_val) { | 682 | mp_state_enum get_query_result(dbi_conn conn, dbi_result res, const char **res_val_str, double *res_val, mp_dbi_metric metric, mp_dbi_type type) { |
| 664 | unsigned short field_type; | 683 | unsigned short field_type; |
| 665 | double val = NAN; | 684 | double val = NAN; |
| 666 | 685 | ||
| 667 | if (dbi_result_get_numrows(res) == DBI_ROW_ERROR) { | 686 | if (dbi_result_get_numrows(res) == DBI_ROW_ERROR) { |
| 668 | CHECK_IGNORE_ERROR(STATE_OK); | 687 | if (metric != METRIC_QUERY_RESULT) { |
| 688 | return STATE_OK; | ||
| 689 | } | ||
| 669 | np_dbi_print_error(conn, "CRITICAL - failed to fetch rows"); | 690 | np_dbi_print_error(conn, "CRITICAL - failed to fetch rows"); |
| 670 | return STATE_CRITICAL; | 691 | return STATE_CRITICAL; |
| 671 | } | 692 | } |
| 672 | 693 | ||
| 673 | if (dbi_result_get_numrows(res) < 1) { | 694 | if (dbi_result_get_numrows(res) < 1) { |
| 674 | CHECK_IGNORE_ERROR(STATE_OK); | 695 | if (metric != METRIC_QUERY_RESULT) { |
| 696 | return STATE_OK; | ||
| 697 | } | ||
| 675 | printf("WARNING - no rows returned\n"); | 698 | printf("WARNING - no rows returned\n"); |
| 676 | return STATE_WARNING; | 699 | return STATE_WARNING; |
| 677 | } | 700 | } |
| 678 | 701 | ||
| 679 | if (dbi_result_get_numfields(res) == DBI_FIELD_ERROR) { | 702 | if (dbi_result_get_numfields(res) == DBI_FIELD_ERROR) { |
| 680 | CHECK_IGNORE_ERROR(STATE_OK); | 703 | if (metric != METRIC_QUERY_RESULT) { |
| 704 | return STATE_OK; | ||
| 705 | } | ||
| 681 | np_dbi_print_error(conn, "CRITICAL - failed to fetch fields"); | 706 | np_dbi_print_error(conn, "CRITICAL - failed to fetch fields"); |
| 682 | return STATE_CRITICAL; | 707 | return STATE_CRITICAL; |
| 683 | } | 708 | } |
| 684 | 709 | ||
| 685 | if (dbi_result_get_numfields(res) < 1) { | 710 | if (dbi_result_get_numfields(res) < 1) { |
| 686 | CHECK_IGNORE_ERROR(STATE_OK); | 711 | if (metric != METRIC_QUERY_RESULT) { |
| 712 | return STATE_OK; | ||
| 713 | } | ||
| 687 | printf("WARNING - no fields returned\n"); | 714 | printf("WARNING - no fields returned\n"); |
| 688 | return STATE_WARNING; | 715 | return STATE_WARNING; |
| 689 | } | 716 | } |
| 690 | 717 | ||
| 691 | if (dbi_result_first_row(res) != 1) { | 718 | if (dbi_result_first_row(res) != 1) { |
| 692 | CHECK_IGNORE_ERROR(STATE_OK); | 719 | if (metric != METRIC_QUERY_RESULT) { |
| 720 | return STATE_OK; | ||
| 721 | } | ||
| 693 | np_dbi_print_error(conn, "CRITICAL - failed to fetch first row"); | 722 | np_dbi_print_error(conn, "CRITICAL - failed to fetch first row"); |
| 694 | return STATE_CRITICAL; | 723 | return STATE_CRITICAL; |
| 695 | } | 724 | } |
| 696 | 725 | ||
| 697 | field_type = dbi_result_get_field_type_idx(res, 1); | 726 | field_type = dbi_result_get_field_type_idx(res, 1); |
| 698 | if (field_type != DBI_TYPE_ERROR) { | 727 | if (field_type != DBI_TYPE_ERROR) { |
| 699 | if (type == TYPE_STRING) | 728 | if (type == TYPE_STRING) { |
| 700 | /* the value will be freed in dbi_result_free */ | 729 | /* the value will be freed in dbi_result_free */ |
| 701 | *res_val_str = strdup(get_field_str(conn, res, field_type)); | 730 | *res_val_str = strdup(get_field_str(conn, res, field_type, metric, type)); |
| 702 | else | 731 | } else { |
| 703 | val = get_field(conn, res, &field_type); | 732 | val = get_field(conn, res, &field_type, metric, type); |
| 733 | } | ||
| 704 | } | 734 | } |
| 705 | 735 | ||
| 706 | *res_val = val; | 736 | *res_val = val; |
| 707 | 737 | ||
| 708 | if (field_type == DBI_TYPE_ERROR) { | 738 | if (field_type == DBI_TYPE_ERROR) { |
| 709 | CHECK_IGNORE_ERROR(STATE_OK); | 739 | if (metric != METRIC_QUERY_RESULT) { |
| 740 | return STATE_OK; | ||
| 741 | } | ||
| 710 | np_dbi_print_error(conn, "CRITICAL - failed to fetch data"); | 742 | np_dbi_print_error(conn, "CRITICAL - failed to fetch data"); |
| 711 | return STATE_CRITICAL; | 743 | return STATE_CRITICAL; |
| 712 | } | 744 | } |
| @@ -715,19 +747,19 @@ double get_query_result(dbi_conn conn, dbi_result res, const char **res_val_str, | |||
| 715 | return STATE_OK; | 747 | return STATE_OK; |
| 716 | } | 748 | } |
| 717 | 749 | ||
| 718 | #undef CHECK_IGNORE_ERROR | 750 | mp_state_enum do_query(dbi_conn conn, const char **res_val_str, double *res_val, double *res_time, mp_dbi_metric metric, mp_dbi_type type, |
| 719 | 751 | char *np_dbi_query) { | |
| 720 | int do_query(dbi_conn conn, const char **res_val_str, double *res_val, double *res_time) { | ||
| 721 | dbi_result res; | 752 | dbi_result res; |
| 722 | 753 | ||
| 723 | struct timeval timeval_start; | 754 | struct timeval timeval_start; |
| 724 | struct timeval timeval_end; | 755 | struct timeval timeval_end; |
| 725 | int status = STATE_OK; | 756 | mp_state_enum status = STATE_OK; |
| 726 | 757 | ||
| 727 | assert(np_dbi_query); | 758 | assert(np_dbi_query); |
| 728 | 759 | ||
| 729 | if (verbose) | 760 | if (verbose) { |
| 730 | printf("Executing query '%s'\n", np_dbi_query); | 761 | printf("Executing query '%s'\n", np_dbi_query); |
| 762 | } | ||
| 731 | 763 | ||
| 732 | gettimeofday(&timeval_start, NULL); | 764 | gettimeofday(&timeval_start, NULL); |
| 733 | 765 | ||
| @@ -737,13 +769,14 @@ int do_query(dbi_conn conn, const char **res_val_str, double *res_val, double *r | |||
| 737 | return STATE_CRITICAL; | 769 | return STATE_CRITICAL; |
| 738 | } | 770 | } |
| 739 | 771 | ||
| 740 | status = get_query_result(conn, res, res_val_str, res_val); | 772 | status = get_query_result(conn, res, res_val_str, res_val, metric, type); |
| 741 | 773 | ||
| 742 | gettimeofday(&timeval_end, NULL); | 774 | gettimeofday(&timeval_end, NULL); |
| 743 | *res_time = timediff(timeval_start, timeval_end); | 775 | *res_time = timediff(timeval_start, timeval_end); |
| 744 | 776 | ||
| 745 | if (verbose) | 777 | if (verbose) { |
| 746 | printf("Time elapsed: %f\n", *res_time); | 778 | printf("Time elapsed: %f\n", *res_time); |
| 779 | } | ||
| 747 | 780 | ||
| 748 | return status; | 781 | return status; |
| 749 | } | 782 | } |
diff --git a/plugins/check_dbi.d/config.h b/plugins/check_dbi.d/config.h new file mode 100644 index 00000000..f6f0d7b3 --- /dev/null +++ b/plugins/check_dbi.d/config.h | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "../../config.h" | ||
| 4 | #include <stddef.h> | ||
| 5 | #include "../../lib/monitoringplug.h" | ||
| 6 | |||
| 7 | typedef enum { | ||
| 8 | METRIC_CONN_TIME, | ||
| 9 | METRIC_SERVER_VERSION, | ||
| 10 | METRIC_QUERY_RESULT, | ||
| 11 | METRIC_QUERY_TIME, | ||
| 12 | } mp_dbi_metric; | ||
| 13 | |||
| 14 | typedef enum { | ||
| 15 | TYPE_NUMERIC, | ||
| 16 | TYPE_STRING, | ||
| 17 | } mp_dbi_type; | ||
| 18 | |||
| 19 | typedef struct { | ||
| 20 | char *key; | ||
| 21 | char *value; | ||
| 22 | } driver_option_t; | ||
| 23 | |||
| 24 | typedef struct { | ||
| 25 | char *dbi_driver; | ||
| 26 | char *host; | ||
| 27 | driver_option_t *dbi_options; | ||
| 28 | size_t dbi_options_num; | ||
| 29 | char *dbi_database; | ||
| 30 | char *dbi_query; | ||
| 31 | |||
| 32 | char *expect; | ||
| 33 | char *expect_re_str; | ||
| 34 | int expect_re_cflags; | ||
| 35 | mp_dbi_metric metric; | ||
| 36 | mp_dbi_type type; | ||
| 37 | char *warning_range; | ||
| 38 | char *critical_range; | ||
| 39 | thresholds *dbi_thresholds; | ||
| 40 | |||
| 41 | } check_dbi_config; | ||
| 42 | |||
| 43 | check_dbi_config check_dbi_config_init() { | ||
| 44 | check_dbi_config tmp = { | ||
| 45 | .dbi_driver = NULL, | ||
| 46 | .host = NULL, | ||
| 47 | .dbi_options = NULL, | ||
| 48 | .dbi_options_num = 0, | ||
| 49 | .dbi_database = NULL, | ||
| 50 | .dbi_query = NULL, | ||
| 51 | |||
| 52 | .expect = NULL, | ||
| 53 | .expect_re_str = NULL, | ||
| 54 | .expect_re_cflags = 0, | ||
| 55 | .metric = METRIC_QUERY_RESULT, | ||
| 56 | .type = TYPE_NUMERIC, | ||
| 57 | |||
| 58 | .warning_range = NULL, | ||
| 59 | .critical_range = NULL, | ||
| 60 | .dbi_thresholds = NULL, | ||
| 61 | }; | ||
| 62 | return tmp; | ||
| 63 | } | ||
diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c index 8a73772d..2b6cfeaf 100644 --- a/plugins/check_mysql.c +++ b/plugins/check_mysql.c | |||
| @@ -1,40 +1,40 @@ | |||
| 1 | /***************************************************************************** | 1 | /***************************************************************************** |
| 2 | * | 2 | * |
| 3 | * Monitoring check_mysql plugin | 3 | * Monitoring check_mysql plugin |
| 4 | * | 4 | * |
| 5 | * License: GPL | 5 | * License: GPL |
| 6 | * Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at) | 6 | * Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at) |
| 7 | * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) | 7 | * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) |
| 8 | * Copyright (c) 1999-2024 Monitoring Plugins Development Team | 8 | * Copyright (c) 1999-2024 Monitoring Plugins Development Team |
| 9 | * | 9 | * |
| 10 | * Description: | 10 | * Description: |
| 11 | * | 11 | * |
| 12 | * This file contains the check_mysql plugin | 12 | * This file contains the check_mysql plugin |
| 13 | * | 13 | * |
| 14 | * This program tests connections to a mysql server | 14 | * This program tests connections to a mysql server |
| 15 | * | 15 | * |
| 16 | * | 16 | * |
| 17 | * This program is free software: you can redistribute it and/or modify | 17 | * This program is free software: you can redistribute it and/or modify |
| 18 | * it under the terms of the GNU General Public License as published by | 18 | * it under the terms of the GNU General Public License as published by |
| 19 | * the Free Software Foundation, either version 3 of the License, or | 19 | * the Free Software Foundation, either version 3 of the License, or |
| 20 | * (at your option) any later version. | 20 | * (at your option) any later version. |
| 21 | * | 21 | * |
| 22 | * This program is distributed in the hope that it will be useful, | 22 | * This program is distributed in the hope that it will be useful, |
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | * GNU General Public License for more details. | 25 | * GNU General Public License for more details. |
| 26 | * | 26 | * |
| 27 | * You should have received a copy of the GNU General Public License | 27 | * You should have received a copy of the GNU General Public License |
| 28 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 28 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 29 | * | 29 | * |
| 30 | * | 30 | * |
| 31 | *****************************************************************************/ | 31 | *****************************************************************************/ |
| 32 | 32 | ||
| 33 | const char *progname = "check_mysql"; | 33 | const char *progname = "check_mysql"; |
| 34 | const char *copyright = "1999-2024"; | 34 | const char *copyright = "1999-2024"; |
| 35 | const char *email = "devel@monitoring-plugins.org"; | 35 | const char *email = "devel@monitoring-plugins.org"; |
| 36 | 36 | ||
| 37 | #define SLAVERESULTSIZE 96 | 37 | #define REPLICA_RESULTSIZE 96 |
| 38 | 38 | ||
| 39 | #include "common.h" | 39 | #include "common.h" |
| 40 | #include "utils.h" | 40 | #include "utils.h" |
| @@ -59,7 +59,7 @@ static bool ssl = false; | |||
| 59 | static char *opt_file = NULL; | 59 | static char *opt_file = NULL; |
| 60 | static char *opt_group = NULL; | 60 | static char *opt_group = NULL; |
| 61 | static unsigned int db_port = MYSQL_PORT; | 61 | static unsigned int db_port = MYSQL_PORT; |
| 62 | static bool check_slave = false; | 62 | static bool check_replica = false; |
| 63 | static bool ignore_auth = false; | 63 | static bool ignore_auth = false; |
| 64 | static int verbose = 0; | 64 | static int verbose = 0; |
| 65 | 65 | ||
| @@ -68,39 +68,24 @@ static double critical_time = 0; | |||
| 68 | 68 | ||
| 69 | #define LENGTH_METRIC_UNIT 6 | 69 | #define LENGTH_METRIC_UNIT 6 |
| 70 | static const char *metric_unit[LENGTH_METRIC_UNIT] = { | 70 | static const char *metric_unit[LENGTH_METRIC_UNIT] = { |
| 71 | "Open_files", | 71 | "Open_files", "Open_tables", "Qcache_free_memory", "Qcache_queries_in_cache", "Threads_connected", "Threads_running"}; |
| 72 | "Open_tables", | ||
| 73 | "Qcache_free_memory", | ||
| 74 | "Qcache_queries_in_cache", | ||
| 75 | "Threads_connected", | ||
| 76 | "Threads_running" | ||
| 77 | }; | ||
| 78 | 72 | ||
| 79 | #define LENGTH_METRIC_COUNTER 9 | 73 | #define LENGTH_METRIC_COUNTER 9 |
| 80 | static const char *metric_counter[LENGTH_METRIC_COUNTER] = { | 74 | static const char *metric_counter[LENGTH_METRIC_COUNTER] = { |
| 81 | "Connections", | 75 | "Connections", "Qcache_hits", "Qcache_inserts", "Qcache_lowmem_prunes", "Qcache_not_cached", "Queries", |
| 82 | "Qcache_hits", | 76 | "Questions", "Table_locks_waited", "Uptime"}; |
| 83 | "Qcache_inserts", | 77 | |
| 84 | "Qcache_lowmem_prunes", | 78 | #define MYSQLDUMP_THREADS_QUERY \ |
| 85 | "Qcache_not_cached", | 79 | "SELECT COUNT(1) mysqldumpThreads FROM information_schema.processlist WHERE info LIKE 'SELECT /*!40001 SQL_NO_CACHE */%'" |
| 86 | "Queries", | ||
| 87 | "Questions", | ||
| 88 | "Table_locks_waited", | ||
| 89 | "Uptime" | ||
| 90 | }; | ||
| 91 | |||
| 92 | #define MYSQLDUMP_THREADS_QUERY "SELECT COUNT(1) mysqldumpThreads FROM information_schema.processlist WHERE info LIKE 'SELECT /*!40001 SQL_NO_CACHE */%'" | ||
| 93 | 80 | ||
| 94 | static thresholds *my_threshold = NULL; | 81 | static thresholds *my_threshold = NULL; |
| 95 | 82 | ||
| 96 | static int process_arguments (int, char **); | 83 | static int process_arguments(int, char **); |
| 97 | static int validate_arguments (void); | 84 | static int validate_arguments(void); |
| 98 | static void print_help (void); | 85 | static void print_help(void); |
| 99 | void print_usage (void); | 86 | void print_usage(void); |
| 100 | 87 | ||
| 101 | int | 88 | int main(int argc, char **argv) { |
| 102 | main (int argc, char **argv) | ||
| 103 | { | ||
| 104 | 89 | ||
| 105 | MYSQL mysql; | 90 | MYSQL mysql; |
| 106 | MYSQL_RES *res; | 91 | MYSQL_RES *res; |
| @@ -110,158 +95,206 @@ main (int argc, char **argv) | |||
| 110 | 95 | ||
| 111 | char *result = NULL; | 96 | char *result = NULL; |
| 112 | char *error = NULL; | 97 | char *error = NULL; |
| 113 | char slaveresult[SLAVERESULTSIZE] = { 0 }; | 98 | char replica_result[REPLICA_RESULTSIZE] = {0}; |
| 114 | char* perf; | 99 | char *perf; |
| 115 | 100 | ||
| 116 | perf = strdup (""); | 101 | perf = strdup(""); |
| 117 | 102 | ||
| 118 | setlocale (LC_ALL, ""); | 103 | setlocale(LC_ALL, ""); |
| 119 | bindtextdomain (PACKAGE, LOCALEDIR); | 104 | bindtextdomain(PACKAGE, LOCALEDIR); |
| 120 | textdomain (PACKAGE); | 105 | textdomain(PACKAGE); |
| 121 | 106 | ||
| 122 | /* Parse extra opts if any */ | 107 | /* Parse extra opts if any */ |
| 123 | argv=np_extra_opts (&argc, argv, progname); | 108 | argv = np_extra_opts(&argc, argv, progname); |
| 124 | 109 | ||
| 125 | if (process_arguments (argc, argv) == ERROR) | 110 | if (process_arguments(argc, argv) == ERROR) { |
| 126 | usage4 (_("Could not parse arguments")); | 111 | usage4(_("Could not parse arguments")); |
| 112 | } | ||
| 127 | 113 | ||
| 128 | /* initialize mysql */ | 114 | /* initialize mysql */ |
| 129 | mysql_init (&mysql); | 115 | mysql_init(&mysql); |
| 130 | 116 | ||
| 131 | if (opt_file != NULL) | 117 | if (opt_file != NULL) { |
| 132 | mysql_options(&mysql,MYSQL_READ_DEFAULT_FILE,opt_file); | 118 | mysql_options(&mysql, MYSQL_READ_DEFAULT_FILE, opt_file); |
| 133 | 119 | } | |
| 134 | if (opt_group != NULL) | 120 | |
| 135 | mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,opt_group); | 121 | if (opt_group != NULL) { |
| 136 | else | 122 | mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, opt_group); |
| 137 | mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client"); | 123 | } else { |
| 138 | 124 | mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, "client"); | |
| 139 | if (ssl) | 125 | } |
| 140 | mysql_ssl_set(&mysql,key,cert,ca_cert,ca_dir,ciphers); | 126 | |
| 127 | if (ssl) { | ||
| 128 | mysql_ssl_set(&mysql, key, cert, ca_cert, ca_dir, ciphers); | ||
| 129 | } | ||
| 141 | /* establish a connection to the server and error checking */ | 130 | /* establish a connection to the server and error checking */ |
| 142 | if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,db_socket,0)) { | 131 | if (!mysql_real_connect(&mysql, db_host, db_user, db_pass, db, db_port, db_socket, 0)) { |
| 143 | /* Depending on internally-selected auth plugin MySQL might return */ | 132 | /* Depending on internally-selected auth plugin MySQL might return */ |
| 144 | /* ER_ACCESS_DENIED_NO_PASSWORD_ERROR or ER_ACCESS_DENIED_ERROR. */ | 133 | /* ER_ACCESS_DENIED_NO_PASSWORD_ERROR or ER_ACCESS_DENIED_ERROR. */ |
| 145 | /* Semantically these errors are the same. */ | 134 | /* Semantically these errors are the same. */ |
| 146 | if (ignore_auth && (mysql_errno (&mysql) == ER_ACCESS_DENIED_ERROR || mysql_errno (&mysql) == ER_ACCESS_DENIED_NO_PASSWORD_ERROR)) | 135 | if (ignore_auth && (mysql_errno(&mysql) == ER_ACCESS_DENIED_ERROR || mysql_errno(&mysql) == ER_ACCESS_DENIED_NO_PASSWORD_ERROR)) { |
| 147 | { | 136 | printf("MySQL OK - Version: %s (protocol %d)\n", mysql_get_server_info(&mysql), mysql_get_proto_info(&mysql)); |
| 148 | printf("MySQL OK - Version: %s (protocol %d)\n", | 137 | mysql_close(&mysql); |
| 149 | mysql_get_server_info(&mysql), | ||
| 150 | mysql_get_proto_info(&mysql) | ||
| 151 | ); | ||
| 152 | mysql_close (&mysql); | ||
| 153 | return STATE_OK; | 138 | return STATE_OK; |
| 139 | } else if (mysql_errno(&mysql) == CR_UNKNOWN_HOST) { | ||
| 140 | die(STATE_WARNING, "%s\n", mysql_error(&mysql)); | ||
| 141 | } else if (mysql_errno(&mysql) == CR_VERSION_ERROR) { | ||
| 142 | die(STATE_WARNING, "%s\n", mysql_error(&mysql)); | ||
| 143 | } else if (mysql_errno(&mysql) == CR_OUT_OF_MEMORY) { | ||
| 144 | die(STATE_WARNING, "%s\n", mysql_error(&mysql)); | ||
| 145 | } else if (mysql_errno(&mysql) == CR_IPSOCK_ERROR) { | ||
| 146 | die(STATE_WARNING, "%s\n", mysql_error(&mysql)); | ||
| 147 | } else if (mysql_errno(&mysql) == CR_SOCKET_CREATE_ERROR) { | ||
| 148 | die(STATE_WARNING, "%s\n", mysql_error(&mysql)); | ||
| 149 | } else { | ||
| 150 | die(STATE_CRITICAL, "%s\n", mysql_error(&mysql)); | ||
| 154 | } | 151 | } |
| 155 | else if (mysql_errno (&mysql) == CR_UNKNOWN_HOST) | ||
| 156 | die (STATE_WARNING, "%s\n", mysql_error (&mysql)); | ||
| 157 | else if (mysql_errno (&mysql) == CR_VERSION_ERROR) | ||
| 158 | die (STATE_WARNING, "%s\n", mysql_error (&mysql)); | ||
| 159 | else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY) | ||
| 160 | die (STATE_WARNING, "%s\n", mysql_error (&mysql)); | ||
| 161 | else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR) | ||
| 162 | die (STATE_WARNING, "%s\n", mysql_error (&mysql)); | ||
| 163 | else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR) | ||
| 164 | die (STATE_WARNING, "%s\n", mysql_error (&mysql)); | ||
| 165 | else | ||
| 166 | die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); | ||
| 167 | } | 152 | } |
| 168 | 153 | ||
| 169 | /* get the server stats */ | 154 | /* get the server stats */ |
| 170 | result = strdup (mysql_stat (&mysql)); | 155 | result = strdup(mysql_stat(&mysql)); |
| 171 | 156 | ||
| 172 | /* error checking once more */ | 157 | /* error checking once more */ |
| 173 | if (mysql_error (&mysql)) { | 158 | if (mysql_error(&mysql)) { |
| 174 | if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR) | 159 | if (mysql_errno(&mysql) == CR_SERVER_GONE_ERROR) { |
| 175 | die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); | 160 | die(STATE_CRITICAL, "%s\n", mysql_error(&mysql)); |
| 176 | else if (mysql_errno (&mysql) == CR_SERVER_LOST) | 161 | } else if (mysql_errno(&mysql) == CR_SERVER_LOST) { |
| 177 | die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); | 162 | die(STATE_CRITICAL, "%s\n", mysql_error(&mysql)); |
| 178 | else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR) | 163 | } else if (mysql_errno(&mysql) == CR_UNKNOWN_ERROR) { |
| 179 | die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); | 164 | die(STATE_CRITICAL, "%s\n", mysql_error(&mysql)); |
| 165 | } | ||
| 180 | } | 166 | } |
| 181 | 167 | ||
| 182 | /* try to fetch some perf data */ | 168 | /* try to fetch some perf data */ |
| 183 | if (mysql_query (&mysql, "show global status") == 0) { | 169 | if (mysql_query(&mysql, "show global status") == 0) { |
| 184 | if ( (res = mysql_store_result (&mysql)) == NULL) { | 170 | if ((res = mysql_store_result(&mysql)) == NULL) { |
| 185 | error = strdup(mysql_error(&mysql)); | 171 | error = strdup(mysql_error(&mysql)); |
| 186 | mysql_close (&mysql); | 172 | mysql_close(&mysql); |
| 187 | die (STATE_CRITICAL, _("status store_result error: %s\n"), error); | 173 | die(STATE_CRITICAL, _("status store_result error: %s\n"), error); |
| 188 | } | 174 | } |
| 189 | 175 | ||
| 190 | while ( (row = mysql_fetch_row (res)) != NULL) { | 176 | while ((row = mysql_fetch_row(res)) != NULL) { |
| 191 | int i; | 177 | int i; |
| 192 | 178 | ||
| 193 | for(i = 0; i < LENGTH_METRIC_UNIT; i++) { | 179 | for (i = 0; i < LENGTH_METRIC_UNIT; i++) { |
| 194 | if (strcmp(row[0], metric_unit[i]) == 0) { | 180 | if (strcmp(row[0], metric_unit[i]) == 0) { |
| 195 | xasprintf(&perf, "%s%s ", perf, perfdata(metric_unit[i], | 181 | xasprintf(&perf, "%s%s ", perf, perfdata(metric_unit[i], atol(row[1]), "", false, 0, false, 0, false, 0, false, 0)); |
| 196 | atol(row[1]), "", false, 0, false, 0, false, 0, false, 0)); | ||
| 197 | continue; | 182 | continue; |
| 198 | } | 183 | } |
| 199 | } | 184 | } |
| 200 | for(i = 0; i < LENGTH_METRIC_COUNTER; i++) { | 185 | for (i = 0; i < LENGTH_METRIC_COUNTER; i++) { |
| 201 | if (strcmp(row[0], metric_counter[i]) == 0) { | 186 | if (strcmp(row[0], metric_counter[i]) == 0) { |
| 202 | xasprintf(&perf, "%s%s ", perf, perfdata(metric_counter[i], | 187 | xasprintf(&perf, "%s%s ", perf, perfdata(metric_counter[i], atol(row[1]), "c", false, 0, false, 0, false, 0, false, 0)); |
| 203 | atol(row[1]), "c", false, 0, false, 0, false, 0, false, 0)); | ||
| 204 | continue; | 188 | continue; |
| 205 | } | 189 | } |
| 206 | } | 190 | } |
| 207 | } | 191 | } |
| 208 | /* remove trailing space */ | 192 | /* remove trailing space */ |
| 209 | if (strlen(perf) > 0) | 193 | if (strlen(perf) > 0) { |
| 210 | perf[strlen(perf) - 1] = '\0'; | 194 | perf[strlen(perf) - 1] = '\0'; |
| 195 | } | ||
| 211 | } | 196 | } |
| 212 | 197 | ||
| 213 | if(check_slave) { | 198 | if (check_replica) { |
| 214 | /* check the slave status */ | 199 | |
| 215 | if (mysql_query (&mysql, "show slave status") != 0) { | 200 | // Detect which version we are, on older version |
| 201 | // "show slave status" should work, on newer ones | ||
| 202 | // "show replica status" | ||
| 203 | // But first we have to find out whether this is | ||
| 204 | // MySQL or MariaDB since the version numbering scheme | ||
| 205 | // is different | ||
| 206 | bool use_deprecated_slave_status = false; | ||
| 207 | const char *server_version = mysql_get_server_info(&mysql); | ||
| 208 | unsigned long server_verion_int = mysql_get_server_version(&mysql); | ||
| 209 | unsigned long major_version = server_verion_int / 10000; | ||
| 210 | unsigned long minor_version = (server_verion_int % 10000) / 100; | ||
| 211 | unsigned long patch_version = (server_verion_int % 100); | ||
| 212 | if (verbose) { | ||
| 213 | printf("Found MariaDB: %s, main version: %lu, minor version: %lu, patch version: %lu\n", server_version, major_version, | ||
| 214 | minor_version, patch_version); | ||
| 215 | } | ||
| 216 | |||
| 217 | if (strstr(server_version, "MariaDB") != NULL) { | ||
| 218 | // Looks like MariaDB, new commands should be available after 10.5.1 | ||
| 219 | if (major_version < 10) { | ||
| 220 | use_deprecated_slave_status = true; | ||
| 221 | } else if (major_version == 10) { | ||
| 222 | if (minor_version < 5) { | ||
| 223 | use_deprecated_slave_status = true; | ||
| 224 | } else if (minor_version == 5 && patch_version < 1) { | ||
| 225 | use_deprecated_slave_status = true; | ||
| 226 | } | ||
| 227 | } | ||
| 228 | } else if (strstr(server_version, "MySQL") != NULL) { | ||
| 229 | // Looks like MySQL | ||
| 230 | if (major_version < 8) { | ||
| 231 | use_deprecated_slave_status = true; | ||
| 232 | } else if (major_version == 10 && minor_version < 4) { | ||
| 233 | use_deprecated_slave_status = true; | ||
| 234 | } | ||
| 235 | } else { | ||
| 236 | printf("Not a known sever implementation: %s\n", server_version); | ||
| 237 | exit(STATE_UNKNOWN); | ||
| 238 | } | ||
| 239 | |||
| 240 | char *replica_query = NULL; | ||
| 241 | if (use_deprecated_slave_status) { | ||
| 242 | replica_query = "show slave status"; | ||
| 243 | } else { | ||
| 244 | replica_query = "show replica status"; | ||
| 245 | } | ||
| 246 | |||
| 247 | /* check the replica status */ | ||
| 248 | if (mysql_query(&mysql, replica_query) != 0) { | ||
| 216 | error = strdup(mysql_error(&mysql)); | 249 | error = strdup(mysql_error(&mysql)); |
| 217 | mysql_close (&mysql); | 250 | mysql_close(&mysql); |
| 218 | die (STATE_CRITICAL, _("slave query error: %s\n"), error); | 251 | die(STATE_CRITICAL, _("replica query error: %s\n"), error); |
| 219 | } | 252 | } |
| 220 | 253 | ||
| 221 | /* store the result */ | 254 | /* store the result */ |
| 222 | if ( (res = mysql_store_result (&mysql)) == NULL) { | 255 | if ((res = mysql_store_result(&mysql)) == NULL) { |
| 223 | error = strdup(mysql_error(&mysql)); | 256 | error = strdup(mysql_error(&mysql)); |
| 224 | mysql_close (&mysql); | 257 | mysql_close(&mysql); |
| 225 | die (STATE_CRITICAL, _("slave store_result error: %s\n"), error); | 258 | die(STATE_CRITICAL, _("replica store_result error: %s\n"), error); |
| 226 | } | 259 | } |
| 227 | 260 | ||
| 228 | /* Check there is some data */ | 261 | /* Check there is some data */ |
| 229 | if (mysql_num_rows(res) == 0) { | 262 | if (mysql_num_rows(res) == 0) { |
| 230 | mysql_close(&mysql); | 263 | mysql_close(&mysql); |
| 231 | die (STATE_WARNING, "%s\n", _("No slaves defined")); | 264 | die(STATE_WARNING, "%s\n", _("No replicas defined")); |
| 232 | } | 265 | } |
| 233 | 266 | ||
| 234 | /* fetch the first row */ | 267 | /* fetch the first row */ |
| 235 | if ( (row = mysql_fetch_row (res)) == NULL) { | 268 | if ((row = mysql_fetch_row(res)) == NULL) { |
| 236 | error = strdup(mysql_error(&mysql)); | 269 | error = strdup(mysql_error(&mysql)); |
| 237 | mysql_free_result (res); | 270 | mysql_free_result(res); |
| 238 | mysql_close (&mysql); | 271 | mysql_close(&mysql); |
| 239 | die (STATE_CRITICAL, _("slave fetch row error: %s\n"), error); | 272 | die(STATE_CRITICAL, _("replica fetch row error: %s\n"), error); |
| 240 | } | 273 | } |
| 241 | 274 | ||
| 242 | if (mysql_field_count (&mysql) == 12) { | 275 | if (mysql_field_count(&mysql) == 12) { |
| 243 | /* mysql 3.23.x */ | 276 | /* mysql 3.23.x */ |
| 244 | snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]); | 277 | snprintf(replica_result, REPLICA_RESULTSIZE, _("Replica running: %s"), row[6]); |
| 245 | if (strcmp (row[6], "Yes") != 0) { | 278 | if (strcmp(row[6], "Yes") != 0) { |
| 246 | mysql_free_result (res); | 279 | mysql_free_result(res); |
| 247 | mysql_close (&mysql); | 280 | mysql_close(&mysql); |
| 248 | die (STATE_CRITICAL, "%s\n", slaveresult); | 281 | die(STATE_CRITICAL, "%s\n", replica_result); |
| 249 | } | 282 | } |
| 250 | 283 | ||
| 251 | } else { | 284 | } else { |
| 252 | /* mysql 4.x.x and mysql 5.x.x */ | 285 | /* mysql 4.x.x and mysql 5.x.x */ |
| 253 | int slave_io_field = -1 , slave_sql_field = -1, seconds_behind_field = -1, i, num_fields; | 286 | int replica_io_field = -1, replica_sql_field = -1, seconds_behind_field = -1, i, num_fields; |
| 254 | MYSQL_FIELD* fields; | 287 | MYSQL_FIELD *fields; |
| 255 | 288 | ||
| 256 | num_fields = mysql_num_fields(res); | 289 | num_fields = mysql_num_fields(res); |
| 257 | fields = mysql_fetch_fields(res); | 290 | fields = mysql_fetch_fields(res); |
| 258 | for(i = 0; i < num_fields; i++) { | 291 | for (i = 0; i < num_fields; i++) { |
| 259 | if (strcmp(fields[i].name, "Slave_IO_Running") == 0) { | 292 | if (strcmp(fields[i].name, "Slave_IO_Running") == 0) { |
| 260 | slave_io_field = i; | 293 | replica_io_field = i; |
| 261 | continue; | 294 | continue; |
| 262 | } | 295 | } |
| 263 | if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) { | 296 | if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) { |
| 264 | slave_sql_field = i; | 297 | replica_sql_field = i; |
| 265 | continue; | 298 | continue; |
| 266 | } | 299 | } |
| 267 | if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) { | 300 | if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) { |
| @@ -270,150 +303,147 @@ main (int argc, char **argv) | |||
| 270 | } | 303 | } |
| 271 | } | 304 | } |
| 272 | 305 | ||
| 273 | /* Check if slave status is available */ | 306 | /* Check if replica status is available */ |
| 274 | if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) { | 307 | if ((replica_io_field < 0) || (replica_sql_field < 0) || (num_fields == 0)) { |
| 275 | mysql_free_result (res); | 308 | mysql_free_result(res); |
| 276 | mysql_close (&mysql); | 309 | mysql_close(&mysql); |
| 277 | die (STATE_CRITICAL, "Slave status unavailable\n"); | 310 | die(STATE_CRITICAL, "Replica status unavailable\n"); |
| 278 | } | 311 | } |
| 279 | 312 | ||
| 280 | /* Save slave status in slaveresult */ | 313 | /* Save replica status in replica_result */ |
| 281 | snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s Seconds Behind Master: %s", row[slave_io_field], row[slave_sql_field], seconds_behind_field!=-1?row[seconds_behind_field]:"Unknown"); | 314 | snprintf(replica_result, REPLICA_RESULTSIZE, "Replica IO: %s Replica SQL: %s Seconds Behind Master: %s", row[replica_io_field], |
| 315 | row[replica_sql_field], seconds_behind_field != -1 ? row[seconds_behind_field] : "Unknown"); | ||
| 282 | 316 | ||
| 283 | /* Raise critical error if SQL THREAD or IO THREAD are stopped, but only if there are no mysqldump threads running */ | 317 | /* Raise critical error if SQL THREAD or IO THREAD are stopped, but only if there are no mysqldump threads running */ |
| 284 | if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) { | 318 | if (strcmp(row[replica_io_field], "Yes") != 0 || strcmp(row[replica_sql_field], "Yes") != 0) { |
| 285 | MYSQL_RES *res_mysqldump; | 319 | MYSQL_RES *res_mysqldump; |
| 286 | MYSQL_ROW row_mysqldump; | 320 | MYSQL_ROW row_mysqldump; |
| 287 | unsigned int mysqldump_threads = 0; | 321 | unsigned int mysqldump_threads = 0; |
| 288 | 322 | ||
| 289 | if (mysql_query (&mysql, MYSQLDUMP_THREADS_QUERY) == 0) { | 323 | if (mysql_query(&mysql, MYSQLDUMP_THREADS_QUERY) == 0) { |
| 290 | /* store the result */ | 324 | /* store the result */ |
| 291 | if ( (res_mysqldump = mysql_store_result (&mysql)) != NULL) { | 325 | if ((res_mysqldump = mysql_store_result(&mysql)) != NULL) { |
| 292 | if (mysql_num_rows(res_mysqldump) == 1) { | 326 | if (mysql_num_rows(res_mysqldump) == 1) { |
| 293 | if ( (row_mysqldump = mysql_fetch_row (res_mysqldump)) != NULL) { | 327 | if ((row_mysqldump = mysql_fetch_row(res_mysqldump)) != NULL) { |
| 294 | mysqldump_threads = atoi(row_mysqldump[0]); | 328 | mysqldump_threads = atoi(row_mysqldump[0]); |
| 295 | } | 329 | } |
| 296 | } | 330 | } |
| 297 | /* free the result */ | 331 | /* free the result */ |
| 298 | mysql_free_result (res_mysqldump); | 332 | mysql_free_result(res_mysqldump); |
| 299 | } | 333 | } |
| 300 | mysql_close (&mysql); | 334 | mysql_close(&mysql); |
| 301 | } | 335 | } |
| 302 | if (mysqldump_threads == 0) { | 336 | if (mysqldump_threads == 0) { |
| 303 | die (STATE_CRITICAL, "%s\n", slaveresult); | 337 | die(STATE_CRITICAL, "%s\n", replica_result); |
| 304 | } else { | 338 | } else { |
| 305 | strncat(slaveresult, " Mysqldump: in progress", SLAVERESULTSIZE-1); | 339 | strncat(replica_result, " Mysqldump: in progress", REPLICA_RESULTSIZE - 1); |
| 306 | } | 340 | } |
| 307 | } | 341 | } |
| 308 | 342 | ||
| 309 | if (verbose >=3) { | 343 | if (verbose >= 3) { |
| 310 | if (seconds_behind_field == -1) { | 344 | if (seconds_behind_field == -1) { |
| 311 | printf("seconds_behind_field not found\n"); | 345 | printf("seconds_behind_field not found\n"); |
| 312 | } else { | 346 | } else { |
| 313 | printf ("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]); | 347 | printf("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]); |
| 314 | } | 348 | } |
| 315 | } | 349 | } |
| 316 | 350 | ||
| 317 | /* Check Seconds Behind against threshold */ | 351 | /* Check Seconds Behind against threshold */ |
| 318 | if ((seconds_behind_field != -1) && (row[seconds_behind_field] != NULL && strcmp (row[seconds_behind_field], "NULL") != 0)) { | 352 | if ((seconds_behind_field != -1) && (row[seconds_behind_field] != NULL && strcmp(row[seconds_behind_field], "NULL") != 0)) { |
| 319 | double value = atof(row[seconds_behind_field]); | 353 | double value = atof(row[seconds_behind_field]); |
| 320 | int status; | 354 | int status; |
| 321 | 355 | ||
| 322 | status = get_status(value, my_threshold); | 356 | status = get_status(value, my_threshold); |
| 323 | 357 | ||
| 324 | xasprintf (&perf, "%s %s", perf, fperfdata ("seconds behind master", value, "s", | 358 | xasprintf(&perf, "%s %s", perf, |
| 325 | true, (double) warning_time, | 359 | fperfdata("seconds behind master", value, "s", true, (double)warning_time, true, (double)critical_time, false, 0, |
| 326 | true, (double) critical_time, | 360 | false, 0)); |
| 327 | false, 0, | ||
| 328 | false, 0)); | ||
| 329 | 361 | ||
| 330 | if (status == STATE_WARNING) { | 362 | if (status == STATE_WARNING) { |
| 331 | printf("SLOW_SLAVE %s: %s|%s\n", _("WARNING"), slaveresult, perf); | 363 | printf("SLOW_REPLICA %s: %s|%s\n", _("WARNING"), replica_result, perf); |
| 332 | exit(STATE_WARNING); | 364 | exit(STATE_WARNING); |
| 333 | } else if (status == STATE_CRITICAL) { | 365 | } else if (status == STATE_CRITICAL) { |
| 334 | printf("SLOW_SLAVE %s: %s|%s\n", _("CRITICAL"), slaveresult, perf); | 366 | printf("SLOW_REPLICA %s: %s|%s\n", _("CRITICAL"), replica_result, perf); |
| 335 | exit(STATE_CRITICAL); | 367 | exit(STATE_CRITICAL); |
| 336 | } | 368 | } |
| 337 | } | 369 | } |
| 338 | } | 370 | } |
| 339 | 371 | ||
| 340 | /* free the result */ | 372 | /* free the result */ |
| 341 | mysql_free_result (res); | 373 | mysql_free_result(res); |
| 342 | } | 374 | } |
| 343 | 375 | ||
| 344 | /* close the connection */ | 376 | /* close the connection */ |
| 345 | mysql_close (&mysql); | 377 | mysql_close(&mysql); |
| 346 | 378 | ||
| 347 | /* print out the result of stats */ | 379 | /* print out the result of stats */ |
| 348 | if (check_slave) { | 380 | if (check_replica) { |
| 349 | printf ("%s %s|%s\n", result, slaveresult, perf); | 381 | printf("%s %s|%s\n", result, replica_result, perf); |
| 350 | } else { | 382 | } else { |
| 351 | printf ("%s|%s\n", result, perf); | 383 | printf("%s|%s\n", result, perf); |
| 352 | } | 384 | } |
| 353 | 385 | ||
| 354 | return STATE_OK; | 386 | return STATE_OK; |
| 355 | } | 387 | } |
| 356 | 388 | ||
| 389 | #define CHECK_REPLICA_OPT CHAR_MAX + 1 | ||
| 357 | 390 | ||
| 358 | /* process command-line arguments */ | 391 | /* process command-line arguments */ |
| 359 | int | 392 | int process_arguments(int argc, char **argv) { |
| 360 | process_arguments (int argc, char **argv) | ||
| 361 | { | ||
| 362 | int c; | 393 | int c; |
| 363 | char *warning = NULL; | 394 | char *warning = NULL; |
| 364 | char *critical = NULL; | 395 | char *critical = NULL; |
| 365 | 396 | ||
| 366 | int option = 0; | 397 | int option = 0; |
| 367 | static struct option longopts[] = { | 398 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, |
| 368 | {"hostname", required_argument, 0, 'H'}, | 399 | {"socket", required_argument, 0, 's'}, |
| 369 | {"socket", required_argument, 0, 's'}, | 400 | {"database", required_argument, 0, 'd'}, |
| 370 | {"database", required_argument, 0, 'd'}, | 401 | {"username", required_argument, 0, 'u'}, |
| 371 | {"username", required_argument, 0, 'u'}, | 402 | {"password", required_argument, 0, 'p'}, |
| 372 | {"password", required_argument, 0, 'p'}, | 403 | {"file", required_argument, 0, 'f'}, |
| 373 | {"file", required_argument, 0, 'f'}, | 404 | {"group", required_argument, 0, 'g'}, |
| 374 | {"group", required_argument, 0, 'g'}, | 405 | {"port", required_argument, 0, 'P'}, |
| 375 | {"port", required_argument, 0, 'P'}, | 406 | {"critical", required_argument, 0, 'c'}, |
| 376 | {"critical", required_argument, 0, 'c'}, | 407 | {"warning", required_argument, 0, 'w'}, |
| 377 | {"warning", required_argument, 0, 'w'}, | 408 | {"check-slave", no_argument, 0, 'S'}, |
| 378 | {"check-slave", no_argument, 0, 'S'}, | 409 | {"check-replica", no_argument, 0, CHECK_REPLICA_OPT}, |
| 379 | {"ignore-auth", no_argument, 0, 'n'}, | 410 | {"ignore-auth", no_argument, 0, 'n'}, |
| 380 | {"verbose", no_argument, 0, 'v'}, | 411 | {"verbose", no_argument, 0, 'v'}, |
| 381 | {"version", no_argument, 0, 'V'}, | 412 | {"version", no_argument, 0, 'V'}, |
| 382 | {"help", no_argument, 0, 'h'}, | 413 | {"help", no_argument, 0, 'h'}, |
| 383 | {"ssl", no_argument, 0, 'l'}, | 414 | {"ssl", no_argument, 0, 'l'}, |
| 384 | {"ca-cert", optional_argument, 0, 'C'}, | 415 | {"ca-cert", optional_argument, 0, 'C'}, |
| 385 | {"key", required_argument,0,'k'}, | 416 | {"key", required_argument, 0, 'k'}, |
| 386 | {"cert", required_argument,0,'a'}, | 417 | {"cert", required_argument, 0, 'a'}, |
| 387 | {"ca-dir", required_argument, 0, 'D'}, | 418 | {"ca-dir", required_argument, 0, 'D'}, |
| 388 | {"ciphers", required_argument, 0, 'L'}, | 419 | {"ciphers", required_argument, 0, 'L'}, |
| 389 | {0, 0, 0, 0} | 420 | {0, 0, 0, 0}}; |
| 390 | }; | 421 | |
| 391 | 422 | if (argc < 1) { | |
| 392 | if (argc < 1) | ||
| 393 | return ERROR; | 423 | return ERROR; |
| 424 | } | ||
| 394 | 425 | ||
| 395 | while (1) { | 426 | while (1) { |
| 396 | c = getopt_long (argc, argv, "hlvVnSP:p:u:d:H:s:c:w:a:k:C:D:L:f:g:", longopts, &option); | 427 | c = getopt_long(argc, argv, "hlvVnSP:p:u:d:H:s:c:w:a:k:C:D:L:f:g:", longopts, &option); |
| 397 | 428 | ||
| 398 | if (c == -1 || c == EOF) | 429 | if (c == -1 || c == EOF) { |
| 399 | break; | 430 | break; |
| 431 | } | ||
| 400 | 432 | ||
| 401 | switch (c) { | 433 | switch (c) { |
| 402 | case 'H': /* hostname */ | 434 | case 'H': /* hostname */ |
| 403 | if (is_host (optarg)) { | 435 | if (is_host(optarg)) { |
| 404 | db_host = optarg; | 436 | db_host = optarg; |
| 405 | } | 437 | } else if (*optarg == '/') { |
| 406 | else if (*optarg == '/') { | ||
| 407 | db_socket = optarg; | 438 | db_socket = optarg; |
| 408 | } | 439 | } else { |
| 409 | else { | 440 | usage2(_("Invalid hostname/address"), optarg); |
| 410 | usage2 (_("Invalid hostname/address"), optarg); | ||
| 411 | } | 441 | } |
| 412 | break; | 442 | break; |
| 413 | case 's': /* socket */ | 443 | case 's': /* socket */ |
| 414 | db_socket = optarg; | 444 | db_socket = optarg; |
| 415 | break; | 445 | break; |
| 416 | case 'd': /* database */ | 446 | case 'd': /* database */ |
| 417 | db = optarg; | 447 | db = optarg; |
| 418 | break; | 448 | break; |
| 419 | case 'l': | 449 | case 'l': |
| @@ -434,10 +464,10 @@ process_arguments (int argc, char **argv) | |||
| 434 | case 'L': | 464 | case 'L': |
| 435 | ciphers = optarg; | 465 | ciphers = optarg; |
| 436 | break; | 466 | break; |
| 437 | case 'u': /* username */ | 467 | case 'u': /* username */ |
| 438 | db_user = optarg; | 468 | db_user = optarg; |
| 439 | break; | 469 | break; |
| 440 | case 'p': /* authentication information: password */ | 470 | case 'p': /* authentication information: password */ |
| 441 | db_pass = strdup(optarg); | 471 | db_pass = strdup(optarg); |
| 442 | 472 | ||
| 443 | /* Delete the password from process list */ | 473 | /* Delete the password from process list */ |
| @@ -446,40 +476,41 @@ process_arguments (int argc, char **argv) | |||
| 446 | optarg++; | 476 | optarg++; |
| 447 | } | 477 | } |
| 448 | break; | 478 | break; |
| 449 | case 'f': /* client options file */ | 479 | case 'f': /* client options file */ |
| 450 | opt_file = optarg; | 480 | opt_file = optarg; |
| 451 | break; | 481 | break; |
| 452 | case 'g': /* client options group */ | 482 | case 'g': /* client options group */ |
| 453 | opt_group = optarg; | 483 | opt_group = optarg; |
| 454 | break; | 484 | break; |
| 455 | case 'P': /* critical time threshold */ | 485 | case 'P': /* critical time threshold */ |
| 456 | db_port = atoi (optarg); | 486 | db_port = atoi(optarg); |
| 457 | break; | 487 | break; |
| 458 | case 'S': | 488 | case 'S': |
| 459 | check_slave = true; /* check-slave */ | 489 | case CHECK_REPLICA_OPT: |
| 490 | check_replica = true; /* check-slave */ | ||
| 460 | break; | 491 | break; |
| 461 | case 'n': | 492 | case 'n': |
| 462 | ignore_auth = true; /* ignore-auth */ | 493 | ignore_auth = true; /* ignore-auth */ |
| 463 | break; | 494 | break; |
| 464 | case 'w': | 495 | case 'w': |
| 465 | warning = optarg; | 496 | warning = optarg; |
| 466 | warning_time = strtod (warning, NULL); | 497 | warning_time = strtod(warning, NULL); |
| 467 | break; | 498 | break; |
| 468 | case 'c': | 499 | case 'c': |
| 469 | critical = optarg; | 500 | critical = optarg; |
| 470 | critical_time = strtod (critical, NULL); | 501 | critical_time = strtod(critical, NULL); |
| 471 | break; | 502 | break; |
| 472 | case 'V': /* version */ | 503 | case 'V': /* version */ |
| 473 | print_revision (progname, NP_VERSION); | 504 | print_revision(progname, NP_VERSION); |
| 474 | exit (STATE_UNKNOWN); | 505 | exit(STATE_UNKNOWN); |
| 475 | case 'h': /* help */ | 506 | case 'h': /* help */ |
| 476 | print_help (); | 507 | print_help(); |
| 477 | exit (STATE_UNKNOWN); | 508 | exit(STATE_UNKNOWN); |
| 478 | case 'v': | 509 | case 'v': |
| 479 | verbose++; | 510 | verbose++; |
| 480 | break; | 511 | break; |
| 481 | case '?': /* help */ | 512 | case '?': /* help */ |
| 482 | usage5 (); | 513 | usage5(); |
| 483 | } | 514 | } |
| 484 | } | 515 | } |
| 485 | 516 | ||
| @@ -487,126 +518,122 @@ process_arguments (int argc, char **argv) | |||
| 487 | 518 | ||
| 488 | set_thresholds(&my_threshold, warning, critical); | 519 | set_thresholds(&my_threshold, warning, critical); |
| 489 | 520 | ||
| 490 | while ( argc > c ) { | 521 | while (argc > c) { |
| 491 | 522 | ||
| 492 | if (db_host == NULL) | 523 | if (db_host == NULL) { |
| 493 | if (is_host (argv[c])) { | 524 | if (is_host(argv[c])) { |
| 494 | db_host = argv[c++]; | 525 | db_host = argv[c++]; |
| 526 | } else { | ||
| 527 | usage2(_("Invalid hostname/address"), argv[c]); | ||
| 495 | } | 528 | } |
| 496 | else { | 529 | } else if (db_user == NULL) { |
| 497 | usage2 (_("Invalid hostname/address"), argv[c]); | ||
| 498 | } | ||
| 499 | else if (db_user == NULL) | ||
| 500 | db_user = argv[c++]; | 530 | db_user = argv[c++]; |
| 501 | else if (db_pass == NULL) | 531 | } else if (db_pass == NULL) { |
| 502 | db_pass = argv[c++]; | 532 | db_pass = argv[c++]; |
| 503 | else if (db == NULL) | 533 | } else if (db == NULL) { |
| 504 | db = argv[c++]; | 534 | db = argv[c++]; |
| 505 | else if (is_intnonneg (argv[c])) | 535 | } else if (is_intnonneg(argv[c])) { |
| 506 | db_port = atoi (argv[c++]); | 536 | db_port = atoi(argv[c++]); |
| 507 | else | 537 | } else { |
| 508 | break; | 538 | break; |
| 539 | } | ||
| 509 | } | 540 | } |
| 510 | 541 | ||
| 511 | return validate_arguments (); | 542 | return validate_arguments(); |
| 512 | } | 543 | } |
| 513 | 544 | ||
| 514 | 545 | int validate_arguments(void) { | |
| 515 | int | 546 | if (db_user == NULL) { |
| 516 | validate_arguments (void) | ||
| 517 | { | ||
| 518 | if (db_user == NULL) | ||
| 519 | db_user = strdup(""); | 547 | db_user = strdup(""); |
| 548 | } | ||
| 520 | 549 | ||
| 521 | if (db_host == NULL) | 550 | if (db_host == NULL) { |
| 522 | db_host = strdup(""); | 551 | db_host = strdup(""); |
| 552 | } | ||
| 523 | 553 | ||
| 524 | if (db == NULL) | 554 | if (db == NULL) { |
| 525 | db = strdup(""); | 555 | db = strdup(""); |
| 556 | } | ||
| 526 | 557 | ||
| 527 | return OK; | 558 | return OK; |
| 528 | } | 559 | } |
| 529 | 560 | ||
| 530 | 561 | void print_help(void) { | |
| 531 | void | ||
| 532 | print_help (void) | ||
| 533 | { | ||
| 534 | char *myport; | 562 | char *myport; |
| 535 | xasprintf (&myport, "%d", MYSQL_PORT); | 563 | xasprintf(&myport, "%d", MYSQL_PORT); |
| 536 | 564 | ||
| 537 | print_revision (progname, NP_VERSION); | 565 | print_revision(progname, NP_VERSION); |
| 538 | 566 | ||
| 539 | printf (_(COPYRIGHT), copyright, email); | 567 | printf(_(COPYRIGHT), copyright, email); |
| 540 | 568 | ||
| 541 | printf ("%s\n", _("This program tests connections to a MySQL server")); | 569 | printf("%s\n", _("This program tests connections to a MySQL server")); |
| 542 | 570 | ||
| 543 | printf ("\n\n"); | 571 | printf("\n\n"); |
| 544 | 572 | ||
| 545 | print_usage (); | 573 | print_usage(); |
| 546 | 574 | ||
| 547 | printf (UT_HELP_VRSN); | 575 | printf(UT_HELP_VRSN); |
| 548 | printf (UT_EXTRA_OPTS); | 576 | printf(UT_EXTRA_OPTS); |
| 549 | 577 | ||
| 550 | printf (UT_HOST_PORT, 'P', myport); | 578 | printf(UT_HOST_PORT, 'P', myport); |
| 551 | printf (" %s\n", "-n, --ignore-auth"); | 579 | printf(" %s\n", "-n, --ignore-auth"); |
| 552 | printf (" %s\n", _("Ignore authentication failure and check for mysql connectivity only")); | 580 | printf(" %s\n", _("Ignore authentication failure and check for mysql connectivity only")); |
| 553 | 581 | ||
| 554 | printf (" %s\n", "-s, --socket=STRING"); | 582 | printf(" %s\n", "-s, --socket=STRING"); |
| 555 | printf (" %s\n", _("Use the specified socket (has no effect if -H is used)")); | 583 | printf(" %s\n", _("Use the specified socket (has no effect if -H is used)")); |
| 556 | 584 | ||
| 557 | printf (" %s\n", "-d, --database=STRING"); | 585 | printf(" %s\n", "-d, --database=STRING"); |
| 558 | printf (" %s\n", _("Check database with indicated name")); | 586 | printf(" %s\n", _("Check database with indicated name")); |
| 559 | printf (" %s\n", "-f, --file=STRING"); | 587 | printf(" %s\n", "-f, --file=STRING"); |
| 560 | printf (" %s\n", _("Read from the specified client options file")); | 588 | printf(" %s\n", _("Read from the specified client options file")); |
| 561 | printf (" %s\n", "-g, --group=STRING"); | 589 | printf(" %s\n", "-g, --group=STRING"); |
| 562 | printf (" %s\n", _("Use a client options group")); | 590 | printf(" %s\n", _("Use a client options group")); |
| 563 | printf (" %s\n", "-u, --username=STRING"); | 591 | printf(" %s\n", "-u, --username=STRING"); |
| 564 | printf (" %s\n", _("Connect using the indicated username")); | 592 | printf(" %s\n", _("Connect using the indicated username")); |
| 565 | printf (" %s\n", "-p, --password=STRING"); | 593 | printf(" %s\n", "-p, --password=STRING"); |
| 566 | printf (" %s\n", _("Use the indicated password to authenticate the connection")); | 594 | printf(" %s\n", _("Use the indicated password to authenticate the connection")); |
| 567 | printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!")); | 595 | printf(" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!")); |
| 568 | printf (" %s\n", _("Your clear-text password could be visible as a process table entry")); | 596 | printf(" %s\n", _("Your clear-text password could be visible as a process table entry")); |
| 569 | printf (" %s\n", "-S, --check-slave"); | 597 | printf(" %s\n", "-S, --check-slave"); |
| 570 | printf (" %s\n", _("Check if the slave thread is running properly.")); | 598 | printf(" %s\n", |
| 571 | printf (" %s\n", "-w, --warning"); | 599 | _("Check if the slave thread is running properly. This option is deprecated in favour of check-replica, which does the same")); |
| 572 | printf (" %s\n", _("Exit with WARNING status if slave server is more than INTEGER seconds")); | 600 | printf(" %s\n", "--check-replica"); |
| 573 | printf (" %s\n", _("behind master")); | 601 | printf(" %s\n", _("Check if the replica thread is running properly.")); |
| 574 | printf (" %s\n", "-c, --critical"); | 602 | printf(" %s\n", "-w, --warning"); |
| 575 | printf (" %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds")); | 603 | printf(" %s\n", _("Exit with WARNING status if replica server is more than INTEGER seconds")); |
| 576 | printf (" %s\n", _("behind master")); | 604 | printf(" %s\n", _("behind master")); |
| 577 | printf (" %s\n", "-l, --ssl"); | 605 | printf(" %s\n", "-c, --critical"); |
| 578 | printf (" %s\n", _("Use ssl encryption")); | 606 | printf(" %s\n", _("Exit with CRITICAL status if replica server is more then INTEGER seconds")); |
| 579 | printf (" %s\n", "-C, --ca-cert=STRING"); | 607 | printf(" %s\n", _("behind master")); |
| 580 | printf (" %s\n", _("Path to CA signing the cert")); | 608 | printf(" %s\n", "-l, --ssl"); |
| 581 | printf (" %s\n", "-a, --cert=STRING"); | 609 | printf(" %s\n", _("Use ssl encryption")); |
| 582 | printf (" %s\n", _("Path to SSL certificate")); | 610 | printf(" %s\n", "-C, --ca-cert=STRING"); |
| 583 | printf (" %s\n", "-k, --key=STRING"); | 611 | printf(" %s\n", _("Path to CA signing the cert")); |
| 584 | printf (" %s\n", _("Path to private SSL key")); | 612 | printf(" %s\n", "-a, --cert=STRING"); |
| 585 | printf (" %s\n", "-D, --ca-dir=STRING"); | 613 | printf(" %s\n", _("Path to SSL certificate")); |
| 586 | printf (" %s\n", _("Path to CA directory")); | 614 | printf(" %s\n", "-k, --key=STRING"); |
| 587 | printf (" %s\n", "-L, --ciphers=STRING"); | 615 | printf(" %s\n", _("Path to private SSL key")); |
| 588 | printf (" %s\n", _("List of valid SSL ciphers")); | 616 | printf(" %s\n", "-D, --ca-dir=STRING"); |
| 589 | 617 | printf(" %s\n", _("Path to CA directory")); | |
| 590 | 618 | printf(" %s\n", "-L, --ciphers=STRING"); | |
| 591 | printf ("\n"); | 619 | printf(" %s\n", _("List of valid SSL ciphers")); |
| 592 | printf (" %s\n", _("There are no required arguments. By default, the local database is checked")); | 620 | |
| 593 | printf (" %s\n", _("using the default unix socket. You can force TCP on localhost by using an")); | 621 | printf("\n"); |
| 594 | printf (" %s\n", _("IP address or FQDN ('localhost' will use the socket as well).")); | 622 | printf(" %s\n", _("There are no required arguments. By default, the local database is checked")); |
| 595 | 623 | printf(" %s\n", _("using the default unix socket. You can force TCP on localhost by using an")); | |
| 596 | printf ("\n"); | 624 | printf(" %s\n", _("IP address or FQDN ('localhost' will use the socket as well).")); |
| 597 | printf ("%s\n", _("Notes:")); | 625 | |
| 598 | printf (" %s\n", _("You must specify -p with an empty string to force an empty password,")); | 626 | printf("\n"); |
| 599 | printf (" %s\n", _("overriding any my.cnf settings.")); | 627 | printf("%s\n", _("Notes:")); |
| 600 | 628 | printf(" %s\n", _("You must specify -p with an empty string to force an empty password,")); | |
| 601 | printf (UT_SUPPORT); | 629 | printf(" %s\n", _("overriding any my.cnf settings.")); |
| 630 | |||
| 631 | printf(UT_SUPPORT); | ||
| 602 | } | 632 | } |
| 603 | 633 | ||
| 604 | 634 | void print_usage(void) { | |
| 605 | void | 635 | printf("%s\n", _("Usage:")); |
| 606 | print_usage (void) | 636 | printf(" %s [-d database] [-H host] [-P port] [-s socket]\n", progname); |
| 607 | { | 637 | printf(" [-u user] [-p password] [-S] [-l] [-a cert] [-k key]\n"); |
| 608 | printf ("%s\n", _("Usage:")); | 638 | printf(" [-C ca-cert] [-D ca-dir] [-L ciphers] [-f optfile] [-g group]\n"); |
| 609 | printf (" %s [-d database] [-H host] [-P port] [-s socket]\n",progname); | ||
| 610 | printf (" [-u user] [-p password] [-S] [-l] [-a cert] [-k key]\n"); | ||
| 611 | printf (" [-C ca-cert] [-D ca-dir] [-L ciphers] [-f optfile] [-g group]\n"); | ||
| 612 | } | 639 | } |
diff --git a/plugins/check_ssh.c b/plugins/check_ssh.c index 42a88cf9..9d0d7cde 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" | ||
| 31 | const char *progname = "check_ssh"; | 34 | const char *progname = "check_ssh"; |
| 32 | const char *copyright = "2000-2024"; | 35 | const char *copyright = "2000-2024"; |
| 33 | const char *email = "devel@monitoring-plugins.org"; | 36 | const char *email = "devel@monitoring-plugins.org"; |
| @@ -35,26 +38,26 @@ const char *email = "devel@monitoring-plugins.org"; | |||
| 35 | #include "./common.h" | 38 | #include "./common.h" |
| 36 | #include "./netutils.h" | 39 | #include "./netutils.h" |
| 37 | #include "utils.h" | 40 | #include "utils.h" |
| 41 | #include "./check_ssh.d/config.h" | ||
| 38 | 42 | ||
| 39 | #ifndef MSG_DONTWAIT | 43 | #ifndef MSG_DONTWAIT |
| 40 | # define MSG_DONTWAIT 0 | 44 | # define MSG_DONTWAIT 0 |
| 41 | #endif | 45 | #endif |
| 42 | 46 | ||
| 43 | #define SSH_DFL_PORT 22 | 47 | #define BUFF_SZ 256 |
| 44 | #define BUFF_SZ 256 | ||
| 45 | 48 | ||
| 46 | static int port = -1; | ||
| 47 | static char *server_name = NULL; | ||
| 48 | static char *remote_version = NULL; | ||
| 49 | static char *remote_protocol = NULL; | ||
| 50 | static bool verbose = false; | 49 | static bool verbose = false; |
| 51 | 50 | ||
| 52 | static int process_arguments(int /*argc*/, char ** /*argv*/); | 51 | typedef struct process_arguments_wrapper { |
| 53 | static int validate_arguments(void); | 52 | int errorcode; |
| 53 | check_ssh_config config; | ||
| 54 | } process_arguments_wrapper; | ||
| 55 | |||
| 56 | static process_arguments_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
| 54 | static void print_help(void); | 57 | static void print_help(void); |
| 55 | void print_usage(void); | 58 | void print_usage(void); |
| 56 | 59 | ||
| 57 | static int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_protocol); | 60 | static int ssh_connect(mp_check *overall, char *haddr, int hport, char *remote_version, char *remote_protocol); |
| 58 | 61 | ||
| 59 | int main(int argc, char **argv) { | 62 | int main(int argc, char **argv) { |
| 60 | setlocale(LC_ALL, ""); | 63 | setlocale(LC_ALL, ""); |
| @@ -64,24 +67,35 @@ int main(int argc, char **argv) { | |||
| 64 | /* Parse extra opts if any */ | 67 | /* Parse extra opts if any */ |
| 65 | argv = np_extra_opts(&argc, argv, progname); | 68 | argv = np_extra_opts(&argc, argv, progname); |
| 66 | 69 | ||
| 67 | if (process_arguments(argc, argv) == ERROR) | 70 | process_arguments_wrapper tmp_config = process_arguments(argc, argv); |
| 71 | |||
| 72 | if (tmp_config.errorcode == ERROR) { | ||
| 68 | usage4(_("Could not parse arguments")); | 73 | usage4(_("Could not parse arguments")); |
| 74 | } | ||
| 75 | |||
| 76 | check_ssh_config config = tmp_config.config; | ||
| 77 | |||
| 78 | mp_check overall = mp_check_init(); | ||
| 79 | if (config.output_format_is_set) { | ||
| 80 | mp_set_format(config.output_format); | ||
| 81 | } | ||
| 69 | 82 | ||
| 70 | /* initialize alarm signal handling */ | 83 | /* initialize alarm signal handling */ |
| 71 | signal(SIGALRM, socket_timeout_alarm_handler); | 84 | signal(SIGALRM, socket_timeout_alarm_handler); |
| 72 | |||
| 73 | alarm(socket_timeout); | 85 | alarm(socket_timeout); |
| 74 | 86 | ||
| 75 | /* ssh_connect exits if error is found */ | 87 | /* ssh_connect exits if error is found */ |
| 76 | int result = ssh_connect(server_name, port, remote_version, remote_protocol); | 88 | ssh_connect(&overall, config.server_name, config.port, config.remote_version, config.remote_protocol); |
| 77 | 89 | ||
| 78 | alarm(0); | 90 | alarm(0); |
| 79 | 91 | ||
| 80 | return (result); | 92 | mp_exit(overall); |
| 81 | } | 93 | } |
| 82 | 94 | ||
| 95 | #define output_format_index CHAR_MAX + 1 | ||
| 96 | |||
| 83 | /* process command-line arguments */ | 97 | /* process command-line arguments */ |
| 84 | int process_arguments(int argc, char **argv) { | 98 | process_arguments_wrapper process_arguments(int argc, char **argv) { |
| 85 | static struct option longopts[] = {{"help", no_argument, 0, 'h'}, | 99 | static struct option longopts[] = {{"help", no_argument, 0, 'h'}, |
| 86 | {"version", no_argument, 0, 'V'}, | 100 | {"version", no_argument, 0, 'V'}, |
| 87 | {"host", required_argument, 0, 'H'}, /* backward compatibility */ | 101 | {"host", required_argument, 0, 'H'}, /* backward compatibility */ |
| @@ -93,22 +107,33 @@ int process_arguments(int argc, char **argv) { | |||
| 93 | {"verbose", no_argument, 0, 'v'}, | 107 | {"verbose", no_argument, 0, 'v'}, |
| 94 | {"remote-version", required_argument, 0, 'r'}, | 108 | {"remote-version", required_argument, 0, 'r'}, |
| 95 | {"remote-protocol", required_argument, 0, 'P'}, | 109 | {"remote-protocol", required_argument, 0, 'P'}, |
| 110 | {"output-format", required_argument, 0, output_format_index}, | ||
| 96 | {0, 0, 0, 0}}; | 111 | {0, 0, 0, 0}}; |
| 97 | 112 | ||
| 98 | if (argc < 2) | 113 | process_arguments_wrapper result = { |
| 99 | return ERROR; | 114 | .config = check_ssh_config_init(), |
| 115 | .errorcode = OK, | ||
| 116 | }; | ||
| 117 | |||
| 118 | if (argc < 2) { | ||
| 119 | result.errorcode = ERROR; | ||
| 120 | return result; | ||
| 121 | } | ||
| 100 | 122 | ||
| 101 | for (int i = 1; i < argc; i++) | 123 | for (int i = 1; i < argc; i++) { |
| 102 | if (strcmp("-to", argv[i]) == 0) | 124 | if (strcmp("-to", argv[i]) == 0) { |
| 103 | strcpy(argv[i], "-t"); | 125 | strcpy(argv[i], "-t"); |
| 126 | } | ||
| 127 | } | ||
| 104 | 128 | ||
| 105 | int option_char; | 129 | int option_char; |
| 106 | while (true) { | 130 | while (true) { |
| 107 | int option = 0; | 131 | int option = 0; |
| 108 | option_char = getopt_long(argc, argv, "+Vhv46t:r:H:p:P:", longopts, &option); | 132 | option_char = getopt_long(argc, argv, "+Vhv46t:r:H:p:P:", longopts, &option); |
| 109 | 133 | ||
| 110 | if (option_char == -1 || option_char == EOF) | 134 | if (option_char == -1 || option_char == EOF) { |
| 111 | break; | 135 | break; |
| 136 | } | ||
| 112 | 137 | ||
| 113 | switch (option_char) { | 138 | switch (option_char) { |
| 114 | case '?': /* help */ | 139 | case '?': /* help */ |
| @@ -123,10 +148,11 @@ int process_arguments(int argc, char **argv) { | |||
| 123 | verbose = true; | 148 | verbose = true; |
| 124 | break; | 149 | break; |
| 125 | case 't': /* timeout period */ | 150 | case 't': /* timeout period */ |
| 126 | if (!is_integer(optarg)) | 151 | if (!is_intpos(optarg)) { |
| 127 | usage2(_("Timeout interval must be a positive integer"), optarg); | 152 | usage2(_("Timeout interval must be a positive integer"), optarg); |
| 128 | else | 153 | } else { |
| 129 | socket_timeout = atoi(optarg); | 154 | socket_timeout = (unsigned int)atoi(optarg); |
| 155 | } | ||
| 130 | break; | 156 | break; |
| 131 | case '4': | 157 | case '4': |
| 132 | address_family = AF_INET; | 158 | address_family = AF_INET; |
| @@ -139,50 +165,61 @@ int process_arguments(int argc, char **argv) { | |||
| 139 | #endif | 165 | #endif |
| 140 | break; | 166 | break; |
| 141 | case 'r': /* remote version */ | 167 | case 'r': /* remote version */ |
| 142 | remote_version = optarg; | 168 | result.config.remote_version = optarg; |
| 143 | break; | 169 | break; |
| 144 | case 'P': /* remote version */ | 170 | case 'P': /* remote version */ |
| 145 | remote_protocol = optarg; | 171 | result.config.remote_protocol = optarg; |
| 146 | break; | 172 | break; |
| 147 | case 'H': /* host */ | 173 | case 'H': /* host */ |
| 148 | if (!is_host(optarg)) | 174 | if (!is_host(optarg)) { |
| 149 | usage2(_("Invalid hostname/address"), optarg); | 175 | usage2(_("Invalid hostname/address"), optarg); |
| 150 | server_name = optarg; | 176 | } |
| 177 | result.config.server_name = optarg; | ||
| 151 | break; | 178 | break; |
| 152 | case 'p': /* port */ | 179 | case 'p': /* port */ |
| 153 | if (is_intpos(optarg)) { | 180 | if (is_intpos(optarg)) { |
| 154 | port = atoi(optarg); | 181 | result.config.port = atoi(optarg); |
| 155 | } else { | 182 | } else { |
| 156 | usage2(_("Port number must be a positive integer"), optarg); | 183 | usage2(_("Port number must be a positive integer"), optarg); |
| 157 | } | 184 | } |
| 185 | break; | ||
| 186 | case output_format_index: { | ||
| 187 | parsed_output_format parser = mp_parse_output_format(optarg); | ||
| 188 | if (!parser.parsing_success) { | ||
| 189 | // TODO List all available formats here, maybe add anothoer usage function | ||
| 190 | printf("Invalid output format: %s\n", optarg); | ||
| 191 | exit(STATE_UNKNOWN); | ||
| 192 | } | ||
| 193 | |||
| 194 | result.config.output_format_is_set = true; | ||
| 195 | result.config.output_format = parser.output_format; | ||
| 196 | break; | ||
| 197 | } | ||
| 158 | } | 198 | } |
| 159 | } | 199 | } |
| 160 | 200 | ||
| 161 | option_char = optind; | 201 | option_char = optind; |
| 162 | if (server_name == NULL && option_char < argc) { | 202 | if (result.config.server_name == NULL && option_char < argc) { |
| 163 | if (is_host(argv[option_char])) { | 203 | if (is_host(argv[option_char])) { |
| 164 | server_name = argv[option_char++]; | 204 | result.config.server_name = argv[option_char++]; |
| 165 | } | 205 | } |
| 166 | } | 206 | } |
| 167 | 207 | ||
| 168 | if (port == -1 && option_char < argc) { | 208 | if (result.config.port == -1 && option_char < argc) { |
| 169 | if (is_intpos(argv[option_char])) { | 209 | if (is_intpos(argv[option_char])) { |
| 170 | port = atoi(argv[option_char++]); | 210 | result.config.port = atoi(argv[option_char++]); |
| 171 | } else { | 211 | } else { |
| 172 | print_usage(); | 212 | print_usage(); |
| 173 | exit(STATE_UNKNOWN); | 213 | exit(STATE_UNKNOWN); |
| 174 | } | 214 | } |
| 175 | } | 215 | } |
| 176 | 216 | ||
| 177 | return validate_arguments(); | 217 | if (result.config.server_name == NULL) { |
| 178 | } | 218 | result.errorcode = ERROR; |
| 219 | return result; | ||
| 220 | } | ||
| 179 | 221 | ||
| 180 | int validate_arguments(void) { | 222 | return result; |
| 181 | if (server_name == NULL) | ||
| 182 | return ERROR; | ||
| 183 | if (port == -1) /* funky, but allows -p to override stray integer in args */ | ||
| 184 | port = SSH_DFL_PORT; | ||
| 185 | return OK; | ||
| 186 | } | 223 | } |
| 187 | 224 | ||
| 188 | /************************************************************************ | 225 | /************************************************************************ |
| @@ -191,28 +228,34 @@ int validate_arguments(void) { | |||
| 191 | * | 228 | * |
| 192 | *-----------------------------------------------------------------------*/ | 229 | *-----------------------------------------------------------------------*/ |
| 193 | 230 | ||
| 194 | int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_protocol) { | 231 | int ssh_connect(mp_check *overall, char *haddr, int hport, char *desired_remote_version, char *desired_remote_protocol) { |
| 195 | struct timeval tv; | 232 | struct timeval tv; |
| 196 | gettimeofday(&tv, NULL); | 233 | gettimeofday(&tv, NULL); |
| 197 | 234 | ||
| 198 | int socket; | 235 | int socket; |
| 199 | int result = my_tcp_connect(haddr, hport, &socket); | 236 | int result = my_tcp_connect(haddr, hport, &socket); |
| 200 | 237 | ||
| 201 | if (result != STATE_OK) | 238 | mp_subcheck connection_sc = mp_subcheck_init(); |
| 239 | if (result != STATE_OK) { | ||
| 240 | connection_sc = mp_set_subcheck_state(connection_sc, STATE_CRITICAL); | ||
| 241 | xasprintf(&connection_sc.output, "Failed to establish TCP connection to Host %s and Port %d", haddr, hport); | ||
| 242 | mp_add_subcheck_to_check(overall, connection_sc); | ||
| 202 | return result; | 243 | return result; |
| 244 | } | ||
| 203 | 245 | ||
| 204 | char *output = (char *)calloc(BUFF_SZ + 1, sizeof(char)); | 246 | char *output = (char *)calloc(BUFF_SZ + 1, sizeof(char)); |
| 205 | char *buffer = NULL; | 247 | char *buffer = NULL; |
| 206 | ssize_t recv_ret = 0; | 248 | size_t recv_ret = 0; |
| 207 | char *version_control_string = NULL; | 249 | char *version_control_string = NULL; |
| 208 | ssize_t byte_offset = 0; | 250 | size_t byte_offset = 0; |
| 209 | while ((version_control_string == NULL) && (recv_ret = recv(socket, output + byte_offset, BUFF_SZ - byte_offset, 0) > 0)) { | 251 | while ((version_control_string == NULL) && |
| 252 | (recv_ret = recv(socket, output + byte_offset, (unsigned long)(BUFF_SZ - byte_offset), 0) > 0)) { | ||
| 210 | 253 | ||
| 211 | if (strchr(output, '\n')) { /* we've got at least one full line, start parsing*/ | 254 | if (strchr(output, '\n')) { /* we've got at least one full line, start parsing*/ |
| 212 | byte_offset = 0; | 255 | byte_offset = 0; |
| 213 | 256 | ||
| 214 | char *index = NULL; | 257 | char *index = NULL; |
| 215 | int len = 0; | 258 | unsigned long len = 0; |
| 216 | while ((index = strchr(output + byte_offset, '\n')) != NULL) { | 259 | while ((index = strchr(output + byte_offset, '\n')) != NULL) { |
| 217 | /*Partition the buffer so that this line is a separate string, | 260 | /*Partition the buffer so that this line is a separate string, |
| 218 | * by replacing the newline with NUL*/ | 261 | * by replacing the newline with NUL*/ |
| @@ -243,14 +286,23 @@ int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_proto | |||
| 243 | } | 286 | } |
| 244 | 287 | ||
| 245 | if (recv_ret < 0) { | 288 | if (recv_ret < 0) { |
| 246 | printf("SSH CRITICAL - %s", strerror(errno)); | 289 | connection_sc = mp_set_subcheck_state(connection_sc, STATE_CRITICAL); |
| 247 | exit(STATE_CRITICAL); | 290 | xasprintf(&connection_sc.output, "%s", "SSH CRITICAL - %s", strerror(errno)); |
| 291 | mp_add_subcheck_to_check(overall, connection_sc); | ||
| 292 | return OK; | ||
| 248 | } | 293 | } |
| 249 | 294 | ||
| 250 | if (version_control_string == NULL) { | 295 | if (version_control_string == NULL) { |
| 251 | printf("SSH CRITICAL - No version control string received"); | 296 | connection_sc = mp_set_subcheck_state(connection_sc, STATE_CRITICAL); |
| 252 | exit(STATE_CRITICAL); | 297 | xasprintf(&connection_sc.output, "%s", "SSH CRITICAL - No version control string received"); |
| 298 | mp_add_subcheck_to_check(overall, connection_sc); | ||
| 299 | return OK; | ||
| 253 | } | 300 | } |
| 301 | |||
| 302 | connection_sc = mp_set_subcheck_state(connection_sc, STATE_OK); | ||
| 303 | xasprintf(&connection_sc.output, "%s", "Initial connection succeeded"); | ||
| 304 | mp_add_subcheck_to_check(overall, connection_sc); | ||
| 305 | |||
| 254 | /* | 306 | /* |
| 255 | * "When the connection has been established, both sides MUST send an | 307 | * "When the connection has been established, both sides MUST send an |
| 256 | * identification string. This identification string MUST be | 308 | * identification string. This identification string MUST be |
| @@ -259,8 +311,9 @@ int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_proto | |||
| 259 | * - RFC 4253:4.2 | 311 | * - RFC 4253:4.2 |
| 260 | */ | 312 | */ |
| 261 | strip(version_control_string); | 313 | strip(version_control_string); |
| 262 | if (verbose) | 314 | if (verbose) { |
| 263 | printf("%s\n", version_control_string); | 315 | printf("%s\n", version_control_string); |
| 316 | } | ||
| 264 | 317 | ||
| 265 | char *ssh_proto = version_control_string + 4; | 318 | char *ssh_proto = version_control_string + 4; |
| 266 | 319 | ||
| @@ -288,41 +341,65 @@ int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_proto | |||
| 288 | if (tmp) { | 341 | if (tmp) { |
| 289 | ssh_server[tmp - ssh_server] = '\0'; | 342 | ssh_server[tmp - ssh_server] = '\0'; |
| 290 | } | 343 | } |
| 344 | |||
| 345 | mp_subcheck protocol_validity_sc = mp_subcheck_init(); | ||
| 291 | if (strlen(ssh_proto) == 0 || strlen(ssh_server) == 0) { | 346 | if (strlen(ssh_proto) == 0 || strlen(ssh_server) == 0) { |
| 292 | printf(_("SSH CRITICAL - Invalid protocol version control string %s\n"), version_control_string); | 347 | protocol_validity_sc = mp_set_subcheck_state(protocol_validity_sc, STATE_CRITICAL); |
| 293 | exit(STATE_CRITICAL); | 348 | xasprintf(&protocol_validity_sc.output, "Invalid protocol version control string %s", version_control_string); |
| 349 | mp_add_subcheck_to_check(overall, protocol_validity_sc); | ||
| 350 | return OK; | ||
| 294 | } | 351 | } |
| 352 | |||
| 353 | protocol_validity_sc = mp_set_subcheck_state(protocol_validity_sc, STATE_OK); | ||
| 354 | xasprintf(&protocol_validity_sc.output, "Valid protocol version control string %s", version_control_string); | ||
| 355 | mp_add_subcheck_to_check(overall, protocol_validity_sc); | ||
| 356 | |||
| 295 | ssh_proto[strspn(ssh_proto, "0123456789. ")] = 0; | 357 | ssh_proto[strspn(ssh_proto, "0123456789. ")] = 0; |
| 296 | 358 | ||
| 297 | static char *rev_no = VERSION; | 359 | static char *rev_no = VERSION; |
| 298 | xasprintf(&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, rev_no); | 360 | xasprintf(&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, rev_no); |
| 299 | send(socket, buffer, strlen(buffer), MSG_DONTWAIT); | 361 | send(socket, buffer, strlen(buffer), MSG_DONTWAIT); |
| 300 | if (verbose) | 362 | if (verbose) { |
| 301 | printf("%s\n", buffer); | 363 | printf("%s\n", buffer); |
| 364 | } | ||
| 302 | 365 | ||
| 303 | if (remote_version && strcmp(remote_version, ssh_server)) { | 366 | if (desired_remote_version && strcmp(desired_remote_version, ssh_server)) { |
| 304 | printf(_("SSH CRITICAL - %s (protocol %s) version mismatch, expected '%s'\n"), ssh_server, ssh_proto, remote_version); | 367 | mp_subcheck remote_version_sc = mp_subcheck_init(); |
| 368 | remote_version_sc = mp_set_subcheck_state(remote_version_sc, STATE_CRITICAL); | ||
| 369 | xasprintf(&remote_version_sc.output, _("%s (protocol %s) version mismatch, expected '%s'"), ssh_server, ssh_proto, | ||
| 370 | desired_remote_version); | ||
| 305 | close(socket); | 371 | close(socket); |
| 306 | exit(STATE_CRITICAL); | 372 | mp_add_subcheck_to_check(overall, remote_version_sc); |
| 373 | return OK; | ||
| 307 | } | 374 | } |
| 308 | 375 | ||
| 309 | double elapsed_time = (double)deltime(tv) / 1.0e6; | 376 | double elapsed_time = (double)deltime(tv) / 1.0e6; |
| 310 | if (remote_protocol && strcmp(remote_protocol, ssh_proto)) { | 377 | mp_perfdata time_pd = perfdata_init(); |
| 311 | printf(_("SSH CRITICAL - %s (protocol %s) protocol version mismatch, expected '%s' | %s\n"), ssh_server, ssh_proto, remote_protocol, | 378 | time_pd.value = mp_create_pd_value(elapsed_time); |
| 312 | fperfdata("time", elapsed_time, "s", false, 0, false, 0, true, 0, true, (int)socket_timeout)); | 379 | time_pd.label = "time"; |
| 313 | close(socket); | 380 | time_pd.max_present = true; |
| 314 | exit(STATE_CRITICAL); | 381 | time_pd.max = mp_create_pd_value(socket_timeout); |
| 382 | |||
| 383 | mp_subcheck protocol_version_sc = mp_subcheck_init(); | ||
| 384 | mp_add_perfdata_to_subcheck(&protocol_version_sc, time_pd); | ||
| 385 | |||
| 386 | if (desired_remote_protocol && strcmp(desired_remote_protocol, ssh_proto)) { | ||
| 387 | protocol_version_sc = mp_set_subcheck_state(protocol_version_sc, STATE_CRITICAL); | ||
| 388 | xasprintf(&protocol_version_sc.output, _("%s (protocol %s) protocol version mismatch, expected '%s'"), ssh_server, ssh_proto, | ||
| 389 | desired_remote_protocol); | ||
| 390 | } else { | ||
| 391 | protocol_version_sc = mp_set_subcheck_state(protocol_version_sc, STATE_OK); | ||
| 392 | xasprintf(&protocol_version_sc.output, "SSH server version: %s (protocol version: %s)", ssh_server, ssh_proto); | ||
| 315 | } | 393 | } |
| 316 | 394 | ||
| 317 | printf(_("SSH OK - %s (protocol %s) | %s\n"), ssh_server, ssh_proto, | 395 | mp_add_subcheck_to_check(overall, protocol_version_sc); |
| 318 | fperfdata("time", elapsed_time, "s", false, 0, false, 0, true, 0, true, (int)socket_timeout)); | ||
| 319 | close(socket); | 396 | close(socket); |
| 320 | exit(STATE_OK); | 397 | return OK; |
| 321 | } | 398 | } |
| 322 | 399 | ||
| 323 | void print_help(void) { | 400 | void print_help(void) { |
| 324 | char *myport; | 401 | char *myport; |
| 325 | xasprintf(&myport, "%d", SSH_DFL_PORT); | 402 | xasprintf(&myport, "%d", default_ssh_port); |
| 326 | 403 | ||
| 327 | print_revision(progname, NP_VERSION); | 404 | print_revision(progname, NP_VERSION); |
| 328 | 405 | ||
| @@ -349,6 +426,7 @@ void print_help(void) { | |||
| 349 | 426 | ||
| 350 | printf(" %s\n", "-P, --remote-protocol=STRING"); | 427 | printf(" %s\n", "-P, --remote-protocol=STRING"); |
| 351 | printf(" %s\n", _("Alert if protocol doesn't match expected protocol version (ex: 2.0)")); | 428 | printf(" %s\n", _("Alert if protocol doesn't match expected protocol version (ex: 2.0)")); |
| 429 | printf(UT_OUTPUT_FORMAT); | ||
| 352 | 430 | ||
| 353 | printf(UT_VERBOSE); | 431 | printf(UT_VERBOSE); |
| 354 | 432 | ||
| @@ -357,5 +435,5 @@ void print_help(void) { | |||
| 357 | 435 | ||
| 358 | void print_usage(void) { | 436 | void print_usage(void) { |
| 359 | printf("%s\n", _("Usage:")); | 437 | printf("%s\n", _("Usage:")); |
| 360 | printf("%s [-4|-6] [-t <timeout>] [-r <remote version>] [-p <port>] <host>\n", progname); | 438 | printf("%s [-4|-6] [-t <timeout>] [-r <remote version>] [-p <port>] --hostname <host>\n", progname); |
| 361 | } | 439 | } |
diff --git a/plugins/check_ssh.d/config.h b/plugins/check_ssh.d/config.h new file mode 100644 index 00000000..c150fd30 --- /dev/null +++ b/plugins/check_ssh.d/config.h | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <stddef.h> | ||
| 4 | #include "../../lib/monitoringplug.h" | ||
| 5 | |||
| 6 | const int default_ssh_port = 22; | ||
| 7 | |||
| 8 | typedef struct check_ssh_config { | ||
| 9 | int port; | ||
| 10 | char *server_name; | ||
| 11 | char *remote_version; | ||
| 12 | char *remote_protocol; | ||
| 13 | |||
| 14 | bool output_format_is_set; | ||
| 15 | mp_output_format output_format; | ||
| 16 | } check_ssh_config; | ||
| 17 | |||
| 18 | check_ssh_config check_ssh_config_init(void) { | ||
| 19 | check_ssh_config tmp = { | ||
| 20 | .port = default_ssh_port, | ||
| 21 | .server_name = NULL, | ||
| 22 | .remote_version = NULL, | ||
| 23 | .remote_protocol = NULL, | ||
| 24 | |||
| 25 | .output_format_is_set = false, | ||
| 26 | }; | ||
| 27 | |||
| 28 | return tmp; | ||
| 29 | } | ||
diff --git a/plugins/check_swap.c b/plugins/check_swap.c index 4d3b6099..cb95949a 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c | |||
| @@ -93,7 +93,7 @@ int main(int argc, char **argv) { | |||
| 93 | double percent_used; | 93 | double percent_used; |
| 94 | mp_check overall = mp_check_init(); | 94 | mp_check overall = mp_check_init(); |
| 95 | if (config.output_format_is_set) { | 95 | if (config.output_format_is_set) { |
| 96 | overall.format = config.output_format; | 96 | mp_set_format(config.output_format); |
| 97 | } | 97 | } |
| 98 | mp_subcheck sc1 = mp_subcheck_init(); | 98 | mp_subcheck sc1 = mp_subcheck_init(); |
| 99 | sc1 = mp_set_subcheck_default_state(sc1, STATE_OK); | 99 | sc1 = mp_set_subcheck_default_state(sc1, STATE_OK); |
diff --git a/plugins/netutils.c b/plugins/netutils.c index ee81912a..e2916c65 100644 --- a/plugins/netutils.c +++ b/plugins/netutils.c | |||
| @@ -28,6 +28,8 @@ | |||
| 28 | *****************************************************************************/ | 28 | *****************************************************************************/ |
| 29 | 29 | ||
| 30 | #include "common.h" | 30 | #include "common.h" |
| 31 | #include "output.h" | ||
| 32 | #include "states.h" | ||
| 31 | #include "netutils.h" | 33 | #include "netutils.h" |
| 32 | 34 | ||
| 33 | unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT; | 35 | unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT; |
| @@ -43,12 +45,19 @@ int address_family = AF_INET; | |||
| 43 | 45 | ||
| 44 | /* handles socket timeouts */ | 46 | /* handles socket timeouts */ |
| 45 | void socket_timeout_alarm_handler(int sig) { | 47 | void socket_timeout_alarm_handler(int sig) { |
| 46 | if (sig == SIGALRM) | 48 | mp_subcheck timeout_sc = mp_subcheck_init(); |
| 47 | printf(_("%s - Socket timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout); | 49 | timeout_sc = mp_set_subcheck_state(timeout_sc, socket_timeout_state); |
| 48 | else | ||
| 49 | printf(_("%s - Abnormal timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout); | ||
| 50 | 50 | ||
| 51 | exit(socket_timeout_state); | 51 | if (sig == SIGALRM) { |
| 52 | xasprintf(&timeout_sc.output, _("Socket timeout after %d seconds\n"), socket_timeout); | ||
| 53 | } else { | ||
| 54 | xasprintf(&timeout_sc.output, _("Abnormal timeout after %d seconds\n"), socket_timeout); | ||
| 55 | } | ||
| 56 | |||
| 57 | mp_check overall = mp_check_init(); | ||
| 58 | mp_add_subcheck_to_check(&overall, timeout_sc); | ||
| 59 | |||
| 60 | mp_exit(overall); | ||
| 52 | } | 61 | } |
| 53 | 62 | ||
| 54 | /* connects to a host on a specified tcp port, sends a string, and gets a | 63 | /* connects to a host on a specified tcp port, sends a string, and gets a |
| @@ -65,12 +74,13 @@ int process_tcp_request2(const char *server_address, int server_port, const char | |||
| 65 | int recv_length = 0; | 74 | int recv_length = 0; |
| 66 | 75 | ||
| 67 | result = np_net_connect(server_address, server_port, &sd, IPPROTO_TCP); | 76 | result = np_net_connect(server_address, server_port, &sd, IPPROTO_TCP); |
| 68 | if (result != STATE_OK) | 77 | if (result != STATE_OK) { |
| 69 | return STATE_CRITICAL; | 78 | return STATE_CRITICAL; |
| 79 | } | ||
| 70 | 80 | ||
| 71 | send_result = send(sd, send_buffer, strlen(send_buffer), 0); | 81 | send_result = send(sd, send_buffer, strlen(send_buffer), 0); |
| 72 | if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { | 82 | if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { |
| 73 | printf("%s\n", _("Send failed")); | 83 | // printf("%s\n", _("Send failed")); |
| 74 | result = STATE_WARNING; | 84 | result = STATE_WARNING; |
| 75 | } | 85 | } |
| 76 | 86 | ||
| @@ -87,7 +97,7 @@ int process_tcp_request2(const char *server_address, int server_port, const char | |||
| 87 | if (!FD_ISSET(sd, &readfds)) { /* it hasn't */ | 97 | if (!FD_ISSET(sd, &readfds)) { /* it hasn't */ |
| 88 | if (!recv_length) { | 98 | if (!recv_length) { |
| 89 | strcpy(recv_buffer, ""); | 99 | strcpy(recv_buffer, ""); |
| 90 | printf("%s\n", _("No data was received from host!")); | 100 | // printf("%s\n", _("No data was received from host!")); |
| 91 | result = STATE_WARNING; | 101 | result = STATE_WARNING; |
| 92 | } else { /* this one failed, but previous ones worked */ | 102 | } else { /* this one failed, but previous ones worked */ |
| 93 | recv_buffer[recv_length] = 0; | 103 | recv_buffer[recv_length] = 0; |
| @@ -130,8 +140,9 @@ int process_request(const char *server_address, int server_port, int proto, cons | |||
| 130 | result = STATE_OK; | 140 | result = STATE_OK; |
| 131 | 141 | ||
| 132 | result = np_net_connect(server_address, server_port, &sd, proto); | 142 | result = np_net_connect(server_address, server_port, &sd, proto); |
| 133 | if (result != STATE_OK) | 143 | if (result != STATE_OK) { |
| 134 | return STATE_CRITICAL; | 144 | return STATE_CRITICAL; |
| 145 | } | ||
| 135 | 146 | ||
| 136 | result = send_request(sd, proto, send_buffer, recv_buffer, recv_size); | 147 | result = send_request(sd, proto, send_buffer, recv_buffer, recv_size); |
| 137 | 148 | ||
| @@ -169,15 +180,16 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { | |||
| 169 | host_name++; | 180 | host_name++; |
| 170 | len -= 2; | 181 | len -= 2; |
| 171 | } | 182 | } |
| 172 | if (len >= sizeof(host)) | 183 | if (len >= sizeof(host)) { |
| 173 | return STATE_UNKNOWN; | 184 | return STATE_UNKNOWN; |
| 185 | } | ||
| 174 | memcpy(host, host_name, len); | 186 | memcpy(host, host_name, len); |
| 175 | host[len] = '\0'; | 187 | host[len] = '\0'; |
| 176 | snprintf(port_str, sizeof(port_str), "%d", port); | 188 | snprintf(port_str, sizeof(port_str), "%d", port); |
| 177 | result = getaddrinfo(host, port_str, &hints, &res); | 189 | result = getaddrinfo(host, port_str, &hints, &res); |
| 178 | 190 | ||
| 179 | if (result != 0) { | 191 | if (result != 0) { |
| 180 | printf("%s\n", gai_strerror(result)); | 192 | // printf("%s\n", gai_strerror(result)); |
| 181 | return STATE_UNKNOWN; | 193 | return STATE_UNKNOWN; |
| 182 | } | 194 | } |
| 183 | 195 | ||
| @@ -187,7 +199,7 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { | |||
| 187 | *sd = socket(r->ai_family, socktype, r->ai_protocol); | 199 | *sd = socket(r->ai_family, socktype, r->ai_protocol); |
| 188 | 200 | ||
| 189 | if (*sd < 0) { | 201 | if (*sd < 0) { |
| 190 | printf("%s\n", _("Socket creation failed")); | 202 | // printf("%s\n", _("Socket creation failed")); |
| 191 | freeaddrinfo(r); | 203 | freeaddrinfo(r); |
| 192 | return STATE_UNKNOWN; | 204 | return STATE_UNKNOWN; |
| 193 | } | 205 | } |
| @@ -226,21 +238,23 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { | |||
| 226 | die(STATE_UNKNOWN, _("Socket creation failed")); | 238 | die(STATE_UNKNOWN, _("Socket creation failed")); |
| 227 | } | 239 | } |
| 228 | result = connect(*sd, (struct sockaddr *)&su, sizeof(su)); | 240 | result = connect(*sd, (struct sockaddr *)&su, sizeof(su)); |
| 229 | if (result < 0 && errno == ECONNREFUSED) | 241 | if (result < 0 && errno == ECONNREFUSED) { |
| 230 | was_refused = true; | 242 | was_refused = true; |
| 243 | } | ||
| 231 | } | 244 | } |
| 232 | 245 | ||
| 233 | if (result == 0) | 246 | if (result == 0) { |
| 234 | return STATE_OK; | 247 | return STATE_OK; |
| 235 | else if (was_refused) { | 248 | } else if (was_refused) { |
| 236 | switch (econn_refuse_state) { /* a user-defined expected outcome */ | 249 | switch (econn_refuse_state) { /* a user-defined expected outcome */ |
| 237 | case STATE_OK: | 250 | case STATE_OK: |
| 238 | case STATE_WARNING: /* user wants WARN or OK on refusal, or... */ | 251 | case STATE_WARNING: /* user wants WARN or OK on refusal, or... */ |
| 239 | case STATE_CRITICAL: /* user did not set econn_refuse_state, or wanted critical */ | 252 | case STATE_CRITICAL: /* user did not set econn_refuse_state, or wanted critical */ |
| 240 | if (is_socket) | 253 | if (is_socket) { |
| 241 | printf("connect to file socket %s: %s\n", host_name, strerror(errno)); | 254 | // printf("connect to file socket %s: %s\n", host_name, strerror(errno)); |
| 242 | else | 255 | } else { |
| 243 | printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno)); | 256 | // printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno)); |
| 257 | } | ||
| 244 | return STATE_CRITICAL; | 258 | return STATE_CRITICAL; |
| 245 | break; | 259 | break; |
| 246 | default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */ | 260 | default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */ |
| @@ -248,10 +262,11 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { | |||
| 248 | break; | 262 | break; |
| 249 | } | 263 | } |
| 250 | } else { | 264 | } else { |
| 251 | if (is_socket) | 265 | if (is_socket) { |
| 252 | printf("connect to file socket %s: %s\n", host_name, strerror(errno)); | 266 | // printf("connect to file socket %s: %s\n", host_name, strerror(errno)); |
| 253 | else | 267 | } else { |
| 254 | printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno)); | 268 | // printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno)); |
| 269 | } | ||
| 255 | return STATE_CRITICAL; | 270 | return STATE_CRITICAL; |
| 256 | } | 271 | } |
| 257 | } | 272 | } |
| @@ -265,7 +280,7 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, | |||
| 265 | 280 | ||
| 266 | send_result = send(sd, send_buffer, strlen(send_buffer), 0); | 281 | send_result = send(sd, send_buffer, strlen(send_buffer), 0); |
| 267 | if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { | 282 | if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { |
| 268 | printf("%s\n", _("Send failed")); | 283 | // printf("%s\n", _("Send failed")); |
| 269 | result = STATE_WARNING; | 284 | result = STATE_WARNING; |
| 270 | } | 285 | } |
| 271 | 286 | ||
| @@ -280,7 +295,7 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, | |||
| 280 | /* make sure some data has arrived */ | 295 | /* make sure some data has arrived */ |
| 281 | if (!FD_ISSET(sd, &readfds)) { | 296 | if (!FD_ISSET(sd, &readfds)) { |
| 282 | strcpy(recv_buffer, ""); | 297 | strcpy(recv_buffer, ""); |
| 283 | printf("%s\n", _("No data was received from host!")); | 298 | // printf("%s\n", _("No data was received from host!")); |
| 284 | result = STATE_WARNING; | 299 | result = STATE_WARNING; |
| 285 | } | 300 | } |
| 286 | 301 | ||
| @@ -288,11 +303,13 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, | |||
| 288 | recv_result = recv(sd, recv_buffer, (size_t)recv_size - 1, 0); | 303 | recv_result = recv(sd, recv_buffer, (size_t)recv_size - 1, 0); |
| 289 | if (recv_result == -1) { | 304 | if (recv_result == -1) { |
| 290 | strcpy(recv_buffer, ""); | 305 | strcpy(recv_buffer, ""); |
| 291 | if (proto != IPPROTO_TCP) | 306 | if (proto != IPPROTO_TCP) { |
| 292 | printf("%s\n", _("Receive failed")); | 307 | // printf("%s\n", _("Receive failed")); |
| 308 | } | ||
| 293 | result = STATE_WARNING; | 309 | result = STATE_WARNING; |
| 294 | } else | 310 | } else { |
| 295 | recv_buffer[recv_result] = 0; | 311 | recv_buffer[recv_result] = 0; |
| 312 | } | ||
| 296 | 313 | ||
| 297 | /* die returned string */ | 314 | /* die returned string */ |
| 298 | recv_buffer[recv_size - 1] = 0; | 315 | recv_buffer[recv_size - 1] = 0; |
| @@ -301,26 +318,30 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, | |||
| 301 | } | 318 | } |
| 302 | 319 | ||
| 303 | bool is_host(const char *address) { | 320 | bool is_host(const char *address) { |
| 304 | if (is_addr(address) || is_hostname(address)) | 321 | if (is_addr(address) || is_hostname(address)) { |
| 305 | return (true); | 322 | return (true); |
| 323 | } | ||
| 306 | 324 | ||
| 307 | return (false); | 325 | return (false); |
| 308 | } | 326 | } |
| 309 | 327 | ||
| 310 | void host_or_die(const char *str) { | 328 | void host_or_die(const char *str) { |
| 311 | if (!str || (!is_addr(str) && !is_hostname(str))) | 329 | if (!str || (!is_addr(str) && !is_hostname(str))) { |
| 312 | usage_va(_("Invalid hostname/address - %s"), str); | 330 | usage_va(_("Invalid hostname/address - %s"), str); |
| 331 | } | ||
| 313 | } | 332 | } |
| 314 | 333 | ||
| 315 | bool is_addr(const char *address) { | 334 | bool is_addr(const char *address) { |
| 316 | #ifdef USE_IPV6 | 335 | #ifdef USE_IPV6 |
| 317 | if (address_family == AF_INET && is_inet_addr(address)) | 336 | if (address_family == AF_INET && is_inet_addr(address)) { |
| 318 | return true; | 337 | return true; |
| 319 | else if (address_family == AF_INET6 && is_inet6_addr(address)) | 338 | } else if (address_family == AF_INET6 && is_inet6_addr(address)) { |
| 320 | return true; | 339 | return true; |
| 340 | } | ||
| 321 | #else | 341 | #else |
| 322 | if (is_inet_addr(address)) | 342 | if (is_inet_addr(address)) { |
| 323 | return (true); | 343 | return (true); |
| 344 | } | ||
| 324 | #endif | 345 | #endif |
| 325 | 346 | ||
| 326 | return (false); | 347 | return (false); |
| @@ -335,11 +356,13 @@ int dns_lookup(const char *in, struct sockaddr_storage *ss, int family) { | |||
| 335 | hints.ai_family = family; | 356 | hints.ai_family = family; |
| 336 | 357 | ||
| 337 | retval = getaddrinfo(in, NULL, &hints, &res); | 358 | retval = getaddrinfo(in, NULL, &hints, &res); |
| 338 | if (retval != 0) | 359 | if (retval != 0) { |
| 339 | return false; | 360 | return false; |
| 361 | } | ||
| 340 | 362 | ||
| 341 | if (ss != NULL) | 363 | if (ss != NULL) { |
| 342 | memcpy(ss, res->ai_addr, res->ai_addrlen); | 364 | memcpy(ss, res->ai_addr, res->ai_addrlen); |
| 365 | } | ||
| 343 | freeaddrinfo(res); | 366 | freeaddrinfo(res); |
| 344 | return true; | 367 | return true; |
| 345 | } | 368 | } |
diff --git a/plugins/t/check_http.t b/plugins/t/check_http.t index 6ab4a5b6..bb1fd27d 100644 --- a/plugins/t/check_http.t +++ b/plugins/t/check_http.t | |||
| @@ -45,7 +45,7 @@ $res = NPTest->testCmd( | |||
| 45 | "./$plugin $host_nonresponsive -wt 1 -ct 2 -t 3" | 45 | "./$plugin $host_nonresponsive -wt 1 -ct 2 -t 3" |
| 46 | ); | 46 | ); |
| 47 | cmp_ok( $res->return_code, '==', 2, "Webserver $host_nonresponsive not responding" ); | 47 | cmp_ok( $res->return_code, '==', 2, "Webserver $host_nonresponsive not responding" ); |
| 48 | cmp_ok( $res->output, 'eq', "CRITICAL - Socket timeout after 3 seconds", "Output OK"); | 48 | like( $res->output, "/Socket timeout after/", "Output OK"); |
| 49 | 49 | ||
| 50 | $res = NPTest->testCmd( | 50 | $res = NPTest->testCmd( |
| 51 | "./$plugin $hostname_invalid -wt 1 -ct 2" | 51 | "./$plugin $hostname_invalid -wt 1 -ct 2" |
diff --git a/plugins/t/check_jabber.t b/plugins/t/check_jabber.t index fcdae179..08cadcbd 100644 --- a/plugins/t/check_jabber.t +++ b/plugins/t/check_jabber.t | |||
| @@ -17,7 +17,7 @@ my $hostname_invalid = getTestParameter("NP_HOSTNAME_INVALID", "An invalid (no | |||
| 17 | 17 | ||
| 18 | my $jabberOK = '/JABBER OK\s-\s\d+\.\d+\ssecond response time on '.$host_tcp_jabber.' port 5222/'; | 18 | my $jabberOK = '/JABBER OK\s-\s\d+\.\d+\ssecond response time on '.$host_tcp_jabber.' port 5222/'; |
| 19 | 19 | ||
| 20 | my $jabberUnresponsive = '/CRITICAL\s-\sSocket timeout after\s\d+\sseconds/'; | 20 | my $jabberUnresponsive = '/Socket timeout after\s\d+\sseconds/'; |
| 21 | 21 | ||
| 22 | my $jabberInvalid = '/JABBER CRITICAL - Invalid hostname, address or socket:\s.+/'; | 22 | my $jabberInvalid = '/JABBER CRITICAL - Invalid hostname, address or socket:\s.+/'; |
| 23 | 23 | ||
diff --git a/plugins/t/check_ldap.t b/plugins/t/check_ldap.t index b8a4a766..fcba0393 100644 --- a/plugins/t/check_ldap.t +++ b/plugins/t/check_ldap.t | |||
| @@ -24,7 +24,7 @@ SKIP: { | |||
| 24 | 24 | ||
| 25 | $result = NPTest->testCmd("$command -H $host_nonresponsive -b ou=blah -t 2 -w 1 -c 1"); | 25 | $result = NPTest->testCmd("$command -H $host_nonresponsive -b ou=blah -t 2 -w 1 -c 1"); |
| 26 | is( $result->return_code, 2, "$command -H $host_nonresponsive -b ou=blah -t 5 -w 2 -c 3" ); | 26 | is( $result->return_code, 2, "$command -H $host_nonresponsive -b ou=blah -t 5 -w 2 -c 3" ); |
| 27 | is( $result->output, 'CRITICAL - Socket timeout after 2 seconds', "output ok" ); | 27 | like($result->output, '/Socket timeout after \d+ seconds/', "output ok" ); |
| 28 | }; | 28 | }; |
| 29 | 29 | ||
| 30 | SKIP: { | 30 | SKIP: { |
diff --git a/plugins/t/check_mysql.t b/plugins/t/check_mysql.t index baf3acc6..a383bc99 100644 --- a/plugins/t/check_mysql.t +++ b/plugins/t/check_mysql.t | |||
| @@ -21,11 +21,11 @@ plan skip_all => "check_mysql not compiled" unless (-x "check_mysql"); | |||
| 21 | plan tests => 15; | 21 | plan tests => 15; |
| 22 | 22 | ||
| 23 | my $bad_login_output = '/Access denied for user /'; | 23 | my $bad_login_output = '/Access denied for user /'; |
| 24 | my $mysqlserver = getTestParameter("NP_MYSQL_SERVER", "A MySQL Server hostname or IP with no slaves setup"); | 24 | my $mysqlserver = getTestParameter("NP_MYSQL_SERVER", "A MySQL Server hostname or IP with no replica setup"); |
| 25 | my $mysqlsocket = getTestParameter("NP_MYSQL_SOCKET", "Full path to a MySQL Server socket with no slaves setup"); | 25 | my $mysqlsocket = getTestParameter("NP_MYSQL_SOCKET", "Full path to a MySQL Server socket with no replica setup"); |
| 26 | my $mysql_login_details = getTestParameter("NP_MYSQL_LOGIN_DETAILS", "Command line parameters to specify login access (requires REPLICATION CLIENT privileges)", "-u test -ptest"); | 26 | my $mysql_login_details = getTestParameter("NP_MYSQL_LOGIN_DETAILS", "Command line parameters to specify login access (requires REPLICATION CLIENT privileges)", "-u test -ptest"); |
| 27 | my $with_slave = getTestParameter("NP_MYSQL_WITH_SLAVE", "MySQL server with slaves setup"); | 27 | my $with_replica = getTestParameter("NP_MYSQL_WITH_REPLICA", "MySQL server with replica setup"); |
| 28 | my $with_slave_login = getTestParameter("NP_MYSQL_WITH_SLAVE_LOGIN", "Login details for server with slave (requires REPLICATION CLIENT privileges)", $mysql_login_details || "-u test -ptest"); | 28 | my $with_replica_login = getTestParameter("NP_MYSQL_WITH_REPLICA_LOGIN", "Login details for server with replica (requires REPLICATION CLIENT privileges)", $mysql_login_details || "-u test -ptest"); |
| 29 | 29 | ||
| 30 | my $result; | 30 | my $result; |
| 31 | 31 | ||
| @@ -39,8 +39,8 @@ SKIP: { | |||
| 39 | like( $result->output, $bad_login_output, "Expected login failure message"); | 39 | like( $result->output, $bad_login_output, "Expected login failure message"); |
| 40 | 40 | ||
| 41 | $result = NPTest->testCmd("./check_mysql -S -H $mysqlserver $mysql_login_details"); | 41 | $result = NPTest->testCmd("./check_mysql -S -H $mysqlserver $mysql_login_details"); |
| 42 | cmp_ok( $result->return_code, "==", 1, "No slaves defined" ); | 42 | cmp_ok( $result->return_code, "==", 1, "No replicas defined" ); |
| 43 | like( $result->output, "/No slaves defined/", "Correct error message"); | 43 | like( $result->output, "/No replicas defined/", "Correct error message"); |
| 44 | } | 44 | } |
| 45 | 45 | ||
| 46 | SKIP: { | 46 | SKIP: { |
| @@ -53,22 +53,22 @@ SKIP: { | |||
| 53 | like( $result->output, $bad_login_output, "Expected login failure message"); | 53 | like( $result->output, $bad_login_output, "Expected login failure message"); |
| 54 | 54 | ||
| 55 | $result = NPTest->testCmd("./check_mysql -S -s $mysqlsocket $mysql_login_details"); | 55 | $result = NPTest->testCmd("./check_mysql -S -s $mysqlsocket $mysql_login_details"); |
| 56 | cmp_ok( $result->return_code, "==", 1, "No slaves defined" ); | 56 | cmp_ok( $result->return_code, "==", 1, "No replicas defined" ); |
| 57 | like( $result->output, "/No slaves defined/", "Correct error message"); | 57 | like( $result->output, "/No replicas defined/", "Correct error message"); |
| 58 | } | 58 | } |
| 59 | 59 | ||
| 60 | SKIP: { | 60 | SKIP: { |
| 61 | skip "No mysql server with slaves defined", 5 unless $with_slave; | 61 | skip "No mysql server with replicas defined", 5 unless $with_replica; |
| 62 | $result = NPTest->testCmd("./check_mysql -H $with_slave $with_slave_login"); | 62 | $result = NPTest->testCmd("./check_mysql -H $with_replica $with_replica_login"); |
| 63 | cmp_ok( $result->return_code, '==', 0, "Login okay"); | 63 | cmp_ok( $result->return_code, '==', 0, "Login okay"); |
| 64 | 64 | ||
| 65 | $result = NPTest->testCmd("./check_mysql -S -H $with_slave $with_slave_login"); | 65 | $result = NPTest->testCmd("./check_mysql -S -H $with_replica $with_replica_login"); |
| 66 | cmp_ok( $result->return_code, "==", 0, "Slaves okay" ); | 66 | cmp_ok( $result->return_code, "==", 0, "Replicas okay" ); |
| 67 | 67 | ||
| 68 | $result = NPTest->testCmd("./check_mysql -S -H $with_slave $with_slave_login -w 60"); | 68 | $result = NPTest->testCmd("./check_mysql -S -H $with_replica $with_replica_login -w 60"); |
| 69 | cmp_ok( $result->return_code, '==', 0, 'Slaves are not > 60 seconds behind'); | 69 | cmp_ok( $result->return_code, '==', 0, 'Replicas are not > 60 seconds behind'); |
| 70 | 70 | ||
| 71 | $result = NPTest->testCmd("./check_mysql -S -H $with_slave $with_slave_login -w 60:"); | 71 | $result = NPTest->testCmd("./check_mysql -S -H $with_replica $with_replica_login -w 60:"); |
| 72 | cmp_ok( $result->return_code, '==', 1, 'Alert warning if < 60 seconds behind'); | 72 | cmp_ok( $result->return_code, '==', 1, 'Alert warning if < 60 seconds behind'); |
| 73 | like( $result->output, "/^SLOW_SLAVE WARNING:/", "Output okay"); | 73 | like( $result->output, "/^SLOW_REPLICA WARNING:/", "Output okay"); |
| 74 | } | 74 | } |
diff --git a/plugins/t/check_ntp.t b/plugins/t/check_ntp.t index b8fc8fdf..a8ac7bb8 100644 --- a/plugins/t/check_ntp.t +++ b/plugins/t/check_ntp.t | |||
| @@ -37,7 +37,7 @@ my $ntp_critmatch1 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})? | |||
| 37 | my $ntp_okmatch2 = '/^NTP\sOK:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2},\struechimers=[0-9]+/'; | 37 | my $ntp_okmatch2 = '/^NTP\sOK:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2},\struechimers=[0-9]+/'; |
| 38 | my $ntp_warnmatch2 = '/^NTP\sWARNING:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2}\s\(WARNING\),\struechimers=[0-9]+/'; | 38 | my $ntp_warnmatch2 = '/^NTP\sWARNING:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2}\s\(WARNING\),\struechimers=[0-9]+/'; |
| 39 | my $ntp_critmatch2 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+\s\(CRITICAL\),\sstratum=[0-9]{1,2},\struechimers=[0-9]+/'; | 39 | my $ntp_critmatch2 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+\s\(CRITICAL\),\sstratum=[0-9]{1,2},\struechimers=[0-9]+/'; |
| 40 | my $ntp_noresponse = '/^(CRITICAL - Socket timeout after 3 seconds)|(NTP CRITICAL: No response from NTP server)$/'; | 40 | my $ntp_noresponse = '/(.*Socket timeout after \d+ seconds.*)|(.*No response from NTP server.*)/'; |
| 41 | my $ntp_nosuchhost = '/^check_ntp.*: Invalid hostname/address - ' . $hostname_invalid . '/'; | 41 | my $ntp_nosuchhost = '/^check_ntp.*: Invalid hostname/address - ' . $hostname_invalid . '/'; |
| 42 | 42 | ||
| 43 | 43 | ||
diff --git a/plugins/t/check_smtp.t b/plugins/t/check_smtp.t index 1a1ebe3e..73b4a1fd 100644 --- a/plugins/t/check_smtp.t +++ b/plugins/t/check_smtp.t | |||
| @@ -24,7 +24,7 @@ my $hostname_invalid = getTestParameter( "NP_HOSTNAME_INVALID", | |||
| 24 | "An invalid (not known to DNS) hostname", "nosuchhost" ); | 24 | "An invalid (not known to DNS) hostname", "nosuchhost" ); |
| 25 | my $res; | 25 | my $res; |
| 26 | 26 | ||
| 27 | plan tests => 16; | 27 | plan tests => 15; |
| 28 | 28 | ||
| 29 | SKIP: { | 29 | SKIP: { |
| 30 | skip "No SMTP server defined", 4 unless $host_tcp_smtp; | 30 | skip "No SMTP server defined", 4 unless $host_tcp_smtp; |
| @@ -73,7 +73,6 @@ SKIP: { | |||
| 73 | my $unused_port = 4465; | 73 | my $unused_port = 4465; |
| 74 | $res = NPTest->testCmd( "./check_smtp -H $host_tcp_smtp_tls -p $unused_port --ssl" ); | 74 | $res = NPTest->testCmd( "./check_smtp -H $host_tcp_smtp_tls -p $unused_port --ssl" ); |
| 75 | is ($res->return_code, 2, "Check rc of connecting to $host_tcp_smtp_tls with TLS on unused port $unused_port" ); | 75 | is ($res->return_code, 2, "Check rc of connecting to $host_tcp_smtp_tls with TLS on unused port $unused_port" ); |
| 76 | like ($res->output, qr/^connect to address $host_tcp_smtp_tls and port $unused_port: Connection refused/, "Check output of connecting to $host_tcp_smtp_tls with TLS on unused port $unused_port"); | ||
| 77 | } | 76 | } |
| 78 | 77 | ||
| 79 | $res = NPTest->testCmd( "./check_smtp $host_nonresponsive" ); | 78 | $res = NPTest->testCmd( "./check_smtp $host_nonresponsive" ); |
diff --git a/plugins/t/check_ssh.t b/plugins/t/check_ssh.t index 907d33a8..8a20782e 100644 --- a/plugins/t/check_ssh.t +++ b/plugins/t/check_ssh.t | |||
| @@ -5,10 +5,10 @@ | |||
| 5 | # | 5 | # |
| 6 | 6 | ||
| 7 | use strict; | 7 | use strict; |
| 8 | use warnings; | ||
| 8 | use Test::More; | 9 | use Test::More; |
| 9 | use NPTest; | 10 | use NPTest; |
| 10 | 11 | use JSON; | |
| 11 | my $res; | ||
| 12 | 12 | ||
| 13 | # Required parameters | 13 | # Required parameters |
| 14 | my $ssh_host = getTestParameter("NP_SSH_HOST", | 14 | my $ssh_host = getTestParameter("NP_SSH_HOST", |
| @@ -23,30 +23,38 @@ my $hostname_invalid = getTestParameter("NP_HOSTNAME_INVALID", | |||
| 23 | "An invalid (not known to DNS) hostname", | 23 | "An invalid (not known to DNS) hostname", |
| 24 | "nosuchhost" ); | 24 | "nosuchhost" ); |
| 25 | 25 | ||
| 26 | my $outputFormat = '--output-format mp-test-json'; | ||
| 27 | |||
| 28 | plan tests => 24; | ||
| 26 | 29 | ||
| 27 | plan tests => 14 + 6; | 30 | my $output; |
| 31 | my $result; | ||
| 28 | 32 | ||
| 29 | SKIP: { | 33 | SKIP: { |
| 30 | skip "SSH_HOST must be defined", 6 unless $ssh_host; | 34 | skip "SSH_HOST must be defined", 6 unless $ssh_host; |
| 35 | |||
| 36 | |||
| 31 | my $result = NPTest->testCmd( | 37 | my $result = NPTest->testCmd( |
| 32 | "./check_ssh -H $ssh_host" | 38 | "./check_ssh -H $ssh_host" ." ". $outputFormat |
| 33 | ); | 39 | ); |
| 34 | cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); | 40 | cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); |
| 35 | like($result->output, '/^SSH OK - /', "Status text if command returned none (OK)"); | 41 | $output = decode_json($result->output); |
| 42 | is($output->{'state'}, "OK", "State was correct"); | ||
| 36 | 43 | ||
| 37 | 44 | ||
| 38 | $result = NPTest->testCmd( | 45 | $result = NPTest->testCmd( |
| 39 | "./check_ssh -H $host_nonresponsive -t 2" | 46 | "./check_ssh -H $host_nonresponsive -t 2" ." ". $outputFormat |
| 40 | ); | 47 | ); |
| 41 | cmp_ok($result->return_code, '==', 2, "Exit with return code 0 (OK)"); | 48 | cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); |
| 42 | like($result->output, '/^CRITICAL - Socket timeout after 2 seconds/', "Status text if command returned none (OK)"); | 49 | $output = decode_json($result->output); |
| 50 | is($output->{'state'}, "CRITICAL", "State was correct"); | ||
| 43 | 51 | ||
| 44 | 52 | ||
| 45 | 53 | ||
| 46 | $result = NPTest->testCmd( | 54 | $result = NPTest->testCmd( |
| 47 | "./check_ssh -H $hostname_invalid -t 2" | 55 | "./check_ssh -H $hostname_invalid -t 2" ." ". $outputFormat |
| 48 | ); | 56 | ); |
| 49 | cmp_ok($result->return_code, '==', 3, "Exit with return code 0 (OK)"); | 57 | cmp_ok($result->return_code, '==', 3, "Exit with return code 3 (UNKNOWN)"); |
| 50 | like($result->output, '/^check_ssh: Invalid hostname/', "Status text if command returned none (OK)"); | 58 | like($result->output, '/^check_ssh: Invalid hostname/', "Status text if command returned none (OK)"); |
| 51 | 59 | ||
| 52 | 60 | ||
| @@ -63,46 +71,80 @@ SKIP: { | |||
| 63 | # | 71 | # |
| 64 | # where `comments` is optional, protoversion is the SSH protocol version and | 72 | # where `comments` is optional, protoversion is the SSH protocol version and |
| 65 | # softwareversion is an arbitrary string representing the server software version | 73 | # softwareversion is an arbitrary string representing the server software version |
| 74 | |||
| 75 | my $found_version = 0; | ||
| 76 | |||
| 66 | open(NC, "echo 'SSH-2.0-nagiosplug.ssh.0.1' | nc ${nc_flags}|"); | 77 | open(NC, "echo 'SSH-2.0-nagiosplug.ssh.0.1' | nc ${nc_flags}|"); |
| 67 | sleep 0.1; | 78 | sleep 0.1; |
| 68 | $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ); | 79 | $result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat); |
| 69 | cmp_ok( $res->return_code, '==', 0, "Got SSH protocol version control string"); | 80 | cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); |
| 70 | like( $res->output, '/^SSH OK - nagiosplug.ssh.0.1 \(protocol 2.0\)/', "Output OK"); | 81 | $output = decode_json($result->output); |
| 82 | is($output->{'state'}, "OK", "State was correct"); | ||
| 83 | |||
| 84 | # looking for the version | ||
| 85 | for my $subcheck (@{$output->{'checks'}}) { | ||
| 86 | if ($subcheck->{'output'} =~ /.*nagiosplug.ssh.0.1 \(protocol version: 2.0\).*/ ){ | ||
| 87 | $found_version = 1; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | cmp_ok($found_version, '==', 1, "Output OK"); | ||
| 71 | close NC; | 91 | close NC; |
| 72 | 92 | ||
| 73 | open(NC, "echo 'SSH-2.0-3.2.9.1' | nc ${nc_flags}|"); | 93 | open(NC, "echo 'SSH-2.0-3.2.9.1' | nc ${nc_flags}|"); |
| 74 | sleep 0.1; | 94 | sleep 0.1; |
| 75 | $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ); | 95 | $result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat); |
| 76 | cmp_ok( $res->return_code, "==", 0, "Got SSH protocol version control string with non-alpha softwareversion string"); | 96 | cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); |
| 77 | like( $res->output, '/^SSH OK - 3.2.9.1 \(protocol 2.0\)/', "Output OK for non-alpha softwareversion string"); | 97 | $output = decode_json($result->output); |
| 98 | is($output->{'state'}, "OK", "State was correct"); | ||
| 99 | |||
| 100 | $found_version = 0; | ||
| 101 | for my $subcheck (@{$output->{'checks'}}) { | ||
| 102 | if ($subcheck->{'output'} =~ /3.2.9.1 \(protocol version: 2.0\)/ ){ | ||
| 103 | $found_version = 1; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | cmp_ok($found_version, '==', 1, "Output OK"); | ||
| 78 | close NC; | 107 | close NC; |
| 79 | 108 | ||
| 80 | open(NC, "echo 'SSH-2.0-nagiosplug.ssh.0.1 this is a comment' | nc ${nc_flags} |"); | 109 | open(NC, "echo 'SSH-2.0-nagiosplug.ssh.0.1 this is a comment' | nc ${nc_flags} |"); |
| 81 | sleep 0.1; | 110 | sleep 0.1; |
| 82 | $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003 -r nagiosplug.ssh.0.1" ); | 111 | $result = NPTest->testCmd( "./check_ssh -H localhost -p 5003 -r nagiosplug.ssh.0.1" ." ". $outputFormat); |
| 83 | cmp_ok( $res->return_code, '==', 0, "Got SSH protocol version control string, and parsed comment appropriately"); | 112 | cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); |
| 84 | like( $res->output, '/^SSH OK - nagiosplug.ssh.0.1 \(protocol 2.0\)/', "Output OK"); | 113 | $output = decode_json($result->output); |
| 114 | is($output->{'state'}, "OK", "State was correct"); | ||
| 115 | |||
| 116 | # looking for the version | ||
| 117 | $found_version = 0; | ||
| 118 | for my $subcheck (@{$output->{'checks'}}) { | ||
| 119 | if ($subcheck->{'output'} =~ /nagiosplug.ssh.0.1 \(protocol version: 2.0\)/ ){ | ||
| 120 | $found_version = 1; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | cmp_ok($found_version, '==', 1, "Output OK"); | ||
| 85 | close NC; | 124 | close NC; |
| 86 | 125 | ||
| 87 | open(NC, "echo 'SSH-' | nc ${nc_flags}|"); | 126 | open(NC, "echo 'SSH-' | nc ${nc_flags}|"); |
| 88 | sleep 0.1; | 127 | sleep 0.1; |
| 89 | $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ); | 128 | $result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat); |
| 90 | cmp_ok( $res->return_code, '==', 2, "Got invalid SSH protocol version control string"); | 129 | cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); |
| 91 | like( $res->output, '/^SSH CRITICAL/', "Output OK"); | 130 | $output = decode_json($result->output); |
| 131 | is($output->{'state'}, "CRITICAL", "Got invalid SSH protocol version control string"); | ||
| 92 | close NC; | 132 | close NC; |
| 93 | 133 | ||
| 94 | open(NC, "echo '' | nc ${nc_flags}|"); | 134 | open(NC, "echo '' | nc ${nc_flags}|"); |
| 95 | sleep 0.1; | 135 | sleep 0.1; |
| 96 | $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ); | 136 | $result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat); |
| 97 | cmp_ok( $res->return_code, '==', 2, "No version control string received"); | 137 | cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); |
| 98 | like( $res->output, '/^SSH CRITICAL - No version control string received/', "Output OK"); | 138 | $output = decode_json($result->output); |
| 139 | is($output->{'state'}, "CRITICAL", "No version control string received"); | ||
| 99 | close NC; | 140 | close NC; |
| 100 | 141 | ||
| 101 | open(NC, "echo 'Not a version control string' | nc ${nc_flags}|"); | 142 | open(NC, "echo 'Not a version control string' | nc ${nc_flags}|"); |
| 102 | sleep 0.1; | 143 | sleep 0.1; |
| 103 | $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ); | 144 | $result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat); |
| 104 | cmp_ok( $res->return_code, '==', 2, "No version control string received"); | 145 | cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); |
| 105 | like( $res->output, '/^SSH CRITICAL - No version control string received/', "Output OK"); | 146 | $output = decode_json($result->output); |
| 147 | is($output->{'state'}, "CRITICAL", "No version control string received"); | ||
| 106 | close NC; | 148 | close NC; |
| 107 | 149 | ||
| 108 | 150 | ||
| @@ -116,8 +158,18 @@ SKIP: { | |||
| 116 | echo 'Some\nPrepended\nData\nLines\n'; sleep 0.2; | 158 | echo 'Some\nPrepended\nData\nLines\n'; sleep 0.2; |
| 117 | echo 'SSH-2.0-nagiosplug.ssh.0.2';} | nc ${nc_flags}|"); | 159 | echo 'SSH-2.0-nagiosplug.ssh.0.2';} | nc ${nc_flags}|"); |
| 118 | sleep 0.1; | 160 | sleep 0.1; |
| 119 | $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ); | 161 | $result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat); |
| 120 | cmp_ok( $res->return_code, '==', 0, "Got delayed SSH protocol version control string"); | 162 | cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); |
| 121 | like( $res->output, '/^SSH OK - nagiosplug.ssh.0.2 \(protocol 2.0\)/', "Output OK"); | 163 | $output = decode_json($result->output); |
| 164 | is($output->{'state'}, "OK", "State was correct"); | ||
| 165 | |||
| 166 | # looking for the version | ||
| 167 | $found_version = 0; | ||
| 168 | for my $subcheck (@{$output->{'checks'}}) { | ||
| 169 | if ($subcheck->{'output'} =~ /nagiosplug.ssh.0.2 \(protocol version: 2.0\)/ ){ | ||
| 170 | $found_version = 1; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | cmp_ok($found_version, '==', 1, "Output OK"); | ||
| 122 | close NC; | 174 | close NC; |
| 123 | } | 175 | } |
diff --git a/plugins/t/check_swap.t b/plugins/t/check_swap.t index 7e61b766..68946f6d 100644 --- a/plugins/t/check_swap.t +++ b/plugins/t/check_swap.t | |||
| @@ -17,42 +17,35 @@ my $message = '/^[0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; | |||
| 17 | 17 | ||
| 18 | $result = NPTest->testCmd( "./check_swap $outputFormat" ); # Always OK | 18 | $result = NPTest->testCmd( "./check_swap $outputFormat" ); # Always OK |
| 19 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); | 19 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); |
| 20 | $output = decode_json($result->output); | 20 | is($result->{'mp_test_result'}->{'state'}, "OK", "State was correct"); |
| 21 | is($output->{'state'}, "OK", "State was correct"); | 21 | like($result->{'mp_test_result'}->{'checks'}->[0]->{'output'}, $message, "Output was correct"); |
| 22 | like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); | ||
| 23 | 22 | ||
| 24 | $result = NPTest->testCmd( "./check_swap -w 1048576 -c 1048576 $outputFormat" ); # 1 MB free | 23 | $result = NPTest->testCmd( "./check_swap -w 1048576 -c 1048576 $outputFormat" ); # 1 MB free |
| 25 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); | 24 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); |
| 26 | $output = decode_json($result->output); | 25 | is($result->{'mp_test_result'}->{'state'}, "OK", "State was correct"); |
| 27 | is($output->{'state'}, "OK", "State was correct"); | 26 | like($result->{'mp_test_result'}->{'checks'}->[0]->{'output'}, $message, "Output was correct"); |
| 28 | like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); | ||
| 29 | 27 | ||
| 30 | $result = NPTest->testCmd( "./check_swap -w 1% -c 1% $outputFormat" ); # 1% free | 28 | $result = NPTest->testCmd( "./check_swap -w 1% -c 1% $outputFormat" ); # 1% free |
| 31 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); | 29 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); |
| 32 | $output = decode_json($result->output); | 30 | is($result->{'mp_test_result'}->{'state'}, "OK", "State was correct"); |
| 33 | is($output->{'state'}, "OK", "State was correct"); | 31 | like($result->{'mp_test_result'}->{'checks'}->[0]->{'output'}, $message, "Output was correct"); |
| 34 | like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); | ||
| 35 | 32 | ||
| 36 | $result = NPTest->testCmd( "./check_swap -w 100% -c 100% $outputFormat" ); # 100% (always critical) | 33 | $result = NPTest->testCmd( "./check_swap -w 100% -c 100% $outputFormat" ); # 100% (always critical) |
| 37 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); | 34 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); |
| 38 | $output = decode_json($result->output); | 35 | is($result->{'mp_test_result'}->{'state'}, "CRITICAL", "State was correct"); |
| 39 | is($output->{'state'}, "CRITICAL", "State was correct"); | 36 | like($result->{'mp_test_result'}->{'checks'}->[0]->{'output'}, $message, "Output was correct"); |
| 40 | like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); | ||
| 41 | 37 | ||
| 42 | $result = NPTest->testCmd( "./check_swap -w 100% -c 1% $outputFormat" ); # 100% (always warn) | 38 | $result = NPTest->testCmd( "./check_swap -w 100% -c 1% $outputFormat" ); # 100% (always warn) |
| 43 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); | 39 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); |
| 44 | $output = decode_json($result->output); | 40 | is($result->{'mp_test_result'}->{'state'}, "WARNING", "State was correct"); |
| 45 | is($output->{'state'}, "WARNING", "State was correct"); | 41 | like($result->{'mp_test_result'}->{'checks'}->[0]->{'output'}, $message, "Output was correct"); |
| 46 | like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); | ||
| 47 | 42 | ||
| 48 | $result = NPTest->testCmd( "./check_swap -w 100% $outputFormat" ); # 100% (single threshold, always warn) | 43 | $result = NPTest->testCmd( "./check_swap -w 100% $outputFormat" ); # 100% (single threshold, always warn) |
| 49 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); | 44 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); |
| 50 | $output = decode_json($result->output); | 45 | is($result->{'mp_test_result'}->{'state'}, "WARNING", "State was correct"); |
| 51 | is($output->{'state'}, "WARNING", "State was correct"); | 46 | like($result->{'mp_test_result'}->{'checks'}->[0]->{'output'}, $message, "Output was correct"); |
| 52 | like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); | ||
| 53 | 47 | ||
| 54 | $result = NPTest->testCmd( "./check_swap -c 100% $outputFormat" ); # 100% (single threshold, always critical) | 48 | $result = NPTest->testCmd( "./check_swap -c 100% $outputFormat" ); # 100% (single threshold, always critical) |
| 55 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); | 49 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); |
| 56 | $output = decode_json($result->output); | 50 | is($result->{'mp_test_result'}->{'state'}, "CRITICAL", "State was correct"); |
| 57 | is($output->{'state'}, "CRITICAL", "State was correct"); | 51 | like($result->{'mp_test_result'}->{'checks'}->[0]->{'output'}, $message, "Output was correct"); |
| 58 | like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); | ||
