utils/eventlogadm.c

説明を見る。
00001 
00002 /*
00003  * Samba Unix/Linux SMB client utility 
00004  * Write Eventlog records to a tdb, perform other eventlog related functions
00005  *
00006  *
00007  * Copyright (C) Brian Moran                2005.
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00022  */
00023 
00024 
00025 #include "includes.h"
00026 
00027 #undef  DBGC_CLASS
00028 #define DBGC_CLASS DBGC_UTIL_EVENTLOG
00029 
00030 
00031 extern int optind;
00032 extern char *optarg;
00033 
00034 int opt_debug = 0;
00035 
00036 static void usage( char *s )
00037 {
00038         printf( "\nUsage: %s [OPTION]\n\n", s );
00039         printf( " -o write <Eventlog Name> \t\t\t\t\tWrites records to eventlog from STDIN\n" );
00040         printf( " -o addsource <EventlogName> <sourcename> <msgfileDLLname> \tAdds the specified source & DLL eventlog registry entry\n" );
00041         printf( "\nMiscellaneous options:\n" );
00042         printf( " -d\t\t\t\t\t\t\t\tturn debug on\n" );
00043         printf( " -h\t\t\t\t\t\t\t\tdisplay help\n\n" );
00044 }
00045 
00046 static void display_eventlog_names( void )
00047 {
00048         const char **elogs;
00049         int i;
00050 
00051         elogs = lp_eventlog_list(  );
00052         printf( "Active eventlog names (from smb.conf):\n" );
00053         printf( "--------------------------------------\n" );
00054         if ( elogs ) {
00055                 for ( i = 0; elogs[i]; i++ ) {
00056                         printf( "\t%s\n", elogs[i] );
00057                 }
00058         } 
00059         else
00060                 printf( "\t<None specified>\n");
00061 }
00062 
00063 static int DoAddSourceCommand( int argc, char **argv, BOOL debugflag, char *exename )
00064 {
00065 
00066         if ( argc < 3 ) {
00067                 printf( "need more arguments:\n" );
00068                 printf( "-o addsource EventlogName SourceName /path/to/EventMessageFile.dll\n" );
00069                 return -1;
00070         }
00071         /* must open the registry before we access it */
00072         if ( !regdb_init(  ) ) {
00073                 printf( "Can't open the registry.\n" );
00074                 return -1;
00075         }
00076 
00077         if ( !eventlog_add_source( argv[0], argv[1], argv[2] ) )
00078                 return -2;
00079         return 0;
00080 }
00081 
00082 static int DoWriteCommand( int argc, char **argv, BOOL debugflag, char *exename )
00083 {
00084         FILE *f1;
00085         char *argfname;
00086         ELOG_TDB *etdb;
00087 
00088         /* fixed constants are bad bad bad  */
00089         pstring linein;
00090         BOOL is_eor;
00091         Eventlog_entry ee;
00092         int rcnum;
00093 
00094         f1 = stdin;
00095         if ( !f1 ) {
00096                 printf( "Can't open STDIN\n" );
00097                 return -1;
00098         }
00099 
00100         if ( debugflag ) {
00101                 printf( "Starting write for eventlog [%s]\n", argv[0] );
00102                 display_eventlog_names(  );
00103         }
00104 
00105         argfname = argv[0];
00106 
00107         if ( !( etdb = elog_open_tdb( argfname, False ) ) ) {
00108                 printf( "can't open the eventlog TDB (%s)\n", argfname );
00109                 return -1;
00110         }
00111 
00112         ZERO_STRUCT( ee );      /* MUST initialize between records */
00113 
00114         while ( !feof( f1 ) ) {
00115                 fgets( linein, sizeof( linein ) - 1, f1 );
00116                 linein[strlen( linein ) - 1] = 0;       /* whack the line delimiter */
00117 
00118                 if ( debugflag )
00119                         printf( "Read line [%s]\n", linein );
00120 
00121                 is_eor = False;
00122 
00123 
00124                 parse_logentry( ( char * ) &linein, &ee, &is_eor );
00125                 /* should we do something with the return code? */
00126 
00127                 if ( is_eor ) {
00128                         fixup_eventlog_entry( &ee );
00129 
00130                         if ( opt_debug )
00131                                 printf( "record number [%d], tg [%d] , tw [%d]\n", ee.record.record_number, ee.record.time_generated, ee.record.time_written );
00132 
00133                         if ( ee.record.time_generated != 0 ) {
00134 
00135                                 /* printf("Writing to the event log\n"); */
00136 
00137                                 rcnum = write_eventlog_tdb( ELOG_TDB_CTX(etdb), &ee );
00138                                 if ( !rcnum ) {
00139                                         printf( "Can't write to the event log\n" );
00140                                 } else {
00141                                         if ( opt_debug )
00142                                                 printf( "Wrote record %d\n",
00143                                                         rcnum );
00144                                 }
00145                         } else {
00146                                 if ( opt_debug )
00147                                         printf( "<null record>\n" );
00148                         }
00149                         ZERO_STRUCT( ee );      /* MUST initialize between records */
00150                 }
00151         }
00152 
00153         elog_close_tdb( etdb , False );
00154 
00155         return 0;
00156 }
00157 
00158 /* would be nice to use the popT stuff here, however doing so forces us to drag in a lot of other infrastructure */
00159 
00160 int main( int argc, char *argv[] )
00161 {
00162         int opt, rc;
00163         char *exename;
00164 
00165 
00166         fstring opname;
00167 
00168         load_case_tables();
00169 
00170         opt_debug = 0;          /* todo set this from getopts */
00171 
00172         lp_load( dyn_CONFIGFILE, True, False, False, True);
00173 
00174         exename = argv[0];
00175 
00176         /* default */
00177 
00178         fstrcpy( opname, "write" );     /* the default */
00179 
00180 #if 0                           /* TESTING CODE */
00181         eventlog_add_source( "System", "TestSourceX", "SomeTestPathX" );
00182 #endif
00183         while ( ( opt = getopt( argc, argv, "dho:" ) ) != EOF ) {
00184                 switch ( opt ) {
00185 
00186                 case 'o':
00187                         fstrcpy( opname, optarg );
00188                         break;
00189 
00190                 case 'h':
00191                         usage( exename );
00192                         display_eventlog_names(  );
00193                         exit( 0 );
00194                         break;
00195 
00196                 case 'd':
00197                         opt_debug = 1;
00198                         break;
00199                 }
00200         }
00201 
00202         argc -= optind;
00203         argv += optind;
00204 
00205         if ( argc < 1 ) {
00206                 printf( "\nNot enough arguments!\n" );
00207                 usage( exename );
00208                 exit( 1 );
00209         }
00210 
00211         /*  note that the separate command types should call usage if they need to... */
00212         while ( 1 ) {
00213                 if ( !StrCaseCmp( opname, "addsource" ) ) {
00214                         rc = DoAddSourceCommand( argc, argv, opt_debug,
00215                                                  exename );
00216                         break;
00217                 }
00218                 if ( !StrCaseCmp( opname, "write" ) ) {
00219                         rc = DoWriteCommand( argc, argv, opt_debug, exename );
00220                         break;
00221                 }
00222                 printf( "unknown command [%s]\n", opname );
00223                 usage( exename );
00224                 exit( 1 );
00225                 break;
00226         }
00227         return rc;
00228 }

Sambaに対してSat Aug 29 21:23:28 2009に生成されました。  doxygen 1.4.7