示例#1
0
int
main (int argc, char **argv)
{
	int found = 0, result = STATE_UNKNOWN;
	char *buf, *sub;
	char **command_line;
	output chld_out, chld_err;
	int i;

	setlocale (LC_ALL, "");
	bindtextdomain (PACKAGE, LOCALEDIR);
	textdomain (PACKAGE);

	timeout_interval = DEFAULT_TIMEOUT;

	command_line = (char **) process_arguments (argc, argv);

	/* Set signal handling and alarm */
	if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR)
		die (STATE_UNKNOWN, _("Cannot catch SIGALRM"));

	(void) alarm ((unsigned) timeout_interval);

	/* catch when the command is quoted */
	if(command_line[1] == NULL) {
		result = cmd_run (command_line[0], &chld_out, &chld_err, 0);
	} else {
		result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
	}
	if (chld_err.lines > 0) {
		printf ("Error output from command:\n");
		for (i = 0; i < chld_err.lines; i++) {
			printf ("%s\n", chld_err.line[i]);
		}
		exit (STATE_WARNING);
	}

	/* Return UNKNOWN or worse if no output is returned */
	if (chld_out.lines == 0)
		die (max_state_alt (result, STATE_UNKNOWN), _("No data returned from command\n"));

	for (i = 0; i < chld_out.lines; i++) {
		if (subst_text && result != state[result] &&
		    result >= 0 && result <= 4) {
			/* Loop over each match found */
			while ((sub = strstr (chld_out.line[i], state_text (result)))) {
				/* Terminate the first part and skip over the string we'll substitute */
				*sub = '\0';
				sub += strlen (state_text (result));
				/* then put everything back together */
				xasprintf (&chld_out.line[i], "%s%s%s", chld_out.line[i], state_text (state[result]), sub);
			}
		}
		printf ("%s\n", chld_out.line[i]);
	}

	if (result >= 0 && result <= 4) {
		exit (state[result]);
	} else {
		exit (result);
	}
}
示例#2
0
int
main (int argc, char **argv)
{

	char *status_text;
	int cresult;
	int result = STATE_UNKNOWN;
	int i;
	time_t local_time;
	FILE *fp = NULL;
	output chld_out, chld_err;

	remotecmd = "";
	comm_append(SSH_COMMAND);

	setlocale (LC_ALL, "");
	bindtextdomain (PACKAGE, LOCALEDIR);
	textdomain (PACKAGE);

	/* Parse extra opts if any */
	argv=np_extra_opts (&argc, argv, progname);

	/* process arguments */
	if (process_arguments (argc, argv) == ERROR)
		usage_va(_("Could not parse arguments"));

	/* Set signal handling and alarm timeout */
	if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
		usage_va(_("Cannot catch SIGALRM"));
	}
	alarm (timeout_interval);

	/* run the command */
	if (verbose) {
		printf ("Command: %s\n", commargv[0]);
		for (i=1; i<commargc; i++)
			printf ("Argument %i: %s\n", i, commargv[i]);
	}

	result = cmd_run_array (commargv, &chld_out, &chld_err, 0);

	if (skip_stdout == -1) /* --skip-stdout specified without argument */
		skip_stdout = chld_out.lines;
	if (skip_stderr == -1) /* --skip-stderr specified without argument */
		skip_stderr = chld_err.lines;

	/* UNKNOWN or worse if (non-skipped) output found on stderr */
	if(chld_err.lines > skip_stderr) {
		printf (_("Remote command execution failed: %s\n"),
		        chld_err.line[skip_stderr]);
		return max_state_alt(result, STATE_UNKNOWN);
	}

	/* this is simple if we're not supposed to be passive.
	 * Wrap up quickly and keep the tricks below */
	if(!passive) {
		if (chld_out.lines > skip_stdout)
			for (i = skip_stdout; i < chld_out.lines; i++)
				puts (chld_out.line[i]);
		else
			printf (_("%s - check_by_ssh: Remote command '%s' returned status %d\n"),
			        state_text(result), remotecmd, result);
		return result; 	/* return error status from remote command */
	}


	/*
	 * Passive mode
	 */

	/* process output */
	if (!(fp = fopen (outputfile, "a"))) {
		printf (_("SSH WARNING: could not open %s\n"), outputfile);
		exit (STATE_UNKNOWN);
	}

	local_time = time (NULL);
	commands = 0;
	for(i = skip_stdout; i < chld_out.lines; i++) {
		status_text = chld_out.line[i++];
		if (i == chld_out.lines || strstr (chld_out.line[i], "STATUS CODE: ") == NULL)
			die (STATE_UNKNOWN, _("%s: Error parsing output\n"), progname);

		if (service[commands] && status_text
			&& sscanf (chld_out.line[i], "STATUS CODE: %d", &cresult) == 1)
		{
			fprintf (fp, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
			         (int) local_time, host_shortname, service[commands++],
			         cresult, status_text);
		}
	}
	
	/* Multiple commands and passive checking should always return OK */
	return result;
}