summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-07 09:14:37 +0100
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-07 09:14:37 +0100
commiteb1dd9fe2e0e965b4c9326195a778c3794492ef4 (patch)
tree2f6442d095d5c8384d80212a5e973cbc18b8cb91 /plugins
parent06fa1036f9e7216aac27107cd7d4c4903fa61ab2 (diff)
downloadmonitoring-plugins-eb1dd9fe2e0e965b4c9326195a778c3794492ef4.tar.gz
check_dbi: clang_format
Diffstat (limited to 'plugins')
-rw-r--r--plugins/check_dbi.c140
1 files changed, 89 insertions, 51 deletions
diff --git a/plugins/check_dbi.c b/plugins/check_dbi.c
index 96575672..ae8ec952 100644
--- a/plugins/check_dbi.c
+++ b/plugins/check_dbi.c
@@ -128,8 +128,9 @@ int main(int argc, char **argv) {
128 /* Parse extra opts if any */ 128 /* Parse extra opts if any */
129 argv = np_extra_opts(&argc, argv, progname); 129 argv = np_extra_opts(&argc, argv, progname);
130 130
131 if (process_arguments(argc, argv) == ERROR) 131 if (process_arguments(argc, argv) == ERROR) {
132 usage4(_("Could not parse arguments")); 132 usage4(_("Could not parse arguments"));
133 }
133 134
134 /* Set signal handling and alarm */ 135 /* Set signal handling and alarm */
135 if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { 136 if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) {
@@ -137,8 +138,9 @@ int main(int argc, char **argv) {
137 } 138 }
138 alarm(timeout_interval); 139 alarm(timeout_interval);
139 140
140 if (verbose > 2) 141 if (verbose > 2) {
141 printf("Initializing DBI\n"); 142 printf("Initializing DBI\n");
143 }
142 144
143 dbi_inst *instance_p = {0}; 145 dbi_inst *instance_p = {0};
144 146
@@ -152,8 +154,9 @@ int main(int argc, char **argv) {
152 return STATE_UNKNOWN; 154 return STATE_UNKNOWN;
153 } 155 }
154 156
155 if (verbose) 157 if (verbose) {
156 printf("Opening DBI driver '%s'\n", np_dbi_driver); 158 printf("Opening DBI driver '%s'\n", np_dbi_driver);
159 }
157 160
158 driver = dbi_driver_open_r(np_dbi_driver, instance_p); 161 driver = dbi_driver_open_r(np_dbi_driver, instance_p);
159 if (!driver) { 162 if (!driver) {
@@ -179,11 +182,13 @@ int main(int argc, char **argv) {
179 for (i = 0; i < np_dbi_options_num; ++i) { 182 for (i = 0; i < np_dbi_options_num; ++i) {
180 const char *opt; 183 const char *opt;
181 184
182 if (verbose > 1) 185 if (verbose > 1) {
183 printf("Setting DBI driver option '%s' to '%s'\n", np_dbi_options[i].key, np_dbi_options[i].value); 186 printf("Setting DBI driver option '%s' to '%s'\n", np_dbi_options[i].key, np_dbi_options[i].value);
187 }
184 188
185 if (!dbi_conn_set_option(conn, np_dbi_options[i].key, np_dbi_options[i].value)) 189 if (!dbi_conn_set_option(conn, np_dbi_options[i].key, np_dbi_options[i].value)) {
186 continue; 190 continue;
191 }
187 /* else: status != 0 */ 192 /* else: status != 0 */
188 193
189 np_dbi_print_error(conn, "UNKNOWN - failed to set option '%s' to '%s'", np_dbi_options[i].key, np_dbi_options[i].value); 194 np_dbi_print_error(conn, "UNKNOWN - failed to set option '%s' to '%s'", np_dbi_options[i].key, np_dbi_options[i].value);
@@ -197,8 +202,9 @@ int main(int argc, char **argv) {
197 } 202 }
198 203
199 if (host) { 204 if (host) {
200 if (verbose > 1) 205 if (verbose > 1) {
201 printf("Setting DBI driver option 'host' to '%s'\n", host); 206 printf("Setting DBI driver option 'host' to '%s'\n", host);
207 }
202 dbi_conn_set_option(conn, "host", host); 208 dbi_conn_set_option(conn, "host", host);
203 } 209 }
204 210
@@ -209,10 +215,12 @@ int main(int argc, char **argv) {
209 dbname = dbi_conn_get_option(conn, "dbname"); 215 dbname = dbi_conn_get_option(conn, "dbname");
210 host = dbi_conn_get_option(conn, "host"); 216 host = dbi_conn_get_option(conn, "host");
211 217
212 if (!dbname) 218 if (!dbname) {
213 dbname = "<unspecified>"; 219 dbname = "<unspecified>";
214 if (!host) 220 }
221 if (!host) {
215 host = "<unspecified>"; 222 host = "<unspecified>";
223 }
216 224
217 printf("Connecting to database '%s' at host '%s'\n", dbname, host); 225 printf("Connecting to database '%s' at host '%s'\n", dbname, host);
218 } 226 }
@@ -226,22 +234,27 @@ int main(int argc, char **argv) {
226 conn_time = timediff(start_timeval, end_timeval); 234 conn_time = timediff(start_timeval, end_timeval);
227 235
228 server_version = dbi_conn_get_engine_version(conn); 236 server_version = dbi_conn_get_engine_version(conn);
229 if (verbose) 237 if (verbose) {
230 printf("Connected to server version %u\n", server_version); 238 printf("Connected to server version %u\n", server_version);
239 }
231 240
232 if (metric == METRIC_SERVER_VERSION) 241 if (metric == METRIC_SERVER_VERSION) {
233 status = get_status(server_version, dbi_thresholds); 242 status = get_status(server_version, dbi_thresholds);
243 }
234 244
235 if (verbose) 245 if (verbose) {
236 printf("Time elapsed: %f\n", conn_time); 246 printf("Time elapsed: %f\n", conn_time);
247 }
237 248
238 if (metric == METRIC_CONN_TIME) 249 if (metric == METRIC_CONN_TIME) {
239 status = get_status(conn_time, dbi_thresholds); 250 status = get_status(conn_time, dbi_thresholds);
251 }
240 252
241 /* select a database */ 253 /* select a database */
242 if (np_dbi_database) { 254 if (np_dbi_database) {
243 if (verbose > 1) 255 if (verbose > 1) {
244 printf("Selecting database '%s'\n", np_dbi_database); 256 printf("Selecting database '%s'\n", np_dbi_database);
257 }
245 258
246 if (dbi_conn_select_db(conn, np_dbi_database)) { 259 if (dbi_conn_select_db(conn, np_dbi_database)) {
247 np_dbi_print_error(conn, "UNKNOWN - failed to select database '%s'", np_dbi_database); 260 np_dbi_print_error(conn, "UNKNOWN - failed to select database '%s'", np_dbi_database);
@@ -252,38 +265,43 @@ int main(int argc, char **argv) {
252 if (np_dbi_query) { 265 if (np_dbi_query) {
253 /* execute query */ 266 /* execute query */
254 status = do_query(conn, &query_val_str, &query_val, &query_time); 267 status = do_query(conn, &query_val_str, &query_val, &query_time);
255 if (status != STATE_OK) 268 if (status != STATE_OK) {
256 /* do_query prints an error message in this case */ 269 /* do_query prints an error message in this case */
257 return status; 270 return status;
271 }
258 272
259 if (metric == METRIC_QUERY_RESULT) { 273 if (metric == METRIC_QUERY_RESULT) {
260 if (expect) { 274 if (expect) {
261 if ((!query_val_str) || strcmp(query_val_str, expect)) 275 if ((!query_val_str) || strcmp(query_val_str, expect)) {
262 status = STATE_CRITICAL; 276 status = STATE_CRITICAL;
263 else 277 } else {
264 status = STATE_OK; 278 status = STATE_OK;
279 }
265 } else if (expect_re_str) { 280 } else if (expect_re_str) {
266 int err; 281 int err;
267 282
268 err = regexec(&expect_re, query_val_str, 0, NULL, /* flags = */ 0); 283 err = regexec(&expect_re, query_val_str, 0, NULL, /* flags = */ 0);
269 if (!err) 284 if (!err) {
270 status = STATE_OK; 285 status = STATE_OK;
271 else if (err == REG_NOMATCH) 286 } else if (err == REG_NOMATCH) {
272 status = STATE_CRITICAL; 287 status = STATE_CRITICAL;
273 else { 288 } else {
274 char errmsg[1024]; 289 char errmsg[1024];
275 regerror(err, &expect_re, errmsg, sizeof(errmsg)); 290 regerror(err, &expect_re, errmsg, sizeof(errmsg));
276 printf("ERROR - failed to execute regular expression: %s\n", errmsg); 291 printf("ERROR - failed to execute regular expression: %s\n", errmsg);
277 status = STATE_CRITICAL; 292 status = STATE_CRITICAL;
278 } 293 }
279 } else 294 } else {
280 status = get_status(query_val, dbi_thresholds); 295 status = get_status(query_val, dbi_thresholds);
281 } else if (metric == METRIC_QUERY_TIME) 296 }
297 } else if (metric == METRIC_QUERY_TIME) {
282 status = get_status(query_time, dbi_thresholds); 298 status = get_status(query_time, dbi_thresholds);
299 }
283 } 300 }
284 301
285 if (verbose) 302 if (verbose) {
286 printf("Closing connection\n"); 303 printf("Closing connection\n");
304 }
287 dbi_conn_close(conn); 305 dbi_conn_close(conn);
288 306
289 /* In case of METRIC_QUERY_RESULT, isnan(query_val) indicates an error 307 /* In case of METRIC_QUERY_RESULT, isnan(query_val) indicates an error
@@ -299,15 +317,17 @@ int main(int argc, char **argv) {
299 assert(expect || expect_re_str); 317 assert(expect || expect_re_str);
300 printf(", '%s' returned '%s' in %fs", np_dbi_query, query_val_str ? query_val_str : "<nothing>", query_time); 318 printf(", '%s' returned '%s' in %fs", np_dbi_query, query_val_str ? query_val_str : "<nothing>", query_time);
301 if (status != STATE_OK) { 319 if (status != STATE_OK) {
302 if (expect) 320 if (expect) {
303 printf(" (expected '%s')", expect); 321 printf(" (expected '%s')", expect);
304 else if (expect_re_str) 322 } else if (expect_re_str) {
305 printf(" (expected regex /%s/%s)", expect_re_str, ((expect_re_cflags & REG_ICASE) ? "i" : "")); 323 printf(" (expected regex /%s/%s)", expect_re_str, ((expect_re_cflags & REG_ICASE) ? "i" : ""));
324 }
306 } 325 }
307 } else if (isnan(query_val)) 326 } else if (isnan(query_val)) {
308 printf(", '%s' query execution time: %fs", np_dbi_query, query_time); 327 printf(", '%s' query execution time: %fs", np_dbi_query, query_time);
309 else 328 } else {
310 printf(", '%s' returned %f in %fs", np_dbi_query, query_val, query_time); 329 printf(", '%s' returned %f in %fs", np_dbi_query, query_val, query_time);
330 }
311 } 331 }
312 332
313 printf(" | conntime=%fs;%s;%s;0; server_version=%u;%s;%s;0;", conn_time, 333 printf(" | conntime=%fs;%s;%s;0; server_version=%u;%s;%s;0;", conn_time,
@@ -316,9 +336,10 @@ int main(int argc, char **argv) {
316 ((metric == METRIC_SERVER_VERSION) && warning_range) ? warning_range : "", 336 ((metric == METRIC_SERVER_VERSION) && warning_range) ? warning_range : "",
317 ((metric == METRIC_SERVER_VERSION) && critical_range) ? critical_range : ""); 337 ((metric == METRIC_SERVER_VERSION) && critical_range) ? critical_range : "");
318 if (np_dbi_query) { 338 if (np_dbi_query) {
319 if (!isnan(query_val)) /* this is also true when -e is used */ 339 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 : "", 340 printf(" query=%f;%s;%s;;", query_val, ((metric == METRIC_QUERY_RESULT) && warning_range) ? warning_range : "",
321 ((metric == METRIC_QUERY_RESULT) && critical_range) ? critical_range : ""); 341 ((metric == METRIC_QUERY_RESULT) && critical_range) ? critical_range : "");
342 }
322 printf(" querytime=%fs;%s;%s;0;", query_time, ((metric == METRIC_QUERY_TIME) && warning_range) ? warning_range : "", 343 printf(" querytime=%fs;%s;%s;0;", query_time, ((metric == METRIC_QUERY_TIME) && warning_range) ? warning_range : "",
323 ((metric == METRIC_QUERY_TIME) && critical_range) ? critical_range : ""); 344 ((metric == METRIC_QUERY_TIME) && critical_range) ? critical_range : "");
324 } 345 }
@@ -346,8 +367,9 @@ int process_arguments(int argc, char **argv) {
346 while (1) { 367 while (1) {
347 c = getopt_long(argc, argv, "Vvht:c:w:e:r:R:m:H:d:o:q:D:", longopts, &option); 368 c = getopt_long(argc, argv, "Vvht:c:w:e:r:R:m:H:d:o:q:D:", longopts, &option);
348 369
349 if (c == EOF) 370 if (c == EOF) {
350 break; 371 break;
372 }
351 373
352 switch (c) { 374 switch (c) {
353 case '?': /* usage */ 375 case '?': /* usage */
@@ -392,29 +414,32 @@ int process_arguments(int argc, char **argv) {
392 } 414 }
393 415
394 case 'm': 416 case 'm':
395 if (!strcasecmp(optarg, "CONN_TIME")) 417 if (!strcasecmp(optarg, "CONN_TIME")) {
396 metric = METRIC_CONN_TIME; 418 metric = METRIC_CONN_TIME;
397 else if (!strcasecmp(optarg, "SERVER_VERSION")) 419 } else if (!strcasecmp(optarg, "SERVER_VERSION")) {
398 metric = METRIC_SERVER_VERSION; 420 metric = METRIC_SERVER_VERSION;
399 else if (!strcasecmp(optarg, "QUERY_RESULT")) 421 } else if (!strcasecmp(optarg, "QUERY_RESULT")) {
400 metric = METRIC_QUERY_RESULT; 422 metric = METRIC_QUERY_RESULT;
401 else if (!strcasecmp(optarg, "QUERY_TIME")) 423 } else if (!strcasecmp(optarg, "QUERY_TIME")) {
402 metric = METRIC_QUERY_TIME; 424 metric = METRIC_QUERY_TIME;
403 else 425 } else {
404 usage2(_("Invalid metric"), optarg); 426 usage2(_("Invalid metric"), optarg);
427 }
405 break; 428 break;
406 case 't': /* timeout */ 429 case 't': /* timeout */
407 if (!is_intnonneg(optarg)) 430 if (!is_intnonneg(optarg)) {
408 usage2(_("Timeout interval must be a positive integer"), optarg); 431 usage2(_("Timeout interval must be a positive integer"), optarg);
409 else 432 } else {
410 timeout_interval = atoi(optarg); 433 timeout_interval = atoi(optarg);
434 }
411 435
412 break; 436 break;
413 case 'H': /* host */ 437 case 'H': /* host */
414 if (!is_host(optarg)) 438 if (!is_host(optarg)) {
415 usage2(_("Invalid hostname/address"), optarg); 439 usage2(_("Invalid hostname/address"), optarg);
416 else 440 } else {
417 host = optarg; 441 host = optarg;
442 }
418 break; 443 break;
419 case 'v': 444 case 'v':
420 verbose++; 445 verbose++;
@@ -432,8 +457,9 @@ int process_arguments(int argc, char **argv) {
432 k = optarg; 457 k = optarg;
433 v = strchr(k, (int)'='); 458 v = strchr(k, (int)'=');
434 459
435 if (!v) 460 if (!v) {
436 usage2(_("Option must be '<key>=<value>'"), optarg); 461 usage2(_("Option must be '<key>=<value>'"), optarg);
462 }
437 463
438 *v = '\0'; 464 *v = '\0';
439 ++v; 465 ++v;
@@ -466,27 +492,34 @@ int process_arguments(int argc, char **argv) {
466} 492}
467 493
468int validate_arguments(void) { 494int validate_arguments(void) {
469 if (!np_dbi_driver) 495 if (!np_dbi_driver) {
470 usage("Must specify a DBI driver"); 496 usage("Must specify a DBI driver");
497 }
471 498
472 if (((metric == METRIC_QUERY_RESULT) || (metric == METRIC_QUERY_TIME)) && (!np_dbi_query)) 499 if (((metric == METRIC_QUERY_RESULT) || (metric == METRIC_QUERY_TIME)) && (!np_dbi_query)) {
473 usage("Must specify a query to execute (metric == QUERY_RESULT)"); 500 usage("Must specify a query to execute (metric == QUERY_RESULT)");
501 }
474 502
475 if ((metric != METRIC_CONN_TIME) && (metric != METRIC_SERVER_VERSION) && (metric != METRIC_QUERY_RESULT) && 503 if ((metric != METRIC_CONN_TIME) && (metric != METRIC_SERVER_VERSION) && (metric != METRIC_QUERY_RESULT) &&
476 (metric != METRIC_QUERY_TIME)) 504 (metric != METRIC_QUERY_TIME)) {
477 usage("Invalid metric specified"); 505 usage("Invalid metric specified");
506 }
478 507
479 if (expect && (warning_range || critical_range || expect_re_str)) 508 if (expect && (warning_range || critical_range || expect_re_str)) {
480 usage("Do not mix -e and -w/-c/-r/-R"); 509 usage("Do not mix -e and -w/-c/-r/-R");
510 }
481 511
482 if (expect_re_str && (warning_range || critical_range || expect)) 512 if (expect_re_str && (warning_range || critical_range || expect)) {
483 usage("Do not mix -r/-R and -w/-c/-e"); 513 usage("Do not mix -r/-R and -w/-c/-e");
514 }
484 515
485 if (expect && (metric != METRIC_QUERY_RESULT)) 516 if (expect && (metric != METRIC_QUERY_RESULT)) {
486 usage("Option -e requires metric QUERY_RESULT"); 517 usage("Option -e requires metric QUERY_RESULT");
518 }
487 519
488 if (expect_re_str && (metric != METRIC_QUERY_RESULT)) 520 if (expect_re_str && (metric != METRIC_QUERY_RESULT)) {
489 usage("Options -r/-R require metric QUERY_RESULT"); 521 usage("Options -r/-R require metric QUERY_RESULT");
522 }
490 523
491 return OK; 524 return OK;
492} 525}
@@ -613,8 +646,9 @@ const char *get_field_str(dbi_conn conn, dbi_result res, unsigned short field_ty
613 return NULL; 646 return NULL;
614 } 647 }
615 648
616 if ((verbose && (type == TYPE_STRING)) || (verbose > 2)) 649 if ((verbose && (type == TYPE_STRING)) || (verbose > 2)) {
617 printf("Query returned string '%s'\n", str); 650 printf("Query returned string '%s'\n", str);
651 }
618 return str; 652 return str;
619} 653}
620 654
@@ -644,8 +678,9 @@ double get_field(dbi_conn conn, dbi_result res, unsigned short *field_type) {
644 return NAN; 678 return NAN;
645 } 679 }
646 if ((endptr != NULL) && (*endptr != '\0')) { 680 if ((endptr != NULL) && (*endptr != '\0')) {
647 if (verbose) 681 if (verbose) {
648 printf("Garbage after value: %s\n", endptr); 682 printf("Garbage after value: %s\n", endptr);
683 }
649 } 684 }
650 } else { 685 } else {
651 CHECK_IGNORE_ERROR(NAN); 686 CHECK_IGNORE_ERROR(NAN);
@@ -696,11 +731,12 @@ double get_query_result(dbi_conn conn, dbi_result res, const char **res_val_str,
696 731
697 field_type = dbi_result_get_field_type_idx(res, 1); 732 field_type = dbi_result_get_field_type_idx(res, 1);
698 if (field_type != DBI_TYPE_ERROR) { 733 if (field_type != DBI_TYPE_ERROR) {
699 if (type == TYPE_STRING) 734 if (type == TYPE_STRING) {
700 /* the value will be freed in dbi_result_free */ 735 /* the value will be freed in dbi_result_free */
701 *res_val_str = strdup(get_field_str(conn, res, field_type)); 736 *res_val_str = strdup(get_field_str(conn, res, field_type));
702 else 737 } else {
703 val = get_field(conn, res, &field_type); 738 val = get_field(conn, res, &field_type);
739 }
704 } 740 }
705 741
706 *res_val = val; 742 *res_val = val;
@@ -726,8 +762,9 @@ int do_query(dbi_conn conn, const char **res_val_str, double *res_val, double *r
726 762
727 assert(np_dbi_query); 763 assert(np_dbi_query);
728 764
729 if (verbose) 765 if (verbose) {
730 printf("Executing query '%s'\n", np_dbi_query); 766 printf("Executing query '%s'\n", np_dbi_query);
767 }
731 768
732 gettimeofday(&timeval_start, NULL); 769 gettimeofday(&timeval_start, NULL);
733 770
@@ -742,8 +779,9 @@ int do_query(dbi_conn conn, const char **res_val_str, double *res_val, double *r
742 gettimeofday(&timeval_end, NULL); 779 gettimeofday(&timeval_end, NULL);
743 *res_time = timediff(timeval_start, timeval_end); 780 *res_time = timediff(timeval_start, timeval_end);
744 781
745 if (verbose) 782 if (verbose) {
746 printf("Time elapsed: %f\n", *res_time); 783 printf("Time elapsed: %f\n", *res_time);
784 }
747 785
748 return status; 786 return status;
749} 787}