diff options
author | Holger Weiss <holger@zedat.fu-berlin.de> | 2013-08-17 22:28:38 +0200 |
---|---|---|
committer | Holger Weiss <holger@zedat.fu-berlin.de> | 2013-08-17 22:28:38 +0200 |
commit | e47a06f77d58ca0c01d56e2e9993095a4849d149 (patch) | |
tree | c1e16c32d09d931219a3fe689801cd711c102b12 /plugins/check_pgsql.c | |
parent | 4a3901ec70dc4ba0049f24d213b7f07e931d3dda (diff) | |
parent | c0bef3da51bd8b6be658f619a4659c95ad83d4bd (diff) | |
download | monitoring-plugins-e47a06f77d58ca0c01d56e2e9993095a4849d149.tar.gz |
Merge remote-tracking branch 'github/tokkee/sh/check_pgsql'
* github/tokkee/sh/check_pgsql:
check_pgsql: Determine connection time in µs-resolution.
check_pgsql: Leave 'min' value in query perfdata empty.
check_pgsql: Updated copyright.
check_pgsql: Added support for the -o command line option.
check_pgsql: Removed -4/-6 flags from help output.
check_pgsql: Allow UNIX socket directories as hostname as well.
check_pgsql: Use PQconnectdb() rather than PQsetdbLogin().
check_pgsql: Fixed query perfdata output for empty warn/crit ranges.
check_pgsql: Added support for executing queries.
Conflicts:
plugins/check_pgsql.c
Diffstat (limited to 'plugins/check_pgsql.c')
-rw-r--r-- | plugins/check_pgsql.c | 234 |
1 files changed, 202 insertions, 32 deletions
diff --git a/plugins/check_pgsql.c b/plugins/check_pgsql.c index 8b0769f1..c109955a 100644 --- a/plugins/check_pgsql.c +++ b/plugins/check_pgsql.c | |||
@@ -3,7 +3,7 @@ | |||
3 | * Nagios check_pgsql plugin | 3 | * Nagios check_pgsql plugin |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 1999-2007 Nagios Plugins Development Team | 6 | * Copyright (c) 1999-2011 Nagios Plugins Development Team |
7 | * | 7 | * |
8 | * Description: | 8 | * Description: |
9 | * | 9 | * |
@@ -29,7 +29,7 @@ | |||
29 | *****************************************************************************/ | 29 | *****************************************************************************/ |
30 | 30 | ||
31 | const char *progname = "check_pgsql"; | 31 | const char *progname = "check_pgsql"; |
32 | const char *copyright = "1999-2007"; | 32 | const char *copyright = "1999-2011"; |
33 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | 33 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; |
34 | 34 | ||
35 | #include "common.h" | 35 | #include "common.h" |
@@ -42,6 +42,20 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net"; | |||
42 | #define DEFAULT_DB "template1" | 42 | #define DEFAULT_DB "template1" |
43 | #define DEFAULT_HOST "127.0.0.1" | 43 | #define DEFAULT_HOST "127.0.0.1" |
44 | 44 | ||
45 | /* return the PSQL server version as a 3-tuple */ | ||
46 | #define PSQL_SERVER_VERSION3(server_version) \ | ||
47 | (server_version) / 10000, \ | ||
48 | (server_version) / 100 - (int)((server_version) / 10000) * 100, \ | ||
49 | (server_version) - (int)((server_version) / 100) * 100 | ||
50 | /* return true if the given host is a UNIX domain socket */ | ||
51 | #define PSQL_IS_UNIX_DOMAIN_SOCKET(host) \ | ||
52 | ((NULL == (host)) || ('\0' == *(host)) || ('/' == *(host))) | ||
53 | /* return a 3-tuple identifying a host/port independent of the socket type */ | ||
54 | #define PSQL_SOCKET3(host, port) \ | ||
55 | ((NULL == (host)) || ('\0' == *(host))) ? DEFAULT_PGSOCKET_DIR : host, \ | ||
56 | PSQL_IS_UNIX_DOMAIN_SOCKET (host) ? "/.s.PGSQL." : ":", \ | ||
57 | port | ||
58 | |||
45 | enum { | 59 | enum { |
46 | DEFAULT_PORT = 5432, | 60 | DEFAULT_PORT = 5432, |
47 | DEFAULT_WARN = 2, | 61 | DEFAULT_WARN = 2, |
@@ -56,6 +70,7 @@ void print_usage (void); | |||
56 | void print_help (void); | 70 | void print_help (void); |
57 | int is_pg_dbname (char *); | 71 | int is_pg_dbname (char *); |
58 | int is_pg_logname (char *); | 72 | int is_pg_logname (char *); |
73 | int do_query (PGconn *, char *); | ||
59 | 74 | ||
60 | char *pghost = NULL; /* host name of the backend server */ | 75 | char *pghost = NULL; /* host name of the backend server */ |
61 | char *pgport = NULL; /* port of the backend server */ | 76 | char *pgport = NULL; /* port of the backend server */ |
@@ -65,14 +80,15 @@ char *pgtty = NULL; | |||
65 | char dbName[NAMEDATALEN] = DEFAULT_DB; | 80 | char dbName[NAMEDATALEN] = DEFAULT_DB; |
66 | char *pguser = NULL; | 81 | char *pguser = NULL; |
67 | char *pgpasswd = NULL; | 82 | char *pgpasswd = NULL; |
83 | char *pgparams = NULL; | ||
68 | double twarn = (double)DEFAULT_WARN; | 84 | double twarn = (double)DEFAULT_WARN; |
69 | double tcrit = (double)DEFAULT_CRIT; | 85 | double tcrit = (double)DEFAULT_CRIT; |
86 | char *pgquery = NULL; | ||
87 | char *query_warning = NULL; | ||
88 | char *query_critical = NULL; | ||
89 | thresholds *qthresholds = NULL; | ||
70 | int verbose = 0; | 90 | int verbose = 0; |
71 | 91 | ||
72 | PGconn *conn; | ||
73 | /*PGresult *res;*/ | ||
74 | |||
75 | |||
76 | /****************************************************************************** | 92 | /****************************************************************************** |
77 | 93 | ||
78 | The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@ | 94 | The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@ |
@@ -115,10 +131,6 @@ Please note that all tags must be lowercase to use the DocBook XML DTD. | |||
115 | <sect2> | 131 | <sect2> |
116 | <title>Future Enhancements</title> | 132 | <title>Future Enhancements</title> |
117 | <para>ToDo List</para> | 133 | <para>ToDo List</para> |
118 | <itemizedlist> | ||
119 | <listitem>Add option to get password from a secured file rather than the command line</listitem> | ||
120 | <listitem>Add option to specify the query to execute</listitem> | ||
121 | </itemizedlist> | ||
122 | </sect2> | 134 | </sect2> |
123 | 135 | ||
124 | 136 | ||
@@ -132,8 +144,14 @@ Please note that all tags must be lowercase to use the DocBook XML DTD. | |||
132 | int | 144 | int |
133 | main (int argc, char **argv) | 145 | main (int argc, char **argv) |
134 | { | 146 | { |
135 | int elapsed_time; | 147 | PGconn *conn; |
148 | char *conninfo = NULL; | ||
149 | |||
150 | struct timeval start_timeval; | ||
151 | struct timeval end_timeval; | ||
152 | double elapsed_time; | ||
136 | int status = STATE_UNKNOWN; | 153 | int status = STATE_UNKNOWN; |
154 | int query_status = STATE_UNKNOWN; | ||
137 | 155 | ||
138 | /* begin, by setting the parameters for a backend connection if the | 156 | /* begin, by setting the parameters for a backend connection if the |
139 | * parameters are null, then the system will try to use reasonable | 157 | * parameters are null, then the system will try to use reasonable |
@@ -161,20 +179,41 @@ main (int argc, char **argv) | |||
161 | } | 179 | } |
162 | alarm (timeout_interval); | 180 | alarm (timeout_interval); |
163 | 181 | ||
164 | if (verbose) | 182 | if (pgparams) |
165 | printf("Connecting to database:\n DB: %s\n User: %s\n Host: %s\n Port: %d\n", dbName, | 183 | asprintf (&conninfo, "%s ", pgparams); |
166 | (pguser != NULL) ? pguser : "unspecified", | 184 | |
167 | (pghost != NULL) ? pghost : "unspecified", | 185 | asprintf (&conninfo, "%sdbname = '%s'", conninfo ? conninfo : "", dbName); |
168 | (pgport != NULL) ? atoi(pgport) : DEFAULT_PORT); | 186 | if (pghost) |
187 | asprintf (&conninfo, "%s host = '%s'", conninfo, pghost); | ||
188 | if (pgport) | ||
189 | asprintf (&conninfo, "%s port = '%s'", conninfo, pgport); | ||
190 | if (pgoptions) | ||
191 | asprintf (&conninfo, "%s options = '%s'", conninfo, pgoptions); | ||
192 | /* if (pgtty) -- ignored by PQconnectdb */ | ||
193 | if (pguser) | ||
194 | asprintf (&conninfo, "%s user = '%s'", conninfo, pguser); | ||
195 | |||
196 | if (verbose) /* do not include password (see right below) in output */ | ||
197 | printf ("Connecting to PostgreSQL using conninfo: %s%s\n", conninfo, | ||
198 | pgpasswd ? " password = <hidden>" : ""); | ||
199 | |||
200 | if (pgpasswd) | ||
201 | asprintf (&conninfo, "%s password = '%s'", conninfo, pgpasswd); | ||
169 | 202 | ||
170 | /* make a connection to the database */ | 203 | /* make a connection to the database */ |
171 | time (&start_time); | 204 | gettimeofday (&start_timeval, NULL); |
172 | conn = | 205 | conn = PQconnectdb (conninfo); |
173 | PQsetdbLogin (pghost, pgport, pgoptions, pgtty, dbName, pguser, pgpasswd); | 206 | gettimeofday (&end_timeval, NULL); |
174 | time (&end_time); | 207 | |
175 | elapsed_time = (int) (end_time - start_time); | 208 | while (start_timeval.tv_usec > end_timeval.tv_usec) { |
209 | --end_timeval.tv_sec; | ||
210 | end_timeval.tv_usec += 1000000; | ||
211 | } | ||
212 | elapsed_time = (double)(end_timeval.tv_sec - start_timeval.tv_sec) | ||
213 | + (double)(end_timeval.tv_usec - start_timeval.tv_usec) / 1000000.0; | ||
214 | |||
176 | if (verbose) | 215 | if (verbose) |
177 | printf("Time elapsed: %d\n", elapsed_time); | 216 | printf("Time elapsed: %f\n", elapsed_time); |
178 | 217 | ||
179 | /* check to see that the backend connection was successfully made */ | 218 | /* check to see that the backend connection was successfully made */ |
180 | if (verbose) | 219 | if (verbose) |
@@ -194,14 +233,32 @@ main (int argc, char **argv) | |||
194 | else { | 233 | else { |
195 | status = STATE_OK; | 234 | status = STATE_OK; |
196 | } | 235 | } |
236 | |||
237 | if (verbose) { | ||
238 | char *server_host = PQhost (conn); | ||
239 | int server_version = PQserverVersion (conn); | ||
240 | |||
241 | printf ("Successfully connected to database %s (user %s) " | ||
242 | "at server %s%s%s (server version: %d.%d.%d, " | ||
243 | "protocol version: %d, pid: %d)\n", | ||
244 | PQdb (conn), PQuser (conn), | ||
245 | PSQL_SOCKET3 (server_host, PQport (conn)), | ||
246 | PSQL_SERVER_VERSION3 (server_version), | ||
247 | PQprotocolVersion (conn), PQbackendPID (conn)); | ||
248 | } | ||
249 | |||
250 | printf (_(" %s - database %s (%f sec.)|%s\n"), | ||
251 | state_text(status), dbName, elapsed_time, | ||
252 | fperfdata("time", elapsed_time, "s", | ||
253 | !!(twarn > 0.0), twarn, !!(tcrit > 0.0), tcrit, TRUE, 0, FALSE,0)); | ||
254 | |||
255 | if (pgquery) | ||
256 | query_status = do_query (conn, pgquery); | ||
257 | |||
197 | if (verbose) | 258 | if (verbose) |
198 | printf("Closing connection\n"); | 259 | printf("Closing connection\n"); |
199 | PQfinish (conn); | 260 | PQfinish (conn); |
200 | printf (_(" %s - database %s (%d sec.)|%s\n"), | 261 | return (query_status > status) ? query_status : status; |
201 | state_text(status), dbName, elapsed_time, | ||
202 | fperfdata("time", elapsed_time, "s", | ||
203 | (int)twarn, twarn, (int)tcrit, tcrit, TRUE, 0, FALSE,0)); | ||
204 | return status; | ||
205 | } | 262 | } |
206 | 263 | ||
207 | 264 | ||
@@ -225,12 +282,16 @@ process_arguments (int argc, char **argv) | |||
225 | {"authorization", required_argument, 0, 'a'}, | 282 | {"authorization", required_argument, 0, 'a'}, |
226 | {"port", required_argument, 0, 'P'}, | 283 | {"port", required_argument, 0, 'P'}, |
227 | {"database", required_argument, 0, 'd'}, | 284 | {"database", required_argument, 0, 'd'}, |
285 | {"option", required_argument, 0, 'o'}, | ||
286 | {"query", required_argument, 0, 'q'}, | ||
287 | {"query_critical", required_argument, 0, 'C'}, | ||
288 | {"query_warning", required_argument, 0, 'W'}, | ||
228 | {"verbose", no_argument, 0, 'v'}, | 289 | {"verbose", no_argument, 0, 'v'}, |
229 | {0, 0, 0, 0} | 290 | {0, 0, 0, 0} |
230 | }; | 291 | }; |
231 | 292 | ||
232 | while (1) { | 293 | while (1) { |
233 | c = getopt_long (argc, argv, "hVt:c:w:H:P:d:l:p:a:v", | 294 | c = getopt_long (argc, argv, "hVt:c:w:H:P:d:l:p:a:o:q:C:W:v", |
234 | longopts, &option); | 295 | longopts, &option); |
235 | 296 | ||
236 | if (c == EOF) | 297 | if (c == EOF) |
@@ -263,8 +324,14 @@ process_arguments (int argc, char **argv) | |||
263 | else | 324 | else |
264 | twarn = strtod (optarg, NULL); | 325 | twarn = strtod (optarg, NULL); |
265 | break; | 326 | break; |
327 | case 'C': /* critical query threshold */ | ||
328 | query_critical = optarg; | ||
329 | break; | ||
330 | case 'W': /* warning query threshold */ | ||
331 | query_warning = optarg; | ||
332 | break; | ||
266 | case 'H': /* host */ | 333 | case 'H': /* host */ |
267 | if (!is_host (optarg)) | 334 | if ((*optarg != '/') && (!is_host (optarg))) |
268 | usage2 (_("Invalid hostname/address"), optarg); | 335 | usage2 (_("Invalid hostname/address"), optarg); |
269 | else | 336 | else |
270 | pghost = optarg; | 337 | pghost = optarg; |
@@ -291,12 +358,23 @@ process_arguments (int argc, char **argv) | |||
291 | case 'a': | 358 | case 'a': |
292 | pgpasswd = optarg; | 359 | pgpasswd = optarg; |
293 | break; | 360 | break; |
361 | case 'o': | ||
362 | if (pgparams) | ||
363 | asprintf (&pgparams, "%s %s", pgparams, optarg); | ||
364 | else | ||
365 | asprintf (&pgparams, "%s", optarg); | ||
366 | break; | ||
367 | case 'q': | ||
368 | pgquery = optarg; | ||
369 | break; | ||
294 | case 'v': | 370 | case 'v': |
295 | verbose++; | 371 | verbose++; |
296 | break; | 372 | break; |
297 | } | 373 | } |
298 | } | 374 | } |
299 | 375 | ||
376 | set_thresholds (&qthresholds, query_warning, query_critical); | ||
377 | |||
300 | return validate_arguments (); | 378 | return validate_arguments (); |
301 | } | 379 | } |
302 | 380 | ||
@@ -434,8 +512,6 @@ print_help (void) | |||
434 | 512 | ||
435 | printf (UT_HOST_PORT, 'P', myport); | 513 | printf (UT_HOST_PORT, 'P', myport); |
436 | 514 | ||
437 | printf (UT_IPv46); | ||
438 | |||
439 | printf (" %s\n", "-d, --database=STRING"); | 515 | printf (" %s\n", "-d, --database=STRING"); |
440 | printf (" %s", _("Database to check ")); | 516 | printf (" %s", _("Database to check ")); |
441 | printf (_("(default: %s)"), DEFAULT_DB); | 517 | printf (_("(default: %s)"), DEFAULT_DB); |
@@ -443,11 +519,20 @@ print_help (void) | |||
443 | printf (" %s\n", _("Login name of user")); | 519 | printf (" %s\n", _("Login name of user")); |
444 | printf (" %s\n", "-p, --password = STRING"); | 520 | printf (" %s\n", "-p, --password = STRING"); |
445 | printf (" %s\n", _("Password (BIG SECURITY ISSUE)")); | 521 | printf (" %s\n", _("Password (BIG SECURITY ISSUE)")); |
522 | printf (" %s\n", "-o, --option = STRING"); | ||
523 | printf (" %s\n", _("Connection parameters (keyword = value), see below")); | ||
446 | 524 | ||
447 | printf (UT_WARN_CRIT); | 525 | printf (UT_WARN_CRIT); |
448 | 526 | ||
449 | printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 527 | printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
450 | 528 | ||
529 | printf (" %s\n", "-q, --query=STRING"); | ||
530 | printf (" %s\n", _("SQL query to run. Only first column in first row will be read")); | ||
531 | printf (" %s\n", "-W, --query-warning=RANGE"); | ||
532 | printf (" %s\n", _("SQL query value to result in warning status (double)")); | ||
533 | printf (" %s\n", "-C, --query-critical=RANGE"); | ||
534 | printf (" %s\n", _("SQL query value to result in critical status (double)")); | ||
535 | |||
451 | printf (UT_VERBOSE); | 536 | printf (UT_VERBOSE); |
452 | 537 | ||
453 | printf ("\n"); | 538 | printf ("\n"); |
@@ -458,6 +543,22 @@ print_help (void) | |||
458 | printf (" %s\n", _("connects to the template1 database, which is present in every functioning")); | 543 | printf (" %s\n", _("connects to the template1 database, which is present in every functioning")); |
459 | printf (" %s\n\n", _("PostgreSQL DBMS.")); | 544 | printf (" %s\n\n", _("PostgreSQL DBMS.")); |
460 | 545 | ||
546 | printf (" %s\n", _("If a query is specified using the -q option, it will be executed after")); | ||
547 | printf (" %s\n", _("connecting to the server. The result from the query has to be numeric.")); | ||
548 | printf (" %s\n", _("Multiple SQL commands, separated by semicolon, are allowed but the result ")); | ||
549 | printf (" %s\n", _("of the last command is taken into account only. The value of the first")); | ||
550 | printf (" %s\n\n", _("column in the first row is used as the check result.")); | ||
551 | |||
552 | printf (" %s\n", _("See the chapter \"Monitoring Database Activity\" of the PostgreSQL manual")); | ||
553 | printf (" %s\n\n", _("for details about how to access internal statistics of the database server.")); | ||
554 | |||
555 | printf (" %s\n", _("For a list of available connection parameters which may be used with the -o")); | ||
556 | printf (" %s\n", _("command line option, see the documentation for PQconnectdb() in the chapter")); | ||
557 | printf (" %s\n", _("\"libpq - C Library\" of the PostgreSQL manual. For example, this may be")); | ||
558 | printf (" %s\n", _("used to specify a service name in pg_service.conf to be used for additional")); | ||
559 | printf (" %s\n", _("connection parameters: -o 'service=<name>' or to specify the SSL mode:")); | ||
560 | printf (" %s\n\n", _("-o 'sslmode=require'.")); | ||
561 | |||
461 | printf (" %s\n", _("The plugin will connect to a local postmaster if no host is specified. To")); | 562 | printf (" %s\n", _("The plugin will connect to a local postmaster if no host is specified. To")); |
462 | printf (" %s\n", _("connect to a remote host, be sure that the remote postmaster accepts TCP/IP")); | 563 | printf (" %s\n", _("connect to a remote host, be sure that the remote postmaster accepts TCP/IP")); |
463 | printf (" %s\n\n", _("connections (start the postmaster with the -i option).")); | 564 | printf (" %s\n\n", _("connections (start the postmaster with the -i option).")); |
@@ -476,5 +577,74 @@ print_usage (void) | |||
476 | { | 577 | { |
477 | printf ("%s\n", _("Usage:")); | 578 | printf ("%s\n", _("Usage:")); |
478 | printf ("%s [-H <host>] [-4|-6] [-P <port>] [-c <critical time>] [-w <warning time>]\n", progname); | 579 | printf ("%s [-H <host>] [-4|-6] [-P <port>] [-c <critical time>] [-w <warning time>]\n", progname); |
479 | printf (" [-t <timeout>] [-d <database>] [-l <logname>] [-p <password>]\n"); | 580 | printf (" [-t <timeout>] [-d <database>] [-l <logname>] [-p <password>]\n" |
581 | "[-q <query>] [-C <critical query range>] [-W <warning query range>]\n"); | ||
480 | } | 582 | } |
583 | |||
584 | int | ||
585 | do_query (PGconn *conn, char *query) | ||
586 | { | ||
587 | PGresult *res; | ||
588 | |||
589 | char *val_str; | ||
590 | double value; | ||
591 | |||
592 | char *endptr = NULL; | ||
593 | |||
594 | int my_status = STATE_UNKNOWN; | ||
595 | |||
596 | if (verbose) | ||
597 | printf ("Executing SQL query \"%s\".\n"); | ||
598 | res = PQexec (conn, query); | ||
599 | |||
600 | if (PGRES_TUPLES_OK != PQresultStatus (res)) { | ||
601 | printf (_("QUERY %s - %s: %s.\n"), _("CRITICAL"), _("Error with query"), | ||
602 | PQerrorMessage (conn)); | ||
603 | return STATE_CRITICAL; | ||
604 | } | ||
605 | |||
606 | if (PQntuples (res) < 1) { | ||
607 | printf ("QUERY %s - %s.\n", _("WARNING"), _("No rows returned")); | ||
608 | return STATE_WARNING; | ||
609 | } | ||
610 | |||
611 | if (PQnfields (res) < 1) { | ||
612 | printf ("QUERY %s - %s.\n", _("WARNING"), _("No columns returned")); | ||
613 | return STATE_WARNING; | ||
614 | } | ||
615 | |||
616 | val_str = PQgetvalue (res, 0, 0); | ||
617 | if (! val_str) { | ||
618 | printf ("QUERY %s - %s.\n", _("CRITICAL"), _("No data returned")); | ||
619 | return STATE_CRITICAL; | ||
620 | } | ||
621 | |||
622 | value = strtod (val_str, &endptr); | ||
623 | if (verbose) | ||
624 | printf ("Query result: %f\n", value); | ||
625 | |||
626 | if (endptr == val_str) { | ||
627 | printf ("QUERY %s - %s: %s\n", _("CRITICAL"), _("Is not a numeric"), val_str); | ||
628 | return STATE_CRITICAL; | ||
629 | } | ||
630 | else if ((endptr != NULL) && (*endptr != '\0')) { | ||
631 | if (verbose) | ||
632 | printf ("Garbage after value: %s.\n", endptr); | ||
633 | } | ||
634 | |||
635 | my_status = get_status (value, qthresholds); | ||
636 | printf ("QUERY %s - ", | ||
637 | (my_status == STATE_OK) | ||
638 | ? _("OK") | ||
639 | : (my_status == STATE_WARNING) | ||
640 | ? _("WARNING") | ||
641 | : (my_status == STATE_CRITICAL) | ||
642 | ? _("CRITICAL") | ||
643 | : _("UNKNOWN")); | ||
644 | printf (_("'%s' returned %f"), query, value); | ||
645 | printf ("|query=%f;%s;%s;;\n", value, | ||
646 | query_warning ? query_warning : "", | ||
647 | query_critical ? query_critical : ""); | ||
648 | return my_status; | ||
649 | } | ||
650 | |||