Icinga-core 1.4.0
next gen monitoring
module/idoutils/src/log2ido.c
Go to the documentation of this file.
00001 /***************************************************************
00002  * LOG2IDO.C - Sends archived logs files to IDO2DB daemon
00003  *
00004  * Copyright (c) 2005-2007 Ethan Galstad
00005  * Copyright (c) 2009-2011 Icinga Development Team (http://www.icinga.org) 
00006  *
00007  **************************************************************/
00008 
00009 #include "../../../include/config.h"
00010 #include "../include/common.h"
00011 #include "../include/io.h"
00012 #include "../include/protoapi.h"
00013 
00014 #define LOG2IDO_VERSION "1.4.0"
00015 #define LOG2IDO_NAME "LOG2IDO"
00016 #define LOG2IDO_DATE "05-11-2011"
00017 
00018 
00019 
00020 int process_arguments(int,char **);
00021 
00022 char *source_name=NULL;
00023 char *dest_name=NULL;
00024 char *instance_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 
00032 int main(int argc, char **argv){
00033         ido_mmapfile *thefile=NULL;
00034         char *connection_type=NULL;
00035         char *input=NULL;
00036         char *input2=NULL;
00037         int sd=2;
00038         char tempbuf[1024];
00039         int result=0;
00040 
00041 
00042         result=process_arguments(argc,argv);
00043 
00044         if(result!=IDO_OK || show_help==IDO_TRUE || show_license==IDO_TRUE || show_version==IDO_TRUE){
00045 
00046                 if(result!=IDO_OK)
00047                         printf("Incorrect command line arguments supplied\n");
00048 
00049                 printf("\n");
00050                 printf("%s %s\n",LOG2IDO_NAME,LOG2IDO_VERSION);
00051                 printf("Copyright(c) 2009-2011 Icinga Development Team (http://www.icinga.org)\n");
00052                 printf("Copyright(c) 2005-2007 Ethan Galstad (nagios@nagios.org)\n");
00053                 printf("Last Mofieid: %s\n",LOG2IDO_DATE);
00054                 printf("License: GPL v2\n");
00055                 printf("\n");
00056                 printf("Sends the contents of an archived Icinga log file to STDOUT,\n");
00057                 printf("a TCP socket, or a Unix domain socket in a format that is understood by the\n");
00058                 printf("IDO2DB daemon.\n");
00059                 printf("\n");
00060                 printf("Usage: %s -s <source> -d <dest> -i <instance> [-t <type>] [-p <port>]\n",argv[0]);
00061                 printf("\n");
00062                 printf("<source>   = Name of the Icinga log file to read from.\n");
00063                 printf("<dest>     = If destination is a TCP socket, the address/hostname to connect to.\n");
00064                 printf("             If destination is a Unix domain socket, the path to the socket.\n");
00065                 printf("             If destination is STDOUT (for redirection, etc), a single dash (-).\n");
00066                 printf("<type>     = Specifies the type of destination socket.  Valid values include:\n");
00067                 printf("                 tcp\n");
00068                 printf("                 unix (default)\n");
00069                 printf("<port>     = Port number to connect to if destination is TCP socket.\n");
00070                 printf("\n");
00071 
00072                 exit(1);
00073                 }
00074 
00075         /* send output to STDOUT rather than a socket */
00076         if(!strcmp(dest_name,"-")){
00077                 sd=STDOUT_FILENO;
00078                 socket_type=IDO_SINK_FD;
00079                 }
00080 
00081         /* open the file for reading */
00082         if((thefile=ido_mmap_fopen(source_name))==NULL){
00083                 perror("Unable to open source file for reading");
00084                 exit(1);
00085                 }
00086 
00087         /* open the destination */
00088         if(ido_sink_open(dest_name,sd,socket_type,tcp_port,0,&sd)==IDO_ERROR){
00089                 ido_mmap_fclose(thefile);
00090                 exit(1);
00091                 }
00092 
00093 
00094         /***** SEND HEADER INFORMATION *****/
00095 
00096         /* get the connection type string */
00097         if(socket_type==IDO_SINK_FD || socket_type==IDO_SINK_FILE)
00098                 connection_type=IDO_API_CONNECTION_FILE;
00099         else if(socket_type==IDO_SINK_TCPSOCKET)
00100                 connection_type=IDO_API_CONNECTION_TCPSOCKET;
00101         else
00102                 connection_type=IDO_API_CONNECTION_UNIXSOCKET;
00103 
00104         snprintf(tempbuf,sizeof(tempbuf)-1
00105                  ,"%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"
00106                  ,IDO_API_HELLO
00107                  ,IDO_API_PROTOCOL
00108                  ,IDO_API_PROTOVERSION
00109                  ,IDO_API_AGENT
00110                  ,LOG2IDO_NAME
00111                  ,IDO_API_AGENTVERSION
00112                  ,LOG2IDO_VERSION
00113                  ,IDO_API_STARTTIME
00114                  ,(unsigned long)time(NULL)
00115                  ,IDO_API_DISPOSITION
00116                  ,IDO_API_DISPOSITION_ARCHIVED
00117                  ,IDO_API_CONNECTION
00118                  ,connection_type
00119                  ,IDO_API_CONNECTTYPE
00120                  ,IDO_API_CONNECTTYPE_INITIAL
00121                  ,IDO_API_INSTANCENAME
00122                  ,(instance_name==NULL)?"default":instance_name
00123                  ,IDO_API_STARTDATADUMP
00124                 );
00125         tempbuf[sizeof(tempbuf)-1]='\x0';
00126         ido_sink_write(sd,tempbuf,strlen(tempbuf));
00127 
00128 
00129 
00130         /***** SEND THE LOG CONTENTS *****/
00131 
00132         while((input=ido_mmap_fgets(thefile))){
00133 
00134                 /* strip and escape log entry */
00135                 ido_strip_buffer(input);
00136                 if((input2=ido_escape_buffer(input))==NULL){
00137                         free(input);
00138                         input2=NULL;
00139                         continue;
00140                         }
00141 
00142                 /* write log entry header */
00143                 snprintf(tempbuf,sizeof(tempbuf)-1
00144                          ,"%d:\n%d=%s\n%d\n\n"
00145                          ,IDO_API_LOGENTRY
00146                          ,IDO_DATA_LOGENTRY
00147                          ,input2
00148                          ,IDO_API_ENDDATA
00149                         );
00150                 tempbuf[sizeof(tempbuf)-1]='\x0';
00151                 ido_sink_write(sd,tempbuf,strlen(tempbuf));
00152 
00153                 /* free allocated memory */
00154                 free(input);
00155                 free(input2);
00156                 input=NULL;
00157                 input2=NULL;
00158                 }
00159 
00160 
00161 
00162         /***** SAY GOODBYE *****/
00163 
00164         snprintf(tempbuf,sizeof(tempbuf)-1,"\n%d\n%s: %lu\n%s\n"
00165                  ,IDO_API_ENDDATADUMP
00166                  ,IDO_API_ENDTIME
00167                  ,(unsigned long)time(NULL)
00168                  ,IDO_API_GOODBYE
00169                 );
00170         tempbuf[sizeof(tempbuf)-1]='\x0';
00171         ido_sink_write(sd,tempbuf,strlen(tempbuf));
00172 
00173 
00174 
00175         /* close the destination */
00176         ido_sink_flush(sd);
00177         ido_sink_close(sd);
00178 
00179         /* close the file */
00180         ido_mmap_fclose(thefile);
00181 
00182         return 0;
00183         }
00184 
00185 
00186 /* process command line arguments */
00187 int process_arguments(int argc, char **argv){
00188         char optchars[32];
00189         int c=1;
00190 
00191 #ifdef HAVE_GETOPT_H
00192         int option_index=0;
00193         static struct option long_options[]={
00194                 {"source", required_argument, 0, 's'},
00195                 {"dest", required_argument, 0, 'd'},
00196                 {"instance", required_argument, 0, 'i'},
00197                 {"type", required_argument, 0, 't'},
00198                 {"port", required_argument, 0, 'p'},
00199                 {"help", no_argument, 0, 'h'},
00200                 {"license", no_argument, 0, 'l'},
00201                 {"version", no_argument, 0, 'V'},
00202                 {0, 0, 0, 0}
00203                 };
00204 #endif
00205 
00206         /* no options were supplied */
00207         if(argc<2){
00208                 show_help=IDO_TRUE;
00209                 return IDO_OK;
00210                 }
00211 
00212         snprintf(optchars,sizeof(optchars),"s:d:i:t:p:hlV");
00213 
00214         while(1){
00215 #ifdef HAVE_GETOPT_H
00216                 c=getopt_long(argc,argv,optchars,long_options,&option_index);
00217 #else
00218                 c=getopt(argc,argv,optchars);
00219 #endif
00220                 if(c==-1 || c==EOF)
00221                         break;
00222 
00223                 /* process all arguments */
00224                 switch(c){
00225 
00226                 case '?':
00227                 case 'h':
00228                         show_help=IDO_TRUE;
00229                         break;
00230                 case 'V':
00231                         show_version=IDO_TRUE;
00232                         break;
00233                 case 'l':
00234                         show_license=IDO_TRUE;
00235                         break;
00236                 case 't':
00237                         if(!strcmp(optarg,"tcp"))
00238                                 socket_type=IDO_SINK_TCPSOCKET;
00239                         else if(!strcmp(optarg,"unix"))
00240                                 socket_type=IDO_SINK_UNIXSOCKET;
00241                         else
00242                                 return IDO_ERROR;
00243                         break;
00244                 case 'p':
00245                         tcp_port=atoi(optarg);
00246                         if(tcp_port<=0)
00247                                 return IDO_ERROR;
00248                         break;
00249                 case 's':
00250                         source_name=strdup(optarg);
00251                         break;
00252                 case 'd':
00253                         dest_name=strdup(optarg);
00254                         break;
00255                 case 'i':
00256                         instance_name=strdup(optarg);
00257                         break;
00258                 default:
00259                         return IDO_ERROR;
00260                         break;
00261                         }
00262                 }
00263 
00264         /* make sure required args were supplied */
00265         if((source_name==NULL || dest_name==NULL || instance_name==NULL) && show_help==IDO_FALSE && show_version==IDO_FALSE  && show_license==IDO_FALSE)
00266                 return IDO_ERROR;
00267 
00268         return IDO_OK;
00269         }
00270 
 All Data Structures Files Functions Variables Typedefs Defines