check_by_ssh.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. {0, 0, 0, 0}
  150. };
  151. if (argc < 2)
  152. return ERROR;
  153. for (c = 1; c < argc; c++)
  154. if (strcmp ("-to", argv[c]) == 0)
  155. strcpy (argv[c], "-t");
  156. while (1) {
  157. c = getopt_long (argc, argv, "Vvh1246ft:H:O:p:i:u:l:C:S:n:s:", longopts,
  158. &option);
  159. if (c == -1 || c == EOF)
  160. break;
  161. switch (c) {
  162. case 'V': /* version */
  163. print_revision (progname, revision);
  164. exit (STATE_OK);
  165. case 'h': /* help */
  166. print_help ();
  167. exit (STATE_OK);
  168. case 'v': /* help */
  169. verbose = TRUE;
  170. break;
  171. case 't': /* timeout period */
  172. if (!is_integer (optarg))
  173. usage_va(_("Timeout interval must be a positive integer"));
  174. else
  175. timeout_interval = atoi (optarg);
  176. break;
  177. case 'H': /* host */
  178. host_or_die(optarg);
  179. hostname = optarg;
  180. break;
  181. case 'p': /* port number */
  182. if (!is_integer (optarg))
  183. usage_va(_("Port must be a positive integer"));
  184. asprintf (&comm,"%s -p %s", comm, optarg);
  185. break;
  186. case 'O': /* output file */
  187. outputfile = optarg;
  188. passive = TRUE;
  189. break;
  190. case 's': /* description of service to check */
  191. p1 = optarg;
  192. service = realloc (service, (++services) * sizeof(char *));
  193. while ((p2 = index (p1, ':'))) {
  194. *p2 = '\0';
  195. service[services - 1] = p1;
  196. service = realloc (service, (++services) * sizeof(char *));
  197. p1 = p2 + 1;
  198. }
  199. service[services - 1] = p1;
  200. break;
  201. case 'n': /* short name of host in nagios configuration */
  202. host_shortname = optarg;
  203. break;
  204. case 'u':
  205. c = 'l';
  206. case 'l': /* login name */
  207. case 'i': /* identity */
  208. asprintf (&comm, "%s -%c %s", comm, c, optarg);
  209. break;
  210. case '1': /* Pass these switches directly to ssh */
  211. case '2': /* 1 to force version 1, 2 to force version 2 */
  212. case '4': /* -4 for IPv4 */
  213. case '6': /* -6 for IPv6 */
  214. case 'f': /* fork to background */
  215. asprintf (&comm, "%s -%c", comm, c);
  216. break;
  217. case 'C': /* Command for remote machine */
  218. commands++;
  219. if (commands > 1)
  220. asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
  221. asprintf (&remotecmd, "%s%s", remotecmd, optarg);
  222. break;
  223. case 'S': /* Skip n lines in the output to ignore system banner */
  224. if (!is_integer (optarg))
  225. usage_va(_("skip lines must be an integer"));
  226. else
  227. skip = atoi (optarg);
  228. break;
  229. default: /* help */
  230. usage_va(_("Unknown argument - %s"), optarg);
  231. }
  232. }
  233. c = optind;
  234. if (hostname == NULL) {
  235. if (c <= argc) {
  236. die (STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname);
  237. }
  238. host_or_die(argv[c]);
  239. hostname = argv[c++];
  240. }
  241. if (strlen(remotecmd) == 0) {
  242. for (; c < argc; c++)
  243. if (strlen(remotecmd) > 0)
  244. asprintf (&remotecmd, "%s %s", remotecmd, argv[c]);
  245. else
  246. asprintf (&remotecmd, "%s", argv[c]);
  247. }
  248. if (commands > 1)
  249. asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
  250. if (remotecmd == NULL || strlen (remotecmd) <= 1)
  251. usage_va(_("No remotecmd"));
  252. asprintf (&comm, "%s %s '%s'", comm, hostname, remotecmd);
  253. return validate_arguments ();
  254. }
  255. int
  256. validate_arguments (void)
  257. {
  258. if (remotecmd == NULL || hostname == NULL)
  259. return ERROR;
  260. if (passive && commands != services)
  261. die (STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname);
  262. if (passive && host_shortname == NULL)
  263. die (STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the nagios configs.\n"), progname);
  264. return OK;
  265. }
  266. void
  267. print_help (void)
  268. {
  269. print_revision (progname, revision);
  270. printf ("Copyright (c) 1999 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
  271. printf (COPYRIGHT, copyright, email);
  272. printf (_("This plugin uses SSH to execute commands on a remote host"));
  273. printf ("\n\n");
  274. print_usage ();
  275. printf (_(UT_HELP_VRSN));
  276. printf (_(UT_HOST_PORT), 'p', "none");
  277. printf (_(UT_IPv46));
  278. printf (" %s\n", "-1, --proto1");
  279. printf (" %s\n", _("tell ssh to use Protocol 1"));
  280. printf (" %s\n", "-2, --proto2");
  281. printf (" %s\n", _("tell ssh to use Protocol 2"));
  282. printf (" %s\n", "-S, --skiplines=n");
  283. printf (" %s\n", _("Ignore first n lines on STDERR (to suppress a logon banner)"));
  284. printf (" %s\n", "-f");
  285. printf (" %s\n", _("tells ssh to fork rather than create a tty"));
  286. printf (" %s\n","-C, --command='COMMAND STRING'");
  287. printf (" %s\n", _("command to execute on the remote machine"));
  288. printf (" %s\n","-l, --logname=USERNAME");
  289. printf (" %s\n", _("SSH user name on remote host [optional]"));
  290. printf (" %s\n","-i, --identity=KEYFILE");
  291. printf (" %s\n", _("identity of an authorized key [optional]"));
  292. printf (" %s\n","-O, --output=FILE");
  293. printf (" %s\n", _("external command file for nagios [optional]"));
  294. printf (" %s\n","-s, --services=LIST");
  295. printf (" %s\n", _("list of nagios service names, separated by ':' [optional]"));
  296. printf (" %s\n","-n, --name=NAME");
  297. printf (" %s\n", _("short name of host in nagios configuration [optional]"));
  298. printf (_(UT_WARN_CRIT));
  299. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  300. printf (" %s\n", _("The most common mode of use is to refer to a local identity file with"));
  301. printf (" %s\n", _("the '-i' option. In this mode, the identity pair should have a null"));
  302. printf (" %s\n", _("passphrase and the public key should be listed in the authorized_keys"));
  303. printf (" %s\n", _("file of the remote host. Usually the key will be restricted to running"));
  304. printf (" %s\n", _("only one command on the remote server. If the remote SSH server tracks"));
  305. printf (" %s\n", _("invocation arguments, the one remote program may be an agent that can"));
  306. printf (" %s\n", _("execute additional commands as proxy"));
  307. printf (" %s\n", _("To use passive mode, provide multiple '-C' options, and provide"));
  308. printf (" %s\n", _("all of -O, -s, and -n options (servicelist order must match '-C'options)"));
  309. printf ("\n");
  310. printf ("%s\n", _("Examples:"));
  311. printf (" %s\n", "$ check_by_ssh -H localhost -n lh -s c1:c2:c3 -C uptime -C uptime -C uptime -O /tmp/foo");
  312. printf (" %s\n", "$ cat /tmp/foo");
  313. printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c1;0; up 2 days");
  314. printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c2;0; up 2 days");
  315. printf (" %s\n", "[1080933700] PROCESS_SERVICE_CHECK_RESULT;flint;c3;0; up 2 days");
  316. printf (_(UT_SUPPORT));
  317. }
  318. void
  319. print_usage (void)
  320. {
  321. printf (_("Usage:"));
  322. printf(" %s [-f46] [-t timeout] [-i identity] [-l user] -H <host> -C <command>",progname);
  323. printf(" [-n name] [-s servicelist] [-O outputfile] [-p port]\n");
  324. }