check_pgsql.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. if (process_arguments (argc, argv) == ERROR)
  125. usage4 (_("Could not parse arguments"));
  126. /* Set signal handling and alarm */
  127. if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
  128. usage4 (_("Cannot catch SIGALRM"));
  129. }
  130. alarm (timeout_interval);
  131. /* make a connection to the database */
  132. time (&start_time);
  133. conn =
  134. PQsetdbLogin (pghost, pgport, pgoptions, pgtty, dbName, pguser, pgpasswd);
  135. time (&end_time);
  136. elapsed_time = (int) (end_time - start_time);
  137. /* check to see that the backend connection was successfully made */
  138. if (PQstatus (conn) == CONNECTION_BAD) {
  139. printf (_("CRITICAL - no connection to '%s' (%s).\n"),
  140. dbName, PQerrorMessage (conn));
  141. PQfinish (conn);
  142. return STATE_CRITICAL;
  143. }
  144. else if (elapsed_time > tcrit) {
  145. status = STATE_CRITICAL;
  146. }
  147. else if (elapsed_time > twarn) {
  148. status = STATE_WARNING;
  149. }
  150. else {
  151. status = STATE_OK;
  152. }
  153. PQfinish (conn);
  154. printf (_(" %s - database %s (%d sec.)|%s\n"),
  155. state_text(status), dbName, elapsed_time,
  156. fperfdata("time", elapsed_time, "s",
  157. (int)twarn, twarn, (int)tcrit, tcrit, TRUE, 0, FALSE,0));
  158. return status;
  159. }
  160. /* process command-line arguments */
  161. int
  162. process_arguments (int argc, char **argv)
  163. {
  164. int c;
  165. int option = 0;
  166. static struct option longopts[] = {
  167. {"help", no_argument, 0, 'h'},
  168. {"version", no_argument, 0, 'V'},
  169. {"timeout", required_argument, 0, 't'},
  170. {"critical", required_argument, 0, 'c'},
  171. {"warning", required_argument, 0, 'w'},
  172. {"hostname", required_argument, 0, 'H'},
  173. {"logname", required_argument, 0, 'l'},
  174. {"password", required_argument, 0, 'p'},
  175. {"authorization", required_argument, 0, 'a'},
  176. {"port", required_argument, 0, 'P'},
  177. {"database", required_argument, 0, 'd'},
  178. {0, 0, 0, 0}
  179. };
  180. while (1) {
  181. c = getopt_long (argc, argv, "hVt:c:w:H:P:d:l:p:a:",
  182. longopts, &option);
  183. if (c == EOF)
  184. break;
  185. switch (c) {
  186. case '?': /* usage */
  187. usage5 ();
  188. case 'h': /* help */
  189. print_help ();
  190. exit (STATE_OK);
  191. case 'V': /* version */
  192. print_revision (progname, revision);
  193. exit (STATE_OK);
  194. case 't': /* timeout period */
  195. if (!is_integer (optarg))
  196. usage2 (_("Timeout interval must be a positive integer"), optarg);
  197. else
  198. timeout_interval = atoi (optarg);
  199. break;
  200. case 'c': /* critical time threshold */
  201. if (!is_nonnegative (optarg))
  202. usage2 (_("Critical threshold must be a positive integer"), optarg);
  203. else
  204. tcrit = strtod (optarg, NULL);
  205. break;
  206. case 'w': /* warning time threshold */
  207. if (!is_nonnegative (optarg))
  208. usage2 (_("Warning threshold must be a positive integer"), optarg);
  209. else
  210. twarn = strtod (optarg, NULL);
  211. break;
  212. case 'H': /* host */
  213. if (!is_host (optarg))
  214. usage2 (_("Invalid hostname/address"), optarg);
  215. else
  216. pghost = optarg;
  217. break;
  218. case 'P': /* port */
  219. if (!is_integer (optarg))
  220. usage2 (_("Port must be a positive integer"), optarg);
  221. else
  222. pgport = optarg;
  223. break;
  224. case 'd': /* database name */
  225. if (!is_pg_dbname (optarg)) /* checks length and valid chars */
  226. usage2 (_("Database name is not valid"), optarg);
  227. else /* we know length, and know optarg is terminated, so us strcpy */
  228. strcpy (dbName, optarg);
  229. break;
  230. case 'l': /* login name */
  231. if (!is_pg_logname (optarg))
  232. usage2 (_("User name is not valid"), optarg);
  233. else
  234. pguser = optarg;
  235. break;
  236. case 'p': /* authentication password */
  237. case 'a':
  238. pgpasswd = optarg;
  239. break;
  240. }
  241. }
  242. return validate_arguments ();
  243. }
  244. /******************************************************************************
  245. @@-
  246. <sect3>
  247. <title>validate_arguments</title>
  248. <para>&PROTO_validate_arguments;</para>
  249. <para>Given a database name, this function returns TRUE if the string
  250. is a valid PostgreSQL database name, and returns false if it is
  251. not.</para>
  252. <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
  253. characters long and consist of letters, numbers, and underscores. The
  254. first character cannot be a number, however.</para>
  255. </sect3>
  256. -@@
  257. ******************************************************************************/
  258. int
  259. validate_arguments ()
  260. {
  261. return OK;
  262. }
  263. /******************************************************************************
  264. @@-
  265. <sect3>
  266. <title>is_pg_dbname</title>
  267. <para>&PROTO_is_pg_dbname;</para>
  268. <para>Given a database name, this function returns TRUE if the string
  269. is a valid PostgreSQL database name, and returns false if it is
  270. not.</para>
  271. <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
  272. characters long and consist of letters, numbers, and underscores. The
  273. first character cannot be a number, however.</para>
  274. </sect3>
  275. -@@
  276. ******************************************************************************/
  277. int
  278. is_pg_dbname (char *dbname)
  279. {
  280. char txt[NAMEDATALEN];
  281. char tmp[NAMEDATALEN];
  282. if (strlen (dbname) > NAMEDATALEN - 1)
  283. return (FALSE);
  284. strncpy (txt, dbname, NAMEDATALEN - 1);
  285. txt[NAMEDATALEN - 1] = 0;
  286. if (sscanf (txt, "%[_a-zA-Z]%[^_a-zA-Z0-9-]", tmp, tmp) == 1)
  287. return (TRUE);
  288. if (sscanf (txt, "%[_a-zA-Z]%[_a-zA-Z0-9-]%[^_a-zA-Z0-9-]", tmp, tmp, tmp) ==
  289. 2) return (TRUE);
  290. return (FALSE);
  291. }
  292. /**
  293. the tango program should eventually create an entity here based on the
  294. function prototype
  295. @@-
  296. <sect3>
  297. <title>is_pg_logname</title>
  298. <para>&PROTO_is_pg_logname;</para>
  299. <para>Given a username, this function returns TRUE if the string is a
  300. valid PostgreSQL username, and returns false if it is not. Valid PostgreSQL
  301. usernames are less than &NAMEDATALEN; characters long and consist of
  302. letters, numbers, dashes, and underscores, plus possibly some other
  303. characters.</para>
  304. <para>Currently this function only checks string length. Additional checks
  305. should be added.</para>
  306. </sect3>
  307. -@@
  308. ******************************************************************************/
  309. int
  310. is_pg_logname (char *username)
  311. {
  312. if (strlen (username) > NAMEDATALEN - 1)
  313. return (FALSE);
  314. return (TRUE);
  315. }
  316. /******************************************************************************
  317. @@-
  318. </sect2>
  319. </sect1>
  320. </article>
  321. -@@
  322. ******************************************************************************/
  323. void
  324. print_help (void)
  325. {
  326. char *myport;
  327. asprintf (&myport, "%d", DEFAULT_PORT);
  328. print_revision (progname, revision);
  329. printf (COPYRIGHT, copyright, email);
  330. printf (_("Test whether a PostgreSQL Database is accepting connections."));
  331. printf ("\n\n");
  332. print_usage ();
  333. printf (_(UT_HELP_VRSN));
  334. printf (_(UT_HOST_PORT), 'P', myport);
  335. printf (_(UT_IPv46));
  336. printf (" %s\n", "-d, --database=STRING");
  337. printf (" %s", _("Database to check "));
  338. printf (_("(default: %s)"), DEFAULT_DB);
  339. printf (" %s\n", "-l, --logname = STRING");
  340. printf (" %s\n", _("Login name of user"));
  341. printf (" %s\n", "-p, --password = STRING");
  342. printf (" %s\n", _("Password (BIG SECURITY ISSUE)"));
  343. printf (_(UT_WARN_CRIT));
  344. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  345. printf (_(UT_VERBOSE));
  346. printf ("\n");
  347. printf (" %s\n", _("All parameters are optional."));
  348. printf (" %s\n", _("This plugin tests a PostgreSQL DBMS to determine whether it is active and"));
  349. printf (" %s\n", _("accepting queries. In its current operation, it simply connects to the"));
  350. printf (" %s\n", _("specified database, and then disconnects. If no database is specified, it"));
  351. printf (" %s\n", _("connects to the template1 database, which is present in every functioning"));
  352. printf (" %s\n\n", _("PostgreSQL DBMS."));
  353. printf (" %s\n", _("The plugin will connect to a local postmaster if no host is specified. To"));
  354. printf (" %s\n", _("connect to a remote host, be sure that the remote postmaster accepts TCP/IP"));
  355. printf (" %s\n\n", _("connections (start the postmaster with the -i option)."));
  356. printf (" %s\n", _("Typically, the nagios user (unless the --logname option is used) should be"));
  357. printf (" %s\n", _("able to connect to the database without a password. The plugin can also send"));
  358. printf (" %s\n", _("a password, but no effort is made to obsure or encrypt the password."));
  359. printf (_(UT_SUPPORT));
  360. }
  361. void
  362. print_usage (void)
  363. {
  364. printf (_("Usage:"));
  365. printf ("%s [-H <host>] [-P <port>] [-c <critical time>] [-w <warning time>]\n", progname);
  366. printf (" [-t <timeout>] [-d <database>] [-l <logname>] [-p <password>]\n");
  367. }