diff options
| -rw-r--r-- | plugins/Makefile.am | 1 | ||||
| -rw-r--r-- | plugins/check_pgsql.c | 216 | ||||
| -rw-r--r-- | plugins/check_pgsql.d/config.h | 61 |
3 files changed, 177 insertions, 101 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 097763fc..a6c0ba65 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am | |||
| @@ -62,6 +62,7 @@ EXTRA_DIST = t \ | |||
| 62 | check_mysql_query.d \ | 62 | check_mysql_query.d \ |
| 63 | check_mrtg.d \ | 63 | check_mrtg.d \ |
| 64 | check_apt.d \ | 64 | check_apt.d \ |
| 65 | check_pgsql.d \ | ||
| 65 | check_by_ssh.d \ | 66 | check_by_ssh.d \ |
| 66 | check_smtp.d \ | 67 | check_smtp.d \ |
| 67 | check_mysql.d \ | 68 | check_mysql.d \ |
diff --git a/plugins/check_pgsql.c b/plugins/check_pgsql.c index 6613634d..84305adb 100644 --- a/plugins/check_pgsql.c +++ b/plugins/check_pgsql.c | |||
| @@ -28,6 +28,7 @@ | |||
| 28 | * | 28 | * |
| 29 | *****************************************************************************/ | 29 | *****************************************************************************/ |
| 30 | 30 | ||
| 31 | #include "states.h" | ||
| 31 | const char *progname = "check_pgsql"; | 32 | const char *progname = "check_pgsql"; |
| 32 | const char *copyright = "1999-2024"; | 33 | const char *copyright = "1999-2024"; |
| 33 | const char *email = "devel@monitoring-plugins.org"; | 34 | const char *email = "devel@monitoring-plugins.org"; |
| @@ -35,12 +36,13 @@ const char *email = "devel@monitoring-plugins.org"; | |||
| 35 | #include "common.h" | 36 | #include "common.h" |
| 36 | #include "utils.h" | 37 | #include "utils.h" |
| 37 | #include "utils_cmd.h" | 38 | #include "utils_cmd.h" |
| 39 | #include "check_pgsql.d/config.h" | ||
| 40 | #include "thresholds.h" | ||
| 38 | 41 | ||
| 39 | #include "netutils.h" | 42 | #include "netutils.h" |
| 40 | #include <libpq-fe.h> | 43 | #include <libpq-fe.h> |
| 41 | #include <pg_config_manual.h> | 44 | #include <pg_config_manual.h> |
| 42 | 45 | ||
| 43 | #define DEFAULT_DB "template1" | ||
| 44 | #define DEFAULT_HOST "127.0.0.1" | 46 | #define DEFAULT_HOST "127.0.0.1" |
| 45 | 47 | ||
| 46 | /* return the PSQL server version as a 3-tuple */ | 48 | /* return the PSQL server version as a 3-tuple */ |
| @@ -53,33 +55,18 @@ const char *email = "devel@monitoring-plugins.org"; | |||
| 53 | #define PSQL_SOCKET3(host, port) \ | 55 | #define PSQL_SOCKET3(host, port) \ |
| 54 | ((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, PSQL_IS_UNIX_DOMAIN_SOCKET(host) ? "/.s.PGSQL." : ":", port |
| 55 | 57 | ||
| 56 | enum { | 58 | typedef struct { |
| 57 | DEFAULT_PORT = 5432, | 59 | int errorcode; |
| 58 | DEFAULT_WARN = 2, | 60 | check_pgsql_config config; |
| 59 | DEFAULT_CRIT = 8 | 61 | } check_pgsql_config_wrapper; |
| 60 | }; | 62 | static check_pgsql_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); |
| 61 | 63 | ||
| 62 | static int process_arguments(int /*argc*/, char ** /*argv*/); | ||
| 63 | static void print_help(void); | 64 | static void print_help(void); |
| 64 | static bool is_pg_logname(char * /*username*/); | 65 | static bool is_pg_logname(char * /*username*/); |
| 65 | static int do_query(PGconn * /*conn*/, char * /*query*/); | 66 | static mp_state_enum do_query(PGconn * /*conn*/, char * /*query*/, const char /*pgqueryname*/[], thresholds * /*qthresholds*/, |
| 67 | char * /*query_warning*/, char * /*query_critical*/); | ||
| 66 | void print_usage(void); | 68 | void print_usage(void); |
| 67 | 69 | ||
| 68 | static char *pghost = NULL; /* host name of the backend server */ | ||
| 69 | static char *pgport = NULL; /* port of the backend server */ | ||
| 70 | static char *pgoptions = NULL; | ||
| 71 | static char *pgtty = NULL; | ||
| 72 | static char dbName[NAMEDATALEN] = DEFAULT_DB; | ||
| 73 | static char *pguser = NULL; | ||
| 74 | static char *pgpasswd = NULL; | ||
| 75 | static char *pgparams = NULL; | ||
| 76 | static double twarn = (double)DEFAULT_WARN; | ||
| 77 | static double tcrit = (double)DEFAULT_CRIT; | ||
| 78 | static char *pgquery = NULL; | ||
| 79 | static char *pgqueryname = NULL; | ||
| 80 | static char *query_warning = NULL; | ||
| 81 | static char *query_critical = NULL; | ||
| 82 | static thresholds *qthresholds = NULL; | ||
| 83 | static int verbose = 0; | 70 | static int verbose = 0; |
| 84 | 71 | ||
| 85 | #define OPTID_QUERYNAME -1000 | 72 | #define OPTID_QUERYNAME -1000 |
| @@ -139,21 +126,19 @@ int main(int argc, char **argv) { | |||
| 139 | bindtextdomain(PACKAGE, LOCALEDIR); | 126 | bindtextdomain(PACKAGE, LOCALEDIR); |
| 140 | textdomain(PACKAGE); | 127 | textdomain(PACKAGE); |
| 141 | 128 | ||
| 142 | /* begin, by setting the parameters for a backend connection if the | ||
| 143 | * parameters are null, then the system will try to use reasonable | ||
| 144 | * defaults by looking up environment variables or, failing that, | ||
| 145 | * using hardwired constants */ | ||
| 146 | |||
| 147 | pgoptions = NULL; /* special options to start up the backend server */ | ||
| 148 | pgtty = NULL; /* debugging tty for the backend server */ | ||
| 149 | |||
| 150 | /* Parse extra opts if any */ | 129 | /* Parse extra opts if any */ |
| 151 | argv = np_extra_opts(&argc, argv, progname); | 130 | argv = np_extra_opts(&argc, argv, progname); |
| 152 | 131 | ||
| 153 | if (process_arguments(argc, argv) == ERROR) | 132 | check_pgsql_config_wrapper tmp_config = process_arguments(argc, argv); |
| 133 | if (tmp_config.errorcode == ERROR) { | ||
| 154 | usage4(_("Could not parse arguments")); | 134 | usage4(_("Could not parse arguments")); |
| 155 | if (verbose > 2) | 135 | } |
| 136 | |||
| 137 | const check_pgsql_config config = tmp_config.config; | ||
| 138 | |||
| 139 | if (verbose > 2) { | ||
| 156 | printf("Arguments initialized\n"); | 140 | printf("Arguments initialized\n"); |
| 141 | } | ||
| 157 | 142 | ||
| 158 | /* Set signal handling and alarm */ | 143 | /* Set signal handling and alarm */ |
| 159 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { | 144 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { |
| @@ -162,25 +147,32 @@ int main(int argc, char **argv) { | |||
| 162 | alarm(timeout_interval); | 147 | alarm(timeout_interval); |
| 163 | 148 | ||
| 164 | char *conninfo = NULL; | 149 | char *conninfo = NULL; |
| 165 | if (pgparams) | 150 | if (config.pgparams) { |
| 166 | asprintf(&conninfo, "%s ", pgparams); | 151 | asprintf(&conninfo, "%s ", config.pgparams); |
| 167 | 152 | } | |
| 168 | asprintf(&conninfo, "%sdbname = '%s'", conninfo ? conninfo : "", dbName); | 153 | |
| 169 | if (pghost) | 154 | asprintf(&conninfo, "%sdbname = '%s'", conninfo ? conninfo : "", config.dbName); |
| 170 | asprintf(&conninfo, "%s host = '%s'", conninfo, pghost); | 155 | if (config.pghost) { |
| 171 | if (pgport) | 156 | asprintf(&conninfo, "%s host = '%s'", conninfo, config.pghost); |
| 172 | asprintf(&conninfo, "%s port = '%s'", conninfo, pgport); | 157 | } |
| 173 | if (pgoptions) | 158 | if (config.pgport) { |
| 174 | asprintf(&conninfo, "%s options = '%s'", conninfo, pgoptions); | 159 | asprintf(&conninfo, "%s port = '%s'", conninfo, config.pgport); |
| 160 | } | ||
| 161 | if (config.pgoptions) { | ||
| 162 | asprintf(&conninfo, "%s options = '%s'", conninfo, config.pgoptions); | ||
| 163 | } | ||
| 175 | /* if (pgtty) -- ignored by PQconnectdb */ | 164 | /* if (pgtty) -- ignored by PQconnectdb */ |
| 176 | if (pguser) | 165 | if (config.pguser) { |
| 177 | asprintf(&conninfo, "%s user = '%s'", conninfo, pguser); | 166 | asprintf(&conninfo, "%s user = '%s'", conninfo, config.pguser); |
| 167 | } | ||
| 178 | 168 | ||
| 179 | if (verbose) /* do not include password (see right below) in output */ | 169 | 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>" : ""); | 170 | printf("Connecting to PostgreSQL using conninfo: %s%s\n", conninfo, config.pgpasswd ? " password = <hidden>" : ""); |
| 171 | } | ||
| 181 | 172 | ||
| 182 | if (pgpasswd) | 173 | if (config.pgpasswd) { |
| 183 | asprintf(&conninfo, "%s password = '%s'", conninfo, pgpasswd); | 174 | asprintf(&conninfo, "%s password = '%s'", conninfo, config.pgpasswd); |
| 175 | } | ||
| 184 | 176 | ||
| 185 | /* make a connection to the database */ | 177 | /* make a connection to the database */ |
| 186 | struct timeval start_timeval; | 178 | struct timeval start_timeval; |
| @@ -194,24 +186,26 @@ int main(int argc, char **argv) { | |||
| 194 | end_timeval.tv_usec += 1000000; | 186 | end_timeval.tv_usec += 1000000; |
| 195 | } | 187 | } |
| 196 | double elapsed_time = | 188 | double elapsed_time = |
| 197 | (double)(end_timeval.tv_sec - start_timeval.tv_sec) + (double)(end_timeval.tv_usec - start_timeval.tv_usec) / 1000000.0; | 189 | (double)(end_timeval.tv_sec - start_timeval.tv_sec) + ((double)(end_timeval.tv_usec - start_timeval.tv_usec) / 1000000.0); |
| 198 | 190 | ||
| 199 | if (verbose) | 191 | if (verbose) { |
| 200 | printf("Time elapsed: %f\n", elapsed_time); | 192 | printf("Time elapsed: %f\n", elapsed_time); |
| 193 | } | ||
| 201 | 194 | ||
| 202 | /* check to see that the backend connection was successfully made */ | 195 | /* check to see that the backend connection was successfully made */ |
| 203 | if (verbose) | 196 | if (verbose) { |
| 204 | printf("Verifying connection\n"); | 197 | printf("Verifying connection\n"); |
| 198 | } | ||
| 205 | if (PQstatus(conn) == CONNECTION_BAD) { | 199 | if (PQstatus(conn) == CONNECTION_BAD) { |
| 206 | printf(_("CRITICAL - no connection to '%s' (%s).\n"), dbName, PQerrorMessage(conn)); | 200 | printf(_("CRITICAL - no connection to '%s' (%s).\n"), config.dbName, PQerrorMessage(conn)); |
| 207 | PQfinish(conn); | 201 | PQfinish(conn); |
| 208 | return STATE_CRITICAL; | 202 | return STATE_CRITICAL; |
| 209 | } | 203 | } |
| 210 | 204 | ||
| 211 | int status = STATE_UNKNOWN; | 205 | mp_state_enum status = STATE_UNKNOWN; |
| 212 | if (elapsed_time > tcrit) { | 206 | if (elapsed_time > config.tcrit) { |
| 213 | status = STATE_CRITICAL; | 207 | status = STATE_CRITICAL; |
| 214 | } else if (elapsed_time > twarn) { | 208 | } else if (elapsed_time > config.twarn) { |
| 215 | status = STATE_WARNING; | 209 | status = STATE_WARNING; |
| 216 | } else { | 210 | } else { |
| 217 | status = STATE_OK; | 211 | status = STATE_OK; |
| @@ -228,21 +222,23 @@ int main(int argc, char **argv) { | |||
| 228 | PQprotocolVersion(conn), PQbackendPID(conn)); | 222 | PQprotocolVersion(conn), PQbackendPID(conn)); |
| 229 | } | 223 | } |
| 230 | 224 | ||
| 231 | printf(_(" %s - database %s (%f sec.)|%s\n"), state_text(status), dbName, elapsed_time, | 225 | printf(_(" %s - database %s (%f sec.)|%s\n"), state_text(status), config.dbName, elapsed_time, |
| 232 | fperfdata("time", elapsed_time, "s", !!(twarn > 0.0), twarn, !!(tcrit > 0.0), tcrit, true, 0, false, 0)); | 226 | fperfdata("time", elapsed_time, "s", (config.twarn > 0.0), config.twarn, (config.tcrit > 0.0), config.tcrit, true, 0, false, 0)); |
| 233 | 227 | ||
| 234 | int query_status = STATE_UNKNOWN; | 228 | mp_state_enum query_status = STATE_UNKNOWN; |
| 235 | if (pgquery) | 229 | if (config.pgquery) { |
| 236 | query_status = do_query(conn, pgquery); | 230 | query_status = do_query(conn, config.pgquery, config.pgqueryname, config.qthresholds, config.query_warning, config.query_critical); |
| 231 | } | ||
| 237 | 232 | ||
| 238 | if (verbose) | 233 | if (verbose) { |
| 239 | printf("Closing connection\n"); | 234 | printf("Closing connection\n"); |
| 235 | } | ||
| 240 | PQfinish(conn); | 236 | PQfinish(conn); |
| 241 | return (pgquery && query_status > status) ? query_status : status; | 237 | return (config.pgquery && query_status > status) ? query_status : status; |
| 242 | } | 238 | } |
| 243 | 239 | ||
| 244 | /* process command-line arguments */ | 240 | /* process command-line arguments */ |
| 245 | int process_arguments(int argc, char **argv) { | 241 | check_pgsql_config_wrapper process_arguments(int argc, char **argv) { |
| 246 | static struct option longopts[] = {{"help", no_argument, 0, 'h'}, | 242 | static struct option longopts[] = {{"help", no_argument, 0, 'h'}, |
| 247 | {"version", no_argument, 0, 'V'}, | 243 | {"version", no_argument, 0, 'V'}, |
| 248 | {"timeout", required_argument, 0, 't'}, | 244 | {"timeout", required_argument, 0, 't'}, |
| @@ -262,12 +258,18 @@ int process_arguments(int argc, char **argv) { | |||
| 262 | {"verbose", no_argument, 0, 'v'}, | 258 | {"verbose", no_argument, 0, 'v'}, |
| 263 | {0, 0, 0, 0}}; | 259 | {0, 0, 0, 0}}; |
| 264 | 260 | ||
| 261 | check_pgsql_config_wrapper result = { | ||
| 262 | .errorcode = OK, | ||
| 263 | .config = check_pgsql_config_init(), | ||
| 264 | }; | ||
| 265 | |||
| 265 | while (true) { | 266 | while (true) { |
| 266 | int option = 0; | 267 | 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); | 268 | int option_char = getopt_long(argc, argv, "hVt:c:w:H:P:d:l:p:a:o:q:C:W:v", longopts, &option); |
| 268 | 269 | ||
| 269 | if (option_char == EOF) | 270 | if (option_char == EOF) { |
| 270 | break; | 271 | break; |
| 272 | } | ||
| 271 | 273 | ||
| 272 | switch (option_char) { | 274 | switch (option_char) { |
| 273 | case '?': /* usage */ | 275 | case '?': /* usage */ |
| @@ -279,68 +281,75 @@ int process_arguments(int argc, char **argv) { | |||
| 279 | print_revision(progname, NP_VERSION); | 281 | print_revision(progname, NP_VERSION); |
| 280 | exit(STATE_UNKNOWN); | 282 | exit(STATE_UNKNOWN); |
| 281 | case 't': /* timeout period */ | 283 | case 't': /* timeout period */ |
| 282 | if (!is_integer(optarg)) | 284 | if (!is_integer(optarg)) { |
| 283 | usage2(_("Timeout interval must be a positive integer"), optarg); | 285 | usage2(_("Timeout interval must be a positive integer"), optarg); |
| 284 | else | 286 | } else { |
| 285 | timeout_interval = atoi(optarg); | 287 | timeout_interval = atoi(optarg); |
| 288 | } | ||
| 286 | break; | 289 | break; |
| 287 | case 'c': /* critical time threshold */ | 290 | case 'c': /* critical time threshold */ |
| 288 | if (!is_nonnegative(optarg)) | 291 | if (!is_nonnegative(optarg)) { |
| 289 | usage2(_("Critical threshold must be a positive integer"), optarg); | 292 | usage2(_("Critical threshold must be a positive integer"), optarg); |
| 290 | else | 293 | } else { |
| 291 | tcrit = strtod(optarg, NULL); | 294 | result.config.tcrit = strtod(optarg, NULL); |
| 295 | } | ||
| 292 | break; | 296 | break; |
| 293 | case 'w': /* warning time threshold */ | 297 | case 'w': /* warning time threshold */ |
| 294 | if (!is_nonnegative(optarg)) | 298 | if (!is_nonnegative(optarg)) { |
| 295 | usage2(_("Warning threshold must be a positive integer"), optarg); | 299 | usage2(_("Warning threshold must be a positive integer"), optarg); |
| 296 | else | 300 | } else { |
| 297 | twarn = strtod(optarg, NULL); | 301 | result.config.twarn = strtod(optarg, NULL); |
| 302 | } | ||
| 298 | break; | 303 | break; |
| 299 | case 'C': /* critical query threshold */ | 304 | case 'C': /* critical query threshold */ |
| 300 | query_critical = optarg; | 305 | result.config.query_critical = optarg; |
| 301 | break; | 306 | break; |
| 302 | case 'W': /* warning query threshold */ | 307 | case 'W': /* warning query threshold */ |
| 303 | query_warning = optarg; | 308 | result.config.query_warning = optarg; |
| 304 | break; | 309 | break; |
| 305 | case 'H': /* host */ | 310 | case 'H': /* host */ |
| 306 | if ((*optarg != '/') && (!is_host(optarg))) | 311 | if ((*optarg != '/') && (!is_host(optarg))) { |
| 307 | usage2(_("Invalid hostname/address"), optarg); | 312 | usage2(_("Invalid hostname/address"), optarg); |
| 308 | else | 313 | } else { |
| 309 | pghost = optarg; | 314 | result.config.pghost = optarg; |
| 315 | } | ||
| 310 | break; | 316 | break; |
| 311 | case 'P': /* port */ | 317 | case 'P': /* port */ |
| 312 | if (!is_integer(optarg)) | 318 | if (!is_integer(optarg)) { |
| 313 | usage2(_("Port must be a positive integer"), optarg); | 319 | usage2(_("Port must be a positive integer"), optarg); |
| 314 | else | 320 | } else { |
| 315 | pgport = optarg; | 321 | result.config.pgport = optarg; |
| 322 | } | ||
| 316 | break; | 323 | break; |
| 317 | case 'd': /* database name */ | 324 | case 'd': /* database name */ |
| 318 | if (strlen(optarg) >= NAMEDATALEN) { | 325 | if (strlen(optarg) >= NAMEDATALEN) { |
| 319 | usage2(_("Database name exceeds the maximum length"), optarg); | 326 | usage2(_("Database name exceeds the maximum length"), optarg); |
| 320 | } | 327 | } |
| 321 | snprintf(dbName, NAMEDATALEN, "%s", optarg); | 328 | snprintf(result.config.dbName, NAMEDATALEN, "%s", optarg); |
| 322 | break; | 329 | break; |
| 323 | case 'l': /* login name */ | 330 | case 'l': /* login name */ |
| 324 | if (!is_pg_logname(optarg)) | 331 | if (!is_pg_logname(optarg)) { |
| 325 | usage2(_("User name is not valid"), optarg); | 332 | usage2(_("User name is not valid"), optarg); |
| 326 | else | 333 | } else { |
| 327 | pguser = optarg; | 334 | result.config.pguser = optarg; |
| 335 | } | ||
| 328 | break; | 336 | break; |
| 329 | case 'p': /* authentication password */ | 337 | case 'p': /* authentication password */ |
| 330 | case 'a': | 338 | case 'a': |
| 331 | pgpasswd = optarg; | 339 | result.config.pgpasswd = optarg; |
| 332 | break; | 340 | break; |
| 333 | case 'o': | 341 | case 'o': |
| 334 | if (pgparams) | 342 | if (result.config.pgparams) { |
| 335 | asprintf(&pgparams, "%s %s", pgparams, optarg); | 343 | asprintf(&result.config.pgparams, "%s %s", result.config.pgparams, optarg); |
| 336 | else | 344 | } else { |
| 337 | asprintf(&pgparams, "%s", optarg); | 345 | asprintf(&result.config.pgparams, "%s", optarg); |
| 346 | } | ||
| 338 | break; | 347 | break; |
| 339 | case 'q': | 348 | case 'q': |
| 340 | pgquery = optarg; | 349 | result.config.pgquery = optarg; |
| 341 | break; | 350 | break; |
| 342 | case OPTID_QUERYNAME: | 351 | case OPTID_QUERYNAME: |
| 343 | pgqueryname = optarg; | 352 | result.config.pgqueryname = optarg; |
| 344 | break; | 353 | break; |
| 345 | case 'v': | 354 | case 'v': |
| 346 | verbose++; | 355 | verbose++; |
| @@ -348,9 +357,9 @@ int process_arguments(int argc, char **argv) { | |||
| 348 | } | 357 | } |
| 349 | } | 358 | } |
| 350 | 359 | ||
| 351 | set_thresholds(&qthresholds, query_warning, query_critical); | 360 | set_thresholds(&result.config.qthresholds, result.config.query_warning, result.config.query_critical); |
| 352 | 361 | ||
| 353 | return OK; | 362 | return result; |
| 354 | } | 363 | } |
| 355 | 364 | ||
| 356 | /** | 365 | /** |
| @@ -378,8 +387,9 @@ should be added.</para> | |||
| 378 | ******************************************************************************/ | 387 | ******************************************************************************/ |
| 379 | 388 | ||
| 380 | bool is_pg_logname(char *username) { | 389 | bool is_pg_logname(char *username) { |
| 381 | if (strlen(username) > NAMEDATALEN - 1) | 390 | if (strlen(username) > NAMEDATALEN - 1) { |
| 382 | return (false); | 391 | return (false); |
| 392 | } | ||
| 383 | return (true); | 393 | return (true); |
| 384 | } | 394 | } |
| 385 | 395 | ||
| @@ -394,7 +404,7 @@ bool is_pg_logname(char *username) { | |||
| 394 | void print_help(void) { | 404 | void print_help(void) { |
| 395 | char *myport; | 405 | char *myport; |
| 396 | 406 | ||
| 397 | xasprintf(&myport, "%d", DEFAULT_PORT); | 407 | xasprintf(&myport, "%d", 5432); |
| 398 | 408 | ||
| 399 | print_revision(progname, NP_VERSION); | 409 | print_revision(progname, NP_VERSION); |
| 400 | 410 | ||
| @@ -482,9 +492,11 @@ void print_usage(void) { | |||
| 482 | "[-q <query>] [-C <critical query range>] [-W <warning query range>]\n"); | 492 | "[-q <query>] [-C <critical query range>] [-W <warning query range>]\n"); |
| 483 | } | 493 | } |
| 484 | 494 | ||
| 485 | int do_query(PGconn *conn, char *query) { | 495 | mp_state_enum do_query(PGconn *conn, char *query, const char pgqueryname[], thresholds *qthresholds, char *query_warning, |
| 486 | if (verbose) | 496 | char *query_critical) { |
| 497 | if (verbose) { | ||
| 487 | printf("Executing SQL query \"%s\".\n", query); | 498 | printf("Executing SQL query \"%s\".\n", query); |
| 499 | } | ||
| 488 | PGresult *res = PQexec(conn, query); | 500 | PGresult *res = PQexec(conn, query); |
| 489 | 501 | ||
| 490 | if (PGRES_TUPLES_OK != PQresultStatus(res)) { | 502 | if (PGRES_TUPLES_OK != PQresultStatus(res)) { |
| @@ -510,8 +522,9 @@ int do_query(PGconn *conn, char *query) { | |||
| 510 | 522 | ||
| 511 | char *endptr = NULL; | 523 | char *endptr = NULL; |
| 512 | double value = strtod(val_str, &endptr); | 524 | double value = strtod(val_str, &endptr); |
| 513 | if (verbose) | 525 | if (verbose) { |
| 514 | printf("Query result: %f\n", value); | 526 | printf("Query result: %f\n", value); |
| 527 | } | ||
| 515 | 528 | ||
| 516 | if (endptr == val_str) { | 529 | if (endptr == val_str) { |
| 517 | printf("QUERY %s - %s: %s\n", _("CRITICAL"), _("Is not a numeric"), val_str); | 530 | printf("QUERY %s - %s: %s\n", _("CRITICAL"), _("Is not a numeric"), val_str); |
| @@ -519,11 +532,12 @@ int do_query(PGconn *conn, char *query) { | |||
| 519 | } | 532 | } |
| 520 | 533 | ||
| 521 | if ((endptr != NULL) && (*endptr != '\0')) { | 534 | if ((endptr != NULL) && (*endptr != '\0')) { |
| 522 | if (verbose) | 535 | if (verbose) { |
| 523 | printf("Garbage after value: %s.\n", endptr); | 536 | printf("Garbage after value: %s.\n", endptr); |
| 537 | } | ||
| 524 | } | 538 | } |
| 525 | 539 | ||
| 526 | int my_status = get_status(value, qthresholds); | 540 | mp_state_enum my_status = get_status(value, qthresholds); |
| 527 | printf("QUERY %s - ", (my_status == STATE_OK) ? _("OK") | 541 | printf("QUERY %s - ", (my_status == STATE_OK) ? _("OK") |
| 528 | : (my_status == STATE_WARNING) ? _("WARNING") | 542 | : (my_status == STATE_WARNING) ? _("WARNING") |
| 529 | : (my_status == STATE_CRITICAL) ? _("CRITICAL") | 543 | : (my_status == STATE_CRITICAL) ? _("CRITICAL") |
diff --git a/plugins/check_pgsql.d/config.h b/plugins/check_pgsql.d/config.h new file mode 100644 index 00000000..2d4b8b89 --- /dev/null +++ b/plugins/check_pgsql.d/config.h | |||
| @@ -0,0 +1,61 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "../../config.h" | ||
| 4 | #include "thresholds.h" | ||
| 5 | #include <stddef.h> | ||
| 6 | #include <pg_config_manual.h> | ||
| 7 | |||
| 8 | #define DEFAULT_DB "template1" | ||
| 9 | |||
| 10 | enum { | ||
| 11 | DEFAULT_WARN = 2, | ||
| 12 | DEFAULT_CRIT = 8, | ||
| 13 | }; | ||
| 14 | |||
| 15 | typedef struct { | ||
| 16 | char *pghost; /* host name of the backend server */ | ||
| 17 | char *pgport; /* port of the backend server */ | ||
| 18 | char *pgoptions; /* special options to start up the backend server */ | ||
| 19 | char *pgtty; /* debugging tty for the backend server */ | ||
| 20 | char dbName[NAMEDATALEN]; | ||
| 21 | char *pguser; | ||
| 22 | char *pgpasswd; | ||
| 23 | char *pgparams; | ||
| 24 | char *pgquery; | ||
| 25 | char *pgqueryname; | ||
| 26 | |||
| 27 | double twarn; | ||
| 28 | double tcrit; | ||
| 29 | thresholds *qthresholds; | ||
| 30 | char *query_warning; | ||
| 31 | char *query_critical; | ||
| 32 | } check_pgsql_config; | ||
| 33 | |||
| 34 | /* begin, by setting the parameters for a backend connection if the | ||
| 35 | * parameters are null, then the system will try to use reasonable | ||
| 36 | * defaults by looking up environment variables or, failing that, | ||
| 37 | * using hardwired constants | ||
| 38 | * this targets .pgoptions and .pgtty | ||
| 39 | */ | ||
| 40 | |||
| 41 | check_pgsql_config check_pgsql_config_init() { | ||
| 42 | check_pgsql_config tmp = { | ||
| 43 | .pghost = NULL, | ||
| 44 | .pgport = NULL, | ||
| 45 | .pgoptions = NULL, | ||
| 46 | .pgtty = NULL, | ||
| 47 | .dbName = DEFAULT_DB, | ||
| 48 | .pguser = NULL, | ||
| 49 | .pgpasswd = NULL, | ||
| 50 | .pgparams = NULL, | ||
| 51 | .pgquery = NULL, | ||
| 52 | .pgqueryname = NULL, | ||
| 53 | |||
| 54 | .twarn = (double)DEFAULT_WARN, | ||
| 55 | .tcrit = (double)DEFAULT_CRIT, | ||
| 56 | .qthresholds = NULL, | ||
| 57 | .query_warning = NULL, | ||
| 58 | .query_critical = NULL, | ||
| 59 | }; | ||
| 60 | return tmp; | ||
| 61 | } | ||
