summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins/check_pgsql.c89
1 files changed, 57 insertions, 32 deletions
diff --git a/plugins/check_pgsql.c b/plugins/check_pgsql.c
index 6613634d..0c0ef415 100644
--- a/plugins/check_pgsql.c
+++ b/plugins/check_pgsql.c
@@ -150,10 +150,12 @@ int main(int argc, char **argv) {
150 /* Parse extra opts if any */ 150 /* Parse extra opts if any */
151 argv = np_extra_opts(&argc, argv, progname); 151 argv = np_extra_opts(&argc, argv, progname);
152 152
153 if (process_arguments(argc, argv) == ERROR) 153 if (process_arguments(argc, argv) == ERROR) {
154 usage4(_("Could not parse arguments")); 154 usage4(_("Could not parse arguments"));
155 if (verbose > 2) 155 }
156 if (verbose > 2) {
156 printf("Arguments initialized\n"); 157 printf("Arguments initialized\n");
158 }
157 159
158 /* Set signal handling and alarm */ 160 /* Set signal handling and alarm */
159 if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { 161 if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) {
@@ -162,25 +164,32 @@ int main(int argc, char **argv) {
162 alarm(timeout_interval); 164 alarm(timeout_interval);
163 165
164 char *conninfo = NULL; 166 char *conninfo = NULL;
165 if (pgparams) 167 if (pgparams) {
166 asprintf(&conninfo, "%s ", pgparams); 168 asprintf(&conninfo, "%s ", pgparams);
169 }
167 170
168 asprintf(&conninfo, "%sdbname = '%s'", conninfo ? conninfo : "", dbName); 171 asprintf(&conninfo, "%sdbname = '%s'", conninfo ? conninfo : "", dbName);
169 if (pghost) 172 if (pghost) {
170 asprintf(&conninfo, "%s host = '%s'", conninfo, pghost); 173 asprintf(&conninfo, "%s host = '%s'", conninfo, pghost);
171 if (pgport) 174 }
175 if (pgport) {
172 asprintf(&conninfo, "%s port = '%s'", conninfo, pgport); 176 asprintf(&conninfo, "%s port = '%s'", conninfo, pgport);
173 if (pgoptions) 177 }
178 if (pgoptions) {
174 asprintf(&conninfo, "%s options = '%s'", conninfo, pgoptions); 179 asprintf(&conninfo, "%s options = '%s'", conninfo, pgoptions);
180 }
175 /* if (pgtty) -- ignored by PQconnectdb */ 181 /* if (pgtty) -- ignored by PQconnectdb */
176 if (pguser) 182 if (pguser) {
177 asprintf(&conninfo, "%s user = '%s'", conninfo, pguser); 183 asprintf(&conninfo, "%s user = '%s'", conninfo, pguser);
184 }
178 185
179 if (verbose) /* do not include password (see right below) in output */ 186 if (verbose) { /* do not include password (see right below) in output */
180 printf("Connecting to PostgreSQL using conninfo: %s%s\n", conninfo, pgpasswd ? " password = <hidden>" : ""); 187 printf("Connecting to PostgreSQL using conninfo: %s%s\n", conninfo, pgpasswd ? " password = <hidden>" : "");
188 }
181 189
182 if (pgpasswd) 190 if (pgpasswd) {
183 asprintf(&conninfo, "%s password = '%s'", conninfo, pgpasswd); 191 asprintf(&conninfo, "%s password = '%s'", conninfo, pgpasswd);
192 }
184 193
185 /* make a connection to the database */ 194 /* make a connection to the database */
186 struct timeval start_timeval; 195 struct timeval start_timeval;
@@ -196,12 +205,14 @@ int main(int argc, char **argv) {
196 double elapsed_time = 205 double elapsed_time =
197 (double)(end_timeval.tv_sec - start_timeval.tv_sec) + (double)(end_timeval.tv_usec - start_timeval.tv_usec) / 1000000.0; 206 (double)(end_timeval.tv_sec - start_timeval.tv_sec) + (double)(end_timeval.tv_usec - start_timeval.tv_usec) / 1000000.0;
198 207
199 if (verbose) 208 if (verbose) {
200 printf("Time elapsed: %f\n", elapsed_time); 209 printf("Time elapsed: %f\n", elapsed_time);
210 }
201 211
202 /* check to see that the backend connection was successfully made */ 212 /* check to see that the backend connection was successfully made */
203 if (verbose) 213 if (verbose) {
204 printf("Verifying connection\n"); 214 printf("Verifying connection\n");
215 }
205 if (PQstatus(conn) == CONNECTION_BAD) { 216 if (PQstatus(conn) == CONNECTION_BAD) {
206 printf(_("CRITICAL - no connection to '%s' (%s).\n"), dbName, PQerrorMessage(conn)); 217 printf(_("CRITICAL - no connection to '%s' (%s).\n"), dbName, PQerrorMessage(conn));
207 PQfinish(conn); 218 PQfinish(conn);
@@ -232,11 +243,13 @@ int main(int argc, char **argv) {
232 fperfdata("time", elapsed_time, "s", !!(twarn > 0.0), twarn, !!(tcrit > 0.0), tcrit, true, 0, false, 0)); 243 fperfdata("time", elapsed_time, "s", !!(twarn > 0.0), twarn, !!(tcrit > 0.0), tcrit, true, 0, false, 0));
233 244
234 int query_status = STATE_UNKNOWN; 245 int query_status = STATE_UNKNOWN;
235 if (pgquery) 246 if (pgquery) {
236 query_status = do_query(conn, pgquery); 247 query_status = do_query(conn, pgquery);
248 }
237 249
238 if (verbose) 250 if (verbose) {
239 printf("Closing connection\n"); 251 printf("Closing connection\n");
252 }
240 PQfinish(conn); 253 PQfinish(conn);
241 return (pgquery && query_status > status) ? query_status : status; 254 return (pgquery && query_status > status) ? query_status : status;
242} 255}
@@ -266,8 +279,9 @@ int process_arguments(int argc, char **argv) {
266 int option = 0; 279 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); 280 int option_char = getopt_long(argc, argv, "hVt:c:w:H:P:d:l:p:a:o:q:C:W:v", longopts, &option);
268 281
269 if (option_char == EOF) 282 if (option_char == EOF) {
270 break; 283 break;
284 }
271 285
272 switch (option_char) { 286 switch (option_char) {
273 case '?': /* usage */ 287 case '?': /* usage */
@@ -279,22 +293,25 @@ int process_arguments(int argc, char **argv) {
279 print_revision(progname, NP_VERSION); 293 print_revision(progname, NP_VERSION);
280 exit(STATE_UNKNOWN); 294 exit(STATE_UNKNOWN);
281 case 't': /* timeout period */ 295 case 't': /* timeout period */
282 if (!is_integer(optarg)) 296 if (!is_integer(optarg)) {
283 usage2(_("Timeout interval must be a positive integer"), optarg); 297 usage2(_("Timeout interval must be a positive integer"), optarg);
284 else 298 } else {
285 timeout_interval = atoi(optarg); 299 timeout_interval = atoi(optarg);
300 }
286 break; 301 break;
287 case 'c': /* critical time threshold */ 302 case 'c': /* critical time threshold */
288 if (!is_nonnegative(optarg)) 303 if (!is_nonnegative(optarg)) {
289 usage2(_("Critical threshold must be a positive integer"), optarg); 304 usage2(_("Critical threshold must be a positive integer"), optarg);
290 else 305 } else {
291 tcrit = strtod(optarg, NULL); 306 tcrit = strtod(optarg, NULL);
307 }
292 break; 308 break;
293 case 'w': /* warning time threshold */ 309 case 'w': /* warning time threshold */
294 if (!is_nonnegative(optarg)) 310 if (!is_nonnegative(optarg)) {
295 usage2(_("Warning threshold must be a positive integer"), optarg); 311 usage2(_("Warning threshold must be a positive integer"), optarg);
296 else 312 } else {
297 twarn = strtod(optarg, NULL); 313 twarn = strtod(optarg, NULL);
314 }
298 break; 315 break;
299 case 'C': /* critical query threshold */ 316 case 'C': /* critical query threshold */
300 query_critical = optarg; 317 query_critical = optarg;
@@ -303,16 +320,18 @@ int process_arguments(int argc, char **argv) {
303 query_warning = optarg; 320 query_warning = optarg;
304 break; 321 break;
305 case 'H': /* host */ 322 case 'H': /* host */
306 if ((*optarg != '/') && (!is_host(optarg))) 323 if ((*optarg != '/') && (!is_host(optarg))) {
307 usage2(_("Invalid hostname/address"), optarg); 324 usage2(_("Invalid hostname/address"), optarg);
308 else 325 } else {
309 pghost = optarg; 326 pghost = optarg;
327 }
310 break; 328 break;
311 case 'P': /* port */ 329 case 'P': /* port */
312 if (!is_integer(optarg)) 330 if (!is_integer(optarg)) {
313 usage2(_("Port must be a positive integer"), optarg); 331 usage2(_("Port must be a positive integer"), optarg);
314 else 332 } else {
315 pgport = optarg; 333 pgport = optarg;
334 }
316 break; 335 break;
317 case 'd': /* database name */ 336 case 'd': /* database name */
318 if (strlen(optarg) >= NAMEDATALEN) { 337 if (strlen(optarg) >= NAMEDATALEN) {
@@ -321,20 +340,22 @@ int process_arguments(int argc, char **argv) {
321 snprintf(dbName, NAMEDATALEN, "%s", optarg); 340 snprintf(dbName, NAMEDATALEN, "%s", optarg);
322 break; 341 break;
323 case 'l': /* login name */ 342 case 'l': /* login name */
324 if (!is_pg_logname(optarg)) 343 if (!is_pg_logname(optarg)) {
325 usage2(_("User name is not valid"), optarg); 344 usage2(_("User name is not valid"), optarg);
326 else 345 } else {
327 pguser = optarg; 346 pguser = optarg;
347 }
328 break; 348 break;
329 case 'p': /* authentication password */ 349 case 'p': /* authentication password */
330 case 'a': 350 case 'a':
331 pgpasswd = optarg; 351 pgpasswd = optarg;
332 break; 352 break;
333 case 'o': 353 case 'o':
334 if (pgparams) 354 if (pgparams) {
335 asprintf(&pgparams, "%s %s", pgparams, optarg); 355 asprintf(&pgparams, "%s %s", pgparams, optarg);
336 else 356 } else {
337 asprintf(&pgparams, "%s", optarg); 357 asprintf(&pgparams, "%s", optarg);
358 }
338 break; 359 break;
339 case 'q': 360 case 'q':
340 pgquery = optarg; 361 pgquery = optarg;
@@ -378,8 +399,9 @@ should be added.</para>
378******************************************************************************/ 399******************************************************************************/
379 400
380bool is_pg_logname(char *username) { 401bool is_pg_logname(char *username) {
381 if (strlen(username) > NAMEDATALEN - 1) 402 if (strlen(username) > NAMEDATALEN - 1) {
382 return (false); 403 return (false);
404 }
383 return (true); 405 return (true);
384} 406}
385 407
@@ -483,8 +505,9 @@ void print_usage(void) {
483} 505}
484 506
485int do_query(PGconn *conn, char *query) { 507int do_query(PGconn *conn, char *query) {
486 if (verbose) 508 if (verbose) {
487 printf("Executing SQL query \"%s\".\n", query); 509 printf("Executing SQL query \"%s\".\n", query);
510 }
488 PGresult *res = PQexec(conn, query); 511 PGresult *res = PQexec(conn, query);
489 512
490 if (PGRES_TUPLES_OK != PQresultStatus(res)) { 513 if (PGRES_TUPLES_OK != PQresultStatus(res)) {
@@ -510,8 +533,9 @@ int do_query(PGconn *conn, char *query) {
510 533
511 char *endptr = NULL; 534 char *endptr = NULL;
512 double value = strtod(val_str, &endptr); 535 double value = strtod(val_str, &endptr);
513 if (verbose) 536 if (verbose) {
514 printf("Query result: %f\n", value); 537 printf("Query result: %f\n", value);
538 }
515 539
516 if (endptr == val_str) { 540 if (endptr == val_str) {
517 printf("QUERY %s - %s: %s\n", _("CRITICAL"), _("Is not a numeric"), val_str); 541 printf("QUERY %s - %s: %s\n", _("CRITICAL"), _("Is not a numeric"), val_str);
@@ -519,8 +543,9 @@ int do_query(PGconn *conn, char *query) {
519 } 543 }
520 544
521 if ((endptr != NULL) && (*endptr != '\0')) { 545 if ((endptr != NULL) && (*endptr != '\0')) {
522 if (verbose) 546 if (verbose) {
523 printf("Garbage after value: %s.\n", endptr); 547 printf("Garbage after value: %s.\n", endptr);
548 }
524 } 549 }
525 550
526 int my_status = get_status(value, qthresholds); 551 int my_status = get_status(value, qthresholds);