diff options
| author | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2023-09-18 22:59:46 +0200 |
|---|---|---|
| committer | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2023-09-18 22:59:46 +0200 |
| commit | 0e70e81133c25274fe2dd2309556b41357dd759b (patch) | |
| tree | 9a680b36788ee1ad4e7ecc5ccfeb4494db9fdc72 /plugins/check_pgsql.c | |
| parent | ce355c80cf6054bfa5e1dcf81f9e2183ef963ee1 (diff) | |
| parent | 2ddc75e69db5a3dd379c896d8420c9af20ec1cee (diff) | |
| download | monitoring-plugins-0e70e81133c25274fe2dd2309556b41357dd759b.tar.gz | |
Merge branch 'master' into mysql_detect_mysqldump
Diffstat (limited to 'plugins/check_pgsql.c')
| -rw-r--r-- | plugins/check_pgsql.c | 79 |
1 files changed, 32 insertions, 47 deletions
diff --git a/plugins/check_pgsql.c b/plugins/check_pgsql.c index b8fc5f1d..61990335 100644 --- a/plugins/check_pgsql.c +++ b/plugins/check_pgsql.c | |||
| @@ -69,7 +69,6 @@ int process_arguments (int, char **); | |||
| 69 | int validate_arguments (void); | 69 | int validate_arguments (void); |
| 70 | void print_usage (void); | 70 | void print_usage (void); |
| 71 | void print_help (void); | 71 | void print_help (void); |
| 72 | int is_pg_dbname (char *); | ||
| 73 | int is_pg_logname (char *); | 72 | int is_pg_logname (char *); |
| 74 | int do_query (PGconn *, char *); | 73 | int do_query (PGconn *, char *); |
| 75 | 74 | ||
| @@ -85,6 +84,8 @@ char *pgparams = NULL; | |||
| 85 | double twarn = (double)DEFAULT_WARN; | 84 | double twarn = (double)DEFAULT_WARN; |
| 86 | double tcrit = (double)DEFAULT_CRIT; | 85 | double tcrit = (double)DEFAULT_CRIT; |
| 87 | char *pgquery = NULL; | 86 | char *pgquery = NULL; |
| 87 | #define OPTID_QUERYNAME -1000 | ||
| 88 | char *pgqueryname = NULL; | ||
| 88 | char *query_warning = NULL; | 89 | char *query_warning = NULL; |
| 89 | char *query_critical = NULL; | 90 | char *query_critical = NULL; |
| 90 | thresholds *qthresholds = NULL; | 91 | thresholds *qthresholds = NULL; |
| @@ -92,7 +93,7 @@ int verbose = 0; | |||
| 92 | 93 | ||
| 93 | /****************************************************************************** | 94 | /****************************************************************************** |
| 94 | 95 | ||
| 95 | The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@ | 96 | The (pseudo?)literate programming XML is contained within \@\@\- <XML> \-\@\@ |
| 96 | tags in the comments. With in the tags, the XML is assembled sequentially. | 97 | tags in the comments. With in the tags, the XML is assembled sequentially. |
| 97 | You can define entities in tags. You also have all the #defines available as | 98 | You can define entities in tags. You also have all the #defines available as |
| 98 | entities. | 99 | entities. |
| @@ -285,6 +286,7 @@ process_arguments (int argc, char **argv) | |||
| 285 | {"database", required_argument, 0, 'd'}, | 286 | {"database", required_argument, 0, 'd'}, |
| 286 | {"option", required_argument, 0, 'o'}, | 287 | {"option", required_argument, 0, 'o'}, |
| 287 | {"query", required_argument, 0, 'q'}, | 288 | {"query", required_argument, 0, 'q'}, |
| 289 | {"queryname", required_argument, 0, OPTID_QUERYNAME}, | ||
| 288 | {"query_critical", required_argument, 0, 'C'}, | 290 | {"query_critical", required_argument, 0, 'C'}, |
| 289 | {"query_warning", required_argument, 0, 'W'}, | 291 | {"query_warning", required_argument, 0, 'W'}, |
| 290 | {"verbose", no_argument, 0, 'v'}, | 292 | {"verbose", no_argument, 0, 'v'}, |
| @@ -344,10 +346,10 @@ process_arguments (int argc, char **argv) | |||
| 344 | pgport = optarg; | 346 | pgport = optarg; |
| 345 | break; | 347 | break; |
| 346 | case 'd': /* database name */ | 348 | case 'd': /* database name */ |
| 347 | if (!is_pg_dbname (optarg)) /* checks length and valid chars */ | 349 | if (strlen(optarg) >= NAMEDATALEN) { |
| 348 | usage2 (_("Database name is not valid"), optarg); | 350 | usage2 (_("Database name exceeds the maximum length"), optarg); |
| 349 | else /* we know length, and know optarg is terminated, so us strcpy */ | 351 | } |
| 350 | snprintf(dbName, NAMEDATALEN, "%s", optarg); | 352 | snprintf(dbName, NAMEDATALEN, "%s", optarg); |
| 351 | break; | 353 | break; |
| 352 | case 'l': /* login name */ | 354 | case 'l': /* login name */ |
| 353 | if (!is_pg_logname (optarg)) | 355 | if (!is_pg_logname (optarg)) |
| @@ -368,6 +370,9 @@ process_arguments (int argc, char **argv) | |||
| 368 | case 'q': | 370 | case 'q': |
| 369 | pgquery = optarg; | 371 | pgquery = optarg; |
| 370 | break; | 372 | break; |
| 373 | case OPTID_QUERYNAME: | ||
| 374 | pgqueryname = optarg; | ||
| 375 | break; | ||
| 371 | case 'v': | 376 | case 'v': |
| 372 | verbose++; | 377 | verbose++; |
| 373 | break; | 378 | break; |
| @@ -408,45 +413,6 @@ validate_arguments () | |||
| 408 | return OK; | 413 | return OK; |
| 409 | } | 414 | } |
| 410 | 415 | ||
| 411 | |||
| 412 | /****************************************************************************** | ||
| 413 | |||
| 414 | @@- | ||
| 415 | <sect3> | ||
| 416 | <title>is_pg_dbname</title> | ||
| 417 | |||
| 418 | <para>&PROTO_is_pg_dbname;</para> | ||
| 419 | |||
| 420 | <para>Given a database name, this function returns TRUE if the string | ||
| 421 | is a valid PostgreSQL database name, and returns false if it is | ||
| 422 | not.</para> | ||
| 423 | |||
| 424 | <para>Valid PostgreSQL database names are less than &NAMEDATALEN; | ||
| 425 | characters long and consist of letters, numbers, and underscores. The | ||
| 426 | first character cannot be a number, however.</para> | ||
| 427 | |||
| 428 | </sect3> | ||
| 429 | -@@ | ||
| 430 | ******************************************************************************/ | ||
| 431 | |||
| 432 | |||
| 433 | |||
| 434 | int | ||
| 435 | is_pg_dbname (char *dbname) | ||
| 436 | { | ||
| 437 | char txt[NAMEDATALEN]; | ||
| 438 | char tmp[NAMEDATALEN]; | ||
| 439 | if (strlen (dbname) > NAMEDATALEN - 1) | ||
| 440 | return (FALSE); | ||
| 441 | strncpy (txt, dbname, NAMEDATALEN - 1); | ||
| 442 | txt[NAMEDATALEN - 1] = 0; | ||
| 443 | if (sscanf (txt, "%[_a-zA-Z]%[^_a-zA-Z0-9-]", tmp, tmp) == 1) | ||
| 444 | return (TRUE); | ||
| 445 | if (sscanf (txt, "%[_a-zA-Z]%[_a-zA-Z0-9-]%[^_a-zA-Z0-9-]", tmp, tmp, tmp) == | ||
| 446 | 2) return (TRUE); | ||
| 447 | return (FALSE); | ||
| 448 | } | ||
| 449 | |||
| 450 | /** | 416 | /** |
| 451 | 417 | ||
| 452 | the tango program should eventually create an entity here based on the | 418 | the tango program should eventually create an entity here based on the |
| @@ -529,6 +495,9 @@ print_help (void) | |||
| 529 | 495 | ||
| 530 | printf (" %s\n", "-q, --query=STRING"); | 496 | printf (" %s\n", "-q, --query=STRING"); |
| 531 | printf (" %s\n", _("SQL query to run. Only first column in first row will be read")); | 497 | printf (" %s\n", _("SQL query to run. Only first column in first row will be read")); |
| 498 | printf (" %s\n", "--queryname=STRING"); | ||
| 499 | printf (" %s\n", _("A name for the query, this string is used instead of the query")); | ||
| 500 | printf (" %s\n", _("in the long output of the plugin")); | ||
| 532 | printf (" %s\n", "-W, --query-warning=RANGE"); | 501 | printf (" %s\n", "-W, --query-warning=RANGE"); |
| 533 | printf (" %s\n", _("SQL query value to result in warning status (double)")); | 502 | printf (" %s\n", _("SQL query value to result in warning status (double)")); |
| 534 | printf (" %s\n", "-C, --query-critical=RANGE"); | 503 | printf (" %s\n", "-C, --query-critical=RANGE"); |
| @@ -548,7 +517,10 @@ print_help (void) | |||
| 548 | printf (" %s\n", _("connecting to the server. The result from the query has to be numeric.")); | 517 | printf (" %s\n", _("connecting to the server. The result from the query has to be numeric.")); |
| 549 | printf (" %s\n", _("Multiple SQL commands, separated by semicolon, are allowed but the result ")); | 518 | printf (" %s\n", _("Multiple SQL commands, separated by semicolon, are allowed but the result ")); |
| 550 | printf (" %s\n", _("of the last command is taken into account only. The value of the first")); | 519 | printf (" %s\n", _("of the last command is taken into account only. The value of the first")); |
| 551 | printf (" %s\n\n", _("column in the first row is used as the check result.")); | 520 | printf (" %s\n", _("column in the first row is used as the check result. If a second column is")); |
| 521 | printf (" %s\n", _("present in the result set, this is added to the plugin output with a")); | ||
| 522 | printf (" %s\n", _("prefix of \"Extra Info:\". This information can be displayed in the system")); | ||
| 523 | printf (" %s\n\n", _("executing the plugin.")); | ||
| 552 | 524 | ||
| 553 | printf (" %s\n", _("See the chapter \"Monitoring Database Activity\" of the PostgreSQL manual")); | 525 | printf (" %s\n", _("See the chapter \"Monitoring Database Activity\" of the PostgreSQL manual")); |
| 554 | printf (" %s\n\n", _("for details about how to access internal statistics of the database server.")); | 526 | printf (" %s\n\n", _("for details about how to access internal statistics of the database server.")); |
| @@ -588,6 +560,7 @@ do_query (PGconn *conn, char *query) | |||
| 588 | PGresult *res; | 560 | PGresult *res; |
| 589 | 561 | ||
| 590 | char *val_str; | 562 | char *val_str; |
| 563 | char *extra_info; | ||
| 591 | double value; | 564 | double value; |
| 592 | 565 | ||
| 593 | char *endptr = NULL; | 566 | char *endptr = NULL; |
| @@ -642,10 +615,22 @@ do_query (PGconn *conn, char *query) | |||
| 642 | : (my_status == STATE_CRITICAL) | 615 | : (my_status == STATE_CRITICAL) |
| 643 | ? _("CRITICAL") | 616 | ? _("CRITICAL") |
| 644 | : _("UNKNOWN")); | 617 | : _("UNKNOWN")); |
| 645 | printf (_("'%s' returned %f"), query, value); | 618 | if(pgqueryname) { |
| 619 | printf (_("%s returned %f"), pgqueryname, value); | ||
| 620 | } | ||
| 621 | else { | ||
| 622 | printf (_("'%s' returned %f"), query, value); | ||
| 623 | } | ||
| 624 | |||
| 646 | printf ("|query=%f;%s;%s;;\n", value, | 625 | printf ("|query=%f;%s;%s;;\n", value, |
| 647 | query_warning ? query_warning : "", | 626 | query_warning ? query_warning : "", |
| 648 | query_critical ? query_critical : ""); | 627 | query_critical ? query_critical : ""); |
| 628 | if (PQnfields (res) > 1) { | ||
| 629 | extra_info = PQgetvalue (res, 0, 1); | ||
| 630 | if (extra_info != NULL) { | ||
| 631 | printf ("Extra Info: %s\n", extra_info); | ||
| 632 | } | ||
| 633 | } | ||
| 649 | return my_status; | 634 | return my_status; |
| 650 | } | 635 | } |
| 651 | 636 | ||
