check_by_ssh.c 13 KB

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