check_pgsql.c 11 KB

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