-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpush_log.c
More file actions
367 lines (294 loc) · 6.68 KB
/
push_log.c
File metadata and controls
367 lines (294 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include <errno.h>
#include <fcntl.h>
//#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/inotify.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include "daemon.h"
#include "log.h"
#include "parse_auditlog.h"
#include "parse_conf_file.h"
#include "rule.h"
#include "type.h"
#include "database.h"
#define READ_BUF_SIZE 8192
int process_audit_file(char *, char *);
int process_cc_file(char *, char *);
int send_size;
int send_wait;
//extern int efd;
struct datatype types[TYPE_MAX] = {
[AUDITLOG] = { .name = "auditlog",
.line_f = process_audit_file, },
[CC] = { .name = "cc",
.line_f = process_cc_file, },
};
static int process_lines(char *buffer, int n, struct inot_file *i)
{
char *lf, *line, *end;
int t;
line = buffer;
end = buffer + n;
while (1) {
if (line >= end) {
line = end;
break;
}
lf = memchr(line, '\n', end - line);
if (!lf)
break;
*lf = '\0';
//logg("%s\n", line);
t = i->etype;
if (t >= 0 && t < TYPE_MAX &&
types[t].line_f) {
types[t].line_f(line, lf);
}
line = lf + 1;
}
return (line - buffer);
}
int tail_file(struct inot_file *inot)
{
struct stat st;
int rt, fd;
static char buffer[READ_BUF_SIZE];
rt = stat(inot->path, &st);
if (rt < 0)
goto err;
if ((inot->mtype == APPEND && st.st_size < inot->offset) || (st.st_size == 0)) {
/* file truncated */
inot->offset = st.st_size;
goto err;
}
fd = open(inot->path, O_RDONLY);
if (fd < 0) {
logg("open %s [%d]\n", inot->path, errno);
goto err;
}
if ((lseek(fd, inot->offset, SEEK_SET) < 0)) {
logg("lseek %s %d [%d]\n", inot->path, inot->offset, errno);
goto err_after_fd;
}
while(1) {
size_t n, t;
n = read(fd, buffer, READ_BUF_SIZE);
if (n < 0) {
logg("read %s [%d]\n", inot->path, errno);
goto err_after_fd;
}
if (n == 0)
break;
t = process_lines(buffer, n, inot);
if (t == n) {
inot->offset += n;
} else if (t == 0) {
if (n < READ_BUF_SIZE) { /* half line in file*/
break;
} else { /* line too long */
inot->offset += n;
}
} else if (t < n) { /* half line in buffer */
inot->offset += t;
if ((lseek(fd, inot->offset, SEEK_SET) < 0)) {
logg("lseek %s %d [%d]\n", inot->path, inot->offset, errno);
goto err_after_fd;
}
}
if (inot->offset >= st.st_size)
break;
}
close(fd);
return 0;
err_after_fd:
close(fd);
err:
return -1;
}
int fill_mysql_buf(struct action *act, void *log)
{
struct chunk *chunk;
//struct push *push;
char *buf, *last, *end, *tablename, *str;
int i, n;
enum events_type type;
buf = act->push->mysql_buf.buf;
last = act->push->mysql_buf.last;
end = act->push->mysql_buf.end;
memcpy(&type, log, sizeof(enum events_type));
if (type == AUDITLOG) {
tablename = "alarms";
} else if (type == CC) {
tablename = "cc_alarms";
}else {
tablename = "";
}
n = sprintf(last, "INSERT INFO %s (", tablename);
last += n;
for (i = 0; i < act->num; i++) {
str = map[act->send_contents[i]].mcname;
n = sprintf(last, "%s%s", str, (i + 1 >= act->num) ? ")" : ",");
last += n;
}
n = sprintf(last, " VALUES (");
last += n;
for (i = 0; i < act->num; i++) {
chunk = map[act->send_contents[i]].fetch(log);
if (chunk->len == -1) {
n = sprintf(last, "%s%s", chunk->str, (i + 1 >= act->num) ? ")" : ",");
last += n;
} else {
n = snprintf(last, chunk->len + 1, "%s%s", chunk->str, (i + 1 >= act->num) ? ")" : ",");
last += n;
}
}
return 1;
}
void auditlog_traverse_push()
{
struct alarm_info *ainfo, *next;
struct push *push;
struct action *act;
int ret;
//struct chunk *entry;
list_for_each_entry_safe(ainfo, next, &auditlog_list, list) {
push = push_list;
while (push != NULL){
act = push->act_list;
while (act != NULL) {
if ((ret = traverse_rule_tree(ainfo, act->rule)) == 1) {
if (act->s_type == S_MYSQL) {
fill_mysql_buf(act, ainfo);
} else if (act->s_type == SYSLOG) {
}
}
act = act->next;
}
push = push->next;
}
ainfo->sinfo->count--;
if (ainfo->sinfo->count == 0)
free(ainfo->sinfo);
free(ainfo);
}
}
int process_cc_file(char *line, char *end)
{
return 1;
}
int process_audit_file(char *line, char *end)
{
char *fname = NULL;
/* find the audit file name from index file*/
while (line < end) {
if (*line == ' ' && *(line + 1) == '\\') {
fname = line + 1;
while(line < end) {
if (*line == ' ') {
*line = '\0';
break;
}
line++;
}
}
line++;
}
if (fname == NULL)
return 0;
parse_auditlog(fname);
auditlog_traverse_push();
return 1;
}
int main(int argc, char **argv)
{
const char * config_file;
char c;
int ifd;
struct inot_file *inot;
config_file = "/waf/config/misc/push_log.conf";
while ((c = getopt(argc, argv, "f:")) != -1) {
switch (c) {
case 'f': config_file = strdup(optarg); break;
default: usage();
}
}
argc -= optind;
argv += optind;
if (argc < 1)
usage();
if (strcmp(argv[0], "start") == 0) {
logg("<push_log start>\n");
start();
} else if (strcmp(argv[0], "stop") == 0) {
logg("<push_log stop>\n");
stop();
exit(0);
} else if (strcmp(argv[0], "restart") == 0) {
logg("<push_log restart>\n");
restart();
} else {
usage();
}
if (parse_config_file(config_file) == -1) {
logg("can`t open config file: %s\n", config_file);
exit(1);
}
ifd = inotify_init();
efd = epoll_create(10);
inot = inot_list;
while (inot) {
struct epoll_event ev;
inotify_add_watch(ifd, inot->path, IN_ALL_EVENTS);
ev.events = EPOLLIN|EPOLLOUT|EPOLLET;
ev.data.ptr = inot;
epoll_ctl(efd, EPOLL_CTL_ADD, ifd, &ev);
inot = inot->next;
}
while (1) {
int n;
int i;
enum events_type etype;
struct epoll_event events[10];
struct mysql_info *minfo;
//char *fname;
struct push *p;
n = epoll_wait(efd, events, 10, 0);
for (i = 0; i < n; i++) {
memcpy(&etype, events[i].data.ptr, sizeof(enum events_type));
switch (etype) {
case AUDITLOG:
/*inot = (struct inot_file *)events[i].data.ptr;
while (fname = parse_auditlog_index_file(inot->fd)) {
if (strlen(fname) != INDEX_FILENAME_LENGTH) {
logg("invalid auditlog file name %s:", fname);
continue;
}
deal_auditlog(fname);
}
break;*/
case CC:
inot = (struct inot_file *)events[i].data.ptr;
tail_file(inot);
break;
case E_MYSQL:
minfo = (struct mysql_info *)events[i].data.ptr;
deal_db_return(minfo);
break;
case TYPE_MAX:
break;
}
}
p = push_list;
while (p) {
if (p->mysql_buf.num > send_size || p->first + send_wait < time(NULL))
db_insert(p);
p = p->next;
}
}/* while (1) main loop*/
}