diff options
-rw-r--r-- | plugins/check_pgsql.c | 97 |
1 files changed, 32 insertions, 65 deletions
diff --git a/plugins/check_pgsql.c b/plugins/check_pgsql.c index a6411a5..1e35a4c 100644 --- a/plugins/check_pgsql.c +++ b/plugins/check_pgsql.c | |||
@@ -59,12 +59,11 @@ enum { | |||
59 | DEFAULT_CRIT = 8 | 59 | DEFAULT_CRIT = 8 |
60 | }; | 60 | }; |
61 | 61 | ||
62 | static int process_arguments(int, char **); | 62 | static int process_arguments(int /*argc*/, char ** /*argv*/); |
63 | static int validate_arguments(void); | ||
64 | static void print_usage(void); | 63 | static void print_usage(void); |
65 | static void print_help(void); | 64 | static void print_help(void); |
66 | static bool is_pg_logname(char *); | 65 | static bool is_pg_logname(char * /*username*/); |
67 | static int do_query(PGconn *, char *); | 66 | static int do_query(PGconn * /*conn*/, char * /*query*/); |
68 | 67 | ||
69 | static char *pghost = NULL; /* host name of the backend server */ | 68 | static char *pghost = NULL; /* host name of the backend server */ |
70 | static char *pgport = NULL; /* port of the backend server */ | 69 | static char *pgport = NULL; /* port of the backend server */ |
@@ -136,14 +135,9 @@ Please note that all tags must be lowercase to use the DocBook XML DTD. | |||
136 | ******************************************************************************/ | 135 | ******************************************************************************/ |
137 | 136 | ||
138 | int main(int argc, char **argv) { | 137 | int main(int argc, char **argv) { |
139 | PGconn *conn; | 138 | setlocale(LC_ALL, ""); |
140 | char *conninfo = NULL; | 139 | bindtextdomain(PACKAGE, LOCALEDIR); |
141 | 140 | textdomain(PACKAGE); | |
142 | struct timeval start_timeval; | ||
143 | struct timeval end_timeval; | ||
144 | double elapsed_time; | ||
145 | int status = STATE_UNKNOWN; | ||
146 | int query_status = STATE_UNKNOWN; | ||
147 | 141 | ||
148 | /* begin, by setting the parameters for a backend connection if the | 142 | /* begin, by setting the parameters for a backend connection if the |
149 | * parameters are null, then the system will try to use reasonable | 143 | * parameters are null, then the system will try to use reasonable |
@@ -153,10 +147,6 @@ int main(int argc, char **argv) { | |||
153 | pgoptions = NULL; /* special options to start up the backend server */ | 147 | pgoptions = NULL; /* special options to start up the backend server */ |
154 | pgtty = NULL; /* debugging tty for the backend server */ | 148 | pgtty = NULL; /* debugging tty for the backend server */ |
155 | 149 | ||
156 | setlocale(LC_ALL, ""); | ||
157 | bindtextdomain(PACKAGE, LOCALEDIR); | ||
158 | textdomain(PACKAGE); | ||
159 | |||
160 | /* Parse extra opts if any */ | 150 | /* Parse extra opts if any */ |
161 | argv = np_extra_opts(&argc, argv, progname); | 151 | argv = np_extra_opts(&argc, argv, progname); |
162 | 152 | ||
@@ -171,6 +161,7 @@ int main(int argc, char **argv) { | |||
171 | } | 161 | } |
172 | alarm(timeout_interval); | 162 | alarm(timeout_interval); |
173 | 163 | ||
164 | char *conninfo = NULL; | ||
174 | if (pgparams) | 165 | if (pgparams) |
175 | asprintf(&conninfo, "%s ", pgparams); | 166 | asprintf(&conninfo, "%s ", pgparams); |
176 | 167 | ||
@@ -192,15 +183,18 @@ int main(int argc, char **argv) { | |||
192 | asprintf(&conninfo, "%s password = '%s'", conninfo, pgpasswd); | 183 | asprintf(&conninfo, "%s password = '%s'", conninfo, pgpasswd); |
193 | 184 | ||
194 | /* make a connection to the database */ | 185 | /* make a connection to the database */ |
186 | struct timeval start_timeval; | ||
195 | gettimeofday(&start_timeval, NULL); | 187 | gettimeofday(&start_timeval, NULL); |
196 | conn = PQconnectdb(conninfo); | 188 | PGconn *conn = PQconnectdb(conninfo); |
189 | struct timeval end_timeval; | ||
197 | gettimeofday(&end_timeval, NULL); | 190 | gettimeofday(&end_timeval, NULL); |
198 | 191 | ||
199 | while (start_timeval.tv_usec > end_timeval.tv_usec) { | 192 | while (start_timeval.tv_usec > end_timeval.tv_usec) { |
200 | --end_timeval.tv_sec; | 193 | --end_timeval.tv_sec; |
201 | end_timeval.tv_usec += 1000000; | 194 | end_timeval.tv_usec += 1000000; |
202 | } | 195 | } |
203 | elapsed_time = (double)(end_timeval.tv_sec - start_timeval.tv_sec) + (double)(end_timeval.tv_usec - start_timeval.tv_usec) / 1000000.0; | 196 | double elapsed_time = |
197 | (double)(end_timeval.tv_sec - start_timeval.tv_sec) + (double)(end_timeval.tv_usec - start_timeval.tv_usec) / 1000000.0; | ||
204 | 198 | ||
205 | if (verbose) | 199 | if (verbose) |
206 | printf("Time elapsed: %f\n", elapsed_time); | 200 | printf("Time elapsed: %f\n", elapsed_time); |
@@ -212,7 +206,10 @@ int main(int argc, char **argv) { | |||
212 | printf(_("CRITICAL - no connection to '%s' (%s).\n"), dbName, PQerrorMessage(conn)); | 206 | printf(_("CRITICAL - no connection to '%s' (%s).\n"), dbName, PQerrorMessage(conn)); |
213 | PQfinish(conn); | 207 | PQfinish(conn); |
214 | return STATE_CRITICAL; | 208 | return STATE_CRITICAL; |
215 | } else if (elapsed_time > tcrit) { | 209 | } |
210 | |||
211 | int status = STATE_UNKNOWN; | ||
212 | if (elapsed_time > tcrit) { | ||
216 | status = STATE_CRITICAL; | 213 | status = STATE_CRITICAL; |
217 | } else if (elapsed_time > twarn) { | 214 | } else if (elapsed_time > twarn) { |
218 | status = STATE_WARNING; | 215 | status = STATE_WARNING; |
@@ -234,6 +231,7 @@ int main(int argc, char **argv) { | |||
234 | printf(_(" %s - database %s (%f sec.)|%s\n"), state_text(status), dbName, elapsed_time, | 231 | printf(_(" %s - database %s (%f sec.)|%s\n"), state_text(status), dbName, elapsed_time, |
235 | fperfdata("time", elapsed_time, "s", !!(twarn > 0.0), twarn, !!(tcrit > 0.0), tcrit, true, 0, false, 0)); | 232 | fperfdata("time", elapsed_time, "s", !!(twarn > 0.0), twarn, !!(tcrit > 0.0), tcrit, true, 0, false, 0)); |
236 | 233 | ||
234 | int query_status = STATE_UNKNOWN; | ||
237 | if (pgquery) | 235 | if (pgquery) |
238 | query_status = do_query(conn, pgquery); | 236 | query_status = do_query(conn, pgquery); |
239 | 237 | ||
@@ -245,9 +243,6 @@ int main(int argc, char **argv) { | |||
245 | 243 | ||
246 | /* process command-line arguments */ | 244 | /* process command-line arguments */ |
247 | int process_arguments(int argc, char **argv) { | 245 | int process_arguments(int argc, char **argv) { |
248 | int c; | ||
249 | |||
250 | int option = 0; | ||
251 | static struct option longopts[] = {{"help", no_argument, 0, 'h'}, | 246 | static struct option longopts[] = {{"help", no_argument, 0, 'h'}, |
252 | {"version", no_argument, 0, 'V'}, | 247 | {"version", no_argument, 0, 'V'}, |
253 | {"timeout", required_argument, 0, 't'}, | 248 | {"timeout", required_argument, 0, 't'}, |
@@ -267,13 +262,14 @@ int process_arguments(int argc, char **argv) { | |||
267 | {"verbose", no_argument, 0, 'v'}, | 262 | {"verbose", no_argument, 0, 'v'}, |
268 | {0, 0, 0, 0}}; | 263 | {0, 0, 0, 0}}; |
269 | 264 | ||
270 | while (1) { | 265 | while (true) { |
271 | c = getopt_long(argc, argv, "hVt:c:w:H:P:d:l:p:a:o:q:C:W:v", longopts, &option); | 266 | int option = 0; |
267 | int option_char = getopt_long(argc, argv, "hVt:c:w:H:P:d:l:p:a:o:q:C:W:v", longopts, &option); | ||
272 | 268 | ||
273 | if (c == EOF) | 269 | if (option_char == EOF) |
274 | break; | 270 | break; |
275 | 271 | ||
276 | switch (c) { | 272 | switch (option_char) { |
277 | case '?': /* usage */ | 273 | case '?': /* usage */ |
278 | usage5(); | 274 | usage5(); |
279 | case 'h': /* help */ | 275 | case 'h': /* help */ |
@@ -354,31 +350,9 @@ int process_arguments(int argc, char **argv) { | |||
354 | 350 | ||
355 | set_thresholds(&qthresholds, query_warning, query_critical); | 351 | set_thresholds(&qthresholds, query_warning, query_critical); |
356 | 352 | ||
357 | return validate_arguments(); | 353 | return OK; |
358 | } | 354 | } |
359 | 355 | ||
360 | /****************************************************************************** | ||
361 | |||
362 | @@- | ||
363 | <sect3> | ||
364 | <title>validate_arguments</title> | ||
365 | |||
366 | <para>&PROTO_validate_arguments;</para> | ||
367 | |||
368 | <para>Given a database name, this function returns true if the string | ||
369 | is a valid PostgreSQL database name, and returns false if it is | ||
370 | not.</para> | ||
371 | |||
372 | <para>Valid PostgreSQL database names are less than &NAMEDATALEN; | ||
373 | characters long and consist of letters, numbers, and underscores. The | ||
374 | first character cannot be a number, however.</para> | ||
375 | |||
376 | </sect3> | ||
377 | -@@ | ||
378 | ******************************************************************************/ | ||
379 | |||
380 | int validate_arguments() { return OK; } | ||
381 | |||
382 | /** | 356 | /** |
383 | 357 | ||
384 | the tango program should eventually create an entity here based on the | 358 | the tango program should eventually create an entity here based on the |
@@ -509,19 +483,9 @@ void print_usage(void) { | |||
509 | } | 483 | } |
510 | 484 | ||
511 | int do_query(PGconn *conn, char *query) { | 485 | int do_query(PGconn *conn, char *query) { |
512 | PGresult *res; | ||
513 | |||
514 | char *val_str; | ||
515 | char *extra_info; | ||
516 | double value; | ||
517 | |||
518 | char *endptr = NULL; | ||
519 | |||
520 | int my_status = STATE_UNKNOWN; | ||
521 | |||
522 | if (verbose) | 486 | if (verbose) |
523 | printf("Executing SQL query \"%s\".\n", query); | 487 | printf("Executing SQL query \"%s\".\n", query); |
524 | res = PQexec(conn, query); | 488 | PGresult *res = PQexec(conn, query); |
525 | 489 | ||
526 | if (PGRES_TUPLES_OK != PQresultStatus(res)) { | 490 | if (PGRES_TUPLES_OK != PQresultStatus(res)) { |
527 | printf(_("QUERY %s - %s: %s.\n"), _("CRITICAL"), _("Error with query"), PQerrorMessage(conn)); | 491 | printf(_("QUERY %s - %s: %s.\n"), _("CRITICAL"), _("Error with query"), PQerrorMessage(conn)); |
@@ -538,25 +502,28 @@ int do_query(PGconn *conn, char *query) { | |||
538 | return STATE_WARNING; | 502 | return STATE_WARNING; |
539 | } | 503 | } |
540 | 504 | ||
541 | val_str = PQgetvalue(res, 0, 0); | 505 | char *val_str = PQgetvalue(res, 0, 0); |
542 | if (!val_str) { | 506 | if (!val_str) { |
543 | printf("QUERY %s - %s.\n", _("CRITICAL"), _("No data returned")); | 507 | printf("QUERY %s - %s.\n", _("CRITICAL"), _("No data returned")); |
544 | return STATE_CRITICAL; | 508 | return STATE_CRITICAL; |
545 | } | 509 | } |
546 | 510 | ||
547 | value = strtod(val_str, &endptr); | 511 | char *endptr = NULL; |
512 | double value = strtod(val_str, &endptr); | ||
548 | if (verbose) | 513 | if (verbose) |
549 | printf("Query result: %f\n", value); | 514 | printf("Query result: %f\n", value); |
550 | 515 | ||
551 | if (endptr == val_str) { | 516 | if (endptr == val_str) { |
552 | printf("QUERY %s - %s: %s\n", _("CRITICAL"), _("Is not a numeric"), val_str); | 517 | printf("QUERY %s - %s: %s\n", _("CRITICAL"), _("Is not a numeric"), val_str); |
553 | return STATE_CRITICAL; | 518 | return STATE_CRITICAL; |
554 | } else if ((endptr != NULL) && (*endptr != '\0')) { | 519 | } |
520 | |||
521 | if ((endptr != NULL) && (*endptr != '\0')) { | ||
555 | if (verbose) | 522 | if (verbose) |
556 | printf("Garbage after value: %s.\n", endptr); | 523 | printf("Garbage after value: %s.\n", endptr); |
557 | } | 524 | } |
558 | 525 | ||
559 | my_status = get_status(value, qthresholds); | 526 | int my_status = get_status(value, qthresholds); |
560 | printf("QUERY %s - ", (my_status == STATE_OK) ? _("OK") | 527 | printf("QUERY %s - ", (my_status == STATE_OK) ? _("OK") |
561 | : (my_status == STATE_WARNING) ? _("WARNING") | 528 | : (my_status == STATE_WARNING) ? _("WARNING") |
562 | : (my_status == STATE_CRITICAL) ? _("CRITICAL") | 529 | : (my_status == STATE_CRITICAL) ? _("CRITICAL") |
@@ -569,7 +536,7 @@ int do_query(PGconn *conn, char *query) { | |||
569 | 536 | ||
570 | printf("|query=%f;%s;%s;;\n", value, query_warning ? query_warning : "", query_critical ? query_critical : ""); | 537 | printf("|query=%f;%s;%s;;\n", value, query_warning ? query_warning : "", query_critical ? query_critical : ""); |
571 | if (PQnfields(res) > 1) { | 538 | if (PQnfields(res) > 1) { |
572 | extra_info = PQgetvalue(res, 0, 1); | 539 | char *extra_info = PQgetvalue(res, 0, 1); |
573 | if (extra_info != NULL) { | 540 | if (extra_info != NULL) { |
574 | printf("Extra Info: %s\n", extra_info); | 541 | printf("Extra Info: %s\n", extra_info); |
575 | } | 542 | } |