diff options
Diffstat (limited to 'plugins/check_pgsql.c')
-rw-r--r-- | plugins/check_pgsql.c | 211 |
1 files changed, 110 insertions, 101 deletions
diff --git a/plugins/check_pgsql.c b/plugins/check_pgsql.c index 2dce0b35..6cd217e0 100644 --- a/plugins/check_pgsql.c +++ b/plugins/check_pgsql.c | |||
@@ -1,26 +1,19 @@ | |||
1 | /****************************************************************************** | 1 | /****************************************************************************** |
2 | * | 2 | |
3 | * Program: PostgreSQL plugin for Nagios | 3 | This program is free software; you can redistribute it and/or modify |
4 | * License: GPL | 4 | it under the terms of the GNU General Public License as published by |
5 | * | 5 | the Free Software Foundation; either version 2 of the License, or |
6 | * License Information: | 6 | (at your option) any later version. |
7 | * | 7 | |
8 | * This program is free software; you can redistribute it and/or modify | 8 | This program is distributed in the hope that it will be useful, |
9 | * it under the terms of the GNU General Public License as published by | 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * the Free Software Foundation; either version 2 of the License, or | 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * (at your option) any later version. | 11 | GNU General Public License for more details. |
12 | * | 12 | |
13 | * This program is distributed in the hope that it will be useful, | 13 | You should have received a copy of the GNU General Public License |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 14 | along with this program; if not, write to the Free Software |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 15 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
16 | * GNU General Public License for more details. | 16 | |
17 | * | ||
18 | * You should have received a copy of the GNU General Public License | ||
19 | * along with this program; if not, write to the Free Software | ||
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
21 | * | ||
22 | * $Id$ | ||
23 | * | ||
24 | *****************************************************************************/ | 17 | *****************************************************************************/ |
25 | 18 | ||
26 | #define DEFAULT_DB "template1" | 19 | #define DEFAULT_DB "template1" |
@@ -32,9 +25,9 @@ enum { | |||
32 | DEFAULT_CRIT = 8 | 25 | DEFAULT_CRIT = 8 |
33 | }; | 26 | }; |
34 | 27 | ||
35 | #include "config.h" | ||
36 | #include "common.h" | 28 | #include "common.h" |
37 | #include "utils.h" | 29 | #include "utils.h" |
30 | #include "netutils.h" | ||
38 | #include <libpq-fe.h> | 31 | #include <libpq-fe.h> |
39 | 32 | ||
40 | int process_arguments (int, char **); | 33 | int process_arguments (int, char **); |
@@ -121,72 +114,6 @@ Please note that all tags must be lowercase to use the DocBook XML DTD. | |||
121 | 114 | ||
122 | 115 | ||
123 | 116 | ||
124 | void | ||
125 | print_help (void) | ||
126 | { | ||
127 | char *myport; | ||
128 | |||
129 | asprintf (&myport, "%d", DEFAULT_PORT); | ||
130 | |||
131 | print_revision (progname, revision); | ||
132 | |||
133 | printf (_(COPYRIGHT), copyright, email); | ||
134 | |||
135 | printf (_("Test whether a PostgreSQL DBMS is accepting connections.\n\n")); | ||
136 | |||
137 | print_usage (); | ||
138 | |||
139 | printf (_(UT_HELP_VRSN)); | ||
140 | |||
141 | printf (_(UT_HOST_PORT), 'P', myport); | ||
142 | |||
143 | printf (_(UT_IPv46)); | ||
144 | |||
145 | printf (S_("\ | ||
146 | -d, --database=STRING\n\ | ||
147 | Database to check (default: %s)\n\ | ||
148 | -l, --logname = STRING\n\ | ||
149 | Login name of user\n\ | ||
150 | -p, --password = STRING\n\ | ||
151 | Password (BIG SECURITY ISSUE)\n"), DEFAULT_DB); | ||
152 | |||
153 | printf (_(UT_WARN_CRIT)); | ||
154 | |||
155 | printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT); | ||
156 | |||
157 | printf (_(UT_VERBOSE)); | ||
158 | |||
159 | printf (S_("\nAll parameters are optional.\n\ | ||
160 | \n\ | ||
161 | This plugin tests a PostgreSQL DBMS to determine whether it is active and\n\ | ||
162 | accepting queries. In its current operation, it simply connects to the\n\ | ||
163 | specified database, and then disconnects. If no database is specified, it\n\ | ||
164 | connects to the template1 database, which is present in every functioning \n\ | ||
165 | PostgreSQL DBMS.\n")); | ||
166 | printf (S_("\n\ | ||
167 | The plugin will connect to a local postmaster if no host is specified. To\n\ | ||
168 | connect to a remote host, be sure that the remote postmaster accepts TCP/IP\n\ | ||
169 | connections (start the postmaster with the -i option).\n")); | ||
170 | printf (S_("\n\ | ||
171 | Typically, the nagios user (unless the --logname option is used) should be\n\ | ||
172 | able to connect to the database without a password. The plugin can also send\n\ | ||
173 | a password, but no effort is made to obsure or encrypt the password.\n")); | ||
174 | |||
175 | support (); | ||
176 | } | ||
177 | |||
178 | void | ||
179 | print_usage (void) | ||
180 | { | ||
181 | printf (S_("\ | ||
182 | Usage:\n %s [-H <host>] [-P <port>] [-c <critical time>] [-w <warning time>]\n\ | ||
183 | [-t <timeout>]"), progname); | ||
184 | printf (S_("[-d <database>] [-l <logname>] [-p <password>]\n")); | ||
185 | printf (S_("\ | ||
186 | %s (-h | --help) for detailed help\n\ | ||
187 | %s (-V | --version) for version information\n"), | ||
188 | progname, progname); | ||
189 | } | ||
190 | 117 | ||
191 | int | 118 | int |
192 | main (int argc, char **argv) | 119 | main (int argc, char **argv) |
@@ -251,8 +178,8 @@ process_arguments (int argc, char **argv) | |||
251 | { | 178 | { |
252 | int c; | 179 | int c; |
253 | 180 | ||
254 | int option_index = 0; | 181 | int option = 0; |
255 | static struct option long_options[] = { | 182 | static struct option longopts[] = { |
256 | {"help", no_argument, 0, 'h'}, | 183 | {"help", no_argument, 0, 'h'}, |
257 | {"version", no_argument, 0, 'V'}, | 184 | {"version", no_argument, 0, 'V'}, |
258 | {"timeout", required_argument, 0, 't'}, | 185 | {"timeout", required_argument, 0, 't'}, |
@@ -269,7 +196,7 @@ process_arguments (int argc, char **argv) | |||
269 | 196 | ||
270 | while (1) { | 197 | while (1) { |
271 | c = getopt_long (argc, argv, "hVt:c:w:H:P:d:l:p:a:", | 198 | c = getopt_long (argc, argv, "hVt:c:w:H:P:d:l:p:a:", |
272 | long_options, &option_index); | 199 | longopts, &option); |
273 | 200 | ||
274 | if (c == EOF) | 201 | if (c == EOF) |
275 | break; | 202 | break; |
@@ -277,6 +204,7 @@ process_arguments (int argc, char **argv) | |||
277 | switch (c) { | 204 | switch (c) { |
278 | case '?': /* usage */ | 205 | case '?': /* usage */ |
279 | usage3 (_("Unknown argument"), optopt); | 206 | usage3 (_("Unknown argument"), optopt); |
207 | break; | ||
280 | case 'h': /* help */ | 208 | case 'h': /* help */ |
281 | print_help (); | 209 | print_help (); |
282 | exit (STATE_OK); | 210 | exit (STATE_OK); |
@@ -286,38 +214,44 @@ process_arguments (int argc, char **argv) | |||
286 | case 't': /* timeout period */ | 214 | case 't': /* timeout period */ |
287 | if (!is_integer (optarg)) | 215 | if (!is_integer (optarg)) |
288 | usage2 (_("Timeout Interval must be an integer"), optarg); | 216 | usage2 (_("Timeout Interval must be an integer"), optarg); |
289 | timeout_interval = atoi (optarg); | 217 | else |
218 | timeout_interval = atoi (optarg); | ||
290 | break; | 219 | break; |
291 | case 'c': /* critical time threshold */ | 220 | case 'c': /* critical time threshold */ |
292 | if (!is_integer (optarg)) | 221 | if (!is_integer (optarg)) |
293 | usage2 (_("Invalid critical threshold"), optarg); | 222 | usage2 (_("Invalid critical threshold"), optarg); |
294 | tcrit = atoi (optarg); | 223 | else |
224 | tcrit = atoi (optarg); | ||
295 | break; | 225 | break; |
296 | case 'w': /* warning time threshold */ | 226 | case 'w': /* warning time threshold */ |
297 | if (!is_integer (optarg)) | 227 | if (!is_integer (optarg)) |
298 | usage2 (_("Invalid critical threshold"), optarg); | 228 | usage2 (_("Invalid critical threshold"), optarg); |
299 | twarn = atoi (optarg); | 229 | else |
230 | twarn = atoi (optarg); | ||
300 | break; | 231 | break; |
301 | case 'H': /* host */ | 232 | case 'H': /* host */ |
302 | if (!is_host (optarg)) | 233 | if (!is_host (optarg)) |
303 | usage2 (_("You gave an invalid host name"), optarg); | 234 | usage2 (_("You gave an invalid host name"), optarg); |
304 | pghost = optarg; | 235 | else |
236 | pghost = optarg; | ||
305 | break; | 237 | break; |
306 | case 'P': /* port */ | 238 | case 'P': /* port */ |
307 | if (!is_integer (optarg)) | 239 | if (!is_integer (optarg)) |
308 | usage2 (_("Port must be an integer"), optarg); | 240 | usage2 (_("Port must be an integer"), optarg); |
309 | pgport = optarg; | 241 | else |
242 | pgport = optarg; | ||
310 | break; | 243 | break; |
311 | case 'd': /* database name */ | 244 | case 'd': /* database name */ |
312 | if (!is_pg_dbname (optarg)) | 245 | if (!is_pg_dbname (optarg)) /* checks length and valid chars */ |
313 | usage2 (_("Database name is not valid"), optarg); | 246 | usage2 (_("Database name is not valid"), optarg); |
314 | strncpy (dbName, optarg, NAMEDATALEN - 1); | 247 | else /* we know length, and know optarg is terminated, so us strcpy */ |
315 | dbName[NAMEDATALEN - 1] = 0; | 248 | strcpy (dbName, optarg); |
316 | break; | 249 | break; |
317 | case 'l': /* login name */ | 250 | case 'l': /* login name */ |
318 | if (!is_pg_logname (optarg)) | 251 | if (!is_pg_logname (optarg)) |
319 | usage2 (_("user name is not valid"), optarg); | 252 | usage2 (_("user name is not valid"), optarg); |
320 | pguser = optarg; | 253 | else |
254 | pguser = optarg; | ||
321 | break; | 255 | break; |
322 | case 'p': /* authentication password */ | 256 | case 'p': /* authentication password */ |
323 | case 'a': | 257 | case 'a': |
@@ -433,3 +367,78 @@ is_pg_logname (char *username) | |||
433 | </article> | 367 | </article> |
434 | -@@ | 368 | -@@ |
435 | ******************************************************************************/ | 369 | ******************************************************************************/ |
370 | |||
371 | |||
372 | |||
373 | |||
374 | |||
375 | void | ||
376 | print_help (void) | ||
377 | { | ||
378 | char *myport; | ||
379 | |||
380 | asprintf (&myport, "%d", DEFAULT_PORT); | ||
381 | |||
382 | print_revision (progname, revision); | ||
383 | |||
384 | printf (_(COPYRIGHT), copyright, email); | ||
385 | |||
386 | printf (_("Test whether a PostgreSQL DBMS is accepting connections.\n\n")); | ||
387 | |||
388 | print_usage (); | ||
389 | |||
390 | printf (_(UT_HELP_VRSN)); | ||
391 | |||
392 | printf (_(UT_HOST_PORT), 'P', myport); | ||
393 | |||
394 | printf (_(UT_IPv46)); | ||
395 | |||
396 | printf (S_("\ | ||
397 | -d, --database=STRING\n\ | ||
398 | Database to check (default: %s)\n\ | ||
399 | -l, --logname = STRING\n\ | ||
400 | Login name of user\n\ | ||
401 | -p, --password = STRING\n\ | ||
402 | Password (BIG SECURITY ISSUE)\n"), DEFAULT_DB); | ||
403 | |||
404 | printf (_(UT_WARN_CRIT)); | ||
405 | |||
406 | printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT); | ||
407 | |||
408 | printf (_(UT_VERBOSE)); | ||
409 | |||
410 | printf (S_("\nAll parameters are optional.\n\ | ||
411 | \n\ | ||
412 | This plugin tests a PostgreSQL DBMS to determine whether it is active and\n\ | ||
413 | accepting queries. In its current operation, it simply connects to the\n\ | ||
414 | specified database, and then disconnects. If no database is specified, it\n\ | ||
415 | connects to the template1 database, which is present in every functioning \n\ | ||
416 | PostgreSQL DBMS.\n")); | ||
417 | printf (S_("\n\ | ||
418 | The plugin will connect to a local postmaster if no host is specified. To\n\ | ||
419 | connect to a remote host, be sure that the remote postmaster accepts TCP/IP\n\ | ||
420 | connections (start the postmaster with the -i option).\n")); | ||
421 | printf (S_("\n\ | ||
422 | Typically, the nagios user (unless the --logname option is used) should be\n\ | ||
423 | able to connect to the database without a password. The plugin can also send\n\ | ||
424 | a password, but no effort is made to obsure or encrypt the password.\n")); | ||
425 | |||
426 | printf (_(UT_SUPPORT)); | ||
427 | } | ||
428 | |||
429 | |||
430 | |||
431 | |||
432 | void | ||
433 | print_usage (void) | ||
434 | { | ||
435 | printf (S_("\ | ||
436 | Usage:\n %s [-H <host>] [-P <port>] [-c <critical time>] [-w <warning time>]\n\ | ||
437 | [-t <timeout>]"), progname); | ||
438 | printf (S_("[-d <database>] [-l <logname>] [-p <password>]\n")); | ||
439 | printf (S_("\ | ||
440 | %s (-h | --help) for detailed help\n\ | ||
441 | %s (-V | --version) for version information\n"), | ||
442 | progname, progname); | ||
443 | } | ||
444 | |||