check_by_ssh.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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-2014";
  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. result = min(result, STATE_UNKNOWN);
  105. if (chld_out.lines > skip_stdout)
  106. for (i = skip_stdout; i < chld_out.lines; i++)
  107. puts (chld_out.line[i]);
  108. else
  109. printf (_("%s - check_by_ssh: Remote command '%s' returned status %d\n"),
  110. state_text(result), remotecmd, result);
  111. return result;
  112. }
  113. /*
  114. * Passive mode
  115. */
  116. /* process output */
  117. if (!(fp = fopen (outputfile, "a"))) {
  118. printf (_("SSH WARNING: could not open %s\n"), outputfile);
  119. exit (STATE_UNKNOWN);
  120. }
  121. local_time = time (NULL);
  122. commands = 0;
  123. for(i = skip_stdout; i < chld_out.lines; i++) {
  124. status_text = chld_out.line[i++];
  125. if (i == chld_out.lines || strstr (chld_out.line[i], "STATUS CODE: ") == NULL)
  126. die (STATE_UNKNOWN, _("%s: Error parsing output\n"), progname);
  127. if (service[commands] && status_text
  128. && sscanf (chld_out.line[i], "STATUS CODE: %d", &cresult) == 1)
  129. {
  130. fprintf (fp, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
  131. (int) local_time, host_shortname, service[commands++],
  132. cresult, status_text);
  133. }
  134. }
  135. /* Multiple commands and passive checking should always return OK */
  136. return result;
  137. }
  138. /* process command-line arguments */
  139. int
  140. process_arguments (int argc, char **argv)
  141. {
  142. int c;
  143. char *p1, *p2;
  144. int option = 0;
  145. static struct option longopts[] = {
  146. {"version", no_argument, 0, 'V'},
  147. {"help", no_argument, 0, 'h'},
  148. {"verbose", no_argument, 0, 'v'},
  149. {"fork", no_argument, 0, 'f'},
  150. {"timeout", required_argument, 0, 't'},
  151. {"host", required_argument, 0, 'H'}, /* backward compatibility */
  152. {"hostname", required_argument, 0, 'H'},
  153. {"port", required_argument,0,'p'},
  154. {"output", required_argument, 0, 'O'},
  155. {"name", required_argument, 0, 'n'},
  156. {"services", required_argument, 0, 's'},
  157. {"identity", required_argument, 0, 'i'},
  158. {"user", required_argument, 0, 'u'},
  159. {"logname", required_argument, 0, 'l'},
  160. {"command", required_argument, 0, 'C'},
  161. {"skip", optional_argument, 0, 'S'}, /* backwards compatibility */
  162. {"skip-stdout", optional_argument, 0, 'S'},
  163. {"skip-stderr", optional_argument, 0, 'E'},
  164. {"proto1", no_argument, 0, '1'},
  165. {"proto2", no_argument, 0, '2'},
  166. {"use-ipv4", no_argument, 0, '4'},
  167. {"use-ipv6", no_argument, 0, '6'},
  168. {"ssh-option", required_argument, 0, 'o'},
  169. {"quiet", no_argument, 0, 'q'},
  170. {"configfile", optional_argument, 0, 'F'},
  171. {0, 0, 0, 0}
  172. };
  173. if (argc < 2)
  174. return ERROR;
  175. for (c = 1; c < argc; c++)
  176. if (strcmp ("-to", argv[c]) == 0)
  177. strcpy (argv[c], "-t");
  178. while (1) {
  179. c = getopt_long (argc, argv, "Vvh1246fqt:H:O:p:i:u:l:C:S::E::n:s:o:F:", longopts,
  180. &option);
  181. if (c == -1 || c == EOF)
  182. break;
  183. switch (c) {
  184. case 'V': /* version */
  185. print_revision (progname, NP_VERSION);
  186. exit (STATE_OK);
  187. case 'h': /* help */
  188. print_help ();
  189. exit (STATE_OK);
  190. case 'v': /* help */
  191. verbose = TRUE;
  192. break;
  193. case 't': /* timeout period */
  194. timeout_interval = parse_timeout_string (optarg);
  195. break;
  196. case 'H': /* host */
  197. host_or_die(optarg);
  198. hostname = optarg;
  199. break;
  200. case 'p': /* port number */
  201. if (!is_integer (optarg))
  202. usage_va(_("Port must be a positive integer"));
  203. comm_append("-p");
  204. comm_append(optarg);
  205. break;
  206. case 'O': /* output file */
  207. outputfile = optarg;
  208. passive = TRUE;
  209. break;
  210. case 's': /* description of service to check */
  211. p1 = optarg;
  212. service = realloc (service, (++services) * sizeof(char *));
  213. while ((p2 = index (p1, ':'))) {
  214. *p2 = '\0';
  215. service[services - 1] = p1;
  216. service = realloc (service, (++services) * sizeof(char *));
  217. p1 = p2 + 1;
  218. }
  219. service[services - 1] = p1;
  220. break;
  221. case 'n': /* short name of host in nagios configuration */
  222. host_shortname = optarg;
  223. break;
  224. case 'u':
  225. comm_append("-l");
  226. comm_append(optarg);
  227. break;
  228. case 'l': /* login name */
  229. comm_append("-l");
  230. comm_append(optarg);
  231. break;
  232. case 'i': /* identity */
  233. comm_append("-i");
  234. comm_append(optarg);
  235. break;
  236. case '1': /* Pass these switches directly to ssh */
  237. comm_append("-1");
  238. break;
  239. case '2': /* 1 to force version 1, 2 to force version 2 */
  240. comm_append("-2");
  241. break;
  242. case '4': /* -4 for IPv4 */
  243. comm_append("-4");
  244. break;
  245. case '6': /* -6 for IPv6 */
  246. comm_append("-6");
  247. break;
  248. case 'f': /* fork to background */
  249. comm_append("-f");
  250. break;
  251. case 'C': /* Command for remote machine */
  252. commands++;
  253. if (commands > 1)
  254. xasprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
  255. xasprintf (&remotecmd, "%s%s", remotecmd, optarg);
  256. break;
  257. case 'S': /* skip n (or all) lines on stdout */
  258. if (optarg == NULL)
  259. skip_stdout = -1; /* skip all output on stdout */
  260. else if (!is_integer (optarg))
  261. usage_va(_("skip-stdout argument must be an integer"));
  262. else
  263. skip_stdout = atoi (optarg);
  264. break;
  265. case 'E': /* skip n (or all) lines on stderr */
  266. if (optarg == NULL)
  267. skip_stderr = -1; /* skip all output on stderr */
  268. else if (!is_integer (optarg))
  269. usage_va(_("skip-stderr argument must be an integer"));
  270. else
  271. skip_stderr = atoi (optarg);
  272. break;
  273. case 'o': /* Extra options for the ssh command */
  274. comm_append("-o");
  275. comm_append(optarg);
  276. break;
  277. case 'q': /* Tell the ssh command to be quiet */
  278. comm_append("-q");
  279. break;
  280. case 'F': /* ssh configfile */
  281. comm_append("-F");
  282. comm_append(optarg);
  283. break;
  284. default: /* help */
  285. usage5();
  286. }
  287. }
  288. c = optind;
  289. if (hostname == NULL) {
  290. if (c <= argc) {
  291. die (STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname);
  292. }
  293. host_or_die(argv[c]);
  294. hostname = argv[c++];
  295. }
  296. if (strlen(remotecmd) == 0) {
  297. for (; c < argc; c++)
  298. if (strlen(remotecmd) > 0)
  299. xasprintf (&remotecmd, "%s %s", remotecmd, argv[c]);
  300. else
  301. xasprintf (&remotecmd, "%s", argv[c]);
  302. }
  303. if (commands > 1 || passive)
  304. xasprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
  305. if (remotecmd == NULL || strlen (remotecmd) <= 1)
  306. usage_va(_("No remotecmd"));
  307. comm_append(hostname);
  308. comm_append(remotecmd);
  309. return validate_arguments ();
  310. }
  311. void
  312. comm_append (const char *str)
  313. {
  314. if (++commargc > NP_MAXARGS)
  315. die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS);
  316. if ((commargv = (char **)realloc(commargv, (commargc+1) * sizeof(char *))) == NULL)
  317. die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n"));
  318. commargv[commargc-1] = strdup(str);
  319. commargv[commargc] = NULL;
  320. }
  321. int
  322. validate_arguments (void)
  323. {
  324. if (remotecmd == NULL || hostname == NULL)
  325. return ERROR;
  326. if (passive && commands != services)
  327. die (STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname);
  328. if (passive && host_shortname == NULL)
  329. die (STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the nagios configs.\n"), progname);
  330. return OK;
  331. }
  332. void
  333. print_help (void)
  334. {
  335. print_revision (progname, NP_VERSION);
  336. printf ("Copyright (c) 1999 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
  337. printf (COPYRIGHT, copyright, email);
  338. printf (_("This plugin uses SSH to execute commands on a remote host"));
  339. printf ("\n\n");
  340. print_usage ();
  341. printf (UT_HELP_VRSN);
  342. printf (UT_EXTRA_OPTS);
  343. printf (UT_HOST_PORT, 'p', "none");
  344. printf (UT_IPv46);
  345. printf (" %s\n", "-1, --proto1");
  346. printf (" %s\n", _("tell ssh to use Protocol 1 [optional]"));
  347. printf (" %s\n", "-2, --proto2");
  348. printf (" %s\n", _("tell ssh to use Protocol 2 [optional]"));
  349. printf (" %s\n", "-S, --skip-stdout[=n]");
  350. printf (" %s\n", _("Ignore all or (if specified) first n lines on STDOUT [optional]"));
  351. printf (" %s\n", "-E, --skip-stderr[=n]");
  352. printf (" %s\n", _("Ignore all or (if specified) first n lines on STDERR [optional]"));
  353. printf (" %s\n", "-f");
  354. printf (" %s\n", _("tells ssh to fork rather than create a tty [optional]. This will always return OK if ssh is executed"));
  355. printf (" %s\n","-C, --command='COMMAND STRING'");
  356. printf (" %s\n", _("command to execute on the remote machine"));
  357. printf (" %s\n","-l, --logname=USERNAME");
  358. printf (" %s\n", _("SSH user name on remote host [optional]"));
  359. printf (" %s\n","-i, --identity=KEYFILE");
  360. printf (" %s\n", _("identity of an authorized key [optional]"));
  361. printf (" %s\n","-O, --output=FILE");
  362. printf (" %s\n", _("external command file for nagios [optional]"));
  363. printf (" %s\n","-s, --services=LIST");
  364. printf (" %s\n", _("list of nagios service names, separated by ':' [optional]"));
  365. printf (" %s\n","-n, --name=NAME");
  366. printf (" %s\n", _("short name of host in nagios configuration [optional]"));
  367. printf (" %s\n","-o, --ssh-option=OPTION");
  368. printf (" %s\n", _("Call ssh with '-o OPTION' (may be used multiple times) [optional]"));
  369. printf (" %s\n","-F, --configfile");
  370. printf (" %s\n", _("Tell ssh to use this configfile [optional]"));
  371. printf (" %s\n","-q, --quiet");
  372. printf (" %s\n", _("Tell ssh to suppress warning and diagnostic messages [optional]"));
  373. printf (UT_WARN_CRIT);
  374. printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
  375. printf (UT_VERBOSE);
  376. printf("\n");
  377. printf (" %s\n", _("The most common mode of use is to refer to a local identity file with"));
  378. printf (" %s\n", _("the '-i' option. In this mode, the identity pair should have a null"));
  379. printf (" %s\n", _("passphrase and the public key should be listed in the authorized_keys"));
  380. printf (" %s\n", _("file of the remote host. Usually the key will be restricted to running"));
  381. printf (" %s\n", _("only one command on the remote server. If the remote SSH server tracks"));
  382. printf (" %s\n", _("invocation arguments, the one remote program may be an agent that can"));
  383. printf (" %s\n", _("execute additional commands as proxy"));
  384. printf("\n");
  385. printf (" %s\n", _("To use passive mode, provide multiple '-C' options, and provide"));
  386. printf (" %s\n", _("all of -O, -s, and -n options (servicelist order must match '-C'options)"));
  387. printf ("\n");
  388. printf ("%s\n", _("Examples:"));
  389. printf (" %s\n", "$ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo");
  390. printf (" %s\n", "$ cat /tmp/foo");
  391. printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days");
  392. printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days");
  393. printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c3;0; up 2 days");
  394. printf(UT_SUPPORT);
  395. }
  396. void
  397. print_usage (void)
  398. {
  399. printf ("%s\n", _("Usage:"));
  400. printf (" %s -H <host> -C <command> [-fqv] [-1|-2] [-4|-6]\n"
  401. " [-S [lines]] [-E [lines]] [-t timeout] [-i identity]\n"
  402. " [-l user] [-n name] [-s servicelist] [-O outputfile]\n"
  403. " [-p port] [-o ssh-option] [-F configfile]\n",
  404. progname);
  405. }