check_pgsql.c 11 KB

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