check_by_ssh.c 13 KB

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