check_by_ssh.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /******************************************************************************
  2. *
  3. * This file is part of the Nagios Plugins.
  4. *
  5. * Copyright (c) 1999, 2000, 2001 Karl DeBisschop <karl@debisschop.net>
  6. *
  7. * The Nagios Plugins are free software; you can redistribute them
  8. * and/or modify them under the terms of the GNU General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. * $Id$
  22. *
  23. *****************************************************************************/
  24. #define PROGRAM check_by_ssh
  25. #define DESCRIPTION "Run checks on a remote system using ssh, wrapping the proper timeout around the ssh invocation."
  26. #define AUTHOR "Karl DeBisschop"
  27. #define EMAIL "karl@debisschop.net"
  28. #define COPYRIGHTDATE "1999, 2000, 2001"
  29. #include "config.h"
  30. #include "common.h"
  31. #include "popen.h"
  32. #include "utils.h"
  33. #include <time.h>
  34. #define PROGNAME "check_by_ssh"
  35. int process_arguments (int, char **);
  36. int call_getopt (int, char **);
  37. int validate_arguments (void);
  38. void print_help (char *command_name);
  39. void print_usage (void);
  40. int commands;
  41. char *remotecmd = NULL;
  42. char *comm = NULL;
  43. char *hostname = NULL;
  44. char *outputfile = NULL;
  45. char *host_shortname = NULL;
  46. char *servicelist = NULL;
  47. int passive = FALSE;
  48. int verbose = FALSE;
  49. int
  50. main (int argc, char **argv)
  51. {
  52. char input_buffer[MAX_INPUT_BUFFER] = "";
  53. char *result_text = NULL;
  54. char *status_text;
  55. char *output = NULL;
  56. char *eol = NULL;
  57. char *srvc_desc = NULL;
  58. int cresult;
  59. int result = STATE_UNKNOWN;
  60. time_t local_time;
  61. FILE *fp = NULL;
  62. /* process arguments */
  63. if (process_arguments (argc, argv) == ERROR)
  64. usage ("Could not parse arguments\n");
  65. /* Set signal handling and alarm timeout */
  66. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
  67. printf ("Cannot catch SIGALRM");
  68. return STATE_UNKNOWN;
  69. }
  70. alarm (timeout_interval);
  71. /* run the command */
  72. if (verbose)
  73. printf ("%s\n", comm);
  74. child_process = spopen (comm);
  75. if (child_process == NULL) {
  76. printf ("Unable to open pipe: %s", comm);
  77. return STATE_UNKNOWN;
  78. }
  79. /* open STDERR for spopen */
  80. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  81. if (child_stderr == NULL) {
  82. printf ("Could not open stderr for %s\n", SSH_COMMAND);
  83. }
  84. /* get results from remote command */
  85. result_text = realloc (result_text, 1);
  86. result_text[0] = 0;
  87. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
  88. result_text = strscat (result_text, input_buffer);
  89. /* WARNING if output found on stderr */
  90. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  91. printf ("%s\n", input_buffer);
  92. return STATE_WARNING;
  93. }
  94. (void) fclose (child_stderr);
  95. /* close the pipe */
  96. result = spclose (child_process);
  97. /* process output */
  98. if (passive) {
  99. if (!(fp = fopen (outputfile, "a"))) {
  100. printf ("SSH WARNING: could not open %s\n", outputfile);
  101. exit (STATE_UNKNOWN);
  102. }
  103. time (&local_time);
  104. srvc_desc = strtok (servicelist, ":");
  105. while (result_text != NULL) {
  106. status_text = (strstr (result_text, "STATUS CODE: "));
  107. if (status_text == NULL) {
  108. printf ("%s", result_text);
  109. return result;
  110. }
  111. output = result_text;
  112. result_text = strnl (status_text);
  113. eol = strpbrk (output, "\r\n");
  114. if (eol != NULL)
  115. eol[0] = 0;
  116. if (srvc_desc && status_text
  117. && sscanf (status_text, "STATUS CODE: %d", &cresult) == 1) {
  118. fprintf (fp, "%d PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
  119. (int) local_time, host_shortname, srvc_desc, cresult,
  120. output);
  121. srvc_desc = strtok (NULL, ":");
  122. }
  123. }
  124. }
  125. /* print the first line from the remote command */
  126. else {
  127. eol = strpbrk (result_text, "\r\n");
  128. if (eol)
  129. eol[0] = 0;
  130. printf ("%s\n", result_text);
  131. }
  132. /* return error status from remote command */
  133. return result;
  134. }
  135. /* process command-line arguments */
  136. int
  137. process_arguments (int argc, char **argv)
  138. {
  139. int c;
  140. if (argc < 2)
  141. return ERROR;
  142. remotecmd = realloc (remotecmd, 1);
  143. remotecmd[0] = 0;
  144. for (c = 1; c < argc; c++)
  145. if (strcmp ("-to", argv[c]) == 0)
  146. strcpy (argv[c], "-t");
  147. comm = strscpy (comm, SSH_COMMAND);
  148. c = 0;
  149. while (c += (call_getopt (argc - c, &argv[c]))) {
  150. if (argc <= c)
  151. break;
  152. if (hostname == NULL) {
  153. if (!is_host (argv[c]))
  154. terminate (STATE_UNKNOWN, "%s: Invalid host name %s\n", PROGNAME,
  155. argv[c]);
  156. hostname = argv[c];
  157. }
  158. else if (remotecmd == NULL) {
  159. remotecmd = strscpy (remotecmd, argv[c++]);
  160. for (; c < argc; c++)
  161. remotecmd = ssprintf (remotecmd, "%s %s", remotecmd, argv[c]);
  162. }
  163. }
  164. if (commands > 1)
  165. remotecmd = strscat (remotecmd, ";echo STATUS CODE: $?;");
  166. if (remotecmd == NULL || strlen (remotecmd) <= 1)
  167. usage ("No remotecmd\n");
  168. comm = ssprintf (comm, "%s %s '%s'", comm, hostname, remotecmd);
  169. return validate_arguments ();
  170. }
  171. /* Call getopt */
  172. int
  173. call_getopt (int argc, char **argv)
  174. {
  175. int c, i = 1;
  176. #ifdef HAVE_GETOPT_H
  177. int option_index = 0;
  178. static struct option long_options[] = {
  179. {"version", no_argument, 0, 'V'},
  180. {"help", no_argument, 0, 'h'},
  181. {"verbose", no_argument, 0, 'v'},
  182. {"fork", no_argument, 0, 'f'},
  183. {"timeout", required_argument, 0, 't'},
  184. {"host", required_argument, 0, 'H'},
  185. {"port", required_argument,0,'p'},
  186. {"output", required_argument, 0, 'O'},
  187. {"name", required_argument, 0, 'n'},
  188. {"services", required_argument, 0, 's'},
  189. {"identity", required_argument, 0, 'i'},
  190. {"user", required_argument, 0, 'u'},
  191. {"logname", required_argument, 0, 'l'},
  192. {"command", required_argument, 0, 'C'},
  193. {"use-ipv4", no_argument, 0, '4'},
  194. {"use-ipv6", no_argument, 0, '6'},
  195. {0, 0, 0, 0}
  196. };
  197. #endif
  198. while (1) {
  199. #ifdef HAVE_GETOPT_H
  200. c =
  201. getopt_long (argc, argv, "+?Vvhft46H:O:p:i:u:l:C:n:s:", long_options,
  202. &option_index);
  203. #else
  204. c = getopt (argc, argv, "+?Vvhft46H:O:p:i:u:l:C:n:s:");
  205. #endif
  206. if (c == -1 || c == EOF)
  207. break;
  208. i++;
  209. switch (c) {
  210. case 't':
  211. case 'H':
  212. case 'O':
  213. case 'p':
  214. case 'i':
  215. case 'u':
  216. case 'l':
  217. case 'n':
  218. case 's':
  219. i++;
  220. }
  221. switch (c) {
  222. case '?': /* help */
  223. print_usage ();
  224. exit (STATE_UNKNOWN);
  225. case 'V': /* version */
  226. print_revision (PROGNAME, "$Revision$");
  227. exit (STATE_OK);
  228. case 'h': /* help */
  229. print_help (PROGNAME);
  230. exit (STATE_OK);
  231. case 'v': /* help */
  232. verbose = TRUE;
  233. break;
  234. case 't': /* timeout period */
  235. if (!is_integer (optarg))
  236. usage2 ("timeout interval must be an integer", optarg);
  237. timeout_interval = atoi (optarg);
  238. break;
  239. case 'H': /* host */
  240. if (!is_host (optarg))
  241. usage2 ("invalid host name", optarg);
  242. hostname = optarg;
  243. break;
  244. case 'p': /* port number */
  245. if (!is_integer (optarg))
  246. usage2 ("port must be an integer", optarg);
  247. comm = ssprintf (comm,"%s -p %s", comm, optarg);
  248. break;
  249. case 'O': /* output file */
  250. outputfile = optarg;
  251. passive = TRUE;
  252. break;
  253. case 's': /* description of service to check */
  254. servicelist = optarg;
  255. break;
  256. case 'n': /* short name of host in nagios configuration */
  257. host_shortname = optarg;
  258. break;
  259. case 'u':
  260. c = 'l';
  261. case 'l': /* login name */
  262. case 'i': /* identity */
  263. comm = ssprintf (comm, "%s -%c %s", comm, c, optarg);
  264. break;
  265. case '4': /* Pass these switches directly to ssh */
  266. case '6': /* -4 for IPv4, -6 for IPv6 */
  267. case 'f': /* fork to background */
  268. comm = ssprintf (comm, "%s -%c", comm, c);
  269. break;
  270. case 'C': /* Command for remote machine */
  271. commands++;
  272. if (commands > 1)
  273. remotecmd = strscat (remotecmd, ";echo STATUS CODE: $?;");
  274. remotecmd = strscat (remotecmd, optarg);
  275. }
  276. }
  277. return i;
  278. }
  279. int
  280. validate_arguments (void)
  281. {
  282. if (remotecmd == NULL || hostname == NULL)
  283. return ERROR;
  284. return OK;
  285. }
  286. void
  287. print_help (char *cmd)
  288. {
  289. print_revision (cmd, "$Revision$");
  290. printf
  291. ("Copyright (c) 1999 Karl DeBisschop (kdebisschop@alum.mit.edu)\n\n"
  292. "This plugin will execute a command on a remote host using SSH\n\n");
  293. print_usage ();
  294. printf
  295. ("\nOptions:\n"
  296. "-H, --hostname=HOST\n"
  297. " name or IP address of remote host\n"
  298. "-C, --command='COMMAND STRING'\n"
  299. " command to execute on the remote machine\n"
  300. "-f tells ssh to fork rather than create a tty\n"
  301. "-t, --timeout=INTEGER\n"
  302. " specify timeout (default: %d seconds) [optional]\n"
  303. "-p, --port=PORT\n"
  304. " port to connect to on remote system [optional]\n"
  305. "-l, --logname=USERNAME\n"
  306. " SSH user name on remote host [optional]\n"
  307. "-i, --identity=KEYFILE\n"
  308. " identity of an authorized key [optional]\n"
  309. "-O, --output=FILE\n"
  310. " external command file for nagios [optional]\n"
  311. "-s, --services=LIST\n"
  312. " list of nagios service names, separated by ':' [optional]\n"
  313. "-n, --name=NAME\n"
  314. " short name of host in nagios configuration [optional]\n"
  315. "-4, --use-ipv4\n"
  316. " tell ssh to use IPv4\n"
  317. "-6, --use-ipv6\n"
  318. " tell ssh to use IPv6\n"
  319. "\n"
  320. "The most common mode of use is to refer to a local identity file with\n"
  321. "the '-i' option. In this mode, the identity pair should have a null\n"
  322. "passphrase and the public key should be listed in the authorized_keys\n"
  323. "file of the remote host. Usually the key will be restricted to running\n"
  324. "only one command on the remote server. If the remote SSH server tracks\n"
  325. "invocation agruments, the one remote program may be an agent that can\n"
  326. "execute additional commands as proxy\n"
  327. "\n"
  328. "To use passive mode, provide multiple '-C' options, and provide\n"
  329. "all of -O, -s, and -n options (servicelist order must match '-C'\n"
  330. "options)\n", DEFAULT_SOCKET_TIMEOUT);
  331. }
  332. void
  333. print_usage (void)
  334. {
  335. printf
  336. ("Usage:\n"
  337. "check_by_ssh [-f46] [-t timeout] [-i identity] [-l user] -H <host> -C <command>\n"
  338. " [-n name] [-s servicelist] [-O outputfile] [-p port]\n"
  339. "check_by_ssh -V prints version info\n"
  340. "check_by_ssh -h prints more detailed help\n");
  341. }