check_pgsql.c 12 KB

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