1 /*****************************************************************************
3 * Nagios check_pgsql plugin
6 * Copyright (c) 1999-2007 Nagios Plugins Development Team
10 * This file contains the check_pgsql plugin
12 * Test whether a PostgreSQL Database is accepting connections.
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
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
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/>.
29 *****************************************************************************/
31 const char *progname
= "check_pgsql";
32 const char *copyright
= "1999-2007";
33 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
40 #include <pg_config_manual.h>
42 #define DEFAULT_DB "template1"
43 #define DEFAULT_HOST "127.0.0.1"
53 int process_arguments (int, char **);
54 int validate_arguments (void);
55 void print_usage (void);
56 void print_help (void);
57 int is_pg_dbname (char *);
58 int is_pg_logname (char *);
60 char *pghost
= NULL
; /* host name of the backend server */
61 char *pgport
= NULL
; /* port of the backend server */
62 int default_port
= DEFAULT_PORT
;
63 char *pgoptions
= NULL
;
65 char dbName
[NAMEDATALEN
] = DEFAULT_DB
;
67 char *pgpasswd
= NULL
;
68 double twarn
= (double)DEFAULT_WARN
;
69 double tcrit
= (double)DEFAULT_CRIT
;
75 /******************************************************************************
77 The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@
78 tags in the comments. With in the tags, the XML is assembled sequentially.
79 You can define entities in tags. You also have all the #defines available as
82 Please note that all tags must be lowercase to use the DocBook XML DTD.
87 <title>Quick Reference</title>
88 <!-- The refentry forms a manpage -->
91 <manvolnum>5<manvolnum>
94 <refname>&progname;</refname>
95 <refpurpose>&SUMMARY;</refpurpose>
105 <title>Theory, Installation, and Operation</title>
108 <title>General Description</title>
115 <title>Future Enhancements</title>
116 <para>ToDo List</para>
118 <listitem>Add option to get password from a secured file rather than the command line</listitem>
119 <listitem>Add option to specify the query to execute</listitem>
125 <title>Functions</title>
127 ******************************************************************************/
132 main (int argc
, char **argv
)
135 int status
= STATE_UNKNOWN
;
137 /* begin, by setting the parameters for a backend connection if the
138 * parameters are null, then the system will try to use reasonable
139 * defaults by looking up environment variables or, failing that,
140 * using hardwired constants */
142 pgoptions
= NULL
; /* special options to start up the backend server */
143 pgtty
= NULL
; /* debugging tty for the backend server */
145 setlocale (LC_ALL
, "");
146 bindtextdomain (PACKAGE
, LOCALEDIR
);
147 textdomain (PACKAGE
);
149 /* Parse extra opts if any */
150 argv
=np_extra_opts (&argc
, argv
, progname
);
152 if (process_arguments (argc
, argv
) == ERROR
)
153 usage4 (_("Could not parse arguments"));
155 /* Set signal handling and alarm */
156 if (signal (SIGALRM
, timeout_alarm_handler
) == SIG_ERR
) {
157 usage4 (_("Cannot catch SIGALRM"));
159 alarm (timeout_interval
);
161 /* make a connection to the database */
164 PQsetdbLogin (pghost
, pgport
, pgoptions
, pgtty
, dbName
, pguser
, pgpasswd
);
166 elapsed_time
= (int) (end_time
- start_time
);
168 /* check to see that the backend connection was successfully made */
169 if (PQstatus (conn
) == CONNECTION_BAD
) {
170 printf (_("CRITICAL - no connection to '%s' (%s).\n"),
171 dbName
, PQerrorMessage (conn
));
173 return STATE_CRITICAL
;
175 else if (elapsed_time
> tcrit
) {
176 status
= STATE_CRITICAL
;
178 else if (elapsed_time
> twarn
) {
179 status
= STATE_WARNING
;
185 printf (_(" %s - database %s (%d sec.)|%s\n"),
186 state_text(status
), dbName
, elapsed_time
,
187 fperfdata("time", elapsed_time
, "s",
188 (int)twarn
, twarn
, (int)tcrit
, tcrit
, TRUE
, 0, FALSE
,0));
194 /* process command-line arguments */
196 process_arguments (int argc
, char **argv
)
201 static struct option longopts
[] = {
202 {"help", no_argument
, 0, 'h'},
203 {"version", no_argument
, 0, 'V'},
204 {"timeout", required_argument
, 0, 't'},
205 {"critical", required_argument
, 0, 'c'},
206 {"warning", required_argument
, 0, 'w'},
207 {"hostname", required_argument
, 0, 'H'},
208 {"logname", required_argument
, 0, 'l'},
209 {"password", required_argument
, 0, 'p'},
210 {"authorization", required_argument
, 0, 'a'},
211 {"port", required_argument
, 0, 'P'},
212 {"database", required_argument
, 0, 'd'},
217 c
= getopt_long (argc
, argv
, "hVt:c:w:H:P:d:l:p:a:",
224 case '?': /* usage */
229 case 'V': /* version */
230 print_revision (progname
, NP_VERSION
);
232 case 't': /* timeout period */
233 if (!is_integer (optarg
))
234 usage2 (_("Timeout interval must be a positive integer"), optarg
);
236 timeout_interval
= atoi (optarg
);
238 case 'c': /* critical time threshold */
239 if (!is_nonnegative (optarg
))
240 usage2 (_("Critical threshold must be a positive integer"), optarg
);
242 tcrit
= strtod (optarg
, NULL
);
244 case 'w': /* warning time threshold */
245 if (!is_nonnegative (optarg
))
246 usage2 (_("Warning threshold must be a positive integer"), optarg
);
248 twarn
= strtod (optarg
, NULL
);
251 if (!is_host (optarg
))
252 usage2 (_("Invalid hostname/address"), optarg
);
257 if (!is_integer (optarg
))
258 usage2 (_("Port must be a positive integer"), optarg
);
262 case 'd': /* database name */
263 if (!is_pg_dbname (optarg
)) /* checks length and valid chars */
264 usage2 (_("Database name is not valid"), optarg
);
265 else /* we know length, and know optarg is terminated, so us strcpy */
266 strcpy (dbName
, optarg
);
268 case 'l': /* login name */
269 if (!is_pg_logname (optarg
))
270 usage2 (_("User name is not valid"), optarg
);
274 case 'p': /* authentication password */
281 return validate_arguments ();
285 /******************************************************************************
289 <title>validate_arguments</title>
291 <para>&PROTO_validate_arguments;</para>
293 <para>Given a database name, this function returns TRUE if the string
294 is a valid PostgreSQL database name, and returns false if it is
297 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
298 characters long and consist of letters, numbers, and underscores. The
299 first character cannot be a number, however.</para>
303 ******************************************************************************/
308 validate_arguments ()
314 /******************************************************************************
318 <title>is_pg_dbname</title>
320 <para>&PROTO_is_pg_dbname;</para>
322 <para>Given a database name, this function returns TRUE if the string
323 is a valid PostgreSQL database name, and returns false if it is
326 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
327 characters long and consist of letters, numbers, and underscores. The
328 first character cannot be a number, however.</para>
332 ******************************************************************************/
337 is_pg_dbname (char *dbname
)
339 char txt
[NAMEDATALEN
];
340 char tmp
[NAMEDATALEN
];
341 if (strlen (dbname
) > NAMEDATALEN
- 1)
343 strncpy (txt
, dbname
, NAMEDATALEN
- 1);
344 txt
[NAMEDATALEN
- 1] = 0;
345 if (sscanf (txt
, "%[_a-zA-Z]%[^_a-zA-Z0-9-]", tmp
, tmp
) == 1)
347 if (sscanf (txt
, "%[_a-zA-Z]%[_a-zA-Z0-9-]%[^_a-zA-Z0-9-]", tmp
, tmp
, tmp
) ==
354 the tango program should eventually create an entity here based on the
359 <title>is_pg_logname</title>
361 <para>&PROTO_is_pg_logname;</para>
363 <para>Given a username, this function returns TRUE if the string is a
364 valid PostgreSQL username, and returns false if it is not. Valid PostgreSQL
365 usernames are less than &NAMEDATALEN; characters long and consist of
366 letters, numbers, dashes, and underscores, plus possibly some other
369 <para>Currently this function only checks string length. Additional checks
370 should be added.</para>
374 ******************************************************************************/
379 is_pg_logname (char *username
)
381 if (strlen (username
) > NAMEDATALEN
- 1)
386 /******************************************************************************
392 ******************************************************************************/
401 asprintf (&myport
, "%d", DEFAULT_PORT
);
403 print_revision (progname
, NP_VERSION
);
405 printf (COPYRIGHT
, copyright
, email
);
407 printf (_("Test whether a PostgreSQL Database is accepting connections."));
413 printf (_(UT_HELP_VRSN
));
414 printf (_(UT_EXTRA_OPTS
));
416 printf (_(UT_HOST_PORT
), 'P', myport
);
418 printf (_(UT_IPv46
));
420 printf (" %s\n", "-d, --database=STRING");
421 printf (" %s", _("Database to check "));
422 printf (_("(default: %s)"), DEFAULT_DB
);
423 printf (" %s\n", "-l, --logname = STRING");
424 printf (" %s\n", _("Login name of user"));
425 printf (" %s\n", "-p, --password = STRING");
426 printf (" %s\n", _("Password (BIG SECURITY ISSUE)"));
428 printf (_(UT_WARN_CRIT
));
430 printf (_(UT_TIMEOUT
), DEFAULT_SOCKET_TIMEOUT
);
432 printf (_(UT_VERBOSE
));
435 printf (" %s\n", _("All parameters are optional."));
436 printf (" %s\n", _("This plugin tests a PostgreSQL DBMS to determine whether it is active and"));
437 printf (" %s\n", _("accepting queries. In its current operation, it simply connects to the"));
438 printf (" %s\n", _("specified database, and then disconnects. If no database is specified, it"));
439 printf (" %s\n", _("connects to the template1 database, which is present in every functioning"));
440 printf (" %s\n\n", _("PostgreSQL DBMS."));
442 printf (" %s\n", _("The plugin will connect to a local postmaster if no host is specified. To"));
443 printf (" %s\n", _("connect to a remote host, be sure that the remote postmaster accepts TCP/IP"));
444 printf (" %s\n\n", _("connections (start the postmaster with the -i option)."));
446 printf (" %s\n", _("Typically, the nagios user (unless the --logname option is used) should be"));
447 printf (" %s\n", _("able to connect to the database without a password. The plugin can also send"));
448 printf (" %s\n", _("a password, but no effort is made to obsure or encrypt the password."));
452 printf ("%s\n", _("Notes:"));
453 printf (_(UT_EXTRA_OPTS_NOTES
));
456 printf (_(UT_SUPPORT
));
464 printf (_("Usage:"));
465 printf ("%s [-H <host>] [-P <port>] [-c <critical time>] [-w <warning time>]\n", progname
);
466 printf (" [-t <timeout>] [-d <database>] [-l <logname>] [-p <password>]\n");