check_pgsql.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. *****************************************************************************/
  33. const char *progname = "check_pgsql";
  34. const char *revision = "$Revision$";
  35. const char *copyright = "1999-2007";
  36. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  37. #include "common.h"
  38. #include "utils.h"
  39. #include "netutils.h"
  40. #include <libpq-fe.h>
  41. #include <pg_config_manual.h>
  42. #define DEFAULT_DB "template1"
  43. #define DEFAULT_HOST "127.0.0.1"
  44. enum {
  45. DEFAULT_PORT = 5432,
  46. DEFAULT_WARN = 2,
  47. DEFAULT_CRIT = 8
  48. };
  49. int process_arguments (int, char **);
  50. int validate_arguments (void);
  51. void print_usage (void);
  52. void print_help (void);
  53. int is_pg_dbname (char *);
  54. int is_pg_logname (char *);
  55. char *pghost = NULL; /* host name of the backend server */
  56. char *pgport = NULL; /* port of the backend server */
  57. int default_port = DEFAULT_PORT;
  58. char *pgoptions = NULL;
  59. char *pgtty = NULL;
  60. char dbName[NAMEDATALEN] = DEFAULT_DB;
  61. char *pguser = NULL;
  62. char *pgpasswd = NULL;
  63. double twarn = (double)DEFAULT_WARN;
  64. double tcrit = (double)DEFAULT_CRIT;
  65. PGconn *conn;
  66. /*PGresult *res;*/
  67. /******************************************************************************
  68. The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@
  69. tags in the comments. With in the tags, the XML is assembled sequentially.
  70. You can define entities in tags. You also have all the #defines available as
  71. entities.
  72. Please note that all tags must be lowercase to use the DocBook XML DTD.
  73. @@-<article>
  74. <sect1>
  75. <title>Quick Reference</title>
  76. <!-- The refentry forms a manpage -->
  77. <refentry>
  78. <refmeta>
  79. <manvolnum>5<manvolnum>
  80. </refmeta>
  81. <refnamdiv>
  82. <refname>&progname;</refname>
  83. <refpurpose>&SUMMARY;</refpurpose>
  84. </refnamdiv>
  85. </refentry>
  86. </sect1>
  87. <sect1>
  88. <title>FAQ</title>
  89. </sect1>
  90. <sect1>
  91. <title>Theory, Installation, and Operation</title>
  92. <sect2>
  93. <title>General Description</title>
  94. <para>
  95. &DESCRIPTION;
  96. </para>
  97. </sect2>
  98. <sect2>
  99. <title>Future Enhancements</title>
  100. <para>ToDo List</para>
  101. <itemizedlist>
  102. <listitem>Add option to get password from a secured file rather than the command line</listitem>
  103. <listitem>Add option to specify the query to execute</listitem>
  104. </itemizedlist>
  105. </sect2>
  106. <sect2>
  107. <title>Functions</title>
  108. -@@
  109. ******************************************************************************/
  110. int
  111. main (int argc, char **argv)
  112. {
  113. int elapsed_time;
  114. int status = STATE_UNKNOWN;
  115. /* begin, by setting the parameters for a backend connection if the
  116. * parameters are null, then the system will try to use reasonable
  117. * defaults by looking up environment variables or, failing that,
  118. * using hardwired constants */
  119. pgoptions = NULL; /* special options to start up the backend server */
  120. pgtty = NULL; /* debugging tty for the backend server */
  121. setlocale (LC_ALL, "");
  122. bindtextdomain (PACKAGE, LOCALEDIR);
  123. textdomain (PACKAGE);
  124. /* Parse extra opts if any */
  125. argv=np_extra_opts (&argc, argv, progname);
  126. if (process_arguments (argc, argv) == ERROR)
  127. usage4 (_("Could not parse arguments"));
  128. /* Set signal handling and alarm */
  129. if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
  130. usage4 (_("Cannot catch SIGALRM"));
  131. }
  132. alarm (timeout_interval);
  133. /* make a connection to the database */
  134. time (&start_time);
  135. conn =
  136. PQsetdbLogin (pghost, pgport, pgoptions, pgtty, dbName, pguser, pgpasswd);
  137. time (&end_time);
  138. elapsed_time = (int) (end_time - start_time);
  139. /* check to see that the backend connection was successfully made */
  140. if (PQstatus (conn) == CONNECTION_BAD) {
  141. printf (_("CRITICAL - no connection to '%s' (%s).\n"),
  142. dbName, PQerrorMessage (conn));
  143. PQfinish (conn);
  144. return STATE_CRITICAL;
  145. }
  146. else if (elapsed_time > tcrit) {
  147. status = STATE_CRITICAL;
  148. }
  149. else if (elapsed_time > twarn) {
  150. status = STATE_WARNING;
  151. }
  152. else {
  153. status = STATE_OK;
  154. }
  155. PQfinish (conn);
  156. printf (_(" %s - database %s (%d sec.)|%s\n"),
  157. state_text(status), dbName, elapsed_time,
  158. fperfdata("time", elapsed_time, "s",
  159. (int)twarn, twarn, (int)tcrit, tcrit, TRUE, 0, FALSE,0));
  160. return status;
  161. }
  162. /* process command-line arguments */
  163. int
  164. process_arguments (int argc, char **argv)
  165. {
  166. int c;
  167. int option = 0;
  168. static struct option longopts[] = {
  169. {"help", no_argument, 0, 'h'},
  170. {"version", no_argument, 0, 'V'},
  171. {"timeout", required_argument, 0, 't'},
  172. {"critical", required_argument, 0, 'c'},
  173. {"warning", required_argument, 0, 'w'},
  174. {"hostname", required_argument, 0, 'H'},
  175. {"logname", required_argument, 0, 'l'},
  176. {"password", required_argument, 0, 'p'},
  177. {"authorization", required_argument, 0, 'a'},
  178. {"port", required_argument, 0, 'P'},
  179. {"database", required_argument, 0, 'd'},
  180. {0, 0, 0, 0}
  181. };
  182. while (1) {
  183. c = getopt_long (argc, argv, "hVt:c:w:H:P:d:l:p:a:",
  184. longopts, &option);
  185. if (c == EOF)
  186. break;
  187. switch (c) {
  188. case '?': /* usage */
  189. usage5 ();
  190. case 'h': /* help */
  191. print_help ();
  192. exit (STATE_OK);
  193. case 'V': /* version */
  194. print_revision (progname, revision);
  195. exit (STATE_OK);
  196. case 't': /* timeout period */
  197. if (!is_integer (optarg))
  198. usage2 (_("Timeout interval must be a positive integer"), optarg);
  199. else
  200. timeout_interval = atoi (optarg);
  201. break;
  202. case 'c': /* critical time threshold */
  203. if (!is_nonnegative (optarg))
  204. usage2 (_("Critical threshold must be a positive integer"), optarg);
  205. else
  206. tcrit = strtod (optarg, NULL);
  207. break;
  208. case 'w': /* warning time threshold */
  209. if (!is_nonnegative (optarg))
  210. usage2 (_("Warning threshold must be a positive integer"), optarg);
  211. else
  212. twarn = strtod (optarg, NULL);
  213. break;
  214. case 'H': /* host */
  215. if (!is_host (optarg))
  216. usage2 (_("Invalid hostname/address"), optarg);
  217. else
  218. pghost = optarg;
  219. break;
  220. case 'P': /* port */
  221. if (!is_integer (optarg))
  222. usage2 (_("Port must be a positive integer"), optarg);
  223. else
  224. pgport = optarg;
  225. break;
  226. case 'd': /* database name */
  227. if (!is_pg_dbname (optarg)) /* checks length and valid chars */
  228. usage2 (_("Database name is not valid"), optarg);
  229. else /* we know length, and know optarg is terminated, so us strcpy */
  230. strcpy (dbName, optarg);
  231. break;
  232. case 'l': /* login name */
  233. if (!is_pg_logname (optarg))
  234. usage2 (_("User name is not valid"), optarg);
  235. else
  236. pguser = optarg;
  237. break;
  238. case 'p': /* authentication password */
  239. case 'a':
  240. pgpasswd = optarg;
  241. break;
  242. }
  243. }
  244. return validate_arguments ();
  245. }
  246. /******************************************************************************
  247. @@-
  248. <sect3>
  249. <title>validate_arguments</title>
  250. <para>&PROTO_validate_arguments;</para>
  251. <para>Given a database name, this function returns TRUE if the string
  252. is a valid PostgreSQL database name, and returns false if it is
  253. not.</para>
  254. <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
  255. characters long and consist of letters, numbers, and underscores. The
  256. first character cannot be a number, however.</para>
  257. </sect3>
  258. -@@
  259. ******************************************************************************/
  260. int
  261. validate_arguments ()
  262. {
  263. return OK;
  264. }
  265. /******************************************************************************
  266. @@-
  267. <sect3>
  268. <title>is_pg_dbname</title>
  269. <para>&PROTO_is_pg_dbname;</para>
  270. <para>Given a database name, this function returns TRUE if the string
  271. is a valid PostgreSQL database name, and returns false if it is
  272. not.</para>
  273. <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
  274. characters long and consist of letters, numbers, and underscores. The
  275. first character cannot be a number, however.</para>
  276. </sect3>
  277. -@@
  278. ******************************************************************************/
  279. int
  280. is_pg_dbname (char *dbname)
  281. {
  282. char txt[NAMEDATALEN];
  283. char tmp[NAMEDATALEN];
  284. if (strlen (dbname) > NAMEDATALEN - 1)
  285. return (FALSE);
  286. strncpy (txt, dbname, NAMEDATALEN - 1);
  287. txt[NAMEDATALEN - 1] = 0;
  288. if (sscanf (txt, "%[_a-zA-Z]%[^_a-zA-Z0-9-]", tmp, tmp) == 1)
  289. return (TRUE);
  290. if (sscanf (txt, "%[_a-zA-Z]%[_a-zA-Z0-9-]%[^_a-zA-Z0-9-]", tmp, tmp, tmp) ==
  291. 2) return (TRUE);
  292. return (FALSE);
  293. }
  294. /**
  295. the tango program should eventually create an entity here based on the
  296. function prototype
  297. @@-
  298. <sect3>
  299. <title>is_pg_logname</title>
  300. <para>&PROTO_is_pg_logname;</para>
  301. <para>Given a username, this function returns TRUE if the string is a
  302. valid PostgreSQL username, and returns false if it is not. Valid PostgreSQL
  303. usernames are less than &NAMEDATALEN; characters long and consist of
  304. letters, numbers, dashes, and underscores, plus possibly some other
  305. characters.</para>
  306. <para>Currently this function only checks string length. Additional checks
  307. should be added.</para>
  308. </sect3>
  309. -@@
  310. ******************************************************************************/
  311. int
  312. is_pg_logname (char *username)
  313. {
  314. if (strlen (username) > NAMEDATALEN - 1)
  315. return (FALSE);
  316. return (TRUE);
  317. }
  318. /******************************************************************************
  319. @@-
  320. </sect2>
  321. </sect1>
  322. </article>
  323. -@@
  324. ******************************************************************************/
  325. void
  326. print_help (void)
  327. {
  328. char *myport;
  329. asprintf (&myport, "%d", DEFAULT_PORT);
  330. print_revision (progname, revision);
  331. printf (COPYRIGHT, copyright, email);
  332. printf (_("Test whether a PostgreSQL Database is accepting connections."));
  333. printf ("\n\n");
  334. print_usage ();
  335. printf (_(UT_HELP_VRSN));
  336. printf (_(UT_EXTRA_OPTS));
  337. printf (_(UT_HOST_PORT), 'P', myport);
  338. printf (_(UT_IPv46));
  339. printf (" %s\n", "-d, --database=STRING");
  340. printf (" %s", _("Database to check "));
  341. printf (_("(default: %s)"), DEFAULT_DB);
  342. printf (" %s\n", "-l, --logname = STRING");
  343. printf (" %s\n", _("Login name of user"));
  344. printf (" %s\n", "-p, --password = STRING");
  345. printf (" %s\n", _("Password (BIG SECURITY ISSUE)"));
  346. printf (_(UT_WARN_CRIT));
  347. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  348. printf (_(UT_VERBOSE));
  349. printf ("\n");
  350. printf (" %s\n", _("All parameters are optional."));
  351. printf (" %s\n", _("This plugin tests a PostgreSQL DBMS to determine whether it is active and"));
  352. printf (" %s\n", _("accepting queries. In its current operation, it simply connects to the"));
  353. printf (" %s\n", _("specified database, and then disconnects. If no database is specified, it"));
  354. printf (" %s\n", _("connects to the template1 database, which is present in every functioning"));
  355. printf (" %s\n\n", _("PostgreSQL DBMS."));
  356. printf (" %s\n", _("The plugin will connect to a local postmaster if no host is specified. To"));
  357. printf (" %s\n", _("connect to a remote host, be sure that the remote postmaster accepts TCP/IP"));
  358. printf (" %s\n\n", _("connections (start the postmaster with the -i option)."));
  359. printf (" %s\n", _("Typically, the nagios user (unless the --logname option is used) should be"));
  360. printf (" %s\n", _("able to connect to the database without a password. The plugin can also send"));
  361. printf (" %s\n", _("a password, but no effort is made to obsure or encrypt the password."));
  362. #ifdef NP_EXTRA_OPTS
  363. printf ("\n");
  364. printf ("%s\n", _("Notes:"));
  365. printf (_(UT_EXTRA_OPTS_NOTES));
  366. #endif
  367. printf (_(UT_SUPPORT));
  368. }
  369. void
  370. print_usage (void)
  371. {
  372. printf (_("Usage:"));
  373. printf ("%s [-H <host>] [-P <port>] [-c <critical time>] [-w <warning time>]\n", progname);
  374. printf (" [-t <timeout>] [-d <database>] [-l <logname>] [-p <password>]\n");
  375. }