1 /******************************************************************************
2 *
3 * Program: PostgreSQL plugin for Nagios
4 * License: GPL
5 *
6 * License Information:
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
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 *****************************************************************************/
26 const char *progname = "check_pgsql";
27 #define REVISION "$Revision$"
28 #define COPYRIGHT "1999-2001"
29 #define AUTHOR "Karl DeBisschop"
30 #define EMAIL "kdebisschop@users.sourceforge.net"
31 #define SUMMARY "Tests to see if a PostgreSQL DBMS is accepting connections.\n"
33 #define OPTIONS "[-c critical_time] [-w warning_time] [-t timeout] [-H host]\n\
34 [-P port] [-d database] [-l logname] [-p password]"
36 #define LONGOPTIONS "\
37 -c, --critical=INTEGER\n\
38 Exit STATE_CRITICAL if connection time exceeds threshold (default: %d)\n\
39 -w, --warning=INTEGER\n\
40 Exit STATE_WARNING if connection time exceeds threshold (default: %d)\n\
41 -t, --timeout=INTEGER\n\
42 Terminate test if timeout limit is exceeded (default: %d)\n\
43 -H, --hostname=STRING\n\
44 Name or numeric IP address of machine running backend\n\
45 -P, --port=INTEGER\n\
46 Port running backend (default: %d)\n\
47 -d, --database=STRING\n\
48 Database to check (default: %s)\n\
49 -l, --logname = STRING\n\
50 Login name of user\n\
51 -p, --password = STRING\n\
52 Password (BIG SECURITY ISSUE)\n"
54 #define DESCRIPTION "All parameters are optional.\n\
55 \n\
56 This plugin tests a PostgreSQL DBMS to determine whether it is active and\n\
57 accepting queries. In its current operation, it simply connects to the\n\
58 specified database, and then disconnects. If no database is specified, it\n\
59 connects to the template1 database, which is present in every functioning \n\
60 PostgreSQL DBMS.\n\
61 \n\
62 The plugin will connect to a local postmaster if no host is specified. To\n\
63 connect to a remote host, be sure that the remote postmaster accepts TCP/IP\n\
64 connections (start the postmaster with the -i option).\n\
65 \n\
66 Typically, the nagios user (unless the --logname option is used) should be\n\
67 able to connect to the database without a password. The plugin can also send\n\
68 a password, but no effort is made to obsure or encrypt the password.\n"
70 #define DEFAULT_DB "template1"
71 #define DEFAULT_HOST "127.0.0.1"
72 enum {
73 DEFAULT_PORT = 5432,
74 DEFAULT_WARN = 2,
75 DEFAULT_CRIT = 8,
76 DEFAULT_TIMEOUT = 30
77 };
79 #include "config.h"
80 #include "common.h"
81 #include "utils.h"
82 #include <netdb.h>
83 #include <libpq-fe.h>
85 int process_arguments (int, char **);
86 int validate_arguments (void);
87 void print_usage (void);
88 void print_help (void);
89 int is_pg_dbname (char *);
90 int is_pg_logname (char *);
92 char *pghost = NULL; /* host name of the backend server */
93 char *pgport = NULL; /* port of the backend server */
94 int default_port = DEFAULT_PORT;
95 char *pgoptions = NULL;
96 char *pgtty = NULL;
97 char dbName[NAMEDATALEN] = DEFAULT_DB;
98 char *pguser = NULL;
99 char *pgpasswd = NULL;
100 int twarn = DEFAULT_WARN;
101 int tcrit = DEFAULT_CRIT;
103 PGconn *conn;
104 /*PGresult *res;*/
105 \f
107 /******************************************************************************
109 The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@
110 tags in the comments. With in the tags, the XML is assembled sequentially.
111 You can define entities in tags. You also have all the #defines available as
112 entities.
114 Please note that all tags must be lowercase to use the DocBook XML DTD.
116 @@-<article>
118 <sect1>
119 <title>Quick Reference</title>
120 <!-- The refentry forms a manpage -->
121 <refentry>
122 <refmeta>
123 <manvolnum>5<manvolnum>
124 </refmeta>
125 <refnamdiv>
126 <refname>&progname;</refname>
127 <refpurpose>&SUMMARY;</refpurpose>
128 </refnamdiv>
129 </refentry>
130 </sect1>
132 <sect1>
133 <title>FAQ</title>
134 </sect1>
136 <sect1>
137 <title>Theory, Installation, and Operation</title>
139 <sect2>
140 <title>General Description</title>
141 <para>
142 &DESCRIPTION;
143 </para>
144 </sect2>
146 <sect2>
147 <title>Future Enhancements</title>
148 <para>ToDo List</para>
149 <itemizedlist>
150 <listitem>Add option to get password from a secured file rather than the command line</listitem>
151 <listitem>Add option to specify the query to execute</listitem>
152 </itemizedlist>
153 </sect2>
156 <sect2>
157 <title>Functions</title>
158 -@@
159 ******************************************************************************/
161 int
162 main (int argc, char **argv)
163 {
164 int elapsed_time;
166 /* begin, by setting the parameters for a backend connection if the
167 * parameters are null, then the system will try to use reasonable
168 * defaults by looking up environment variables or, failing that,
169 * using hardwired constants */
171 pgoptions = NULL; /* special options to start up the backend server */
172 pgtty = NULL; /* debugging tty for the backend server */
174 if (process_arguments (argc, argv) == ERROR)
175 usage ("Could not parse arguments");
177 /* Set signal handling and alarm */
178 if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
179 printf ("Cannot catch SIGALRM");
180 return STATE_UNKNOWN;
181 }
182 alarm (timeout_interval);
184 /* make a connection to the database */
185 time (&start_time);
186 conn =
187 PQsetdbLogin (pghost, pgport, pgoptions, pgtty, dbName, pguser, pgpasswd);
188 time (&end_time);
189 elapsed_time = (int) (end_time - start_time);
191 /* check to see that the backend connection was successfully made */
192 if (PQstatus (conn) == CONNECTION_BAD) {
193 printf ("PGSQL: CRITICAL - no connection to '%s' (%s).\n", dbName,
194 PQerrorMessage (conn));
195 PQfinish (conn);
196 return STATE_CRITICAL;
197 }
198 else if (elapsed_time > tcrit) {
199 PQfinish (conn);
200 printf ("PGSQL: CRITICAL - database %s (%d sec.)\n", dbName,
201 elapsed_time);
202 return STATE_CRITICAL;
203 }
204 else if (elapsed_time > twarn) {
205 PQfinish (conn);
206 printf ("PGSQL: WARNING - database %s (%d sec.)\n", dbName, elapsed_time);
207 return STATE_WARNING;
208 }
209 else {
210 PQfinish (conn);
211 printf ("PGSQL: ok - database %s (%d sec.)\n", dbName, elapsed_time);
212 return STATE_OK;
213 }
214 }
215 \f
219 void
220 print_help (void)
221 {
222 print_revision (progname, REVISION);
223 printf
224 ("Copyright (c) %s %s <%s>\n\n%s\n",
225 COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
226 print_usage ();
227 printf
228 ("\nOptions:\n" LONGOPTIONS "\n" DESCRIPTION "\n",
229 DEFAULT_WARN, DEFAULT_CRIT, DEFAULT_TIMEOUT, DEFAULT_PORT, DEFAULT_DB);
230 support ();
231 }
233 void
234 print_usage (void)
235 {
236 printf ("Usage:\n" " %s %s\n"
237 " %s (-h | --help) for detailed help\n"
238 " %s (-V | --version) for version information\n",
239 progname, OPTIONS, progname, progname);
240 }
241 \f
244 /* process command-line arguments */
245 int
246 process_arguments (int argc, char **argv)
247 {
248 int c;
250 #ifdef HAVE_GETOPT_H
251 int option_index = 0;
252 static struct option long_options[] = {
253 {"help", no_argument, 0, 'h'},
254 {"version", no_argument, 0, 'V'},
255 {"timeout", required_argument, 0, 't'},
256 {"critical", required_argument, 0, 'c'},
257 {"warning", required_argument, 0, 'w'},
258 {"hostname", required_argument, 0, 'H'},
259 {"logname", required_argument, 0, 'l'},
260 {"password", required_argument, 0, 'p'},
261 {"authorization", required_argument, 0, 'a'},
262 {"port", required_argument, 0, 'P'},
263 {"database", required_argument, 0, 'd'},
264 {0, 0, 0, 0}
265 };
266 #endif
268 while (1) {
269 #ifdef HAVE_GETOPT_H
270 c = getopt_long (argc, argv, "hVt:c:w:H:P:d:l:p:a:",
271 long_options, &option_index);
272 #else
273 c = getopt (argc, argv, "hVt:c:w:H:P:d:l:p:a:");
274 #endif
275 if (c == EOF)
276 break;
278 switch (c) {
279 case '?': /* usage */
280 usage3 ("Unknown argument", optopt);
281 case 'h': /* help */
282 print_help ();
283 exit (STATE_OK);
284 case 'V': /* version */
285 print_revision (progname, REVISION);
286 exit (STATE_OK);
287 case 't': /* timeout period */
288 if (!is_integer (optarg))
289 usage2 ("Timeout Interval must be an integer", optarg);
290 timeout_interval = atoi (optarg);
291 break;
292 case 'c': /* critical time threshold */
293 if (!is_integer (optarg))
294 usage2 ("Invalid critical threshold", optarg);
295 tcrit = atoi (optarg);
296 break;
297 case 'w': /* warning time threshold */
298 if (!is_integer (optarg))
299 usage2 ("Invalid critical threshold", optarg);
300 twarn = atoi (optarg);
301 break;
302 case 'H': /* host */
303 if (!is_host (optarg))
304 usage2 ("You gave an invalid host name", optarg);
305 pghost = optarg;
306 break;
307 case 'P': /* port */
308 if (!is_integer (optarg))
309 usage2 ("Port must be an integer", optarg);
310 pgport = optarg;
311 break;
312 case 'd': /* database name */
313 if (!is_pg_dbname (optarg))
314 usage2 ("Database name is not valid", optarg);
315 strncpy (dbName, optarg, NAMEDATALEN - 1);
316 dbName[NAMEDATALEN - 1] = 0;
317 break;
318 case 'l': /* login name */
319 if (!is_pg_logname (optarg))
320 usage2 ("user name is not valid", optarg);
321 pguser = optarg;
322 break;
323 case 'p': /* authentication password */
324 case 'a':
325 pgpasswd = optarg;
326 break;
327 }
328 }
330 return validate_arguments ();
331 }
334 /******************************************************************************
336 @@-
337 <sect3>
338 <title>validate_arguments</title>
340 <para>&PROTO_validate_arguments;</para>
342 <para>Given a database name, this function returns TRUE if the string
343 is a valid PostgreSQL database name, and returns false if it is
344 not.</para>
346 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
347 characters long and consist of letters, numbers, and underscores. The
348 first character cannot be a number, however.</para>
350 </sect3>
351 -@@
352 ******************************************************************************/
354 int
355 validate_arguments ()
356 {
357 return OK;
358 }
359 \f
362 /******************************************************************************
364 @@-
365 <sect3>
366 <title>is_pg_dbname</title>
368 <para>&PROTO_is_pg_dbname;</para>
370 <para>Given a database name, this function returns TRUE if the string
371 is a valid PostgreSQL database name, and returns false if it is
372 not.</para>
374 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
375 characters long and consist of letters, numbers, and underscores. The
376 first character cannot be a number, however.</para>
378 </sect3>
379 -@@
380 ******************************************************************************/
382 int
383 is_pg_dbname (char *dbname)
384 {
385 char txt[NAMEDATALEN];
386 char tmp[NAMEDATALEN];
387 if (strlen (dbname) > NAMEDATALEN - 1)
388 return (FALSE);
389 strncpy (txt, dbname, NAMEDATALEN - 1);
390 txt[NAMEDATALEN - 1] = 0;
391 if (sscanf (txt, "%[_a-zA-Z]%[^_a-zA-Z0-9]", tmp, tmp) == 1)
392 return (TRUE);
393 if (sscanf (txt, "%[_a-zA-Z]%[_a-zA-Z0-9]%[^_a-zA-Z0-9]", tmp, tmp, tmp) ==
394 2) return (TRUE);
395 return (FALSE);
396 }
398 /**
400 the tango program should eventually create an entity here based on the
401 function prototype
403 @@-
404 <sect3>
405 <title>is_pg_logname</title>
407 <para>&PROTO_is_pg_logname;</para>
409 <para>Given a username, this function returns TRUE if the string is a
410 valid PostgreSQL username, and returns false if it is not. Valid PostgreSQL
411 usernames are less than &NAMEDATALEN; characters long and consist of
412 letters, numbers, dashes, and underscores, plus possibly some other
413 characters.</para>
415 <para>Currently this function only checks string length. Additional checks
416 should be added.</para>
418 </sect3>
419 -@@
420 ******************************************************************************/
422 int
423 is_pg_logname (char *username)
424 {
425 if (strlen (username) > NAMEDATALEN - 1)
426 return (FALSE);
427 return (TRUE);
428 }
430 /******************************************************************************
431 @@-
432 </sect2>
433 </sect1>
434 </article>
435 -@@
436 ******************************************************************************/