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.c77
1 files changed, 48 insertions, 29 deletions
diff --git a/plugins/check_pgsql.c b/plugins/check_pgsql.c
index 84305adb..793a686f 100644
--- a/plugins/check_pgsql.c
+++ b/plugins/check_pgsql.c
@@ -46,14 +46,15 @@ const char *email = "devel@monitoring-plugins.org";
46#define DEFAULT_HOST "127.0.0.1" 46#define DEFAULT_HOST "127.0.0.1"
47 47
48/* return the PSQL server version as a 3-tuple */ 48/* return the PSQL server version as a 3-tuple */
49#define PSQL_SERVER_VERSION3(server_version) \ 49#define PSQL_SERVER_VERSION3(server_version) \
50 (server_version) / 10000, (server_version) / 100 - (int)((server_version) / 10000) * 100, \ 50 (server_version) / 10000, (server_version) / 100 - (int)((server_version) / 10000) * 100, \
51 (server_version) - (int)((server_version) / 100) * 100 51 (server_version) - (int)((server_version) / 100) * 100
52/* return true if the given host is a UNIX domain socket */ 52/* return true if the given host is a UNIX domain socket */
53#define PSQL_IS_UNIX_DOMAIN_SOCKET(host) ((NULL == (host)) || ('\0' == *(host)) || ('/' == *(host))) 53#define PSQL_IS_UNIX_DOMAIN_SOCKET(host) ((NULL == (host)) || ('\0' == *(host)) || ('/' == *(host)))
54/* return a 3-tuple identifying a host/port independent of the socket type */ 54/* return a 3-tuple identifying a host/port independent of the socket type */
55#define PSQL_SOCKET3(host, port) \ 55#define PSQL_SOCKET3(host, port) \
56 ((NULL == (host)) || ('\0' == *(host))) ? DEFAULT_PGSOCKET_DIR : host, PSQL_IS_UNIX_DOMAIN_SOCKET(host) ? "/.s.PGSQL." : ":", port 56 ((NULL == (host)) || ('\0' == *(host))) ? DEFAULT_PGSOCKET_DIR : host, \
57 PSQL_IS_UNIX_DOMAIN_SOCKET(host) ? "/.s.PGSQL." : ":", port
57 58
58typedef struct { 59typedef struct {
59 int errorcode; 60 int errorcode;
@@ -63,8 +64,9 @@ static check_pgsql_config_wrapper process_arguments(int /*argc*/, char ** /*argv
63 64
64static void print_help(void); 65static void print_help(void);
65static bool is_pg_logname(char * /*username*/); 66static bool is_pg_logname(char * /*username*/);
66static mp_state_enum do_query(PGconn * /*conn*/, char * /*query*/, const char /*pgqueryname*/[], thresholds * /*qthresholds*/, 67static mp_state_enum do_query(PGconn * /*conn*/, char * /*query*/, const char /*pgqueryname*/[],
67 char * /*query_warning*/, char * /*query_critical*/); 68 thresholds * /*qthresholds*/, char * /*query_warning*/,
69 char * /*query_critical*/);
68void print_usage(void); 70void print_usage(void);
69 71
70static int verbose = 0; 72static int verbose = 0;
@@ -167,7 +169,8 @@ int main(int argc, char **argv) {
167 } 169 }
168 170
169 if (verbose) { /* do not include password (see right below) in output */ 171 if (verbose) { /* do not include password (see right below) in output */
170 printf("Connecting to PostgreSQL using conninfo: %s%s\n", conninfo, config.pgpasswd ? " password = <hidden>" : ""); 172 printf("Connecting to PostgreSQL using conninfo: %s%s\n", conninfo,
173 config.pgpasswd ? " password = <hidden>" : "");
171 } 174 }
172 175
173 if (config.pgpasswd) { 176 if (config.pgpasswd) {
@@ -185,8 +188,8 @@ int main(int argc, char **argv) {
185 --end_timeval.tv_sec; 188 --end_timeval.tv_sec;
186 end_timeval.tv_usec += 1000000; 189 end_timeval.tv_usec += 1000000;
187 } 190 }
188 double elapsed_time = 191 double elapsed_time = (double)(end_timeval.tv_sec - start_timeval.tv_sec) +
189 (double)(end_timeval.tv_sec - start_timeval.tv_sec) + ((double)(end_timeval.tv_usec - start_timeval.tv_usec) / 1000000.0); 192 ((double)(end_timeval.tv_usec - start_timeval.tv_usec) / 1000000.0);
190 193
191 if (verbose) { 194 if (verbose) {
192 printf("Time elapsed: %f\n", elapsed_time); 195 printf("Time elapsed: %f\n", elapsed_time);
@@ -218,16 +221,18 @@ int main(int argc, char **argv) {
218 printf("Successfully connected to database %s (user %s) " 221 printf("Successfully connected to database %s (user %s) "
219 "at server %s%s%s (server version: %d.%d.%d, " 222 "at server %s%s%s (server version: %d.%d.%d, "
220 "protocol version: %d, pid: %d)\n", 223 "protocol version: %d, pid: %d)\n",
221 PQdb(conn), PQuser(conn), PSQL_SOCKET3(server_host, PQport(conn)), PSQL_SERVER_VERSION3(server_version), 224 PQdb(conn), PQuser(conn), PSQL_SOCKET3(server_host, PQport(conn)),
222 PQprotocolVersion(conn), PQbackendPID(conn)); 225 PSQL_SERVER_VERSION3(server_version), PQprotocolVersion(conn), PQbackendPID(conn));
223 } 226 }
224 227
225 printf(_(" %s - database %s (%f sec.)|%s\n"), state_text(status), config.dbName, elapsed_time, 228 printf(_(" %s - database %s (%f sec.)|%s\n"), state_text(status), config.dbName, elapsed_time,
226 fperfdata("time", elapsed_time, "s", (config.twarn > 0.0), config.twarn, (config.tcrit > 0.0), config.tcrit, true, 0, false, 0)); 229 fperfdata("time", elapsed_time, "s", (config.twarn > 0.0), config.twarn,
230 (config.tcrit > 0.0), config.tcrit, true, 0, false, 0));
227 231
228 mp_state_enum query_status = STATE_UNKNOWN; 232 mp_state_enum query_status = STATE_UNKNOWN;
229 if (config.pgquery) { 233 if (config.pgquery) {
230 query_status = do_query(conn, config.pgquery, config.pgqueryname, config.qthresholds, config.query_warning, config.query_critical); 234 query_status = do_query(conn, config.pgquery, config.pgqueryname, config.qthresholds,
235 config.query_warning, config.query_critical);
231 } 236 }
232 237
233 if (verbose) { 238 if (verbose) {
@@ -265,7 +270,8 @@ check_pgsql_config_wrapper process_arguments(int argc, char **argv) {
265 270
266 while (true) { 271 while (true) {
267 int option = 0; 272 int option = 0;
268 int option_char = getopt_long(argc, argv, "hVt:c:w:H:P:d:l:p:a:o:q:C:W:v", longopts, &option); 273 int option_char =
274 getopt_long(argc, argv, "hVt:c:w:H:P:d:l:p:a:o:q:C:W:v", longopts, &option);
269 275
270 if (option_char == EOF) { 276 if (option_char == EOF) {
271 break; 277 break;
@@ -357,7 +363,8 @@ check_pgsql_config_wrapper process_arguments(int argc, char **argv) {
357 } 363 }
358 } 364 }
359 365
360 set_thresholds(&result.config.qthresholds, result.config.query_warning, result.config.query_critical); 366 set_thresholds(&result.config.qthresholds, result.config.query_warning,
367 result.config.query_critical);
361 368
362 return result; 369 return result;
363} 370}
@@ -457,29 +464,39 @@ void print_help(void) {
457 464
458 printf(" %s\n", _("If a query is specified using the -q option, it will be executed after")); 465 printf(" %s\n", _("If a query is specified using the -q option, it will be executed after"));
459 printf(" %s\n", _("connecting to the server. The result from the query has to be numeric.")); 466 printf(" %s\n", _("connecting to the server. The result from the query has to be numeric."));
460 printf(" %s\n", _("Multiple SQL commands, separated by semicolon, are allowed but the result ")); 467 printf(" %s\n",
468 _("Multiple SQL commands, separated by semicolon, are allowed but the result "));
461 printf(" %s\n", _("of the last command is taken into account only. The value of the first")); 469 printf(" %s\n", _("of the last command is taken into account only. The value of the first"));
462 printf(" %s\n", _("column in the first row is used as the check result. If a second column is")); 470 printf(" %s\n",
471 _("column in the first row is used as the check result. If a second column is"));
463 printf(" %s\n", _("present in the result set, this is added to the plugin output with a")); 472 printf(" %s\n", _("present in the result set, this is added to the plugin output with a"));
464 printf(" %s\n", _("prefix of \"Extra Info:\". This information can be displayed in the system")); 473 printf(" %s\n",
474 _("prefix of \"Extra Info:\". This information can be displayed in the system"));
465 printf(" %s\n\n", _("executing the plugin.")); 475 printf(" %s\n\n", _("executing the plugin."));
466 476
467 printf(" %s\n", _("See the chapter \"Monitoring Database Activity\" of the PostgreSQL manual")); 477 printf(" %s\n", _("See the chapter \"Monitoring Database Activity\" of the PostgreSQL manual"));
468 printf(" %s\n\n", _("for details about how to access internal statistics of the database server.")); 478 printf(" %s\n\n",
479 _("for details about how to access internal statistics of the database server."));
469 480
470 printf(" %s\n", _("For a list of available connection parameters which may be used with the -o")); 481 printf(" %s\n",
471 printf(" %s\n", _("command line option, see the documentation for PQconnectdb() in the chapter")); 482 _("For a list of available connection parameters which may be used with the -o"));
483 printf(" %s\n",
484 _("command line option, see the documentation for PQconnectdb() in the chapter"));
472 printf(" %s\n", _("\"libpq - C Library\" of the PostgreSQL manual. For example, this may be")); 485 printf(" %s\n", _("\"libpq - C Library\" of the PostgreSQL manual. For example, this may be"));
473 printf(" %s\n", _("used to specify a service name in pg_service.conf to be used for additional")); 486 printf(" %s\n",
487 _("used to specify a service name in pg_service.conf to be used for additional"));
474 printf(" %s\n", _("connection parameters: -o 'service=<name>' or to specify the SSL mode:")); 488 printf(" %s\n", _("connection parameters: -o 'service=<name>' or to specify the SSL mode:"));
475 printf(" %s\n\n", _("-o 'sslmode=require'.")); 489 printf(" %s\n\n", _("-o 'sslmode=require'."));
476 490
477 printf(" %s\n", _("The plugin will connect to a local postmaster if no host is specified. To")); 491 printf(" %s\n", _("The plugin will connect to a local postmaster if no host is specified. To"));
478 printf(" %s\n", _("connect to a remote host, be sure that the remote postmaster accepts TCP/IP")); 492 printf(" %s\n",
493 _("connect to a remote host, be sure that the remote postmaster accepts TCP/IP"));
479 printf(" %s\n\n", _("connections (start the postmaster with the -i option).")); 494 printf(" %s\n\n", _("connections (start the postmaster with the -i option)."));
480 495
481 printf(" %s\n", _("Typically, the monitoring user (unless the --logname option is used) should be")); 496 printf(" %s\n",
482 printf(" %s\n", _("able to connect to the database without a password. The plugin can also send")); 497 _("Typically, the monitoring user (unless the --logname option is used) should be"));
498 printf(" %s\n",
499 _("able to connect to the database without a password. The plugin can also send"));
483 printf(" %s\n", _("a password, but no effort is made to obscure or encrypt the password.")); 500 printf(" %s\n", _("a password, but no effort is made to obscure or encrypt the password."));
484 501
485 printf(UT_SUPPORT); 502 printf(UT_SUPPORT);
@@ -492,15 +509,16 @@ void print_usage(void) {
492 "[-q <query>] [-C <critical query range>] [-W <warning query range>]\n"); 509 "[-q <query>] [-C <critical query range>] [-W <warning query range>]\n");
493} 510}
494 511
495mp_state_enum do_query(PGconn *conn, char *query, const char pgqueryname[], thresholds *qthresholds, char *query_warning, 512mp_state_enum do_query(PGconn *conn, char *query, const char pgqueryname[], thresholds *qthresholds,
496 char *query_critical) { 513 char *query_warning, char *query_critical) {
497 if (verbose) { 514 if (verbose) {
498 printf("Executing SQL query \"%s\".\n", query); 515 printf("Executing SQL query \"%s\".\n", query);
499 } 516 }
500 PGresult *res = PQexec(conn, query); 517 PGresult *res = PQexec(conn, query);
501 518
502 if (PGRES_TUPLES_OK != PQresultStatus(res)) { 519 if (PGRES_TUPLES_OK != PQresultStatus(res)) {
503 printf(_("QUERY %s - %s: %s.\n"), _("CRITICAL"), _("Error with query"), PQerrorMessage(conn)); 520 printf(_("QUERY %s - %s: %s.\n"), _("CRITICAL"), _("Error with query"),
521 PQerrorMessage(conn));
504 return STATE_CRITICAL; 522 return STATE_CRITICAL;
505 } 523 }
506 524
@@ -548,7 +566,8 @@ mp_state_enum do_query(PGconn *conn, char *query, const char pgqueryname[], thre
548 printf(_("'%s' returned %f"), query, value); 566 printf(_("'%s' returned %f"), query, value);
549 } 567 }
550 568
551 printf("|query=%f;%s;%s;;\n", value, query_warning ? query_warning : "", query_critical ? query_critical : ""); 569 printf("|query=%f;%s;%s;;\n", value, query_warning ? query_warning : "",
570 query_critical ? query_critical : "");
552 if (PQnfields(res) > 1) { 571 if (PQnfields(res) > 1) {
553 char *extra_info = PQgetvalue(res, 0, 1); 572 char *extra_info = PQgetvalue(res, 0, 1);
554 if (extra_info != NULL) { 573 if (extra_info != NULL) {