r20261: merge 20260 from samba_3_0_24
[Samba/gbeck.git] / source3 / utils / eventlogadm.c
bloba964043c8307b4dad8b723980f148d0a5d224f7e
2 /*
3 * Samba Unix/Linux SMB client utility
4 * Write Eventlog records to a tdb, perform other eventlog related functions
7 * Copyright (C) Brian Moran 2005.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "includes.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_UTIL_EVENTLOG
31 extern int optind;
32 extern char *optarg;
34 int opt_debug = 0;
36 static void usage( char *s )
38 printf( "\nUsage: %s [OPTION]\n\n", s );
39 printf( " -o write <Eventlog Name> \t\t\t\t\tWrites records to eventlog from STDIN\n" );
40 printf( " -o addsource <EventlogName> <sourcename> <msgfileDLLname> \tAdds the specified source & DLL eventlog registry entry\n" );
41 printf( "\nMiscellaneous options:\n" );
42 printf( " -d\t\t\t\t\t\t\t\tturn debug on\n" );
43 printf( " -h\t\t\t\t\t\t\t\tdisplay help\n\n" );
46 static void display_eventlog_names( void )
48 const char **elogs;
49 int i;
51 elogs = lp_eventlog_list( );
52 printf( "Active eventlog names (from smb.conf):\n" );
53 printf( "--------------------------------------\n" );
54 if ( elogs ) {
55 for ( i = 0; elogs[i]; i++ ) {
56 printf( "\t%s\n", elogs[i] );
59 else
60 printf( "\t<None specified>\n");
63 static int DoAddSourceCommand( int argc, char **argv, BOOL debugflag, char *exename )
66 if ( argc < 3 ) {
67 printf( "need more arguments:\n" );
68 printf( "-o addsource EventlogName SourceName /path/to/EventMessageFile.dll\n" );
69 return -1;
71 /* must open the registry before we access it */
72 if ( !regdb_init( ) ) {
73 printf( "Can't open the registry.\n" );
74 return -1;
77 if ( !eventlog_add_source( argv[0], argv[1], argv[2] ) )
78 return -2;
79 return 0;
82 static int DoWriteCommand( int argc, char **argv, BOOL debugflag, char *exename )
84 FILE *f1;
85 char *argfname;
86 ELOG_TDB *etdb;
88 /* fixed constants are bad bad bad */
89 pstring linein;
90 BOOL is_eor;
91 Eventlog_entry ee;
92 int rcnum;
94 f1 = stdin;
95 if ( !f1 ) {
96 printf( "Can't open STDIN\n" );
97 return -1;
100 if ( debugflag ) {
101 printf( "Starting write for eventlog [%s]\n", argv[0] );
102 display_eventlog_names( );
105 argfname = argv[0];
107 if ( !( etdb = elog_open_tdb( argfname, False ) ) ) {
108 printf( "can't open the eventlog TDB (%s)\n", argfname );
109 return -1;
112 ZERO_STRUCT( ee ); /* MUST initialize between records */
114 while ( !feof( f1 ) ) {
115 fgets( linein, sizeof( linein ) - 1, f1 );
116 linein[strlen( linein ) - 1] = 0; /* whack the line delimiter */
118 if ( debugflag )
119 printf( "Read line [%s]\n", linein );
121 is_eor = False;
124 parse_logentry( ( char * ) &linein, &ee, &is_eor );
125 /* should we do something with the return code? */
127 if ( is_eor ) {
128 fixup_eventlog_entry( &ee );
130 if ( opt_debug )
131 printf( "record number [%d], tg [%d] , tw [%d]\n", ee.record.record_number, ee.record.time_generated, ee.record.time_written );
133 if ( ee.record.time_generated != 0 ) {
135 /* printf("Writing to the event log\n"); */
137 rcnum = write_eventlog_tdb( ELOG_TDB_CTX(etdb), &ee );
138 if ( !rcnum ) {
139 printf( "Can't write to the event log\n" );
140 } else {
141 if ( opt_debug )
142 printf( "Wrote record %d\n",
143 rcnum );
145 } else {
146 if ( opt_debug )
147 printf( "<null record>\n" );
149 ZERO_STRUCT( ee ); /* MUST initialize between records */
153 elog_close_tdb( etdb , False );
155 return 0;
158 /* would be nice to use the popT stuff here, however doing so forces us to drag in a lot of other infrastructure */
160 int main( int argc, char *argv[] )
162 int opt, rc;
163 char *exename;
166 fstring opname;
168 load_case_tables();
170 opt_debug = 0; /* todo set this from getopts */
172 lp_load( dyn_CONFIGFILE, True, False, False, True);
174 exename = argv[0];
176 /* default */
178 fstrcpy( opname, "write" ); /* the default */
180 #if 0 /* TESTING CODE */
181 eventlog_add_source( "System", "TestSourceX", "SomeTestPathX" );
182 #endif
183 while ( ( opt = getopt( argc, argv, "dho:" ) ) != EOF ) {
184 switch ( opt ) {
186 case 'o':
187 fstrcpy( opname, optarg );
188 break;
190 case 'h':
191 usage( exename );
192 display_eventlog_names( );
193 exit( 0 );
194 break;
196 case 'd':
197 opt_debug = 1;
198 break;
202 argc -= optind;
203 argv += optind;
205 if ( argc < 1 ) {
206 printf( "\nNot enough arguments!\n" );
207 usage( exename );
208 exit( 1 );
211 /* note that the separate command types should call usage if they need to... */
212 while ( 1 ) {
213 if ( !StrCaseCmp( opname, "addsource" ) ) {
214 rc = DoAddSourceCommand( argc, argv, opt_debug,
215 exename );
216 break;
218 if ( !StrCaseCmp( opname, "write" ) ) {
219 rc = DoWriteCommand( argc, argv, opt_debug, exename );
220 break;
222 printf( "unknown command [%s]\n", opname );
223 usage( exename );
224 exit( 1 );
225 break;
227 return rc;