summaryrefslogtreecommitdiffstats
path: root/plugins/check_pgsql.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_pgsql.c')
-rw-r--r--plugins/check_pgsql.c79
1 files changed, 32 insertions, 47 deletions
diff --git a/plugins/check_pgsql.c b/plugins/check_pgsql.c
index b8fc5f1..6199033 100644
--- a/plugins/check_pgsql.c
+++ b/plugins/check_pgsql.c
@@ -69,7 +69,6 @@ int process_arguments (int, char **);
69int validate_arguments (void); 69int validate_arguments (void);
70void print_usage (void); 70void print_usage (void);
71void print_help (void); 71void print_help (void);
72int is_pg_dbname (char *);
73int is_pg_logname (char *); 72int is_pg_logname (char *);
74int do_query (PGconn *, char *); 73int do_query (PGconn *, char *);
75 74
@@ -85,6 +84,8 @@ char *pgparams = NULL;
85double twarn = (double)DEFAULT_WARN; 84double twarn = (double)DEFAULT_WARN;
86double tcrit = (double)DEFAULT_CRIT; 85double tcrit = (double)DEFAULT_CRIT;
87char *pgquery = NULL; 86char *pgquery = NULL;
87#define OPTID_QUERYNAME -1000
88char *pgqueryname = NULL;
88char *query_warning = NULL; 89char *query_warning = NULL;
89char *query_critical = NULL; 90char *query_critical = NULL;
90thresholds *qthresholds = NULL; 91thresholds *qthresholds = NULL;
@@ -92,7 +93,7 @@ int verbose = 0;
92 93
93/****************************************************************************** 94/******************************************************************************
94 95
95The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@ 96The (pseudo?)literate programming XML is contained within \@\@\- <XML> \-\@\@
96tags in the comments. With in the tags, the XML is assembled sequentially. 97tags in the comments. With in the tags, the XML is assembled sequentially.
97You can define entities in tags. You also have all the #defines available as 98You can define entities in tags. You also have all the #defines available as
98entities. 99entities.
@@ -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
421is a valid PostgreSQL database name, and returns false if it is
422not.</para>
423
424<para>Valid PostgreSQL database names are less than &NAMEDATALEN;
425characters long and consist of letters, numbers, and underscores. The
426first character cannot be a number, however.</para>
427
428</sect3>
429-@@
430******************************************************************************/
431
432
433
434int
435is_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
452the tango program should eventually create an entity here based on the 418the 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