172f2b9cd240c348f4c62ed538cf789fe75aa0a0
[kai/samba.git] / source3 / utils / eventlogadm.c
1 /*
2  * Samba Unix/Linux SMB client utility 
3  * Write Eventlog records to a tdb, perform other eventlog related functions
4  *
5  *
6  * Copyright (C) Brian Moran                2005.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23
24 #include "includes.h"
25
26 #undef  DBGC_CLASS
27 #define DBGC_CLASS DBGC_UTIL_EVENTLOG
28
29 Eventlog_entry ee;
30
31 extern int optind;
32 extern char *optarg;
33
34 int opt_debug = 0;
35
36 static void usage( char *s )
37 {
38         printf( "\nUsage: %s [-d] [-h] <Eventlog Name>\n", s );
39         printf( "\t-d\tturn debug on\n" );
40         printf( "\t-h\tdisplay help\n\n" );
41 }
42
43 static void display_eventlog_names( void )
44 {
45         const char **elogs;
46         int i;
47
48         elogs = lp_eventlog_list(  );
49         printf( "Active eventlog names (from smb.conf):\n" );
50         printf( "--------------------------------------\n" );
51         for ( i = 0; elogs[i]; i++ ) {
52                 printf( "\t%s\n", elogs[i] );
53         }
54 }
55
56 int main( int argc, char *argv[] )
57 {
58         FILE *f1;
59
60         /* fixed constants are bad bad bad  */
61         pstring linein;
62         BOOL is_eor;
63         int pret, opt;
64         int rcnum;
65         char *argfname, *exename;
66         char *tdbname;
67
68
69         TDB_CONTEXT *elog_tdb;
70
71         opt_debug = 0;          /* todo set this from getopts */
72
73
74         lp_load( dyn_CONFIGFILE, True, False, False );
75
76         exename = argv[0];
77
78 #if 1   /* TESTING CODE */
79         eventlog_add_source("System","TestSourceX","SomeTestPathX");
80 #endif
81
82         while ( ( opt = getopt( argc, argv, "dh" ) ) != -1 ) {
83                 switch ( opt ) {
84                 case 'h':
85                         usage( argv[0] );
86                         display_eventlog_names(  );
87                         exit( 0 );
88                         break;
89
90                 case 'd':
91                         opt_debug = 1;
92                         break;
93                 }
94         }
95
96         argc -= optind;
97         argv += optind;
98
99         if ( argc < 1 ) {
100                 usage( exename );
101                 exit( 1 );
102         }
103
104
105
106         f1 = stdin;
107
108         if ( !f1 ) {
109                 printf( "Can't open STDIN\n" );
110                 return -1;
111         }
112
113
114         if ( opt_debug ) {
115                 printf( "Starting %s for eventlog [%s]\n", exename, argv[0] );
116                 display_eventlog_names(  );
117         }
118
119         argfname = argv[0];
120
121         if ( !(elog_tdb = elog_open_tdb( argfname ) ) ) {
122                 printf( "can't open the eventlog TDB (%s)\n", tdbname );
123                 return -1;
124         }
125
126         ZERO_STRUCT( ee );      /* MUST initialize between records */
127
128         while ( !feof( f1 ) ) {
129                 fgets( linein, sizeof( linein ) - 1, f1 );
130                 linein[strlen( linein ) - 1] = 0;       /* whack the line delimiter */
131
132                 if ( opt_debug )
133                         printf( "Read line [%s]\n", linein );
134
135                 is_eor = False;
136
137                 pret = parse_logentry( ( char * ) &linein, &ee, &is_eor );
138
139                 if ( is_eor ) {
140                         fixup_eventlog_entry( &ee );
141
142                         if ( opt_debug )
143                                 printf( "record number [%d], tg [%d] , tw [%d]\n", 
144                                         ee.record.record_number, 
145                                         ee.record.time_generated, 
146                                         ee.record.time_written );
147
148                         if ( ee.record.time_generated != 0 ) {
149
150                                 /* printf("Writing to the event log\n"); */
151
152                                 rcnum = write_eventlog_tdb( elog_tdb, &ee ); 
153                                 if ( !rcnum ) {
154                                         printf( "Can't write to the event log\n" );
155                                 } else {
156                                         if ( opt_debug )
157                                                 printf( "Wrote record %d\n",
158                                                         rcnum );
159                                 }
160                         } else {
161                                 if ( opt_debug )
162                                         printf( "<null record>\n" );
163                         }
164                         ZERO_STRUCT( ee );      /* MUST initialize between records */
165                 }
166         }
167
168         tdb_close( elog_tdb );
169
170         return 0;
171 }