4
0

check_pgsql.c 12 KB

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