check_pgsql.c 12 KB

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