diff options
Diffstat (limited to 'utility/shell.c')
-rw-r--r-- | utility/shell.c | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/utility/shell.c b/utility/shell.c new file mode 100644 index 0000000..07d9da1 --- /dev/null +++ b/utility/shell.c | |||
@@ -0,0 +1,148 @@ | |||
1 | #include "apr.h" | ||
2 | #include "apr_getopt.h" | ||
3 | #include "apr_tables.h" | ||
4 | |||
5 | #define APR_WANT_STDIO | ||
6 | #include "apr_want.h" | ||
7 | #include "stdlib.h" | ||
8 | |||
9 | #include "shell.h" | ||
10 | #include "config.h" | ||
11 | #include "logparse.h" | ||
12 | |||
13 | const apr_getopt_option_t _opt_config[] = { | ||
14 | {"machineid", 'm', 1, "Machine ID for the log file"}, | ||
15 | {"transaction", 't', 1, "Use a Transaction (yes,no)"}, | ||
16 | {"logformat", 'r', 1, "Use this logformat to parse files"}, | ||
17 | {"file", 'f', 1, "Parse this single log file (input dir is NOT scanned)"}, | ||
18 | {"inputdir", 'd', 1, "Input Directory to look for log files"}, | ||
19 | {"config", 'c', 1, "Configuration file to use (default mod_log_sql.conf)"}, | ||
20 | {"dryrun", 'n', 0, "Perform a dry run (do not actually alter the databse)"}, | ||
21 | {"loglevel", 'l', 1, "Log Level (deubg, warn, error)"}, | ||
22 | {"summary", 's', 1, "Summary (yes,no)"}, | ||
23 | {"help", 'h', 0, "Show Help"}, | ||
24 | {NULL} | ||
25 | }; | ||
26 | |||
27 | void show_help(const char *prog, const apr_getopt_option_t *opts, FILE *output) | ||
28 | { | ||
29 | int ptr = 0; | ||
30 | fprintf(output, "Usage: %s [OPTIONS] [files...]\n\n", prog); | ||
31 | while (opts[ptr].optch != 0) { | ||
32 | if (opts[ptr].optch > 255) { | ||
33 | if (opts[ptr].name) { | ||
34 | fprintf(output, " --%-10s", opts[ptr].name); | ||
35 | } else { | ||
36 | fprintf(output, " "); | ||
37 | } | ||
38 | } else { | ||
39 | if (opts[ptr].name) { | ||
40 | fprintf(output, " -%c --%-10s", opts[ptr].optch, opts[ptr].name); | ||
41 | } else { | ||
42 | fprintf(output, " -%c ", opts[ptr].optch); | ||
43 | } | ||
44 | } | ||
45 | if (opts[ptr].has_arg) { | ||
46 | fprintf(output, " (arg)"); | ||
47 | } else { | ||
48 | fprintf(output, " "); | ||
49 | } | ||
50 | fprintf(output, " %s\n", opts[ptr].description); | ||
51 | ptr++; | ||
52 | } | ||
53 | } | ||
54 | |||
55 | int main(int argc, const char *const argv[]) | ||
56 | { | ||
57 | apr_pool_t *pool; | ||
58 | apr_getopt_t *opts; | ||
59 | int opt; | ||
60 | const char *opt_arg; | ||
61 | apr_status_t rv; | ||
62 | apr_table_t *args; | ||
63 | config_t *base; | ||
64 | |||
65 | apr_app_initialize(&argc, &argv, NULL); | ||
66 | atexit(apr_terminate); | ||
67 | |||
68 | if (apr_pool_create(&pool, NULL) != APR_SUCCESS) { | ||
69 | fprintf(stderr, "Failed to create memory pool!\n"); | ||
70 | exit(1); | ||
71 | } | ||
72 | |||
73 | /** Iterate over command line arguments | ||
74 | * shoving args in a apr_table for processing later*/ | ||
75 | args = apr_table_make(pool, 5); | ||
76 | apr_table_setn(args, "config", "mod_log_sql.conf"); | ||
77 | apr_getopt_init(&opts, pool, argc, argv); | ||
78 | while ((rv = apr_getopt_long(opts, _opt_config, &opt, &opt_arg)) == APR_SUCCESS) { | ||
79 | switch (opt) { | ||
80 | case 'c': | ||
81 | apr_table_setn(args,"config",opt_arg); | ||
82 | break; | ||
83 | case 'd': | ||
84 | apr_table_setn(args,"inputdirectory",opt_arg); | ||
85 | break; | ||
86 | case 'f': | ||
87 | apr_table_setn(args,"inputfile",opt_arg); | ||
88 | break; | ||
89 | case 'h': | ||
90 | show_help(argv[0], _opt_config, stdout); | ||
91 | exit(1); | ||
92 | break; | ||
93 | case 'l': | ||
94 | apr_table_setn(args,"loglevel",opt_arg); | ||
95 | break; | ||
96 | case 'm': | ||
97 | apr_table_setn(args,"machineid",opt_arg); | ||
98 | break; | ||
99 | case 'n': | ||
100 | apr_table_setn(args,"dryrun","yes"); | ||
101 | break; | ||
102 | case 'r': | ||
103 | apr_table_setn(args,"logformat",opt_arg); | ||
104 | break; | ||
105 | case 's': | ||
106 | apr_table_setn(args,"summary",opt_arg); | ||
107 | break; | ||
108 | case 't': | ||
109 | apr_table_setn(args,"usetransactions",opt_arg); | ||
110 | break; | ||
111 | } | ||
112 | } | ||
113 | if (rv != APR_EOF) { | ||
114 | show_help(argv[0], _opt_config, stderr); | ||
115 | exit(1); | ||
116 | } | ||
117 | // Check if no extra args were passed | ||
118 | if (opts->ind != opts->argc) { | ||
119 | show_help(argv[0], _opt_config, stderr); | ||
120 | fprintf(stderr, "\n%s: Extra unknown arguments passed\n\n",argv[0]); | ||
121 | exit(1); | ||
122 | } | ||
123 | |||
124 | // Process configuration file | ||
125 | config_init(pool); | ||
126 | base = config_create(pool); | ||
127 | rv = config_read(base, apr_table_get(args,"Config"), args); | ||
128 | if (APR_STATUS_IS_ENOENT(rv)) { | ||
129 | fprintf(stderr,"Could not load configuration file: %s\n",apr_table_get(args,"config")); | ||
130 | } else if (rv) { | ||
131 | exit(1); | ||
132 | } | ||
133 | config_dump(base); | ||
134 | // Apply overrides from command line | ||
135 | find_log_files(base); | ||
136 | if (!apr_is_empty_array(base->input_files)) { | ||
137 | char **filelist; | ||
138 | int f, l; | ||
139 | filelist = (char **)base->input_files->elts; | ||
140 | for (f=0, l=base->input_files->nelts; f < l; f++) { | ||
141 | printf("Scanning %s\n",filelist[f]); | ||
142 | parse_logfile(base, filelist[f]); | ||
143 | } | ||
144 | } else { | ||
145 | printf("No input files\n"); | ||
146 | } | ||
147 | return 0; | ||
148 | } | ||