diff options
Diffstat (limited to 'plugins/check_pgsql.c')
-rw-r--r-- | plugins/check_pgsql.c | 65 |
1 files changed, 20 insertions, 45 deletions
diff --git a/plugins/check_pgsql.c b/plugins/check_pgsql.c index 11ce6916..c26cd439 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; |
@@ -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 | strcpy (dbName, 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"); |
@@ -642,7 +611,13 @@ do_query (PGconn *conn, char *query) | |||
642 | : (my_status == STATE_CRITICAL) | 611 | : (my_status == STATE_CRITICAL) |
643 | ? _("CRITICAL") | 612 | ? _("CRITICAL") |
644 | : _("UNKNOWN")); | 613 | : _("UNKNOWN")); |
645 | printf (_("'%s' returned %f"), query, value); | 614 | if(pgqueryname) { |
615 | printf (_("%s returned %f"), pgqueryname, value); | ||
616 | } | ||
617 | else { | ||
618 | printf (_("'%s' returned %f"), query, value); | ||
619 | } | ||
620 | |||
646 | printf ("|query=%f;%s;%s;;\n", value, | 621 | printf ("|query=%f;%s;%s;;\n", value, |
647 | query_warning ? query_warning : "", | 622 | query_warning ? query_warning : "", |
648 | query_critical ? query_critical : ""); | 623 | query_critical ? query_critical : ""); |