check_by_ssh.c 11 KB

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