![]() |
Icinga-core 1.4.0
next gen monitoring
|
00001 /*************************************************************** 00002 * FILE2SOCK.c - File to Socket Dump Utility 00003 * 00004 * Copyright (c) 20052-2007 Ethan Galstad 00005 * Copyright (c) 2009-2011 Icinga Development Team (http://www.icinga.org) 00006 * 00007 * License: GPL v2 00008 * 00009 **************************************************************/ 00010 00011 #include "../../../include/config.h" 00012 #include "../include/common.h" 00013 #include "../include/io.h" 00014 00015 #define FILE2SOCK_VERSION "1.4.0" 00016 #define FILE2SOCK_NAME "FILE2SOCK" 00017 #define FILE2SOCK_DATE "05-11-2011" 00018 00019 00020 int process_arguments(int,char **); 00021 00022 00023 char *source_name=NULL; 00024 char *dest_name=NULL; 00025 int socket_type=IDO_SINK_UNIXSOCKET; 00026 int tcp_port=0; 00027 int show_version=IDO_FALSE; 00028 int show_license=IDO_FALSE; 00029 int show_help=IDO_FALSE; 00030 00031 int main(int argc, char **argv){ 00032 int sd=0; 00033 int fd=0; 00034 char ch[1]; 00035 int result=0; 00036 00037 00038 result=process_arguments(argc,argv); 00039 00040 if(result!=IDO_OK || show_help==IDO_TRUE || show_license==IDO_TRUE || show_version==IDO_TRUE){ 00041 00042 if(result!=IDO_OK) 00043 printf("Incorrect command line arguments supplied\n"); 00044 00045 printf("\n"); 00046 printf("%s %s\n",FILE2SOCK_NAME,FILE2SOCK_VERSION); 00047 printf("Copyright(c) 2009-2011 Icinga Development Team (http://www.icinga.org)\n"); 00048 printf("Copyright(c) 2005-2007 Ethan Galstad (nagios@nagios.org)\n"); 00049 printf("Last Modified: %s\n",FILE2SOCK_DATE); 00050 printf("License: GPL v2\n"); 00051 printf("\n"); 00052 printf("Sends the contents of a file to a TCP or UNIX domain socket. The contents of\n"); 00053 printf("the file are sent in their original format - no conversion, encapsulation, or\n"); 00054 printf("other processing is done before sending the contents to the destination socket.\n"); 00055 printf("\n"); 00056 printf("Usage: %s -s <source> -d <dest> [-t <type>] [-p <port>]\n",argv[0]); 00057 printf("\n"); 00058 printf("<source> = Name of the file to read from. Use '-' to read from stdin.\n"); 00059 printf("<dest> = If destination is a TCP socket, the address/hostname to connect to.\n"); 00060 printf(" If destination is a Unix domain socket, the path to the socket.\n"); 00061 printf("<type> = Specifies the type of destination socket. Valid values include:\n"); 00062 printf(" tcp\n"); 00063 printf(" unix (default)\n"); 00064 printf("<port> = Port number to connect to if destination is TCP socket.\n"); 00065 printf("\n"); 00066 00067 exit(1); 00068 } 00069 00070 /* open the source file for reading */ 00071 if(!strcmp(source_name,"-")) 00072 fd=STDIN_FILENO; 00073 else if((fd=open(source_name,O_RDONLY))==-1){ 00074 perror("Unable to open source file for reading"); 00075 exit(1); 00076 } 00077 00078 /* open data sink */ 00079 if(ido_sink_open(dest_name,sd,socket_type,tcp_port,0,&sd)==IDO_ERROR){ 00080 perror("Cannot open destination socket"); 00081 close(fd); 00082 exit(1); 00083 } 00084 00085 /* we're reading from stdin... */ 00086 #ifdef USE_SENDFILE 00087 if(fd==STDIN_FILENO){ 00088 #endif 00089 while((read(fd,&ch,1))){ 00090 if(write(sd,&ch,1)==-1){ 00091 perror("Error while writing to destination socket"); 00092 result=1; 00093 break; 00094 } 00095 } 00096 #ifdef USE_SENDFILE 00097 } 00098 00099 /* we're reading from a standard file... */ 00100 else{ 00101 00102 /* get file size info */ 00103 if(fstat(fd,&stat_buf)==-1){ 00104 perror("fstat() error"); 00105 result=1; 00106 } 00107 00108 /* send the file contents to the socket */ 00109 else if(sendfile(sd,fd,&offset,stat_buf.st_size)==-1){ 00110 perror("sendfile() error"); 00111 result=1; 00112 } 00113 } 00114 #endif 00115 00116 /* close the data sink */ 00117 ido_sink_flush(sd); 00118 ido_sink_close(sd); 00119 00120 /* close the source file */ 00121 close(fd); 00122 00123 return result; 00124 } 00125 00126 00127 /* process command line arguments */ 00128 int process_arguments(int argc, char **argv){ 00129 char optchars[32]; 00130 int c=1; 00131 00132 #ifdef HAVE_GETOPT_H 00133 int option_index=0; 00134 static struct option long_options[]={ 00135 {"source", required_argument, 0, 's'}, 00136 {"dest", required_argument, 0, 'd'}, 00137 {"type", required_argument, 0, 't'}, 00138 {"port", required_argument, 0, 'p'}, 00139 {"help", no_argument, 0, 'h'}, 00140 {"license", no_argument, 0, 'l'}, 00141 {"version", no_argument, 0, 'V'}, 00142 {0, 0, 0, 0} 00143 }; 00144 #endif 00145 00146 /* no options were supplied */ 00147 if(argc<2){ 00148 show_help=IDO_TRUE; 00149 return IDO_OK; 00150 } 00151 00152 snprintf(optchars,sizeof(optchars),"s:d:t:p:hlV"); 00153 00154 while(1){ 00155 #ifdef HAVE_GETOPT_H 00156 c=getopt_long(argc,argv,optchars,long_options,&option_index); 00157 #else 00158 c=getopt(argc,argv,optchars); 00159 #endif 00160 if(c==-1 || c==EOF) 00161 break; 00162 00163 /* process all arguments */ 00164 switch(c){ 00165 00166 case '?': 00167 case 'h': 00168 show_help=IDO_TRUE; 00169 break; 00170 case 'V': 00171 show_version=IDO_TRUE; 00172 break; 00173 case 'l': 00174 show_license=IDO_TRUE; 00175 break; 00176 case 't': 00177 if(!strcmp(optarg,"tcp")) 00178 socket_type=IDO_SINK_TCPSOCKET; 00179 else if(!strcmp(optarg,"unix")) 00180 socket_type=IDO_SINK_UNIXSOCKET; 00181 else 00182 return IDO_ERROR; 00183 break; 00184 case 'p': 00185 tcp_port=atoi(optarg); 00186 if(tcp_port<=0) 00187 return IDO_ERROR; 00188 break; 00189 case 's': 00190 source_name=strdup(optarg); 00191 break; 00192 case 'd': 00193 dest_name=strdup(optarg); 00194 break; 00195 default: 00196 return IDO_ERROR; 00197 break; 00198 } 00199 } 00200 00201 /* make sure required args were supplied */ 00202 if((source_name==NULL || dest_name==NULL) && show_help==IDO_FALSE && show_version==IDO_FALSE && show_license==IDO_FALSE) 00203 return IDO_ERROR; 00204 00205 return IDO_OK; 00206 } 00207