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; }
int main(int argc, char **argv) { int sd=0; int fd=0; char ch[511]; /* Matches ndo2db read buffer size */ int result=0; int nbytesread,n,i; char *p; 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",FILE2SOCK_NAME,FILE2SOCK_VERSION); printf("Copyright (c) 2009 Nagios Core Development Team and Community Contributors\n"); printf("Copyright (c) 2005-2007 Ethan Galstad\n"); printf("Last Modified: %s\n",FILE2SOCK_DATE); printf("License: GPL v2\n"); printf("\n"); printf("Sends the contents of a file to a TCP or UNIX domain socket. The contents of\n"); printf("the file are sent in their original format - no conversion, encapsulation, or\n"); printf("other processing is done before sending the contents to the destination socket.\n"); printf("\n"); printf("Usage: %s -s <source> -d <dest> [-t <type>] [-p <port>]\n",argv[0]); printf("\n"); printf("<source> = Name of the file to read from. Use '-' to read from stdin.\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("<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); } /* open the source file for reading */ if(!strcmp(source_name,"-")) fd=STDIN_FILENO; else if((fd=open(source_name,O_RDONLY))==-1) { perror("Unable to open source file for reading"); exit(1); } /* open data sink */ if(ndo_sink_open(dest_name,sd,socket_type,tcp_port,0,&sd)==NDO_ERROR) { perror("Cannot open destination socket"); close(fd); exit(1); } /* we're reading from stdin... */ #ifdef USE_SENDFILE if(fd==STDIN_FILENO) { #endif while((nbytesread = read(fd,&ch,sizeof(ch))) > 0) { p = &ch[0]; n = nbytesread; while (n > 0) { i = write(sd, p, n); if(i < 0) { perror("Error while writing to destination socket"); result=1; goto breakout; } p += i; n -= i; } } breakout: #ifdef USE_SENDFILE } /* we're reading from a standard file... */ else { /* get file size info */ if(fstat(fd,&stat_buf)==-1) {