예제 #1
0
int main(int argc, char **argv){
	ndo_mmapfile *thefile=NULL;
	char *connection_type=NULL;
	char *input=NULL;
	char *input2=NULL;
	int sd=2;
	char tempbuf[1024];
	int result=0;


	result=process_arguments(argc,argv);

        if(result!=NDO_OK || show_help==NDO_TRUE || show_license==NDO_TRUE || show_version==NDO_TRUE){

		if(result!=NDO_OK)
			printf("Incorrect command line arguments supplied\n");

		printf("\n");
		printf("%s %s\n",LOG2NDO_NAME,LOG2NDO_VERSION);
		printf("Copyright (c) 2009 Nagios Core Development Team and Community Contributors\n");
		printf("Copyright (c) 2005-2007 Ethan Galstad\n");
		printf("Last Mofieid: %s\n",LOG2NDO_DATE);
		printf("License: GPL v2\n");
		printf("\n");
		printf("Sends the contents of an archived Nagios or NetSaint log file to STDOUT,\n");
		printf("a TCP socket, or a Unix domain socket in a format that is understood by the\n");
		printf("NDO2DB daemon.\n");
		printf("\n");
		printf("Usage: %s -s <source> -d <dest> -i <instance> [-t <type>] [-p <port>]\n",argv[0]);
		printf("\n");
		printf("<source>   = Name of the Nagios/NetSaint log file to read from.\n");
		printf("<dest>     = If destination is a TCP socket, the address/hostname to connect to.\n");
		printf("             If destination is a Unix domain socket, the path to the socket.\n");
		printf("             If destination is STDOUT (for redirection, etc), a single dash (-).\n");
		printf("<type>     = Specifies the type of destination socket.  Valid values include:\n");
		printf("                 tcp\n");
		printf("                 unix (default)\n");
		printf("<port>     = Port number to connect to if destination is TCP socket.\n");
		printf("\n");

		exit(1);
	        }

	/* send output to STDOUT rather than a socket */
	if(!strcmp(dest_name,"-")){
		sd=STDOUT_FILENO;
		socket_type=NDO_SINK_FD;
	        }

	/* open the file for reading */
	if((thefile=ndo_mmap_fopen(source_name))==NULL){
		perror("Unable to open source file for reading");
		exit(1);
	        }

	/* open the destination */
	if(ndo_sink_open(dest_name,sd,socket_type,tcp_port,0,&sd)==NDO_ERROR){
		ndo_mmap_fclose(thefile);
		exit(1);
	        }


	/***** SEND HEADER INFORMATION *****/

	/* get the connection type string */
	if(socket_type==NDO_SINK_FD || socket_type==NDO_SINK_FILE)
		connection_type=NDO_API_CONNECTION_FILE;
	else if(socket_type==NDO_SINK_TCPSOCKET)
		connection_type=NDO_API_CONNECTION_TCPSOCKET;
	else
		connection_type=NDO_API_CONNECTION_UNIXSOCKET;

	snprintf(tempbuf,sizeof(tempbuf)-1
		 ,"%s\n%s: %d\n%s: %s\n%s: %s\n%s: %lu\n%s: %s\n%s: %s\n%s: %s\n%s: %s\n%s\n\n"
		 ,NDO_API_HELLO
		 ,NDO_API_PROTOCOL
		 ,NDO_API_PROTOVERSION
		 ,NDO_API_AGENT
		 ,LOG2NDO_NAME
		 ,NDO_API_AGENTVERSION
		 ,LOG2NDO_VERSION
		 ,NDO_API_STARTTIME
		 ,(unsigned long)time(NULL)
		 ,NDO_API_DISPOSITION
		 ,NDO_API_DISPOSITION_ARCHIVED
		 ,NDO_API_CONNECTION
		 ,connection_type
		 ,NDO_API_CONNECTTYPE
		 ,NDO_API_CONNECTTYPE_INITIAL
		 ,NDO_API_INSTANCENAME
		 ,(instance_name==NULL)?"default":instance_name
		 ,NDO_API_STARTDATADUMP
		);
	tempbuf[sizeof(tempbuf)-1]='\x0';
	ndo_sink_write(sd,tempbuf,strlen(tempbuf));



	/***** SEND THE LOG CONTENTS *****/

	while((input=ndo_mmap_fgets(thefile))){

		/* strip and escape log entry */
		ndo_strip_buffer(input);
		if((input2=ndo_escape_buffer(input))==NULL){
			free(input);
			input2=NULL;
			continue;
		        }

		/* write log entry header */
		snprintf(tempbuf,sizeof(tempbuf)-1
			 ,"%d:\n%d=%s\n%d\n\n"
			 ,NDO_API_LOGENTRY
			 ,NDO_DATA_LOGENTRY
			 ,input2
			 ,NDO_API_ENDDATA
			);
		tempbuf[sizeof(tempbuf)-1]='\x0';
		ndo_sink_write(sd,tempbuf,strlen(tempbuf));

		/* free allocated memory */
		free(input);
		free(input2);
		input=NULL;
		input2=NULL;
	        }



	/***** SAY GOODBYE *****/

	snprintf(tempbuf,sizeof(tempbuf)-1,"\n%d\n%s: %lu\n%s\n"
		 ,NDO_API_ENDDATADUMP
		 ,NDO_API_ENDTIME
		 ,(unsigned long)time(NULL)
		 ,NDO_API_GOODBYE
		);
	tempbuf[sizeof(tempbuf)-1]='\x0';
	ndo_sink_write(sd,tempbuf,strlen(tempbuf));



	/* close the destination */
	ndo_sink_flush(sd);
	ndo_sink_close(sd);

	/* close the file */
	ndo_mmap_fclose(thefile);

	return 0;
        }
예제 #2
0
파일: io.c 프로젝트: nagios-mirror/ndoutils
/* writes a newline to data sink */
int ndo_sink_write_newline(int fd){

	return ndo_sink_write(fd,"\n",1);
        }