check_by_ssh.c 13 KB

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