Code

markup for translation
[nagiosplug.git] / plugins / check_pgsql.c
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 #define DEFAULT_DB "template1"
27 #define DEFAULT_HOST "127.0.0.1"
29 enum {
30         DEFAULT_PORT = 5432,
31         DEFAULT_WARN = 2,
32         DEFAULT_CRIT = 8
33 };
35 #include "config.h"
36 #include "common.h"
37 #include "utils.h"
38 #include <libpq-fe.h>
40 int process_arguments (int, char **);
41 int validate_arguments (void);
42 void print_usage (void);
43 void print_help (void);
44 int is_pg_dbname (char *);
45 int is_pg_logname (char *);
47 char *pghost = NULL;                                            /* host name of the backend server */
48 char *pgport = NULL;                                            /* port of the backend server */
49 int default_port = DEFAULT_PORT;
50 char *pgoptions = NULL;
51 char *pgtty = NULL;
52 char dbName[NAMEDATALEN] = DEFAULT_DB;
53 char *pguser = NULL;
54 char *pgpasswd = NULL;
55 int twarn = DEFAULT_WARN;
56 int tcrit = DEFAULT_CRIT;
58 PGconn *conn;
59 /*PGresult   *res;*/
61 const char *progname = "check_pgsql";
62 const char *revision = "$Revision$";
63 const char *copyright = "1999-2003";
64 const char *email = "nagiosplug-devel@lists.sourceforge.net";
65 \f
67 /******************************************************************************
69 The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@
70 tags in the comments. With in the tags, the XML is assembled sequentially.
71 You can define entities in tags. You also have all the #defines available as
72 entities.
74 Please note that all tags must be lowercase to use the DocBook XML DTD.
76 @@-<article>
78 <sect1>
79 <title>Quick Reference</title>
80 <!-- The refentry forms a manpage -->
81 <refentry>
82 <refmeta>
83 <manvolnum>5<manvolnum>
84 </refmeta>
85 <refnamdiv>
86 <refname>&progname;</refname>
87 <refpurpose>&SUMMARY;</refpurpose>
88 </refnamdiv>
89 </refentry>
90 </sect1>
92 <sect1>
93 <title>FAQ</title>
94 </sect1>
96 <sect1>
97 <title>Theory, Installation, and Operation</title>
99 <sect2>
100 <title>General Description</title>
101 <para>
102 &DESCRIPTION;
103 </para>
104 </sect2>
106 <sect2>
107 <title>Future Enhancements</title>
108 <para>ToDo List</para>
109 <itemizedlist>
110 <listitem>Add option to get password from a secured file rather than the command line</listitem>
111 <listitem>Add option to specify the query to execute</listitem>
112 </itemizedlist>
113 </sect2>
116 <sect2>
117 <title>Functions</title>
118 -@@
119 ******************************************************************************/
120 \f
124 void
125 print_help (void)
127         char *myport;
129         asprintf (&myport, "%d", DEFAULT_PORT);
131         print_revision (progname, revision);
133         printf (_(COPYRIGHT), copyright, email);
135         printf (_("Test whether a PostgreSQL DBMS is accepting connections.\n\n"));
137         print_usage ();
139         printf (_(HELP_VRSN));
141         printf (_(HOST_PORT), 'P', myport);
143         printf (_(IPv46));
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);
153         printf (_(WARN_CRIT_TO), DEFAULT_SOCKET_TIMEOUT);
155         printf (_(VRBS));
157         printf (S_("\nAll parameters are optional.\n\
158 \n\
159 This plugin tests a PostgreSQL DBMS to determine whether it is active and\n\
160 accepting queries. In its current operation, it simply connects to the\n\
161 specified database, and then disconnects. If no database is specified, it\n\
162 connects to the template1 database, which is present in every functioning \n\
163 PostgreSQL DBMS.\n"));
164         printf (S_("\n\
165 The plugin will connect to a local postmaster if no host is specified. To\n\
166 connect to a remote host, be sure that the remote postmaster accepts TCP/IP\n\
167 connections (start the postmaster with the -i option).\n"));
168         printf (S_("\n\
169 Typically, the nagios user (unless the --logname option is used) should be\n\
170 able to connect to the database without a password. The plugin can also send\n\
171 a password, but no effort is made to obsure or encrypt the password.\n"));
173         support ();
176 void
177 print_usage (void)
179         printf (S_("\
180 Usage:\n %s [-H <host>] [-P <port>] [-c <critical time>] [-w <warning time>]\n\
181             [-t <timeout>]"), progname);
182         printf (S_("[-d <database>] [-l <logname>] [-p <password>]\n"));
183         printf (S_("\
184          %s (-h | --help) for detailed help\n\
185          %s (-V | --version) for version information\n"),
186                                         progname, progname);
189 int
190 main (int argc, char **argv)
192         int elapsed_time;
194         /* begin, by setting the parameters for a backend connection if the
195          * parameters are null, then the system will try to use reasonable
196          * defaults by looking up environment variables or, failing that,
197          * using hardwired constants */
199         pgoptions = NULL;  /* special options to start up the backend server */
200         pgtty = NULL;      /* debugging tty for the backend server */
202         if (process_arguments (argc, argv) == ERROR)
203                 usage ("Could not parse arguments");
205         /* Set signal handling and alarm */
206         if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
207                 printf (_("Cannot catch SIGALRM"));
208                 return STATE_UNKNOWN;
209         }
210         alarm (timeout_interval);
212         /* make a connection to the database */
213         time (&start_time);
214         conn =
215                 PQsetdbLogin (pghost, pgport, pgoptions, pgtty, dbName, pguser, pgpasswd);
216         time (&end_time);
217         elapsed_time = (int) (end_time - start_time);
219         /* check to see that the backend connection was successfully made */
220         if (PQstatus (conn) == CONNECTION_BAD) {
221                 printf (_("PGSQL: CRITICAL - no connection to '%s' (%s).\n"), dbName,
222                                                 PQerrorMessage (conn));
223                 PQfinish (conn);
224                 return STATE_CRITICAL;
225         }
226         else if (elapsed_time > tcrit) {
227                 PQfinish (conn);
228                 printf (_("PGSQL: CRITICAL - database %s (%d sec.)\n"), dbName,
229                                                 elapsed_time);
230                 return STATE_CRITICAL;
231         }
232         else if (elapsed_time > twarn) {
233                 PQfinish (conn);
234                 printf (_("PGSQL: WARNING - database %s (%d sec.)\n"), dbName, elapsed_time);
235                 return STATE_WARNING;
236         }
237         else {
238                 PQfinish (conn);
239                 printf (_("PGSQL: ok - database %s (%d sec.)\n"), dbName, elapsed_time);
240                 return STATE_OK;
241         }
243 \f
246 /* process command-line arguments */
247 int
248 process_arguments (int argc, char **argv)
250         int c;
252         int option_index = 0;
253         static struct option long_options[] = {
254                 {"help", no_argument, 0, 'h'},
255                 {"version", no_argument, 0, 'V'},
256                 {"timeout", required_argument, 0, 't'},
257                 {"critical", required_argument, 0, 'c'},
258                 {"warning", required_argument, 0, 'w'},
259                 {"hostname", required_argument, 0, 'H'},
260                 {"logname", required_argument, 0, 'l'},
261                 {"password", required_argument, 0, 'p'},
262                 {"authorization", required_argument, 0, 'a'},
263                 {"port", required_argument, 0, 'P'},
264                 {"database", required_argument, 0, 'd'},
265                 {0, 0, 0, 0}
266         };
268         while (1) {
269                 c = getopt_long (argc, argv, "hVt:c:w:H:P:d:l:p:a:",
270                                  long_options, &option_index);
272                 if (c == EOF)
273                         break;
275                 switch (c) {
276                 case '?':     /* usage */
277                         usage3 (_("Unknown argument"), optopt);
278                 case 'h':     /* help */
279                         print_help ();
280                         exit (STATE_OK);
281                 case 'V':     /* version */
282                         print_revision (progname, revision);
283                         exit (STATE_OK);
284                 case 't':     /* timeout period */
285                         if (!is_integer (optarg))
286                                 usage2 (_("Timeout Interval must be an integer"), optarg);
287                         timeout_interval = atoi (optarg);
288                         break;
289                 case 'c':     /* critical time threshold */
290                         if (!is_integer (optarg))
291                                 usage2 (_("Invalid critical threshold"), optarg);
292                         tcrit = atoi (optarg);
293                         break;
294                 case 'w':     /* warning time threshold */
295                         if (!is_integer (optarg))
296                                 usage2 (_("Invalid critical threshold"), optarg);
297                         twarn = atoi (optarg);
298                         break;
299                 case 'H':     /* host */
300                         if (!is_host (optarg))
301                                 usage2 (_("You gave an invalid host name"), optarg);
302                         pghost = optarg;
303                         break;
304                 case 'P':     /* port */
305                         if (!is_integer (optarg))
306                                 usage2 (_("Port must be an integer"), optarg);
307                         pgport = optarg;
308                         break;
309                 case 'd':     /* database name */
310                         if (!is_pg_dbname (optarg))
311                                 usage2 (_("Database name is not valid"), optarg);
312                         strncpy (dbName, optarg, NAMEDATALEN - 1);
313                         dbName[NAMEDATALEN - 1] = 0;
314                         break;
315                 case 'l':     /* login name */
316                         if (!is_pg_logname (optarg))
317                                 usage2 (_("user name is not valid"), optarg);
318                         pguser = optarg;
319                         break;
320                 case 'p':     /* authentication password */
321                 case 'a':
322                         pgpasswd = optarg;
323                         break;
324                 }
325         }
327         return validate_arguments ();
331 /******************************************************************************
333 @@-
334 <sect3>
335 <title>validate_arguments</title>
337 <para>&PROTO_validate_arguments;</para>
339 <para>Given a database name, this function returns TRUE if the string
340 is a valid PostgreSQL database name, and returns false if it is
341 not.</para>
343 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
344 characters long and consist of letters, numbers, and underscores. The
345 first character cannot be a number, however.</para>
347 </sect3>
348 -@@
349 ******************************************************************************/
351 int
352 validate_arguments ()
354         return OK;
356 \f
359 /******************************************************************************
361 @@-
362 <sect3>
363 <title>is_pg_dbname</title>
365 <para>&PROTO_is_pg_dbname;</para>
367 <para>Given a database name, this function returns TRUE if the string
368 is a valid PostgreSQL database name, and returns false if it is
369 not.</para>
371 <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
372 characters long and consist of letters, numbers, and underscores. The
373 first character cannot be a number, however.</para>
375 </sect3>
376 -@@
377 ******************************************************************************/
379 int
380 is_pg_dbname (char *dbname)
382         char txt[NAMEDATALEN];
383         char tmp[NAMEDATALEN];
384         if (strlen (dbname) > NAMEDATALEN - 1)
385                 return (FALSE);
386         strncpy (txt, dbname, NAMEDATALEN - 1);
387         txt[NAMEDATALEN - 1] = 0;
388         if (sscanf (txt, "%[_a-zA-Z]%[^_a-zA-Z0-9]", tmp, tmp) == 1)
389                 return (TRUE);
390         if (sscanf (txt, "%[_a-zA-Z]%[_a-zA-Z0-9]%[^_a-zA-Z0-9]", tmp, tmp, tmp) ==
391                         2) return (TRUE);
392         return (FALSE);
395 /**
397 the tango program should eventually create an entity here based on the 
398 function prototype
400 @@-
401 <sect3>
402 <title>is_pg_logname</title>
404 <para>&PROTO_is_pg_logname;</para>
406 <para>Given a username, this function returns TRUE if the string is a
407 valid PostgreSQL username, and returns false if it is not. Valid PostgreSQL
408 usernames are less than &NAMEDATALEN; characters long and consist of
409 letters, numbers, dashes, and underscores, plus possibly some other
410 characters.</para>
412 <para>Currently this function only checks string length. Additional checks
413 should be added.</para>
415 </sect3>
416 -@@
417 ******************************************************************************/
419 int
420 is_pg_logname (char *username)
422         if (strlen (username) > NAMEDATALEN - 1)
423                 return (FALSE);
424         return (TRUE);
427 /******************************************************************************
428 @@-
429 </sect2> 
430 </sect1>
431 </article>
432 -@@
433 ******************************************************************************/