forked from allinurl/goaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv.c
311 lines (276 loc) · 8.7 KB
/
csv.c
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
/**
* output.c -- output csv to the standard output stream
* Copyright (C) 2009-2014 by Gerardo Orellana <[email protected]>
* GoAccess - An Ncurses apache weblog analyzer & interactive viewer
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* A copy of the GNU General Public License is attached to this
* source distribution for its full text.
*
* Visit http://goaccess.prosoftcorp.com for new releases.
*/
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <inttypes.h>
#include "csv.h"
#ifdef HAVE_LIBTOKYOCABINET
#include "tcabinet.h"
#else
#include "glibht.h"
#endif
#include "commons.h"
#include "error.h"
#include "ui.h"
#include "util.h"
#include "xmalloc.h"
static void
escape_cvs_output (FILE * fp, char *s)
{
while (*s) {
switch (*s) {
case '"':
fprintf (fp, "\"\"");
break;
default:
fputc (*s, fp);
break;
}
s++;
}
}
static void
print_csv_sub_items (FILE * fp, GSubList * sub_list, int process,
const char *id, int *idx)
{
char *data;
float percent;
GSubItem *iter;
int hits, i = 0;
for (iter = sub_list->head; iter; iter = iter->next) {
hits = iter->hits;
data = (char *) iter->data;
percent = get_percentage (process, hits);
percent = percent < 0 ? 0 : percent;
fprintf (fp, "\"%d\",", i); /* idx */
fprintf (fp, "\"%d\",", (*idx)); /* parent idx */
fprintf (fp, "\"%s\",", id);
fprintf (fp, "\"%d\",", hits);
fprintf (fp, "\"%4.2f%%\",", percent);
fprintf (fp, "\"");
escape_cvs_output (fp, data);
fprintf (fp, "\",");
fprintf (fp, "\r\n"); /* parent idx */
i++;
}
}
/**
* Generate CSV on partial fields for the following modules:
* OS, BROWSERS, REFERRERS, REFERRING_SITES, KEYPHRASES, STATUS_CODES
*/
static void
print_csv_generic (FILE * fp, const GHolder * h, int process)
{
char *data;
const char *id = NULL;
float percent;
int i, idx, hits;
if (h->module == BROWSERS)
id = BROWS_ID;
else if (h->module == OS)
id = OPERA_ID;
else if (h->module == REFERRERS)
id = REFER_ID;
else if (h->module == REFERRING_SITES)
id = SITES_ID;
else if (h->module == KEYPHRASES)
id = KEYPH_ID;
else if (h->module == STATUS_CODES)
id = CODES_ID;
#ifdef HAVE_LIBGEOIP
else if (h->module == GEO_LOCATION)
id = GEOLO_ID;
#endif
for (i = 0, idx = 0; i < h->idx; i++, idx++) {
hits = h->items[i].hits;
data = h->items[i].data;
percent = get_percentage (process, hits);
percent = percent < 0 ? 0 : percent;
fprintf (fp, "\"%d\",", idx); /* idx */
fprintf (fp, ","); /* parent idx */
fprintf (fp, "\"%s\",", id);
fprintf (fp, "\"%d\",", hits);
fprintf (fp, "\"%4.2f%%\",", percent);
fprintf (fp, "\"");
escape_cvs_output (fp, data);
fprintf (fp, "\",");
fprintf (fp, "\r\n"); /* parent idx */
if (h->module == OS || h->module == BROWSERS || h->module == STATUS_CODES
#ifdef HAVE_LIBGEOIP
|| h->module == GEO_LOCATION
#endif
)
print_csv_sub_items (fp, h->items[i].sub_list, process, id, &idx);
}
}
/**
* Generate CSV on complete fields for the following modules:
* REQUESTS, REQUESTS_STATIC, NOT_FOUND, HOSTS
*/
static void
print_csv_complete (FILE * fp, GHolder * holder, int process)
{
char *data, *method = NULL, *protocol = NULL;
const char *id = NULL;
float percent;
GHolder *h = holder;
int i, j, hits;
unsigned long long bw, usecs;
for (i = 0; i < 4; i++) {
switch (i) {
case 0:
h = holder + REQUESTS;
id = REQUE_ID;
break;
case 1:
h = holder + REQUESTS_STATIC;
id = STATI_ID;
break;
case 2:
h = holder + NOT_FOUND;
id = FOUND_ID;
break;
case 3:
h = holder + HOSTS;
id = HOSTS_ID;
break;
}
for (j = 0; j < h->idx; j++) {
hits = h->items[j].hits;
data = h->items[j].data;
percent = get_percentage (process, hits);
percent = percent < 0 ? 0 : percent;
bw = h->items[j].bw;
usecs = h->items[j].usecs;
method = h->items[j].method;
protocol = h->items[j].protocol;
fprintf (fp, "\"%d\",", j); /* idx */
fprintf (fp, ","); /* parent idx */
fprintf (fp, "\"%s\",", id);
fprintf (fp, "\"%d\",", hits);
fprintf (fp, "\"%4.2f%%\",", percent);
fprintf (fp, "\"");
escape_cvs_output (fp, data);
fprintf (fp, "\",");
fprintf (fp, "\"%lld\"", bw);
if (conf.serve_usecs)
fprintf (fp, ",\"%lld\"", usecs);
if (conf.append_protocol && protocol)
fprintf (fp, ",\"%s\"", protocol);
if (conf.append_method && method)
fprintf (fp, ",\"%s\"", method);
fprintf (fp, "\r\n");
}
}
}
/* generate CSV unique visitors stats */
static void
print_csv_visitors (FILE * fp, GHolder * h)
{
char *data, buf[DATE_LEN];
float percent;
int hits, bw, i, process = get_ht_size (ht_unique_visitors);
/* make compiler happy */
memset (buf, 0, sizeof (buf));
for (i = 0; i < h->idx; i++) {
hits = h->items[i].hits;
data = h->items[i].data;
percent = get_percentage (process, hits);
percent = percent < 0 ? 0 : percent;
bw = h->items[i].bw;
convert_date (buf, data, "%Y%m%d", "%d/%b/%Y", DATE_LEN);
fprintf (fp, "\"%d\",", i); /* idx */
fprintf (fp, ","); /* parent idx */
fprintf (fp, "\"%s\",", VISIT_ID);
fprintf (fp, "\"%d\",", hits);
fprintf (fp, "\"%4.2f%%\",", percent);
fprintf (fp, "\"%s\",", buf);
fprintf (fp, "\"%d\"\r\n", bw);
}
}
/* generate overview stats */
static void
print_csv_summary (FILE * fp, GLog * logger)
{
int i = 0;
off_t log_size = 0;
char now[DATE_TIME];
generate_time ();
strftime (now, DATE_TIME, "%Y-%m-%d %H:%M:%S", now_tm);
/* general statistics info */
fprintf (fp, "\"%d\",,\"%s\",\"date_time\",\"%s\"\r\n", i++, GENER_ID, now);
fprintf (fp, "\"%d\",,\"%s\",\"total_requests\",\"%d\"\r\n", i++, GENER_ID,
logger->process);
fprintf (fp, "\"%d\",,\"%s\",\"unique_visitors\",\"%d\"\r\n", i++, GENER_ID,
get_ht_size (ht_unique_visitors));
fprintf (fp, "\"%d\",,\"%s\",\"referrers\",\"%d\"\r\n", i++, GENER_ID,
get_ht_size (ht_referrers));
if (!logger->piping)
log_size = file_size (conf.ifile);
fprintf (fp, "\"%d\",,\"%s\",\"log_size\",\"%jd\"\r\n", i++, GENER_ID,
(intmax_t) log_size);
fprintf (fp, "\"%d\",,\"%s\",\"failed_requests\",\"%d\"\r\n", i++, GENER_ID,
logger->invalid);
fprintf (fp, "\"%d\",,\"%s\",\"unique_files\",\"%d\"\r\n", i++, GENER_ID,
get_ht_size (ht_requests));
fprintf (fp, "\"%d\",,\"%s\",\"unique_404\",\"%d\"\r\n", i++, GENER_ID,
get_ht_size (ht_not_found_requests));
fprintf (fp, "\"%d\",,\"%s\",\"bandwidth\",\"%lld\"\r\n", i++, GENER_ID,
logger->resp_size);
fprintf (fp, "\"%d\",,\"%s\",\"generation_time\",\"%llu\"\r\n", i++, GENER_ID,
(long long) end_proc - start_proc);
fprintf (fp, "\"%d\",,\"%s\",\"excluded_ip_hits\",\"%u\"\r\n", i++, GENER_ID,
logger->exclude_ip);
fprintf (fp, "\"%d\",,\"%s\",\"static_files\",\"%d\"\r\n", i++, GENER_ID,
get_ht_size (ht_requests_static));
if (conf.ifile == NULL)
conf.ifile = (char *) "STDIN";
fprintf (fp, "\"%d\",,\"%s\",\"log_file\",\"%s\"\r\n", i, GENER_ID,
conf.ifile);
}
/* entry point to generate a a csv report writing it to the fp */
void
output_csv (GLog * logger, GHolder * holder)
{
FILE *fp = stdout;
print_csv_summary (fp, logger);
print_csv_visitors (fp, holder + VISITORS);
print_csv_complete (fp, holder, logger->process);
print_csv_generic (fp, holder + OS, get_ht_size (ht_unique_visitors));
print_csv_generic (fp, holder + BROWSERS, get_ht_size (ht_unique_visitors));
print_csv_generic (fp, holder + REFERRERS, logger->process);
print_csv_generic (fp, holder + REFERRING_SITES, logger->process);
print_csv_generic (fp, holder + KEYPHRASES, logger->process);
#ifdef HAVE_LIBGEOIP
print_csv_generic (fp, holder + GEO_LOCATION,
get_ht_size (ht_unique_visitors));
#endif
print_csv_generic (fp, holder + STATUS_CODES, logger->process);
fclose (fp);
}