[Nagiosplug-checkins] CVS: nagiosplug/plugins check_by_ssh.c,1.3,1.4

Karl DeBisschop kdebisschop at users.sourceforge.net
Wed Nov 6 23:08:05 CET 2002


Update of /cvsroot/nagiosplug/nagiosplug/plugins
In directory usw-pr-cvs1:/tmp/cvs-serv14552/plugins

Modified Files:
	check_by_ssh.c 
Log Message:
remove call_getopt, add [] arougn time

Index: check_by_ssh.c
===================================================================
RCS file: /cvsroot/nagiosplug/nagiosplug/plugins/check_by_ssh.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -r1.3 -r1.4
*** check_by_ssh.c	18 Aug 2002 18:03:10 -0000	1.3
--- check_by_ssh.c	7 Nov 2002 07:07:17 -0000	1.4
***************
*** 44,54 ****
  
  
! int commands;
! char *remotecmd = NULL;
! char *comm = NULL;
  char *hostname = NULL;
  char *outputfile = NULL;
  char *host_shortname = NULL;
! char *servicelist = NULL;
  int passive = FALSE;
  int verbose = FALSE;
--- 44,55 ----
  
  
! int commands = 0;
! int services = 0;
! char *remotecmd = "";
! char *comm = SSH_COMMAND;
  char *hostname = NULL;
  char *outputfile = NULL;
  char *host_shortname = NULL;
! char **service;
  int passive = FALSE;
  int verbose = FALSE;
***************
*** 60,66 ****
  
  	char input_buffer[MAX_INPUT_BUFFER] = "";
! 	char *result_text = NULL;
  	char *status_text;
! 	char *output = NULL;
  	char *eol = NULL;
  	char *srvc_desc = NULL;
--- 61,68 ----
  
  	char input_buffer[MAX_INPUT_BUFFER] = "";
! 	char *result_text = "";
  	char *status_text;
! 	char *output = "";
! 	char *summary = "";
  	char *eol = NULL;
  	char *srvc_desc = NULL;
***************
*** 105,112 ****
  
  	/* get results from remote command */
- 	result_text = realloc (result_text, 1);
- 	result_text[0] = 0;
  	while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
! 		result_text = strscat (result_text, input_buffer);
  
  
--- 107,112 ----
  
  	/* get results from remote command */
  	while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
! 		asprintf (&result_text, "%s%s", result_text, input_buffer);
  
  
***************
*** 132,137 ****
  
  		time (&local_time);
! 		srvc_desc = strtok (servicelist, ":");
! 		while (result_text != NULL) {
  			status_text = (strstr (result_text, "STATUS CODE: "));
  			if (status_text == NULL) {
--- 132,137 ----
  
  		time (&local_time);
! 		commands = 0;
! 		while (result_text && strlen(result_text) > 0) {
  			status_text = (strstr (result_text, "STATUS CODE: "));
  			if (status_text == NULL) {
***************
*** 139,153 ****
  				return result;
  			}
! 			output = result_text;
  			result_text = strnl (status_text);
  			eol = strpbrk (output, "\r\n");
  			if (eol != NULL)
  				eol[0] = 0;
! 			if (srvc_desc && status_text
  					&& sscanf (status_text, "STATUS CODE: %d", &cresult) == 1) {
! 				fprintf (fp, "%d PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n",
! 								 (int) local_time, host_shortname, srvc_desc, cresult,
  								 output);
- 				srvc_desc = strtok (NULL, ":");
  			}
  		}
--- 139,152 ----
  				return result;
  			}
! 			asprintf (&output, "%s", result_text);
  			result_text = strnl (status_text);
  			eol = strpbrk (output, "\r\n");
  			if (eol != NULL)
  				eol[0] = 0;
! 			if (service[commands] && status_text
  					&& sscanf (status_text, "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,
  								 output);
  			}
  		}
***************
*** 157,168 ****
  	/* print the first line from the remote command */
  	else {
! 		eol = strpbrk (result_text, "\r\n");
! 		if (eol)
! 			eol[0] = 0;
! 		printf ("%s\n", result_text);
! 
  	}
  
- 
  	/* return error status from remote command */
  	return result;
--- 156,165 ----
  	/* print the first line from the remote command */
  	else {
!  		eol = strpbrk (result_text, "\r\n");
!  		if (eol)
!  			eol[0] = 0;
!  		printf ("%s\n", result_text);
  	}
  
  	/* return error status from remote command */
  	return result;
***************
*** 177,234 ****
  process_arguments (int argc, char **argv)
  {
! 	int c;
! 
! 	if (argc < 2)
! 		return ERROR;
! 
! 	remotecmd = realloc (remotecmd, 1);
! 	remotecmd[0] = 0;
! 
! 	for (c = 1; c < argc; c++)
! 		if (strcmp ("-to", argv[c]) == 0)
! 			strcpy (argv[c], "-t");
! 
! 	comm = strscpy (comm, SSH_COMMAND);
! 
! 	c = 0;
! 	while (c += (call_getopt (argc - c, &argv[c]))) {
! 
! 		if (argc <= c)
! 			break;
! 
! 		if (hostname == NULL) {
! 			if (!is_host (argv[c]))
! 				terminate (STATE_UNKNOWN, "%s: Invalid host name %s\n", PROGNAME,
! 									 argv[c]);
! 			hostname = argv[c];
! 		}
! 		else if (remotecmd == NULL) {
! 			remotecmd = strscpy (remotecmd, argv[c++]);
! 			for (; c < argc; c++)
! 				remotecmd = ssprintf (remotecmd, "%s %s", remotecmd, argv[c]);
! 		}
! 
! 	}
! 
! 	if (commands > 1)
! 		remotecmd = strscat (remotecmd, ";echo STATUS CODE: $?;");
! 
! 	if (remotecmd == NULL || strlen (remotecmd) <= 1)
! 		usage ("No remotecmd\n");
! 
! 	comm = ssprintf (comm, "%s %s '%s'", comm, hostname, remotecmd);
! 
! 	return validate_arguments ();
! }
! 
! 
! 
! 
! 
! /* Call getopt */
! int
! call_getopt (int argc, char **argv)
! {
! 	int c, i = 1;
  
  #ifdef HAVE_GETOPT_H
--- 174,180 ----
  process_arguments (int argc, char **argv)
  {
! 	int c, i;
! 	char *p1, *p2;
! 	size_t len;
  
  #ifdef HAVE_GETOPT_H
***************
*** 255,258 ****
--- 201,211 ----
  #endif
  
+ 	if (argc < 2)
+ 		return ERROR;
+ 
+ 	for (c = 1; c < argc; c++)
+ 		if (strcmp ("-to", argv[c]) == 0)
+ 			strcpy (argv[c], "-t");
+ 
  	while (1) {
  #ifdef HAVE_GETOPT_H
***************
*** 267,284 ****
  			break;
  
- 		i++;
- 		switch (c) {
- 		case 't':
- 		case 'H':
- 		case 'O':
- 		case 'p':
- 		case 'i':
- 		case 'u':
- 		case 'l':
- 		case 'n':
- 		case 's':
- 			i++;
- 		}
- 
  		switch (c) {
  		case '?':									/* help */
--- 220,223 ----
***************
*** 307,311 ****
  			if (!is_integer (optarg))
  				usage2 ("port must be an integer", optarg);
! 			comm = ssprintf (comm,"%s -p %s", comm, optarg);
  			break;
  		case 'O':									/* output file */
--- 246,250 ----
  			if (!is_integer (optarg))
  				usage2 ("port must be an integer", optarg);
! 			asprintf (&comm,"%s -p %s", comm, optarg);
  			break;
  		case 'O':									/* output file */
***************
*** 314,318 ****
  			break;
  		case 's':									/* description of service to check */
! 			servicelist = optarg;
  			break;
  		case 'n':									/* short name of host in nagios configuration */
--- 253,265 ----
  			break;
  		case 's':									/* description of service to check */
! 			service = realloc (service, ++services);
! 			p1 = optarg;
! 			while (p2 = index (p1, ':')) {
! 				*p2 = '\0';
! 				asprintf (&service[services-1], "%s", p1);
! 				service = realloc (service, ++services);
! 				p1 = p2 + 1;
! 			}
! 			asprintf (&service[services-1], "%s", p1);
  			break;
  		case 'n':									/* short name of host in nagios configuration */
***************
*** 323,341 ****
  		case 'l':									/* login name */
  		case 'i':									/* identity */
! 			comm = ssprintf (comm, "%s -%c %s", comm, c, optarg);
  			break;
  		case '4':									/* Pass these switches directly to ssh */
  		case '6': 								/* -4 for IPv4, -6 for IPv6 */
  		case 'f':									/* fork to background */
! 			comm = ssprintf (comm, "%s -%c", comm, c);
  			break;
  		case 'C':									/* Command for remote machine */
  			commands++;
  			if (commands > 1)
! 				remotecmd = strscat (remotecmd, ";echo STATUS CODE: $?;");
! 			remotecmd = strscat (remotecmd, optarg);
  		}
  	}
! 	return i;
  }
  
--- 270,309 ----
  		case 'l':									/* login name */
  		case 'i':									/* identity */
! 			asprintf (&comm, "%s -%c %s", comm, c, optarg);
  			break;
  		case '4':									/* Pass these switches directly to ssh */
  		case '6': 								/* -4 for IPv4, -6 for IPv6 */
  		case 'f':									/* fork to background */
! 			asprintf (&comm, "%s -%c", comm, c);
  			break;
  		case 'C':									/* Command for remote machine */
  			commands++;
  			if (commands > 1)
! 				asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
! 			asprintf (&remotecmd, "%s%s", remotecmd, optarg);
  		}
  	}
! 
! 	c = optind;
! 	if (hostname == NULL) {
! 		if (!is_host (argv[c]))
! 			terminate (STATE_UNKNOWN, "%s: Invalid host name %s\n", PROGNAME, argv[c]);
! 		hostname = argv[c++];
! 	}
! 
! 	if (strlen(remotecmd) == 0) {
! 		for (; c < argc; c++)
! 			asprintf (&remotecmd, "%s %s", remotecmd, argv[c]);
! 	}
! 
! 	if (commands > 1)
! 		remotecmd = strscat (remotecmd, ";echo STATUS CODE: $?;");
! 
! 	if (remotecmd == NULL || strlen (remotecmd) <= 1)
! 		usage ("No remotecmd\n");
! 
! 	asprintf (&comm, "%s %s '%s'", comm, hostname, remotecmd);
! 
! 	return validate_arguments ();
  }
  
***************
*** 349,352 ****
--- 317,327 ----
  	if (remotecmd == NULL || hostname == NULL)
  		return ERROR;
+ 
+ 	if (passive && commands != services)
+ 		terminate (STATE_UNKNOWN, "%s: In passive mode, you must provide a service name for each command.\n", PROGNAME);
+ 
+ 	if (passive && host_shortname == NULL)
+ 		terminate (STATE_UNKNOWN, "%s: In passive mode, you must provide the host short name from the nagios configs.\n", PROGNAME);
+ 
  	return OK;
  }





More information about the Commits mailing list