diff options
| author | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2024-10-31 13:24:56 +0100 |
|---|---|---|
| committer | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2024-10-31 13:24:56 +0100 |
| commit | 835be77d74f0d418022a07e9a5d7c0f05e06fcc7 (patch) | |
| tree | 72bb80957dc38ed7bb580d15b769ffff0f3eb499 /plugins | |
| parent | 0700aacf7c50d1da086cd0a77b13fe1afd99b709 (diff) | |
| download | monitoring-plugins-835be77d74f0d418022a07e9a5d7c0f05e06fcc7.tar.gz | |
check_pgsql: clang-format
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/check_pgsql.c | 540 |
1 files changed, 242 insertions, 298 deletions
diff --git a/plugins/check_pgsql.c b/plugins/check_pgsql.c index 94d589e1..3ca3d206 100644 --- a/plugins/check_pgsql.c +++ b/plugins/check_pgsql.c | |||
| @@ -1,32 +1,32 @@ | |||
| 1 | /***************************************************************************** | 1 | /***************************************************************************** |
| 2 | * | 2 | * |
| 3 | * Monitoring check_pgsql plugin | 3 | * Monitoring check_pgsql plugin |
| 4 | * | 4 | * |
| 5 | * License: GPL | 5 | * License: GPL |
| 6 | * Copyright (c) 1999-2011 Monitoring Plugins Development Team | 6 | * Copyright (c) 1999-2011 Monitoring Plugins Development Team |
| 7 | * | 7 | * |
| 8 | * Description: | 8 | * Description: |
| 9 | * | 9 | * |
| 10 | * This file contains the check_pgsql plugin | 10 | * This file contains the check_pgsql plugin |
| 11 | * | 11 | * |
| 12 | * Test whether a PostgreSQL Database is accepting connections. | 12 | * Test whether a PostgreSQL Database is accepting connections. |
| 13 | * | 13 | * |
| 14 | * | 14 | * |
| 15 | * This program is free software: you can redistribute it and/or modify | 15 | * This program is free software: you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License as published by | 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation, either version 3 of the License, or | 17 | * the Free Software Foundation, either version 3 of the License, or |
| 18 | * (at your option) any later version. | 18 | * (at your option) any later version. |
| 19 | * | 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, | 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. | 23 | * GNU General Public License for more details. |
| 24 | * | 24 | * |
| 25 | * You should have received a copy of the GNU General Public License | 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 26 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | * | 27 | * |
| 28 | * | 28 | * |
| 29 | *****************************************************************************/ | 29 | *****************************************************************************/ |
| 30 | 30 | ||
| 31 | const char *progname = "check_pgsql"; | 31 | const char *progname = "check_pgsql"; |
| 32 | const char *copyright = "1999-2011"; | 32 | const char *copyright = "1999-2011"; |
| @@ -40,22 +40,18 @@ const char *email = "devel@monitoring-plugins.org"; | |||
| 40 | #include <libpq-fe.h> | 40 | #include <libpq-fe.h> |
| 41 | #include <pg_config_manual.h> | 41 | #include <pg_config_manual.h> |
| 42 | 42 | ||
| 43 | #define DEFAULT_DB "template1" | 43 | #define DEFAULT_DB "template1" |
| 44 | #define DEFAULT_HOST "127.0.0.1" | 44 | #define DEFAULT_HOST "127.0.0.1" |
| 45 | 45 | ||
| 46 | /* return the PSQL server version as a 3-tuple */ | 46 | /* return the PSQL server version as a 3-tuple */ |
| 47 | #define PSQL_SERVER_VERSION3(server_version) \ | 47 | #define PSQL_SERVER_VERSION3(server_version) \ |
| 48 | (server_version) / 10000, \ | 48 | (server_version) / 10000, (server_version) / 100 - (int)((server_version) / 10000) * 100, \ |
| 49 | (server_version) / 100 - (int)((server_version) / 10000) * 100, \ | 49 | (server_version) - (int)((server_version) / 100) * 100 |
| 50 | (server_version) - (int)((server_version) / 100) * 100 | ||
| 51 | /* return true if the given host is a UNIX domain socket */ | 50 | /* return true if the given host is a UNIX domain socket */ |
| 52 | #define PSQL_IS_UNIX_DOMAIN_SOCKET(host) \ | 51 | #define PSQL_IS_UNIX_DOMAIN_SOCKET(host) ((NULL == (host)) || ('\0' == *(host)) || ('/' == *(host))) |
| 53 | ((NULL == (host)) || ('\0' == *(host)) || ('/' == *(host))) | ||
| 54 | /* return a 3-tuple identifying a host/port independent of the socket type */ | 52 | /* return a 3-tuple identifying a host/port independent of the socket type */ |
| 55 | #define PSQL_SOCKET3(host, port) \ | 53 | #define PSQL_SOCKET3(host, port) \ |
| 56 | ((NULL == (host)) || ('\0' == *(host))) ? DEFAULT_PGSOCKET_DIR : host, \ | 54 | ((NULL == (host)) || ('\0' == *(host))) ? DEFAULT_PGSOCKET_DIR : host, PSQL_IS_UNIX_DOMAIN_SOCKET(host) ? "/.s.PGSQL." : ":", port |
| 57 | PSQL_IS_UNIX_DOMAIN_SOCKET (host) ? "/.s.PGSQL." : ":", \ | ||
| 58 | port | ||
| 59 | 55 | ||
| 60 | enum { | 56 | enum { |
| 61 | DEFAULT_PORT = 5432, | 57 | DEFAULT_PORT = 5432, |
| @@ -63,17 +59,15 @@ enum { | |||
| 63 | DEFAULT_CRIT = 8 | 59 | DEFAULT_CRIT = 8 |
| 64 | }; | 60 | }; |
| 65 | 61 | ||
| 62 | int process_arguments(int, char **); | ||
| 63 | int validate_arguments(void); | ||
| 64 | void print_usage(void); | ||
| 65 | void print_help(void); | ||
| 66 | bool is_pg_logname(char *); | ||
| 67 | int do_query(PGconn *, char *); | ||
| 66 | 68 | ||
| 67 | 69 | char *pghost = NULL; /* host name of the backend server */ | |
| 68 | int process_arguments (int, char **); | 70 | char *pgport = NULL; /* port of the backend server */ |
| 69 | int validate_arguments (void); | ||
| 70 | void print_usage (void); | ||
| 71 | void print_help (void); | ||
| 72 | bool is_pg_logname (char *); | ||
| 73 | int do_query (PGconn *, char *); | ||
| 74 | |||
| 75 | char *pghost = NULL; /* host name of the backend server */ | ||
| 76 | char *pgport = NULL; /* port of the backend server */ | ||
| 77 | int default_port = DEFAULT_PORT; | 71 | int default_port = DEFAULT_PORT; |
| 78 | char *pgoptions = NULL; | 72 | char *pgoptions = NULL; |
| 79 | char *pgtty = NULL; | 73 | char *pgtty = NULL; |
| @@ -141,11 +135,7 @@ Please note that all tags must be lowercase to use the DocBook XML DTD. | |||
| 141 | -@@ | 135 | -@@ |
| 142 | ******************************************************************************/ | 136 | ******************************************************************************/ |
| 143 | 137 | ||
| 144 | 138 | int main(int argc, char **argv) { | |
| 145 | |||
| 146 | int | ||
| 147 | main (int argc, char **argv) | ||
| 148 | { | ||
| 149 | PGconn *conn; | 139 | PGconn *conn; |
| 150 | char *conninfo = NULL; | 140 | char *conninfo = NULL; |
| 151 | 141 | ||
| @@ -160,59 +150,57 @@ main (int argc, char **argv) | |||
| 160 | * defaults by looking up environment variables or, failing that, | 150 | * defaults by looking up environment variables or, failing that, |
| 161 | * using hardwired constants */ | 151 | * using hardwired constants */ |
| 162 | 152 | ||
| 163 | pgoptions = NULL; /* special options to start up the backend server */ | 153 | pgoptions = NULL; /* special options to start up the backend server */ |
| 164 | pgtty = NULL; /* debugging tty for the backend server */ | 154 | pgtty = NULL; /* debugging tty for the backend server */ |
| 165 | 155 | ||
| 166 | setlocale (LC_ALL, ""); | 156 | setlocale(LC_ALL, ""); |
| 167 | bindtextdomain (PACKAGE, LOCALEDIR); | 157 | bindtextdomain(PACKAGE, LOCALEDIR); |
| 168 | textdomain (PACKAGE); | 158 | textdomain(PACKAGE); |
| 169 | 159 | ||
| 170 | /* Parse extra opts if any */ | 160 | /* Parse extra opts if any */ |
| 171 | argv=np_extra_opts (&argc, argv, progname); | 161 | argv = np_extra_opts(&argc, argv, progname); |
| 172 | 162 | ||
| 173 | if (process_arguments (argc, argv) == ERROR) | 163 | if (process_arguments(argc, argv) == ERROR) |
| 174 | usage4 (_("Could not parse arguments")); | 164 | usage4(_("Could not parse arguments")); |
| 175 | if (verbose > 2) | 165 | if (verbose > 2) |
| 176 | printf("Arguments initialized\n"); | 166 | printf("Arguments initialized\n"); |
| 177 | 167 | ||
| 178 | /* Set signal handling and alarm */ | 168 | /* Set signal handling and alarm */ |
| 179 | if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) { | 169 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { |
| 180 | usage4 (_("Cannot catch SIGALRM")); | 170 | usage4(_("Cannot catch SIGALRM")); |
| 181 | } | 171 | } |
| 182 | alarm (timeout_interval); | 172 | alarm(timeout_interval); |
| 183 | 173 | ||
| 184 | if (pgparams) | 174 | if (pgparams) |
| 185 | asprintf (&conninfo, "%s ", pgparams); | 175 | asprintf(&conninfo, "%s ", pgparams); |
| 186 | 176 | ||
| 187 | asprintf (&conninfo, "%sdbname = '%s'", conninfo ? conninfo : "", dbName); | 177 | asprintf(&conninfo, "%sdbname = '%s'", conninfo ? conninfo : "", dbName); |
| 188 | if (pghost) | 178 | if (pghost) |
| 189 | asprintf (&conninfo, "%s host = '%s'", conninfo, pghost); | 179 | asprintf(&conninfo, "%s host = '%s'", conninfo, pghost); |
| 190 | if (pgport) | 180 | if (pgport) |
| 191 | asprintf (&conninfo, "%s port = '%s'", conninfo, pgport); | 181 | asprintf(&conninfo, "%s port = '%s'", conninfo, pgport); |
| 192 | if (pgoptions) | 182 | if (pgoptions) |
| 193 | asprintf (&conninfo, "%s options = '%s'", conninfo, pgoptions); | 183 | asprintf(&conninfo, "%s options = '%s'", conninfo, pgoptions); |
| 194 | /* if (pgtty) -- ignored by PQconnectdb */ | 184 | /* if (pgtty) -- ignored by PQconnectdb */ |
| 195 | if (pguser) | 185 | if (pguser) |
| 196 | asprintf (&conninfo, "%s user = '%s'", conninfo, pguser); | 186 | asprintf(&conninfo, "%s user = '%s'", conninfo, pguser); |
| 197 | 187 | ||
| 198 | if (verbose) /* do not include password (see right below) in output */ | 188 | if (verbose) /* do not include password (see right below) in output */ |
| 199 | printf ("Connecting to PostgreSQL using conninfo: %s%s\n", conninfo, | 189 | printf("Connecting to PostgreSQL using conninfo: %s%s\n", conninfo, pgpasswd ? " password = <hidden>" : ""); |
| 200 | pgpasswd ? " password = <hidden>" : ""); | ||
| 201 | 190 | ||
| 202 | if (pgpasswd) | 191 | if (pgpasswd) |
| 203 | asprintf (&conninfo, "%s password = '%s'", conninfo, pgpasswd); | 192 | asprintf(&conninfo, "%s password = '%s'", conninfo, pgpasswd); |
| 204 | 193 | ||
| 205 | /* make a connection to the database */ | 194 | /* make a connection to the database */ |
| 206 | gettimeofday (&start_timeval, NULL); | 195 | gettimeofday(&start_timeval, NULL); |
| 207 | conn = PQconnectdb (conninfo); | 196 | conn = PQconnectdb(conninfo); |
| 208 | gettimeofday (&end_timeval, NULL); | 197 | gettimeofday(&end_timeval, NULL); |
| 209 | 198 | ||
| 210 | while (start_timeval.tv_usec > end_timeval.tv_usec) { | 199 | while (start_timeval.tv_usec > end_timeval.tv_usec) { |
| 211 | --end_timeval.tv_sec; | 200 | --end_timeval.tv_sec; |
| 212 | end_timeval.tv_usec += 1000000; | 201 | end_timeval.tv_usec += 1000000; |
| 213 | } | 202 | } |
| 214 | elapsed_time = (double)(end_timeval.tv_sec - start_timeval.tv_sec) | 203 | elapsed_time = (double)(end_timeval.tv_sec - start_timeval.tv_sec) + (double)(end_timeval.tv_usec - start_timeval.tv_usec) / 1000000.0; |
| 215 | + (double)(end_timeval.tv_usec - start_timeval.tv_usec) / 1000000.0; | ||
| 216 | 204 | ||
| 217 | if (verbose) | 205 | if (verbose) |
| 218 | printf("Time elapsed: %f\n", elapsed_time); | 206 | printf("Time elapsed: %f\n", elapsed_time); |
| @@ -220,152 +208,137 @@ main (int argc, char **argv) | |||
| 220 | /* check to see that the backend connection was successfully made */ | 208 | /* check to see that the backend connection was successfully made */ |
| 221 | if (verbose) | 209 | if (verbose) |
| 222 | printf("Verifying connection\n"); | 210 | printf("Verifying connection\n"); |
| 223 | if (PQstatus (conn) == CONNECTION_BAD) { | 211 | if (PQstatus(conn) == CONNECTION_BAD) { |
| 224 | printf (_("CRITICAL - no connection to '%s' (%s).\n"), | 212 | printf(_("CRITICAL - no connection to '%s' (%s).\n"), dbName, PQerrorMessage(conn)); |
| 225 | dbName, PQerrorMessage (conn)); | 213 | PQfinish(conn); |
| 226 | PQfinish (conn); | ||
| 227 | return STATE_CRITICAL; | 214 | return STATE_CRITICAL; |
| 228 | } | 215 | } else if (elapsed_time > tcrit) { |
| 229 | else if (elapsed_time > tcrit) { | ||
| 230 | status = STATE_CRITICAL; | 216 | status = STATE_CRITICAL; |
| 231 | } | 217 | } else if (elapsed_time > twarn) { |
| 232 | else if (elapsed_time > twarn) { | ||
| 233 | status = STATE_WARNING; | 218 | status = STATE_WARNING; |
| 234 | } | 219 | } else { |
| 235 | else { | ||
| 236 | status = STATE_OK; | 220 | status = STATE_OK; |
| 237 | } | 221 | } |
| 238 | 222 | ||
| 239 | if (verbose) { | 223 | if (verbose) { |
| 240 | char *server_host = PQhost (conn); | 224 | char *server_host = PQhost(conn); |
| 241 | int server_version = PQserverVersion (conn); | 225 | int server_version = PQserverVersion(conn); |
| 242 | 226 | ||
| 243 | printf ("Successfully connected to database %s (user %s) " | 227 | printf("Successfully connected to database %s (user %s) " |
| 244 | "at server %s%s%s (server version: %d.%d.%d, " | 228 | "at server %s%s%s (server version: %d.%d.%d, " |
| 245 | "protocol version: %d, pid: %d)\n", | 229 | "protocol version: %d, pid: %d)\n", |
| 246 | PQdb (conn), PQuser (conn), | 230 | PQdb(conn), PQuser(conn), PSQL_SOCKET3(server_host, PQport(conn)), PSQL_SERVER_VERSION3(server_version), |
| 247 | PSQL_SOCKET3 (server_host, PQport (conn)), | 231 | PQprotocolVersion(conn), PQbackendPID(conn)); |
| 248 | PSQL_SERVER_VERSION3 (server_version), | ||
| 249 | PQprotocolVersion (conn), PQbackendPID (conn)); | ||
| 250 | } | 232 | } |
| 251 | 233 | ||
| 252 | printf (_(" %s - database %s (%f sec.)|%s\n"), | 234 | printf(_(" %s - database %s (%f sec.)|%s\n"), state_text(status), dbName, elapsed_time, |
| 253 | state_text(status), dbName, elapsed_time, | 235 | fperfdata("time", elapsed_time, "s", !!(twarn > 0.0), twarn, !!(tcrit > 0.0), tcrit, true, 0, false, 0)); |
| 254 | fperfdata("time", elapsed_time, "s", | ||
| 255 | !!(twarn > 0.0), twarn, !!(tcrit > 0.0), tcrit, true, 0, false,0)); | ||
| 256 | 236 | ||
| 257 | if (pgquery) | 237 | if (pgquery) |
| 258 | query_status = do_query (conn, pgquery); | 238 | query_status = do_query(conn, pgquery); |
| 259 | 239 | ||
| 260 | if (verbose) | 240 | if (verbose) |
| 261 | printf("Closing connection\n"); | 241 | printf("Closing connection\n"); |
| 262 | PQfinish (conn); | 242 | PQfinish(conn); |
| 263 | return (pgquery && query_status > status) ? query_status : status; | 243 | return (pgquery && query_status > status) ? query_status : status; |
| 264 | } | 244 | } |
| 265 | 245 | ||
| 266 | |||
| 267 | |||
| 268 | /* process command-line arguments */ | 246 | /* process command-line arguments */ |
| 269 | int | 247 | int process_arguments(int argc, char **argv) { |
| 270 | process_arguments (int argc, char **argv) | ||
| 271 | { | ||
| 272 | int c; | 248 | int c; |
| 273 | 249 | ||
| 274 | int option = 0; | 250 | int option = 0; |
| 275 | static struct option longopts[] = { | 251 | static struct option longopts[] = {{"help", no_argument, 0, 'h'}, |
| 276 | {"help", no_argument, 0, 'h'}, | 252 | {"version", no_argument, 0, 'V'}, |
| 277 | {"version", no_argument, 0, 'V'}, | 253 | {"timeout", required_argument, 0, 't'}, |
| 278 | {"timeout", required_argument, 0, 't'}, | 254 | {"critical", required_argument, 0, 'c'}, |
| 279 | {"critical", required_argument, 0, 'c'}, | 255 | {"warning", required_argument, 0, 'w'}, |
| 280 | {"warning", required_argument, 0, 'w'}, | 256 | {"hostname", required_argument, 0, 'H'}, |
| 281 | {"hostname", required_argument, 0, 'H'}, | 257 | {"logname", required_argument, 0, 'l'}, |
| 282 | {"logname", required_argument, 0, 'l'}, | 258 | {"password", required_argument, 0, 'p'}, |
| 283 | {"password", required_argument, 0, 'p'}, | 259 | {"authorization", required_argument, 0, 'a'}, |
| 284 | {"authorization", required_argument, 0, 'a'}, | 260 | {"port", required_argument, 0, 'P'}, |
| 285 | {"port", required_argument, 0, 'P'}, | 261 | {"database", required_argument, 0, 'd'}, |
| 286 | {"database", required_argument, 0, 'd'}, | 262 | {"option", required_argument, 0, 'o'}, |
| 287 | {"option", required_argument, 0, 'o'}, | 263 | {"query", required_argument, 0, 'q'}, |
| 288 | {"query", required_argument, 0, 'q'}, | 264 | {"queryname", required_argument, 0, OPTID_QUERYNAME}, |
| 289 | {"queryname", required_argument, 0, OPTID_QUERYNAME}, | 265 | {"query_critical", required_argument, 0, 'C'}, |
| 290 | {"query_critical", required_argument, 0, 'C'}, | 266 | {"query_warning", required_argument, 0, 'W'}, |
| 291 | {"query_warning", required_argument, 0, 'W'}, | 267 | {"verbose", no_argument, 0, 'v'}, |
| 292 | {"verbose", no_argument, 0, 'v'}, | 268 | {0, 0, 0, 0}}; |
| 293 | {0, 0, 0, 0} | ||
| 294 | }; | ||
| 295 | 269 | ||
| 296 | while (1) { | 270 | while (1) { |
| 297 | c = getopt_long (argc, argv, "hVt:c:w:H:P:d:l:p:a:o:q:C:W:v", | 271 | c = getopt_long(argc, argv, "hVt:c:w:H:P:d:l:p:a:o:q:C:W:v", longopts, &option); |
| 298 | longopts, &option); | ||
| 299 | 272 | ||
| 300 | if (c == EOF) | 273 | if (c == EOF) |
| 301 | break; | 274 | break; |
| 302 | 275 | ||
| 303 | switch (c) { | 276 | switch (c) { |
| 304 | case '?': /* usage */ | 277 | case '?': /* usage */ |
| 305 | usage5 (); | 278 | usage5(); |
| 306 | case 'h': /* help */ | 279 | case 'h': /* help */ |
| 307 | print_help (); | 280 | print_help(); |
| 308 | exit (STATE_UNKNOWN); | 281 | exit(STATE_UNKNOWN); |
| 309 | case 'V': /* version */ | 282 | case 'V': /* version */ |
| 310 | print_revision (progname, NP_VERSION); | 283 | print_revision(progname, NP_VERSION); |
| 311 | exit (STATE_UNKNOWN); | 284 | exit(STATE_UNKNOWN); |
| 312 | case 't': /* timeout period */ | 285 | case 't': /* timeout period */ |
| 313 | if (!is_integer (optarg)) | 286 | if (!is_integer(optarg)) |
| 314 | usage2 (_("Timeout interval must be a positive integer"), optarg); | 287 | usage2(_("Timeout interval must be a positive integer"), optarg); |
| 315 | else | 288 | else |
| 316 | timeout_interval = atoi (optarg); | 289 | timeout_interval = atoi(optarg); |
| 317 | break; | 290 | break; |
| 318 | case 'c': /* critical time threshold */ | 291 | case 'c': /* critical time threshold */ |
| 319 | if (!is_nonnegative (optarg)) | 292 | if (!is_nonnegative(optarg)) |
| 320 | usage2 (_("Critical threshold must be a positive integer"), optarg); | 293 | usage2(_("Critical threshold must be a positive integer"), optarg); |
| 321 | else | 294 | else |
| 322 | tcrit = strtod (optarg, NULL); | 295 | tcrit = strtod(optarg, NULL); |
| 323 | break; | 296 | break; |
| 324 | case 'w': /* warning time threshold */ | 297 | case 'w': /* warning time threshold */ |
| 325 | if (!is_nonnegative (optarg)) | 298 | if (!is_nonnegative(optarg)) |
| 326 | usage2 (_("Warning threshold must be a positive integer"), optarg); | 299 | usage2(_("Warning threshold must be a positive integer"), optarg); |
| 327 | else | 300 | else |
| 328 | twarn = strtod (optarg, NULL); | 301 | twarn = strtod(optarg, NULL); |
| 329 | break; | 302 | break; |
| 330 | case 'C': /* critical query threshold */ | 303 | case 'C': /* critical query threshold */ |
| 331 | query_critical = optarg; | 304 | query_critical = optarg; |
| 332 | break; | 305 | break; |
| 333 | case 'W': /* warning query threshold */ | 306 | case 'W': /* warning query threshold */ |
| 334 | query_warning = optarg; | 307 | query_warning = optarg; |
| 335 | break; | 308 | break; |
| 336 | case 'H': /* host */ | 309 | case 'H': /* host */ |
| 337 | if ((*optarg != '/') && (!is_host (optarg))) | 310 | if ((*optarg != '/') && (!is_host(optarg))) |
| 338 | usage2 (_("Invalid hostname/address"), optarg); | 311 | usage2(_("Invalid hostname/address"), optarg); |
| 339 | else | 312 | else |
| 340 | pghost = optarg; | 313 | pghost = optarg; |
| 341 | break; | 314 | break; |
| 342 | case 'P': /* port */ | 315 | case 'P': /* port */ |
| 343 | if (!is_integer (optarg)) | 316 | if (!is_integer(optarg)) |
| 344 | usage2 (_("Port must be a positive integer"), optarg); | 317 | usage2(_("Port must be a positive integer"), optarg); |
| 345 | else | 318 | else |
| 346 | pgport = optarg; | 319 | pgport = optarg; |
| 347 | break; | 320 | break; |
| 348 | case 'd': /* database name */ | 321 | case 'd': /* database name */ |
| 349 | if (strlen(optarg) >= NAMEDATALEN) { | 322 | if (strlen(optarg) >= NAMEDATALEN) { |
| 350 | usage2 (_("Database name exceeds the maximum length"), optarg); | 323 | usage2(_("Database name exceeds the maximum length"), optarg); |
| 351 | } | 324 | } |
| 352 | snprintf(dbName, NAMEDATALEN, "%s", optarg); | 325 | snprintf(dbName, NAMEDATALEN, "%s", optarg); |
| 353 | break; | 326 | break; |
| 354 | case 'l': /* login name */ | 327 | case 'l': /* login name */ |
| 355 | if (!is_pg_logname (optarg)) | 328 | if (!is_pg_logname(optarg)) |
| 356 | usage2 (_("User name is not valid"), optarg); | 329 | usage2(_("User name is not valid"), optarg); |
| 357 | else | 330 | else |
| 358 | pguser = optarg; | 331 | pguser = optarg; |
| 359 | break; | 332 | break; |
| 360 | case 'p': /* authentication password */ | 333 | case 'p': /* authentication password */ |
| 361 | case 'a': | 334 | case 'a': |
| 362 | pgpasswd = optarg; | 335 | pgpasswd = optarg; |
| 363 | break; | 336 | break; |
| 364 | case 'o': | 337 | case 'o': |
| 365 | if (pgparams) | 338 | if (pgparams) |
| 366 | asprintf (&pgparams, "%s %s", pgparams, optarg); | 339 | asprintf(&pgparams, "%s %s", pgparams, optarg); |
| 367 | else | 340 | else |
| 368 | asprintf (&pgparams, "%s", optarg); | 341 | asprintf(&pgparams, "%s", optarg); |
| 369 | break; | 342 | break; |
| 370 | case 'q': | 343 | case 'q': |
| 371 | pgquery = optarg; | 344 | pgquery = optarg; |
| @@ -379,12 +352,11 @@ process_arguments (int argc, char **argv) | |||
| 379 | } | 352 | } |
| 380 | } | 353 | } |
| 381 | 354 | ||
| 382 | set_thresholds (&qthresholds, query_warning, query_critical); | 355 | set_thresholds(&qthresholds, query_warning, query_critical); |
| 383 | 356 | ||
| 384 | return validate_arguments (); | 357 | return validate_arguments(); |
| 385 | } | 358 | } |
| 386 | 359 | ||
| 387 | |||
| 388 | /****************************************************************************** | 360 | /****************************************************************************** |
| 389 | 361 | ||
| 390 | @@- | 362 | @@- |
| @@ -405,13 +377,7 @@ first character cannot be a number, however.</para> | |||
| 405 | -@@ | 377 | -@@ |
| 406 | ******************************************************************************/ | 378 | ******************************************************************************/ |
| 407 | 379 | ||
| 408 | 380 | int validate_arguments() { return OK; } | |
| 409 | |||
| 410 | int | ||
| 411 | validate_arguments () | ||
| 412 | { | ||
| 413 | return OK; | ||
| 414 | } | ||
| 415 | 381 | ||
| 416 | /** | 382 | /** |
| 417 | 383 | ||
| @@ -437,10 +403,8 @@ should be added.</para> | |||
| 437 | -@@ | 403 | -@@ |
| 438 | ******************************************************************************/ | 404 | ******************************************************************************/ |
| 439 | 405 | ||
| 440 | 406 | bool is_pg_logname(char *username) { | |
| 441 | 407 | if (strlen(username) > NAMEDATALEN - 1) | |
| 442 | bool is_pg_logname (char *username) { | ||
| 443 | if (strlen (username) > NAMEDATALEN - 1) | ||
| 444 | return (false); | 408 | return (false); |
| 445 | return (true); | 409 | return (true); |
| 446 | } | 410 | } |
| @@ -453,108 +417,98 @@ bool is_pg_logname (char *username) { | |||
| 453 | -@@ | 417 | -@@ |
| 454 | ******************************************************************************/ | 418 | ******************************************************************************/ |
| 455 | 419 | ||
| 456 | 420 | void print_help(void) { | |
| 457 | |||
| 458 | void | ||
| 459 | print_help (void) | ||
| 460 | { | ||
| 461 | char *myport; | 421 | char *myport; |
| 462 | 422 | ||
| 463 | xasprintf (&myport, "%d", DEFAULT_PORT); | 423 | xasprintf(&myport, "%d", DEFAULT_PORT); |
| 464 | 424 | ||
| 465 | print_revision (progname, NP_VERSION); | 425 | print_revision(progname, NP_VERSION); |
| 466 | 426 | ||
| 467 | printf (COPYRIGHT, copyright, email); | 427 | printf(COPYRIGHT, copyright, email); |
| 468 | 428 | ||
| 469 | printf (_("Test whether a PostgreSQL Database is accepting connections.")); | 429 | printf(_("Test whether a PostgreSQL Database is accepting connections.")); |
| 470 | 430 | ||
| 471 | printf ("\n\n"); | 431 | printf("\n\n"); |
| 472 | 432 | ||
| 473 | print_usage (); | 433 | print_usage(); |
| 474 | 434 | ||
| 475 | printf (UT_HELP_VRSN); | 435 | printf(UT_HELP_VRSN); |
| 476 | printf (UT_EXTRA_OPTS); | 436 | printf(UT_EXTRA_OPTS); |
| 477 | 437 | ||
| 478 | printf (UT_HOST_PORT, 'P', myport); | 438 | printf(UT_HOST_PORT, 'P', myport); |
| 479 | 439 | ||
| 480 | printf (" %s\n", "-d, --database=STRING"); | 440 | printf(" %s\n", "-d, --database=STRING"); |
| 481 | printf (" %s", _("Database to check ")); | 441 | printf(" %s", _("Database to check ")); |
| 482 | printf (_("(default: %s)\n"), DEFAULT_DB); | 442 | printf(_("(default: %s)\n"), DEFAULT_DB); |
| 483 | printf (" %s\n", "-l, --logname = STRING"); | 443 | printf(" %s\n", "-l, --logname = STRING"); |
| 484 | printf (" %s\n", _("Login name of user")); | 444 | printf(" %s\n", _("Login name of user")); |
| 485 | printf (" %s\n", "-p, --password = STRING"); | 445 | printf(" %s\n", "-p, --password = STRING"); |
| 486 | printf (" %s\n", _("Password (BIG SECURITY ISSUE)")); | 446 | printf(" %s\n", _("Password (BIG SECURITY ISSUE)")); |
| 487 | printf (" %s\n", "-o, --option = STRING"); | 447 | printf(" %s\n", "-o, --option = STRING"); |
| 488 | printf (" %s\n", _("Connection parameters (keyword = value), see below")); | 448 | printf(" %s\n", _("Connection parameters (keyword = value), see below")); |
| 489 | 449 | ||
| 490 | printf (UT_WARN_CRIT); | 450 | printf(UT_WARN_CRIT); |
| 491 | 451 | ||
| 492 | printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 452 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
| 493 | 453 | ||
| 494 | printf (" %s\n", "-q, --query=STRING"); | 454 | printf(" %s\n", "-q, --query=STRING"); |
| 495 | printf (" %s\n", _("SQL query to run. Only first column in first row will be read")); | 455 | printf(" %s\n", _("SQL query to run. Only first column in first row will be read")); |
| 496 | printf (" %s\n", "--queryname=STRING"); | 456 | printf(" %s\n", "--queryname=STRING"); |
| 497 | printf (" %s\n", _("A name for the query, this string is used instead of the query")); | 457 | printf(" %s\n", _("A name for the query, this string is used instead of the query")); |
| 498 | printf (" %s\n", _("in the long output of the plugin")); | 458 | printf(" %s\n", _("in the long output of the plugin")); |
| 499 | printf (" %s\n", "-W, --query-warning=RANGE"); | 459 | printf(" %s\n", "-W, --query-warning=RANGE"); |
| 500 | printf (" %s\n", _("SQL query value to result in warning status (double)")); | 460 | printf(" %s\n", _("SQL query value to result in warning status (double)")); |
| 501 | printf (" %s\n", "-C, --query-critical=RANGE"); | 461 | printf(" %s\n", "-C, --query-critical=RANGE"); |
| 502 | printf (" %s\n", _("SQL query value to result in critical status (double)")); | 462 | printf(" %s\n", _("SQL query value to result in critical status (double)")); |
| 503 | 463 | ||
| 504 | printf (UT_VERBOSE); | 464 | printf(UT_VERBOSE); |
| 505 | 465 | ||
| 506 | printf ("\n"); | 466 | printf("\n"); |
| 507 | printf (" %s\n", _("All parameters are optional.")); | 467 | printf(" %s\n", _("All parameters are optional.")); |
| 508 | printf (" %s\n", _("This plugin tests a PostgreSQL DBMS to determine whether it is active and")); | 468 | printf(" %s\n", _("This plugin tests a PostgreSQL DBMS to determine whether it is active and")); |
| 509 | printf (" %s\n", _("accepting queries. In its current operation, it simply connects to the")); | 469 | printf(" %s\n", _("accepting queries. In its current operation, it simply connects to the")); |
| 510 | printf (" %s\n", _("specified database, and then disconnects. If no database is specified, it")); | 470 | printf(" %s\n", _("specified database, and then disconnects. If no database is specified, it")); |
| 511 | printf (" %s\n", _("connects to the template1 database, which is present in every functioning")); | 471 | printf(" %s\n", _("connects to the template1 database, which is present in every functioning")); |
| 512 | printf (" %s\n\n", _("PostgreSQL DBMS.")); | 472 | printf(" %s\n\n", _("PostgreSQL DBMS.")); |
| 513 | 473 | ||
| 514 | printf (" %s\n", _("If a query is specified using the -q option, it will be executed after")); | 474 | printf(" %s\n", _("If a query is specified using the -q option, it will be executed after")); |
| 515 | printf (" %s\n", _("connecting to the server. The result from the query has to be numeric.")); | 475 | printf(" %s\n", _("connecting to the server. The result from the query has to be numeric.")); |
| 516 | printf (" %s\n", _("Multiple SQL commands, separated by semicolon, are allowed but the result ")); | 476 | printf(" %s\n", _("Multiple SQL commands, separated by semicolon, are allowed but the result ")); |
| 517 | printf (" %s\n", _("of the last command is taken into account only. The value of the first")); | 477 | printf(" %s\n", _("of the last command is taken into account only. The value of the first")); |
| 518 | printf (" %s\n", _("column in the first row is used as the check result. If a second column is")); | 478 | printf(" %s\n", _("column in the first row is used as the check result. If a second column is")); |
| 519 | printf (" %s\n", _("present in the result set, this is added to the plugin output with a")); | 479 | printf(" %s\n", _("present in the result set, this is added to the plugin output with a")); |
| 520 | printf (" %s\n", _("prefix of \"Extra Info:\". This information can be displayed in the system")); | 480 | printf(" %s\n", _("prefix of \"Extra Info:\". This information can be displayed in the system")); |
| 521 | printf (" %s\n\n", _("executing the plugin.")); | 481 | printf(" %s\n\n", _("executing the plugin.")); |
| 522 | 482 | ||
| 523 | printf (" %s\n", _("See the chapter \"Monitoring Database Activity\" of the PostgreSQL manual")); | 483 | printf(" %s\n", _("See the chapter \"Monitoring Database Activity\" of the PostgreSQL manual")); |
| 524 | printf (" %s\n\n", _("for details about how to access internal statistics of the database server.")); | 484 | printf(" %s\n\n", _("for details about how to access internal statistics of the database server.")); |
| 525 | 485 | ||
| 526 | printf (" %s\n", _("For a list of available connection parameters which may be used with the -o")); | 486 | printf(" %s\n", _("For a list of available connection parameters which may be used with the -o")); |
| 527 | printf (" %s\n", _("command line option, see the documentation for PQconnectdb() in the chapter")); | 487 | printf(" %s\n", _("command line option, see the documentation for PQconnectdb() in the chapter")); |
| 528 | printf (" %s\n", _("\"libpq - C Library\" of the PostgreSQL manual. For example, this may be")); | 488 | printf(" %s\n", _("\"libpq - C Library\" of the PostgreSQL manual. For example, this may be")); |
| 529 | printf (" %s\n", _("used to specify a service name in pg_service.conf to be used for additional")); | 489 | printf(" %s\n", _("used to specify a service name in pg_service.conf to be used for additional")); |
| 530 | printf (" %s\n", _("connection parameters: -o 'service=<name>' or to specify the SSL mode:")); | 490 | printf(" %s\n", _("connection parameters: -o 'service=<name>' or to specify the SSL mode:")); |
| 531 | printf (" %s\n\n", _("-o 'sslmode=require'.")); | 491 | printf(" %s\n\n", _("-o 'sslmode=require'.")); |
| 532 | 492 | ||
| 533 | printf (" %s\n", _("The plugin will connect to a local postmaster if no host is specified. To")); | 493 | printf(" %s\n", _("The plugin will connect to a local postmaster if no host is specified. To")); |
| 534 | printf (" %s\n", _("connect to a remote host, be sure that the remote postmaster accepts TCP/IP")); | 494 | printf(" %s\n", _("connect to a remote host, be sure that the remote postmaster accepts TCP/IP")); |
| 535 | printf (" %s\n\n", _("connections (start the postmaster with the -i option).")); | 495 | printf(" %s\n\n", _("connections (start the postmaster with the -i option).")); |
| 536 | 496 | ||
| 537 | printf (" %s\n", _("Typically, the monitoring user (unless the --logname option is used) should be")); | 497 | printf(" %s\n", _("Typically, the monitoring user (unless the --logname option is used) should be")); |
| 538 | printf (" %s\n", _("able to connect to the database without a password. The plugin can also send")); | 498 | printf(" %s\n", _("able to connect to the database without a password. The plugin can also send")); |
| 539 | printf (" %s\n", _("a password, but no effort is made to obscure or encrypt the password.")); | 499 | printf(" %s\n", _("a password, but no effort is made to obscure or encrypt the password.")); |
| 540 | 500 | ||
| 541 | printf (UT_SUPPORT); | 501 | printf(UT_SUPPORT); |
| 542 | } | 502 | } |
| 543 | 503 | ||
| 544 | 504 | void print_usage(void) { | |
| 545 | 505 | printf("%s\n", _("Usage:")); | |
| 546 | void | 506 | printf("%s [-H <host>] [-P <port>] [-c <critical time>] [-w <warning time>]\n", progname); |
| 547 | print_usage (void) | 507 | printf(" [-t <timeout>] [-d <database>] [-l <logname>] [-p <password>]\n" |
| 548 | { | 508 | "[-q <query>] [-C <critical query range>] [-W <warning query range>]\n"); |
| 549 | printf ("%s\n", _("Usage:")); | ||
| 550 | printf ("%s [-H <host>] [-P <port>] [-c <critical time>] [-w <warning time>]\n", progname); | ||
| 551 | printf (" [-t <timeout>] [-d <database>] [-l <logname>] [-p <password>]\n" | ||
| 552 | "[-q <query>] [-C <critical query range>] [-W <warning query range>]\n"); | ||
| 553 | } | 509 | } |
| 554 | 510 | ||
| 555 | int | 511 | int do_query(PGconn *conn, char *query) { |
| 556 | do_query (PGconn *conn, char *query) | ||
| 557 | { | ||
| 558 | PGresult *res; | 512 | PGresult *res; |
| 559 | 513 | ||
| 560 | char *val_str; | 514 | char *val_str; |
| @@ -566,69 +520,59 @@ do_query (PGconn *conn, char *query) | |||
| 566 | int my_status = STATE_UNKNOWN; | 520 | int my_status = STATE_UNKNOWN; |
| 567 | 521 | ||
| 568 | if (verbose) | 522 | if (verbose) |
| 569 | printf ("Executing SQL query \"%s\".\n", query); | 523 | printf("Executing SQL query \"%s\".\n", query); |
| 570 | res = PQexec (conn, query); | 524 | res = PQexec(conn, query); |
| 571 | 525 | ||
| 572 | if (PGRES_TUPLES_OK != PQresultStatus (res)) { | 526 | if (PGRES_TUPLES_OK != PQresultStatus(res)) { |
| 573 | printf (_("QUERY %s - %s: %s.\n"), _("CRITICAL"), _("Error with query"), | 527 | printf(_("QUERY %s - %s: %s.\n"), _("CRITICAL"), _("Error with query"), PQerrorMessage(conn)); |
| 574 | PQerrorMessage (conn)); | ||
| 575 | return STATE_CRITICAL; | 528 | return STATE_CRITICAL; |
| 576 | } | 529 | } |
| 577 | 530 | ||
| 578 | if (PQntuples (res) < 1) { | 531 | if (PQntuples(res) < 1) { |
| 579 | printf ("QUERY %s - %s.\n", _("WARNING"), _("No rows returned")); | 532 | printf("QUERY %s - %s.\n", _("WARNING"), _("No rows returned")); |
| 580 | return STATE_WARNING; | 533 | return STATE_WARNING; |
| 581 | } | 534 | } |
| 582 | 535 | ||
| 583 | if (PQnfields (res) < 1) { | 536 | if (PQnfields(res) < 1) { |
| 584 | printf ("QUERY %s - %s.\n", _("WARNING"), _("No columns returned")); | 537 | printf("QUERY %s - %s.\n", _("WARNING"), _("No columns returned")); |
| 585 | return STATE_WARNING; | 538 | return STATE_WARNING; |
| 586 | } | 539 | } |
| 587 | 540 | ||
| 588 | val_str = PQgetvalue (res, 0, 0); | 541 | val_str = PQgetvalue(res, 0, 0); |
| 589 | if (! val_str) { | 542 | if (!val_str) { |
| 590 | printf ("QUERY %s - %s.\n", _("CRITICAL"), _("No data returned")); | 543 | printf("QUERY %s - %s.\n", _("CRITICAL"), _("No data returned")); |
| 591 | return STATE_CRITICAL; | 544 | return STATE_CRITICAL; |
| 592 | } | 545 | } |
| 593 | 546 | ||
| 594 | value = strtod (val_str, &endptr); | 547 | value = strtod(val_str, &endptr); |
| 595 | if (verbose) | 548 | if (verbose) |
| 596 | printf ("Query result: %f\n", value); | 549 | printf("Query result: %f\n", value); |
| 597 | 550 | ||
| 598 | if (endptr == val_str) { | 551 | if (endptr == val_str) { |
| 599 | printf ("QUERY %s - %s: %s\n", _("CRITICAL"), _("Is not a numeric"), val_str); | 552 | printf("QUERY %s - %s: %s\n", _("CRITICAL"), _("Is not a numeric"), val_str); |
| 600 | return STATE_CRITICAL; | 553 | return STATE_CRITICAL; |
| 601 | } | 554 | } else if ((endptr != NULL) && (*endptr != '\0')) { |
| 602 | else if ((endptr != NULL) && (*endptr != '\0')) { | ||
| 603 | if (verbose) | 555 | if (verbose) |
| 604 | printf ("Garbage after value: %s.\n", endptr); | 556 | printf("Garbage after value: %s.\n", endptr); |
| 605 | } | 557 | } |
| 606 | 558 | ||
| 607 | my_status = get_status (value, qthresholds); | 559 | my_status = get_status(value, qthresholds); |
| 608 | printf ("QUERY %s - ", | 560 | printf("QUERY %s - ", (my_status == STATE_OK) ? _("OK") |
| 609 | (my_status == STATE_OK) | 561 | : (my_status == STATE_WARNING) ? _("WARNING") |
| 610 | ? _("OK") | 562 | : (my_status == STATE_CRITICAL) ? _("CRITICAL") |
| 611 | : (my_status == STATE_WARNING) | 563 | : _("UNKNOWN")); |
| 612 | ? _("WARNING") | 564 | if (pgqueryname) { |
| 613 | : (my_status == STATE_CRITICAL) | 565 | printf(_("%s returned %f"), pgqueryname, value); |
| 614 | ? _("CRITICAL") | 566 | } else { |
| 615 | : _("UNKNOWN")); | 567 | printf(_("'%s' returned %f"), query, value); |
| 616 | if(pgqueryname) { | ||
| 617 | printf (_("%s returned %f"), pgqueryname, value); | ||
| 618 | } | ||
| 619 | else { | ||
| 620 | printf (_("'%s' returned %f"), query, value); | ||
| 621 | } | 568 | } |
| 622 | 569 | ||
| 623 | printf ("|query=%f;%s;%s;;\n", value, | 570 | printf("|query=%f;%s;%s;;\n", value, query_warning ? query_warning : "", query_critical ? query_critical : ""); |
| 624 | query_warning ? query_warning : "", | 571 | if (PQnfields(res) > 1) { |
| 625 | query_critical ? query_critical : ""); | 572 | extra_info = PQgetvalue(res, 0, 1); |
| 626 | if (PQnfields (res) > 1) { | ||
| 627 | extra_info = PQgetvalue (res, 0, 1); | ||
| 628 | if (extra_info != NULL) { | 573 | if (extra_info != NULL) { |
| 629 | printf ("Extra Info: %s\n", extra_info); | 574 | printf("Extra Info: %s\n", extra_info); |
| 630 | } | 575 | } |
| 631 | } | 576 | } |
| 632 | return my_status; | 577 | return my_status; |
| 633 | } | 578 | } |
| 634 | |||
