4
0

check_by_ssh.c 12 KB

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