check_by_ssh.c 12 KB

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