ultragrep 0.1.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,138 +0,0 @@
1
- #include <stdio.h>
2
- #include <stdlib.h>
3
- #include <getopt.h>
4
- #include <string.h>
5
- #include <time.h>
6
- #include "pcre.h"
7
- #include "req_matcher.h"
8
- #include "rails_req.h"
9
- #include "work_req.h"
10
-
11
-
12
- typedef struct {
13
- time_t start_time;
14
- time_t end_time;
15
- int num_regexps;
16
- pcre **regexps;
17
- req_matcher_t* m;
18
- }context_t;
19
-
20
-
21
- int check_request(int lines, char **request, time_t request_time, pcre **regexps, int num_regexps)
22
- {
23
- int *matches, i, j, matched;
24
-
25
- matches = malloc(sizeof(int) * num_regexps);
26
- memset(matches, 0, (sizeof(int) * num_regexps));
27
-
28
- for(i=0; i < lines; i++) {
29
- for(j=0; j < num_regexps; j++) {
30
- int ovector[30];
31
- if ( matches[j] ) continue;
32
-
33
- matched = pcre_exec(regexps[j], NULL, request[i], strlen(request[i]), 0, 0, ovector, 30);
34
- if ( matched > 0 )
35
- matches[j] = 1;
36
- }
37
- }
38
-
39
- matched = 1;
40
- for (j=0; j < num_regexps; j++) {
41
- matched &= matches[j];
42
- }
43
-
44
- free(matches);
45
- return(matched);
46
- }
47
-
48
- void print_request(int request_lines, char **request)
49
- {
50
- int i, j;
51
- putchar('\n');
52
-
53
- for(i=0; i < request_lines; i++)
54
- printf("%s", request[i]);
55
-
56
- for(j=0; j < strlen(request[request_lines - 1]) && j < 80; j++ )
57
- putchar('-');
58
-
59
- putchar('\n');
60
- fflush(stdout);
61
- }
62
-
63
- void handle_request(request_t* req, void* cxt_arg) {
64
- static int time = 0;
65
- context_t* cxt = (context_t*)cxt_arg;
66
- if( (req->time > cxt->start_time &&
67
- check_request(req->lines, req->buf, req->time, cxt->regexps, cxt->num_regexps))) {
68
- if(req->time != 0) {
69
- printf("@@%lu\n", req->time);
70
- }
71
-
72
- print_request(req->lines, req->buf);
73
- }
74
- if(req->time > time) {
75
- time = req->time;
76
- printf("@@%lu\n", time);
77
- }
78
- if(req->time > cxt->end_time) {
79
- cxt->m->stop(cxt->m);
80
- }
81
- }
82
- int main(int argc, char **argv)
83
- {
84
- int i;
85
- context_t *cxt;
86
- const char *error;
87
- int erroffset;
88
- char *line = NULL;
89
- ssize_t line_size, allocated;
90
-
91
-
92
- if ( argc < 5 ) {
93
- fprintf(stderr, "Usage: ug_guts (work|app) start_time end_time regexps [... regexps]\n");
94
- exit(1);
95
- }
96
-
97
- cxt = malloc(sizeof(context_t));
98
-
99
- if(strcmp(argv[1],"work") == 0)
100
- {
101
- cxt->m = work_req_matcher(&handle_request, NULL, cxt);
102
- }
103
- else if(strcmp(argv[1], "app") == 0)
104
- {
105
- cxt->m = rails_req_matcher(&handle_request, NULL, cxt);
106
- }
107
- else
108
- {
109
- fprintf(stderr, "Usage: ug_guts (work|app) start_time end_time regexps [... regexps]\n");
110
- exit(1);
111
- }
112
-
113
- cxt->start_time = atol(argv[2]);
114
- cxt->end_time = atol(argv[3]);
115
-
116
- cxt->num_regexps = argc - 4;
117
- cxt->regexps = malloc(sizeof(pcre *) * cxt->num_regexps);
118
-
119
- for ( i = 4; i < argc; i++) {
120
- cxt->regexps[i-4] = pcre_compile(argv[i], 0, &error, &erroffset, NULL);
121
- if ( error ) {
122
- fprintf(stderr, "Error compiling regexp \"%s\": %s\n", argv[i], error);
123
- exit;
124
- }
125
- }
126
-
127
-
128
- while(1) {
129
- int ret;
130
- line_size = getline(&line, &allocated, stdin);
131
- ret = cxt->m->process_line(cxt->m, line, line_size, 0);
132
- if(ret == EOF_REACHED || ret == STOP_SIGNAL) {
133
- break;
134
- }
135
- line = NULL;
136
- }
137
- }
138
-
@@ -1,83 +0,0 @@
1
- #include <stdlib.h>
2
- #include <string.h>
3
- #include <libgen.h>
4
- #include "ug_index.h"
5
-
6
- int ug_write_index(FILE *file, uint64_t time, uint64_t offset, char *data, uint32_t data_size)
7
- {
8
- fwrite(&time, 8, 1, file);
9
- fwrite(&offset, 8, 1, file);
10
- fwrite(&data_size, 4, 1, file);
11
-
12
- if ( data_size )
13
- fwrite(data, 1, data_size, file);
14
- }
15
-
16
- int ug_read_index_entry(FILE *file, struct ug_index *idx, int read_data)
17
- {
18
- int nread;
19
- nread = fread(&(idx->time), 8, 1, file);
20
- if ( !nread )
21
- return 0;
22
-
23
- nread = fread(&(idx->offset), 8, 1, file);
24
- nread = fread(&(idx->data_size), 4, 1, file);
25
- if ( idx->data_size ) {
26
- if ( read_data ) {
27
- idx->data = malloc(idx->data_size);
28
- nread = fread(idx->data, 1, idx->data_size, file);
29
- } else {
30
- fseek(file, idx->data_size, SEEK_CUR);
31
- }
32
- }
33
-
34
- return 1;
35
- }
36
-
37
- int ug_get_last_index_entry(FILE *file, struct ug_index *idx) {
38
- while (ug_read_index_entry(file, idx, 0));
39
- }
40
-
41
- void ug_seek_to_timestamp(FILE *flog, FILE *findex, uint64_t time, struct ug_index *param_idx)
42
- {
43
- struct ug_index idx, prev;
44
- off_t last_offset = 0;
45
-
46
- memset(&prev, 0, sizeof(struct ug_index));
47
-
48
- for(;;) {
49
- if ( !ug_read_index_entry(findex, &idx, 0) ) {
50
- memcpy(&prev, &idx, sizeof(struct ug_index));
51
- break;
52
- }
53
-
54
- if ( idx.time > time )
55
- break;
56
-
57
- memcpy(&prev, &idx, sizeof(struct ug_index));
58
- }
59
-
60
- if ( prev.offset ) {
61
- fseek(flog, prev.offset, SEEK_SET);
62
- if ( param_idx ) {
63
- memcpy(param_idx, &prev, sizeof(struct ug_index));
64
- }
65
- }
66
- }
67
-
68
- /* returns malloc'ed memory. */
69
- char *ug_get_index_fname(char *log_fname)
70
- {
71
- char *dir, *index_fname;
72
-
73
- dir = strdup(log_fname);
74
- dir = dirname(dir);
75
-
76
- index_fname = malloc(strlen(dir) + strlen(basename(log_fname)) + strlen("/..idx") + 1);
77
-
78
- sprintf(index_fname, "%s/.%s.idx", dir, basename(log_fname));
79
- free(dir);
80
- return index_fname;
81
- }
82
-
83
-
@@ -1,27 +0,0 @@
1
- #include <stdint.h>
2
- #include <stdio.h>
3
- #include "req_matcher.h"
4
-
5
- #define INDEX_EVERY 10
6
-
7
- struct ug_index {
8
- uint64_t time;
9
- uint64_t offset;
10
- uint32_t data_size;
11
- char *data;
12
- };
13
-
14
- typedef struct {
15
- time_t last_index_time;
16
- FILE *log;
17
- FILE *index;
18
- uint32_t data_size;
19
- req_matcher_t* m;
20
- unsigned char data[32768];
21
- } build_idx_context_t;
22
-
23
- int ug_write_index(FILE *file, uint64_t time, uint64_t offset, char *data, uint32_t data_size);
24
- int ug_get_last_index_entry(FILE *file, struct ug_index *idx);
25
- void ug_seek_to_timestamp(FILE *log, FILE *idx, uint64_t time, struct ug_index *param_idx);
26
- char *ug_get_index_fname(char *log_fname);
27
-
@@ -1,200 +0,0 @@
1
- #include <stdio.h>
2
- #include <string.h>
3
- #include <time.h>
4
- #include "pcre.h"
5
- #include "request.h"
6
- #include "req_matcher.h"
7
-
8
-
9
- typedef struct {
10
- req_matcher_t base;
11
-
12
- on_req on_request;
13
- on_err on_error;
14
- void* arg;
15
-
16
- request_t* curr_req;
17
- request_t* top;
18
-
19
- int depth; //debug
20
-
21
- int stop_requested;
22
- }work_req_matcher_t;
23
-
24
-
25
- static void on_request(work_req_matcher_t* m, request_t* r) {
26
- if(r) {
27
- if(r->lines > 0 && m->on_request) {
28
- m->on_request(r, m->arg);
29
- }
30
-
31
- //disconnect
32
- if(r->next) {
33
- r->next->prev = r->prev;
34
- }
35
- if(r->prev) {
36
- r->prev->next = r->next;
37
- } else {
38
- m->top = r->next;
39
- }
40
-
41
- free_request(r);
42
- m->depth--;
43
- }
44
- }
45
-
46
- static void on_all_requests(work_req_matcher_t* m) {
47
- request_t* r = m->top;
48
- while(r) {
49
- on_request(m, r);
50
- r = m->top;
51
- }
52
- }
53
-
54
- static void work_stop(req_matcher_t* base) {
55
- work_req_matcher_t* m = (work_req_matcher_t*)base;
56
- m->stop_requested = 1;
57
- }
58
-
59
-
60
- static char* extract_session(char* line, ssize_t line_size) {
61
- int matched = 0;
62
- int ovector[30];
63
- char *session_buf;
64
- const char* error;
65
- int erroffset;
66
- static pcre* regex = NULL;
67
- if(regex == NULL) {
68
- regex = pcre_compile("\"(\\w{6}:\\w{6})\"", 0, &error, &erroffset, NULL);
69
- }
70
- matched = pcre_exec(regex, NULL, line, line_size, 0, 0, ovector, 30);
71
- if(matched > 0) {
72
- pcre_get_substring(line, ovector, matched, 1, (const char **)&session_buf);
73
- return(session_buf);
74
- }
75
- return NULL;
76
- }
77
-
78
- static int parse_req_time(char* line, ssize_t line_size, time_t* time) {
79
- int matched = 0;
80
- int ovector[30];
81
- char *date_buf;
82
- struct tm request_tm;
83
- time_t tv;
84
- const char* error;
85
- int erroffset;
86
- static pcre* regex = NULL;
87
-
88
- if(regex == NULL) {
89
- regex = pcre_compile("\"(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2})\"", 0, &error, &erroffset, NULL);
90
- }
91
- matched = pcre_exec(regex, NULL, line, line_size,0,0,ovector, 30);
92
- if(matched > 0) {
93
- pcre_get_substring(line, ovector, matched, 1, (const char **)&date_buf);
94
- strptime(date_buf, "%Y-%m-%d %H:%M:%S", &request_tm);
95
- free(date_buf);
96
-
97
- *time = mktime(&request_tm);
98
- return(1);
99
- }
100
- return(-1);
101
- }
102
-
103
- static int detect_end(char* line, ssize_t line_size) {
104
- int matched = 0;
105
- int ovector[30];
106
- char *session_buf;
107
- const char* error;
108
- int erroffset;
109
- static pcre* regex = NULL;
110
- if(regex == NULL) {
111
- regex = pcre_compile("\"Finished this session\"", 0, &error, &erroffset, NULL);
112
- }
113
- matched = pcre_exec(regex, NULL, line, line_size,0,0,ovector, 30);
114
- return matched;
115
- }
116
-
117
- static int session_match(request_t* r, char* s) {
118
- if(strcmp(r->session, s) == 0) {
119
- return 1;
120
- }
121
- return 0;
122
- }
123
-
124
- static int work_process_line(req_matcher_t* base, char *line, ssize_t line_size, off_t offset)
125
- {
126
- work_req_matcher_t* m = (work_req_matcher_t*)base;
127
- char* session_str;
128
- int matched=0;
129
- request_t* r;
130
-
131
- if((m->stop_requested) || (line_size == -1)) {
132
- on_all_requests(m);
133
- return((m->stop_requested)?STOP_SIGNAL:EOF_REACHED);
134
- }
135
-
136
- session_str = extract_session(line, line_size);
137
-
138
- r = m->top;
139
- if(session_str != NULL) {
140
- if(r && r->next == NULL && r->session == NULL) {
141
- //The only req we have is sessionless
142
- on_request(m, r);
143
- r = NULL;
144
- //Finish and start afresh
145
- }
146
-
147
- //Find the correct req
148
- while(r && !session_match(r, session_str)){
149
- r = r->next;
150
- }
151
- }//else it goes on to the top
152
-
153
- if(!r){
154
- r = alloc_request();
155
- //This is now new top request
156
- if(m->top) {
157
- r->next = m->top;
158
- m->top->prev = r;
159
- }
160
- m->top = r;
161
- r->session = session_str;
162
-
163
- m->depth++;
164
- }else {
165
- free(session_str);
166
- }
167
-
168
- add_to_request(r, line, offset);
169
-
170
- if(r->time == 0) {
171
- parse_req_time(line, line_size, &(r->time));
172
- }
173
-
174
- if(r->session != NULL) {
175
- matched = detect_end(line, line_size);
176
- if(matched >0) {
177
- on_request(m, r);
178
- }
179
- }
180
-
181
- return(0);
182
- }
183
-
184
- req_matcher_t* work_req_matcher(on_req fn1, on_err fn2, void* arg)
185
- {
186
- work_req_matcher_t* m = (work_req_matcher_t*)malloc(sizeof(work_req_matcher_t));
187
- req_matcher_t* base = (req_matcher_t*)m;
188
-
189
- m->on_request = fn1;
190
- m->on_error = fn2;
191
- m->arg = arg;
192
-
193
- m->stop_requested = 0;
194
- m->curr_req = NULL;
195
-
196
- base->process_line = &work_process_line;
197
- base->stop = &work_stop;
198
-
199
- return base;
200
- }