check_by_ssh.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 validate_arguments (void);
  37. void print_help (char *command_name);
  38. void print_usage (void);
  39. int commands = 0;
  40. int services = 0;
  41. char *remotecmd = "";
  42. char *comm = SSH_COMMAND;
  43. char *hostname = NULL;
  44. char *outputfile = NULL;
  45. char *host_shortname = NULL;
  46. char **service;
  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 = "";
  54. char *status_text;
  55. char *output = "";
  56. char *summary = "";
  57. char *eol = NULL;
  58. char *srvc_desc = NULL;
  59. int cresult;
  60. int result = STATE_UNKNOWN;
  61. time_t local_time;
  62. FILE *fp = NULL;
  63. /* process arguments */
  64. if (process_arguments (argc, argv) == ERROR)
  65. usage ("Could not parse arguments\n");
  66. /* Set signal handling and alarm timeout */
  67. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
  68. printf ("Cannot catch SIGALRM");
  69. return STATE_UNKNOWN;
  70. }
  71. alarm (timeout_interval);
  72. /* run the command */
  73. if (verbose)
  74. printf ("%s\n", comm);
  75. child_process = spopen (comm);
  76. if (child_process == NULL) {
  77. printf ("Unable to open pipe: %s", comm);
  78. return STATE_UNKNOWN;
  79. }
  80. /* open STDERR for spopen */
  81. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  82. if (child_stderr == NULL) {
  83. printf ("Could not open stderr for %s\n", SSH_COMMAND);
  84. }
  85. /* get results from remote command */
  86. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
  87. asprintf (&result_text, "%s%s", result_text, input_buffer);
  88. /* WARNING if output found on stderr */
  89. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  90. printf ("%s\n", input_buffer);
  91. return STATE_WARNING;
  92. }
  93. (void) fclose (child_stderr);
  94. /* close the pipe */
  95. result = spclose (child_process);
  96. /* process output */
  97. if (passive) {
  98. if (!(fp = fopen (outputfile, "a"))) {
  99. printf ("SSH WARNING: could not open %s\n", outputfile);
  100. exit (STATE_UNKNOWN);
  101. }
  102. time (&local_time);
  103. commands = 0;
  104. while (result_text && strlen(result_text) > 0) {
  105. status_text = (strstr (result_text, "STATUS CODE: "));
  106. if (status_text == NULL) {
  107. printf ("%s", result_text);
  108. return result;
  109. }
  110. asprintf (&output, "%s", result_text);
  111. result_text = strnl (status_text);
  112. eol = strpbrk (output, "\r\n");
  113. if (eol != NULL)
  114. eol[0] = 0;
  115. if (service[commands] && status_text
  116. && sscanf (status_text, "STATUS CODE: %d", &cresult) == 1) {
  117. fprintf (fp, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
  118. (int) local_time, host_shortname, service[commands++], cresult,
  119. output);
  120. }
  121. }
  122. }
  123. /* print the first line from the remote command */
  124. else {
  125. eol = strpbrk (result_text, "\r\n");
  126. if (eol)
  127. eol[0] = 0;
  128. printf ("%s\n", result_text);
  129. }
  130. /* return error status from remote command */
  131. return result;
  132. }
  133. /* process command-line arguments */
  134. int
  135. process_arguments (int argc, char **argv)
  136. {
  137. int c, i;
  138. char *p1, *p2;
  139. size_t len;
  140. #ifdef HAVE_GETOPT_H
  141. int option_index = 0;
  142. static struct option long_options[] = {
  143. {"version", no_argument, 0, 'V'},
  144. {"help", no_argument, 0, 'h'},
  145. {"verbose", no_argument, 0, 'v'},
  146. {"fork", no_argument, 0, 'f'},
  147. {"timeout", required_argument, 0, 't'},
  148. {"host", required_argument, 0, 'H'},
  149. {"port", required_argument,0,'p'},
  150. {"output", required_argument, 0, 'O'},
  151. {"name", required_argument, 0, 'n'},
  152. {"services", required_argument, 0, 's'},
  153. {"identity", required_argument, 0, 'i'},
  154. {"user", required_argument, 0, 'u'},
  155. {"logname", required_argument, 0, 'l'},
  156. {"command", required_argument, 0, 'C'},
  157. {"use-ipv4", no_argument, 0, '4'},
  158. {"use-ipv6", no_argument, 0, '6'},
  159. {0, 0, 0, 0}
  160. };
  161. #endif
  162. if (argc < 2)
  163. return ERROR;
  164. for (c = 1; c < argc; c++)
  165. if (strcmp ("-to", argv[c]) == 0)
  166. strcpy (argv[c], "-t");
  167. while (1) {
  168. #ifdef HAVE_GETOPT_H
  169. c =
  170. getopt_long (argc, argv, "Vvh46ft:H:O:p:i:u:l:C:n:s:", long_options,
  171. &option_index);
  172. #else
  173. c = getopt (argc, argv, "Vvh46ft:H:O:p:i:u:l:C:n:s:");
  174. #endif
  175. if (c == -1 || c == EOF)
  176. break;
  177. switch (c) {
  178. case '?': /* help */
  179. print_usage ();
  180. exit (STATE_UNKNOWN);
  181. case 'V': /* version */
  182. print_revision (PROGNAME, "$Revision$");
  183. exit (STATE_OK);
  184. case 'h': /* help */
  185. print_help (PROGNAME);
  186. exit (STATE_OK);
  187. case 'v': /* help */
  188. verbose = TRUE;
  189. break;
  190. case 't': /* timeout period */
  191. if (!is_integer (optarg))
  192. usage2 ("timeout interval must be an integer", optarg);
  193. timeout_interval = atoi (optarg);
  194. break;
  195. case 'H': /* host */
  196. if (!is_host (optarg))
  197. usage2 ("invalid host name", optarg);
  198. hostname = optarg;
  199. break;
  200. case 'p': /* port number */
  201. if (!is_integer (optarg))
  202. usage2 ("port must be an integer", optarg);
  203. asprintf (&comm,"%s -p %s", comm, optarg);
  204. break;
  205. case 'O': /* output file */
  206. outputfile = optarg;
  207. passive = TRUE;
  208. break;
  209. case 's': /* description of service to check */
  210. service = realloc (service, ++services);
  211. p1 = optarg;
  212. while (p2 = index (p1, ':')) {
  213. *p2 = '\0';
  214. asprintf (&service[services-1], "%s", p1);
  215. service = realloc (service, ++services);
  216. p1 = p2 + 1;
  217. }
  218. asprintf (&service[services-1], "%s", p1);
  219. break;
  220. case 'n': /* short name of host in nagios configuration */
  221. host_shortname = optarg;
  222. break;
  223. case 'u':
  224. c = 'l';
  225. case 'l': /* login name */
  226. case 'i': /* identity */
  227. asprintf (&comm, "%s -%c %s", comm, c, optarg);
  228. break;
  229. case '4': /* Pass these switches directly to ssh */
  230. case '6': /* -4 for IPv4, -6 for IPv6 */
  231. case 'f': /* fork to background */
  232. asprintf (&comm, "%s -%c", comm, c);
  233. break;
  234. case 'C': /* Command for remote machine */
  235. commands++;
  236. if (commands > 1)
  237. asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
  238. asprintf (&remotecmd, "%s%s", remotecmd, optarg);
  239. }
  240. }
  241. c = optind;
  242. if (hostname == NULL) {
  243. if (!is_host (argv[c]))
  244. terminate (STATE_UNKNOWN, "%s: Invalid host name %s\n", PROGNAME, argv[c]);
  245. hostname = argv[c++];
  246. }
  247. if (strlen(remotecmd) == 0) {
  248. for (; c < argc; c++)
  249. asprintf (&remotecmd, "%s %s", remotecmd, argv[c]);
  250. }
  251. if (commands > 1)
  252. remotecmd = strscat (remotecmd, ";echo STATUS CODE: $?;");
  253. if (remotecmd == NULL || strlen (remotecmd) <= 1)
  254. usage ("No remotecmd\n");
  255. asprintf (&comm, "%s %s '%s'", comm, hostname, remotecmd);
  256. return validate_arguments ();
  257. }
  258. int
  259. validate_arguments (void)
  260. {
  261. if (remotecmd == NULL || hostname == NULL)
  262. return ERROR;
  263. if (passive && commands != services)
  264. terminate (STATE_UNKNOWN, "%s: In passive mode, you must provide a service name for each command.\n", PROGNAME);
  265. if (passive && host_shortname == NULL)
  266. terminate (STATE_UNKNOWN, "%s: In passive mode, you must provide the host short name from the nagios configs.\n", PROGNAME);
  267. return OK;
  268. }
  269. void
  270. print_help (char *cmd)
  271. {
  272. print_revision (cmd, "$Revision$");
  273. printf
  274. ("Copyright (c) 1999 Karl DeBisschop (kdebisschop@alum.mit.edu)\n\n"
  275. "This plugin will execute a command on a remote host using SSH\n\n");
  276. print_usage ();
  277. printf
  278. ("\nOptions:\n"
  279. "-H, --hostname=HOST\n"
  280. " name or IP address of remote host\n"
  281. "-C, --command='COMMAND STRING'\n"
  282. " command to execute on the remote machine\n"
  283. "-f tells ssh to fork rather than create a tty\n"
  284. "-t, --timeout=INTEGER\n"
  285. " specify timeout (default: %d seconds) [optional]\n"
  286. "-p, --port=PORT\n"
  287. " port to connect to on remote system [optional]\n"
  288. "-l, --logname=USERNAME\n"
  289. " SSH user name on remote host [optional]\n"
  290. "-i, --identity=KEYFILE\n"
  291. " identity of an authorized key [optional]\n"
  292. "-O, --output=FILE\n"
  293. " external command file for nagios [optional]\n"
  294. "-s, --services=LIST\n"
  295. " list of nagios service names, separated by ':' [optional]\n"
  296. "-n, --name=NAME\n"
  297. " short name of host in nagios configuration [optional]\n"
  298. "-4, --use-ipv4\n"
  299. " tell ssh to use IPv4\n"
  300. "-6, --use-ipv6\n"
  301. " tell ssh to use IPv6\n"
  302. "\n"
  303. "The most common mode of use is to refer to a local identity file with\n"
  304. "the '-i' option. In this mode, the identity pair should have a null\n"
  305. "passphrase and the public key should be listed in the authorized_keys\n"
  306. "file of the remote host. Usually the key will be restricted to running\n"
  307. "only one command on the remote server. If the remote SSH server tracks\n"
  308. "invocation agruments, the one remote program may be an agent that can\n"
  309. "execute additional commands as proxy\n"
  310. "\n"
  311. "To use passive mode, provide multiple '-C' options, and provide\n"
  312. "all of -O, -s, and -n options (servicelist order must match '-C'\n"
  313. "options)\n", DEFAULT_SOCKET_TIMEOUT);
  314. }
  315. void
  316. print_usage (void)
  317. {
  318. printf
  319. ("Usage:\n"
  320. "check_by_ssh [-f46] [-t timeout] [-i identity] [-l user] -H <host> -C <command>\n"
  321. " [-n name] [-s servicelist] [-O outputfile] [-p port]\n"
  322. "check_by_ssh -V prints version info\n"
  323. "check_by_ssh -h prints more detailed help\n");
  324. }