check_by_ssh.c 14 KB

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