check_pgsql.c 12 KB

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