1 /*****************************************************************************
2 *
3 * Nagios check_pgsql plugin
4 *
5 * License: GPL
6 * Copyright (c) 1999-2007 Nagios Plugins Development Team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
11 *
12 * This file contains the check_pgsql plugin
13 *
14 * Test whether a PostgreSQL Database is accepting connections.
15 *
16 *
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 *
30 * $Id$
31 *
32 *****************************************************************************/
34 const char *progname = "check_pgsql";
35 const char *revision = "$Revision$";
36 const char *copyright = "1999-2007";
37 const char *email = "nagiosplug-devel@lists.sourceforge.net";
39 #include "common.h"
40 #include "utils.h"
42 #include "netutils.h"
43 #include <libpq-fe.h>
44 #include <pg_config_manual.h>
46 #define DEFAULT_DB "template1"
47 #define DEFAULT_HOST "127.0.0.1"
49 enum {
50 DEFAULT_PORT = 5432,
51 DEFAULT_WARN = 2,
52 DEFAULT_CRIT = 8
53 };
57 int process_arguments (int, char **);
58 int validate_arguments (void);
59 void print_usage (void);
60 void print_help (void);
61 int is_pg_dbname (char *);
62 int is_pg_logname (char *);
64 char *pghost = NULL; /* host name of the backend server */
65 char *pgport = NULL; /* port of the backend server */
66 int default_port = DEFAULT_PORT;
67 char *pgoptions = NULL;
68 char *pgtty = NULL;
69 char dbName[NAMEDATALEN] = DEFAULT_DB;
70 char *pguser = NULL;
71 char *pgpasswd = NULL;
72 double twarn = (double)DEFAULT_WARN;
73 double tcrit = (double)DEFAULT_CRIT;
75 PGconn *conn;
76 /*PGresult *res;*/
79 /******************************************************************************
81 The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@
82 tags in the comments. With in the tags, the XML is assembled sequentially.
83 You can define entities in tags. You also have all the #defines available as
84 entities.
86 Please note that all tags must be lowercase to use the DocBook XML DTD.
88 @@-<article>
90 <sect1>
91 <title>Quick Reference</title>
92 <!-- The refentry forms a manpage -->
93 <refentry>
94 <refmeta>
95 <manvolnum>5<manvolnum>
96 </refmeta>
97 <refnamdiv>
98 <refname>&progname;</refname>
99 <refpurpose>&SUMMARY;</refpurpose>
100 </refnamdiv>
101 </refentry>
102 </sect1>
104 <sect1>
105 <title>FAQ</title>
106 </sect1>
108 <sect1>
109 <title>Theory, Installation, and Operation</title>
111 <sect2>
112 <title>General Description</title>
113 <para>
114 &DESCRIPTION;
115 </para>
116 </sect2>
118 <sect2>
119 <title>Future Enhancements</title>
120 <para>ToDo List</para>
121 <itemizedlist>
122 <listitem>Add option to get password from a secured file rather than the command line</listitem>
123 <listitem>Add option to specify the query to execute</listitem>
124 </itemizedlist>
125 </sect2>
128 <sect2>
129 <title>Functions</title>
130 -@@
131 ******************************************************************************/
135 int
136 main (int argc, char **argv)
137 {
138 int elapsed_time;
139 int status = STATE_UNKNOWN;
141 /* begin, by setting the parameters for a backend connection if the
142 * parameters are null, then the system will try to use reasonable
143 * defaults by looking up environment variables or, failing that,
144 * using hardwired constants */
146 pgoptions = NULL; /* special options to start up the backend server */
147 pgtty = NULL; /* debugging tty for the backend server */
149 setlocale (LC_ALL, "");
150 bindtextdomain (PACKAGE, LOCALEDIR);
151 textdomain (PACKAGE);
153 if (process_arguments (argc, argv) == ERROR)
154 usage4 (_("Could not parse arguments"));
156 /* Set signal handling and alarm */
157 if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
158 usage4 (_("Cannot catch SIGALRM"));
159 }
160 alarm (timeout_interval);
162 /* make a connection to the database */
163 time (&start_time);
164 conn =
165 PQsetdbLogin (pghost, pgport, pgoptions, pgtty, dbName, pguser, pgpasswd);
166 time (&end_time);
167 elapsed_time = (int) (end_time - start_time);
169 /* check to see that the backend connection was successfully made */
170 if (PQstatus (conn) == CONNECTION_BAD) {
171 printf (_("CRITICAL - no connection to '%s' (%s).\n"),
172 dbName, PQerrorMessage (conn));
173 PQfinish (conn);
174 return STATE_CRITICAL;
175 }
176 else if (elapsed_time > tcrit) {
177 status = STATE_CRITICAL;
178 }
179 else if (elapsed_time > twarn) {
180 status = STATE_WARNING;
181 }
182 else {
183 status = STATE_OK;
184 }
185 PQfinish (conn);
186 printf (_(" %s - database %s (%d sec.)|%s\n"),
187 state_text(status), dbName, elapsed_time,
188 fperfdata("time", elapsed_time, "s",
189 (int)twarn, twarn, (int)tcrit, tcrit, TRUE, 0, FALSE,0));
190 return status;
191 }
195 /* process command-line arguments */
196 int
197 process_arguments (int argc, char **argv)
198 {
199 int c;
201 int option = 0;
202 static struct option longopts[] = {
203 {"help", no_argument, 0, 'h'},
204 {"version", no_argument, 0, 'V'},
205 {"timeout", required_argument, 0, 't'},
206 {"critical", required_argument, 0, 'c'},
207 {"warning", required_argument, 0, 'w'},
208 {"hostname", required_argument, 0, 'H'},
209 {"logname", required_argument, 0, 'l'},
210 {"password", required_argument, 0, 'p'},
211 {"authorization", required_argument, 0, 'a'},
212 {"port", required_argument, 0, 'P'},
213 {"database", required_argument, 0, 'd'},
214 {0, 0, 0, 0}
215 };
217 while (1) {
218 c = getopt_long (argc, argv, "hVt:c:w:H:P:d:l:p:a:",
219 longopts, &option);
221 if (c == EOF)
222 break;
224 switch (c) {
225 case '?': /* usage */
226 usage5 ();
227 case 'h': /* help */
228 print_help ();
229 exit (STATE_OK);
230 case 'V': /* version */
231 print_revision (progname, revision);
232 exit (STATE_OK);
233 case 't': /* timeout period */
234 if (!is_integer (optarg))
235 usage2 (_("Timeout interval must be a positive integer"), optarg);
236 else
237 timeout_interval = atoi (optarg);
238 break;
239 case 'c': /* critical time threshold */
240 if (!is_nonnegative (optarg))
241 usage2 (_("Critical threshold must be a positive integer"), optarg);
242 else
243 tcrit = strtod (optarg, NULL);
244 break;
245 case 'w': /* warning time threshold */
246 if (!is_nonnegative (optarg))
247 usage2 (_("Warning threshold must be a positive integer"), optarg);
248 else
249 twarn = strtod (optarg, NULL);
250 break;
251 case 'H': /* host */
252 if (!is_host (optarg))
253 usage2 (_("Invalid hostname/address"), optarg);
254 else
255 pghost = optarg;
256 break;
257 case 'P': /* port */
258 if (!is_integer (optarg))
259 usage2 (_("Port must be a positive integer"), optarg);
260 else
261 pgport = optarg;
262 break;
263 case 'd': /* database name */
264 if (!is_pg_dbname (optarg)) /* checks length and valid chars */
265 usage2 (_("Database name is not valid"), optarg);
266 else /* we know length, and know optarg is terminated, so us strcpy */
267 strcpy (dbName, optarg);
268 break;
269 case 'l': /* login name */
270 if (!is_pg_logname (optarg))
271 usage2 (_("User name is not valid"), optarg);
272 else
273 pguser = optarg;
274 break;
275 case 'p': /* authentication password */
276 case 'a':
277 pgpasswd = optarg;
278 break;
279 }
280 }
282 return validate_arguments ();
283 }
286 /******************************************************************************
288 @@-
289 <sect3>
290 <title>validate_arguments</title>
292 <para>&PROTO_validate_arguments;</para>
294 <para>Given a database name, this function returns TRUE if the string
295 is a valid PostgreSQL database name, and returns false if it is
296 not.</para>
298 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
299 characters long and consist of letters, numbers, and underscores. The
300 first character cannot be a number, however.</para>
302 </sect3>
303 -@@
304 ******************************************************************************/
308 int
309 validate_arguments ()
310 {
311 return OK;
312 }
315 /******************************************************************************
317 @@-
318 <sect3>
319 <title>is_pg_dbname</title>
321 <para>&PROTO_is_pg_dbname;</para>
323 <para>Given a database name, this function returns TRUE if the string
324 is a valid PostgreSQL database name, and returns false if it is
325 not.</para>
327 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
328 characters long and consist of letters, numbers, and underscores. The
329 first character cannot be a number, however.</para>
331 </sect3>
332 -@@
333 ******************************************************************************/
337 int
338 is_pg_dbname (char *dbname)
339 {
340 char txt[NAMEDATALEN];
341 char tmp[NAMEDATALEN];
342 if (strlen (dbname) > NAMEDATALEN - 1)
343 return (FALSE);
344 strncpy (txt, dbname, NAMEDATALEN - 1);
345 txt[NAMEDATALEN - 1] = 0;
346 if (sscanf (txt, "%[_a-zA-Z]%[^_a-zA-Z0-9-]", tmp, tmp) == 1)
347 return (TRUE);
348 if (sscanf (txt, "%[_a-zA-Z]%[_a-zA-Z0-9-]%[^_a-zA-Z0-9-]", tmp, tmp, tmp) ==
349 2) return (TRUE);
350 return (FALSE);
351 }
353 /**
355 the tango program should eventually create an entity here based on the
356 function prototype
358 @@-
359 <sect3>
360 <title>is_pg_logname</title>
362 <para>&PROTO_is_pg_logname;</para>
364 <para>Given a username, this function returns TRUE if the string is a
365 valid PostgreSQL username, and returns false if it is not. Valid PostgreSQL
366 usernames are less than &NAMEDATALEN; characters long and consist of
367 letters, numbers, dashes, and underscores, plus possibly some other
368 characters.</para>
370 <para>Currently this function only checks string length. Additional checks
371 should be added.</para>
373 </sect3>
374 -@@
375 ******************************************************************************/
379 int
380 is_pg_logname (char *username)
381 {
382 if (strlen (username) > NAMEDATALEN - 1)
383 return (FALSE);
384 return (TRUE);
385 }
387 /******************************************************************************
388 @@-
389 </sect2>
390 </sect1>
391 </article>
392 -@@
393 ******************************************************************************/
397 void
398 print_help (void)
399 {
400 char *myport;
402 asprintf (&myport, "%d", DEFAULT_PORT);
404 print_revision (progname, revision);
406 printf (COPYRIGHT, copyright, email);
408 printf (_("Test whether a PostgreSQL Database is accepting connections."));
410 printf ("\n\n");
412 print_usage ();
414 printf (_(UT_HELP_VRSN));
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));
434 printf ("\n");
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."));
450 printf (_(UT_SUPPORT));
451 }
455 void
456 print_usage (void)
457 {
458 printf (_("Usage:"));
459 printf ("%s [-H <host>] [-P <port>] [-c <critical time>] [-w <warning time>]\n", progname);
460 printf (" [-t <timeout>] [-d <database>] [-l <logname>] [-p <password>]\n");
461 }