4
0

check_by_ssh.c 13 KB

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