check_by_ssh.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /******************************************************************************
  2. The Nagios Plugins are free software; you can redistribute them
  3. and/or modify them under the terms of the GNU General Public
  4. License as published by the Free Software Foundation; either
  5. version 2 of the License, or (at your option) any later version.
  6. This program is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. 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. $Id$
  14. *****************************************************************************/
  15. const char *progname = "check_by_ssh";
  16. const char *revision = "$Revision$";
  17. const char *copyright = "2000-2004";
  18. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  19. #include "common.h"
  20. #include "netutils.h"
  21. #include "utils.h"
  22. #include "runcmd.h"
  23. int process_arguments (int, char **);
  24. int validate_arguments (void);
  25. void print_help (void);
  26. void print_usage (void);
  27. int commands = 0;
  28. int services = 0;
  29. int skip = 0;
  30. char *remotecmd = NULL;
  31. char *comm = NULL;
  32. char *hostname = NULL;
  33. char *outputfile = NULL;
  34. char *host_shortname = NULL;
  35. char **service;
  36. int passive = FALSE;
  37. int verbose = FALSE;
  38. int
  39. main (int argc, char **argv)
  40. {
  41. char *status_text;
  42. int cresult;
  43. int result = STATE_UNKNOWN;
  44. int i;
  45. time_t local_time;
  46. FILE *fp = NULL;
  47. struct output chld_out, chld_err;
  48. remotecmd = "";
  49. comm = strdup (SSH_COMMAND);
  50. setlocale (LC_ALL, "");
  51. bindtextdomain (PACKAGE, LOCALEDIR);
  52. textdomain (PACKAGE);
  53. /* process arguments */
  54. if (process_arguments (argc, argv) == ERROR)
  55. usage_va(_("Could not parse arguments"));
  56. /* Set signal handling and alarm timeout */
  57. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
  58. usage_va(_("Cannot catch SIGALRM"));
  59. }
  60. alarm (timeout_interval);
  61. /* run the command */
  62. if (verbose)
  63. printf ("%s\n", comm);
  64. result = np_runcmd(comm, &chld_out, &chld_err, 0);
  65. /* UNKNOWN if output found on stderr */
  66. if(chld_err.buflen) {
  67. printf(_("Remote command execution failed: %s\n"),
  68. chld_err.buflen ? chld_err.buf : _("Unknown error"));
  69. return STATE_UNKNOWN;
  70. }
  71. /* this is simple if we're not supposed to be passive.
  72. * Wrap up quickly and keep the tricks below */
  73. if(!passive) {
  74. printf ("%s\n", skip < chld_out.lines ? chld_out.line[skip] : chld_out.buf);
  75. return result; /* return error status from remote command */
  76. }
  77. /*
  78. * Passive mode
  79. */
  80. /* process output */
  81. if (!(fp = fopen (outputfile, "a"))) {
  82. printf (_("SSH WARNING: could not open %s\n"), outputfile);
  83. exit (STATE_UNKNOWN);
  84. }
  85. local_time = time (NULL);
  86. commands = 0;
  87. for(i = skip; chld_out.line[i]; i++) {
  88. status_text = strstr (chld_out.line[i], "STATUS CODE: ");
  89. if (status_text == NULL) {
  90. printf ("%s", chld_out.line[i]);
  91. return result;
  92. }
  93. if (service[commands] && status_text
  94. && sscanf (status_text, "STATUS CODE: %d", &cresult) == 1)
  95. {
  96. fprintf (fp, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
  97. (int) local_time, host_shortname, service[commands++],
  98. cresult, chld_out.line[i]);
  99. }
  100. }
  101. /* force an OK state */
  102. return result;
  103. }
  104. /* process command-line arguments */
  105. int
  106. process_arguments (int argc, char **argv)
  107. {
  108. int c;
  109. char *p1, *p2;
  110. int option = 0;
  111. static struct option longopts[] = {
  112. {"version", no_argument, 0, 'V'},
  113. {"help", no_argument, 0, 'h'},
  114. {"verbose", no_argument, 0, 'v'},
  115. {"fork", no_argument, 0, 'f'},
  116. {"timeout", required_argument, 0, 't'},
  117. {"host", required_argument, 0, 'H'},
  118. {"port", required_argument,0,'p'},
  119. {"output", required_argument, 0, 'O'},
  120. {"name", required_argument, 0, 'n'},
  121. {"services", required_argument, 0, 's'},
  122. {"identity", required_argument, 0, 'i'},
  123. {"user", required_argument, 0, 'u'},
  124. {"logname", required_argument, 0, 'l'},
  125. {"command", required_argument, 0, 'C'},
  126. {"skip", required_argument, 0, 'S'},
  127. {"proto1", no_argument, 0, '1'},
  128. {"proto2", no_argument, 0, '2'},
  129. {"use-ipv4", no_argument, 0, '4'},
  130. {"use-ipv6", no_argument, 0, '6'},
  131. {0, 0, 0, 0}
  132. };
  133. if (argc < 2)
  134. return ERROR;
  135. for (c = 1; c < argc; c++)
  136. if (strcmp ("-to", argv[c]) == 0)
  137. strcpy (argv[c], "-t");
  138. while (1) {
  139. c = getopt_long (argc, argv, "Vvh1246ft:H:O:p:i:u:l:C:S:n:s:", longopts,
  140. &option);
  141. if (c == -1 || c == EOF)
  142. break;
  143. switch (c) {
  144. case 'V': /* version */
  145. print_revision (progname, revision);
  146. exit (STATE_OK);
  147. case 'h': /* help */
  148. print_help ();
  149. exit (STATE_OK);
  150. case 'v': /* help */
  151. verbose = TRUE;
  152. break;
  153. case 't': /* timeout period */
  154. if (!is_integer (optarg))
  155. usage_va(_("Timeout interval must be a positive integer"));
  156. else
  157. timeout_interval = atoi (optarg);
  158. break;
  159. case 'H': /* host */
  160. host_or_die(optarg);
  161. hostname = optarg;
  162. break;
  163. case 'p': /* port number */
  164. if (!is_integer (optarg))
  165. usage_va(_("Port must be a positive integer"));
  166. asprintf (&comm,"%s -p %s", comm, optarg);
  167. break;
  168. case 'O': /* output file */
  169. outputfile = optarg;
  170. passive = TRUE;
  171. break;
  172. case 's': /* description of service to check */
  173. p1 = optarg;
  174. service = realloc (service, (++services) * sizeof(char *));
  175. while ((p2 = index (p1, ':'))) {
  176. *p2 = '\0';
  177. service[services - 1] = p1;
  178. service = realloc (service, (++services) * sizeof(char *));
  179. p1 = p2 + 1;
  180. }
  181. service[services - 1] = p1;
  182. break;
  183. case 'n': /* short name of host in nagios configuration */
  184. host_shortname = optarg;
  185. break;
  186. case 'u':
  187. c = 'l';
  188. case 'l': /* login name */
  189. case 'i': /* identity */
  190. asprintf (&comm, "%s -%c %s", comm, c, optarg);
  191. break;
  192. case '1': /* Pass these switches directly to ssh */
  193. case '2': /* 1 to force version 1, 2 to force version 2 */
  194. case '4': /* -4 for IPv4 */
  195. case '6': /* -6 for IPv6 */
  196. case 'f': /* fork to background */
  197. asprintf (&comm, "%s -%c", comm, c);
  198. break;
  199. case 'C': /* Command for remote machine */
  200. commands++;
  201. if (commands > 1)
  202. asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
  203. asprintf (&remotecmd, "%s%s", remotecmd, optarg);
  204. break;
  205. case 'S': /* Skip n lines in the output to ignore system banner */
  206. if (!is_integer (optarg))
  207. usage_va(_("skip lines must be an integer"));
  208. else
  209. skip = atoi (optarg);
  210. break;
  211. default: /* help */
  212. usage_va(_("Unknown argument - %s"), optarg);
  213. }
  214. }
  215. c = optind;
  216. if (hostname == NULL) {
  217. if (c <= argc) {
  218. die (STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname);
  219. }
  220. host_or_die(argv[c]);
  221. hostname = argv[c++];
  222. }
  223. if (strlen(remotecmd) == 0) {
  224. for (; c < argc; c++)
  225. if (strlen(remotecmd) > 0)
  226. asprintf (&remotecmd, "%s %s", remotecmd, argv[c]);
  227. else
  228. asprintf (&remotecmd, "%s", argv[c]);
  229. }
  230. if (commands > 1)
  231. asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
  232. if (remotecmd == NULL || strlen (remotecmd) <= 1)
  233. usage_va(_("No remotecmd"));
  234. asprintf (&comm, "%s %s '%s'", comm, hostname, remotecmd);
  235. return validate_arguments ();
  236. }
  237. int
  238. validate_arguments (void)
  239. {
  240. if (remotecmd == NULL || hostname == NULL)
  241. return ERROR;
  242. if (passive && commands != services)
  243. die (STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname);
  244. if (passive && host_shortname == NULL)
  245. die (STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the nagios configs.\n"), progname);
  246. return OK;
  247. }
  248. void
  249. print_help (void)
  250. {
  251. print_revision (progname, revision);
  252. printf ("Copyright (c) 1999 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
  253. printf (COPYRIGHT, copyright, email);
  254. printf (_("This plugin uses SSH to execute commands on a remote host\n\n"));
  255. print_usage ();
  256. printf (_(UT_HELP_VRSN));
  257. printf (_(UT_HOST_PORT), 'p', "none");
  258. printf (_(UT_IPv46));
  259. printf (_("\
  260. -1, --proto1\n\
  261. tell ssh to use Protocol 1\n\
  262. -2, --proto2\n\
  263. tell ssh to use Protocol 2\n\
  264. -S, --skiplines=n\n\
  265. Ignore first n lines on STDERR (to suppress a logon banner)\n\
  266. -f\n\
  267. tells ssh to fork rather than create a tty\n"));
  268. printf (_("\
  269. -C, --command='COMMAND STRING'\n\
  270. command to execute on the remote machine\n\
  271. -l, --logname=USERNAME\n\
  272. SSH user name on remote host [optional]\n\
  273. -i, --identity=KEYFILE\n\
  274. identity of an authorized key [optional]\n\
  275. -O, --output=FILE\n\
  276. external command file for nagios [optional]\n\
  277. -s, --services=LIST\n\
  278. list of nagios service names, separated by ':' [optional]\n\
  279. -n, --name=NAME\n\
  280. short name of host in nagios configuration [optional]\n"));
  281. printf (_(UT_WARN_CRIT));
  282. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  283. printf (_("\n\
  284. The most common mode of use is to refer to a local identity file with\n\
  285. the '-i' option. In this mode, the identity pair should have a null\n\
  286. passphrase and the public key should be listed in the authorized_keys\n\
  287. file of the remote host. Usually the key will be restricted to running\n\
  288. only one command on the remote server. If the remote SSH server tracks\n\
  289. invocation arguments, the one remote program may be an agent that can\n\
  290. execute additional commands as proxy\n"));
  291. printf (_("\n\
  292. To use passive mode, provide multiple '-C' options, and provide\n\
  293. all of -O, -s, and -n options (servicelist order must match '-C'\n\
  294. options)\n"));
  295. printf ("\n\
  296. $ check_by_ssh -H localhost -n lh -s c1:c2:c3 \\\n\
  297. -C uptime -C uptime -C uptime -O /tmp/foo\n\
  298. $ cat /tmp/foo\n\
  299. [1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days...\n\
  300. [1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days...\n\
  301. [1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c3;0; up 2 days...\n");
  302. printf (_(UT_SUPPORT));
  303. }
  304. void
  305. print_usage (void)
  306. {
  307. printf ("\n\
  308. Usage: %s [-f46] [-t timeout] [-i identity] [-l user] -H <host> -C <command>\n\
  309. [-n name] [-s servicelist] [-O outputfile] [-p port]\n", progname);
  310. }