forked from allinurl/goaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgstorage.c
1520 lines (1324 loc) · 40.2 KB
/
gstorage.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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* gstorage.c -- common storage handling
* ______ ___
* / ____/___ / | _____________ __________
* / / __/ __ \/ /| |/ ___/ ___/ _ \/ ___/ ___/
* / /_/ / /_/ / ___ / /__/ /__/ __(__ |__ )
* \____/\____/_/ |_\___/\___/\___/____/____/
*
* The MIT License (MIT)
* Copyright (c) 2009-2020 Gerardo Orellana <hello @ goaccess.io>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#if !defined __SUNPRO_C
#include <stdint.h>
#endif
#include <stdlib.h>
#include <string.h>
#include "gstorage.h"
#ifdef HAVE_GEOLOCATION
#include "geoip1.h"
#endif
#include "browsers.h"
#include "commons.h"
#include "error.h"
#include "gkhash.h"
#include "opesys.h"
#include "ui.h"
#include "util.h"
#include "xmalloc.h"
/* private prototypes */
/* key/data generators for each module */
static int gen_visitor_key (GKeyData * kdata, GLogItem * logitem);
static int gen_404_key (GKeyData * kdata, GLogItem * logitem);
static int gen_browser_key (GKeyData * kdata, GLogItem * logitem);
static int gen_host_key (GKeyData * kdata, GLogItem * logitem);
static int gen_keyphrase_key (GKeyData * kdata, GLogItem * logitem);
static int gen_os_key (GKeyData * kdata, GLogItem * logitem);
static int gen_vhost_key (GKeyData * kdata, GLogItem * logitem);
static int gen_remote_user_key (GKeyData * kdata, GLogItem * logitem);
static int gen_cache_status_key (GKeyData * kdata, GLogItem * logitem);
static int gen_referer_key (GKeyData * kdata, GLogItem * logitem);
static int gen_ref_site_key (GKeyData * kdata, GLogItem * logitem);
static int gen_request_key (GKeyData * kdata, GLogItem * logitem);
static int gen_static_request_key (GKeyData * kdata, GLogItem * logitem);
static int gen_status_code_key (GKeyData * kdata, GLogItem * logitem);
static int gen_visit_time_key (GKeyData * kdata, GLogItem * logitem);
#ifdef HAVE_GEOLOCATION
static int gen_geolocation_key (GKeyData * kdata, GLogItem * logitem);
#endif
/* UMS */
static int gen_mime_type_key (GKeyData * kdata, GLogItem * logitem);
static int gen_tls_type_key (GKeyData * kdata, GLogItem * logitem);
/* insertion metric routines */
static void insert_data (GModule module, GKeyData * kdata);
static void insert_rootmap (GModule module, GKeyData * kdata);
static void insert_root (GModule module, GKeyData * kdata);
static void insert_hit (GModule module, GKeyData * kdata);
static void insert_visitor (GModule module, GKeyData * kdata);
static void insert_bw (GModule module, GKeyData * kdata, uint64_t size);
static void insert_cumts (GModule module, GKeyData * kdata, uint64_t ts);
static void insert_maxts (GModule module, GKeyData * kdata, uint64_t ts);
static void insert_method (GModule module, GKeyData * kdata, const char *data);
static void insert_protocol (GModule module, GKeyData * kdata, const char *data);
static void insert_agent (GModule module, GKeyData * kdata, uint32_t agent_nkey);
/* *INDENT-OFF* */
const httpmethods http_methods[] = {
{ "OPTIONS" , 7 } ,
{ "GET" , 3 } ,
{ "HEAD" , 4 } ,
{ "POST" , 4 } ,
{ "PUT" , 3 } ,
{ "DELETE" , 6 } ,
{ "TRACE" , 5 } ,
{ "CONNECT" , 7 } ,
{ "PATCH" , 5 } ,
/* WebDav */
{ "PROPFIND" , 8 } ,
{ "PROPPATCH" , 9 } ,
{ "MKCOL" , 5 } ,
{ "COPY" , 4 } ,
{ "MOVE" , 4 } ,
{ "LOCK" , 4 } ,
{ "UNLOCK" , 6 } ,
{ "VERSION-CONTROL" , 15 } ,
{ "REPORT" , 6 } ,
{ "CHECKOUT" , 8 } ,
{ "CHECKIN" , 7 } ,
{ "UNCHECKOUT" , 10 } ,
{ "MKWORKSPACE" , 11 } ,
{ "UPDATE" , 6 } ,
{ "LABEL" , 5 } ,
{ "MERGE" , 5 } ,
{ "BASELINE-CONTROL" , 16 } ,
{ "MKACTIVITY" , 10 } ,
{ "ORDERPATCH" , 10 } ,
};
size_t http_methods_len = ARRAY_SIZE (http_methods);
const httpprotocols http_protocols[] = {
{ "HTTP/1.0" , 8 } ,
{ "HTTP/1.1" , 8 } ,
{ "HTTP/2" , 6 } ,
{ "HTTP/3" , 6 } ,
};
size_t http_protocols_len = ARRAY_SIZE (http_protocols);
static GParse paneling[] = {
{
VISITORS,
gen_visitor_key,
insert_data,
NULL,
insert_hit,
insert_visitor,
insert_bw,
insert_cumts,
insert_maxts,
NULL,
NULL,
NULL,
}, {
REQUESTS,
gen_request_key,
insert_data,
NULL,
insert_hit,
insert_visitor,
insert_bw,
insert_cumts,
insert_maxts,
insert_method,
insert_protocol,
NULL,
}, {
REQUESTS_STATIC,
gen_static_request_key,
insert_data,
NULL,
insert_hit,
insert_visitor,
insert_bw,
insert_cumts,
insert_maxts,
insert_method,
insert_protocol,
NULL,
}, {
NOT_FOUND,
gen_404_key,
insert_data,
NULL,
insert_hit,
insert_visitor,
insert_bw,
insert_cumts,
insert_maxts,
insert_method,
insert_protocol,
NULL,
}, {
HOSTS,
gen_host_key,
insert_data,
NULL,
insert_hit,
insert_visitor,
insert_bw,
insert_cumts,
insert_maxts,
NULL,
NULL,
insert_agent,
}, {
OS,
gen_os_key,
insert_data,
insert_rootmap,
insert_hit,
insert_visitor,
insert_bw,
insert_cumts,
insert_maxts,
insert_method,
insert_protocol,
NULL,
}, {
BROWSERS,
gen_browser_key,
insert_data,
insert_rootmap,
insert_hit,
insert_visitor,
insert_bw,
insert_cumts,
insert_maxts,
NULL,
NULL,
NULL,
}, {
REFERRERS,
gen_referer_key,
insert_data,
NULL,
insert_hit,
insert_visitor,
insert_bw,
insert_cumts,
insert_maxts,
NULL,
NULL,
NULL,
}, {
REFERRING_SITES,
gen_ref_site_key,
insert_data,
NULL,
insert_hit,
insert_visitor,
insert_bw,
insert_cumts,
insert_maxts,
NULL,
NULL,
NULL,
}, {
KEYPHRASES,
gen_keyphrase_key,
insert_data,
NULL,
insert_hit,
insert_visitor,
insert_bw,
insert_cumts,
insert_maxts,
NULL,
NULL,
NULL,
},
#ifdef HAVE_GEOLOCATION
{
GEO_LOCATION,
gen_geolocation_key,
insert_data,
insert_rootmap,
insert_hit,
insert_visitor,
insert_bw,
insert_cumts,
insert_maxts,
NULL,
NULL,
NULL,
},
#endif
{
STATUS_CODES,
gen_status_code_key,
insert_data,
insert_rootmap,
insert_hit,
insert_visitor,
insert_bw,
insert_cumts,
insert_maxts,
NULL,
NULL,
NULL,
}, {
VISIT_TIMES,
gen_visit_time_key,
insert_data,
NULL,
insert_hit,
insert_visitor,
insert_bw,
insert_cumts,
insert_maxts,
NULL,
NULL,
NULL,
}, {
VIRTUAL_HOSTS,
gen_vhost_key,
insert_data,
NULL,
insert_hit,
insert_visitor,
insert_bw,
insert_cumts,
insert_maxts,
NULL,
NULL,
NULL,
}, {
REMOTE_USER,
gen_remote_user_key,
insert_data,
NULL,
insert_hit,
insert_visitor,
insert_bw,
insert_cumts,
insert_maxts,
NULL,
NULL,
NULL,
}, {
CACHE_STATUS,
gen_cache_status_key,
insert_data,
NULL,
insert_hit,
insert_visitor,
insert_bw,
insert_cumts,
insert_maxts,
NULL,
NULL,
NULL,
}, {
MIME_TYPE,
gen_mime_type_key,
insert_data,
insert_rootmap,
insert_hit,
insert_visitor,
insert_bw,
insert_cumts,
insert_maxts,
NULL, /*method*/
NULL, /*protocol*/
NULL, /*agent*/
}, {
TLS_TYPE,
gen_tls_type_key,
insert_data,
insert_rootmap,
insert_hit,
insert_visitor,
insert_bw,
insert_cumts,
insert_maxts,
NULL,
NULL,
NULL,
},
};
/* *INDENT-ON* */
/* Initialize a new GKeyData instance */
static void
new_modulekey (GKeyData * kdata) {
GKeyData key = {
.data = NULL,
.data_nkey = 0,
.root = NULL,
.dhash = 0,
.rhash = 0,
.root_nkey = 0,
.uniq_key = NULL,
.uniq_nkey = 0,
};
*kdata = key;
}
/* Get a panel from the GParse structure given a module.
*
* On error, or if not found, NULL is returned.
* On success, the panel value is returned. */
static GParse *
panel_lookup (GModule module) {
int i, num_panels = ARRAY_SIZE (paneling);
for (i = 0; i < num_panels; i++) {
if (paneling[i].module == module)
return &paneling[i];
}
return NULL;
}
/* Allocate memory for a new GMetrics instance.
*
* On success, the newly allocated GMetrics is returned . */
GMetrics *
new_gmetrics (void) {
GMetrics *metrics = xcalloc (1, sizeof (GMetrics));
return metrics;
}
/* Free memory of a GMetrics object */
void
free_gmetrics (GMetrics * metric) {
if (metric == NULL)
return;
free (metric->data);
free (metric->method);
free (metric->protocol);
free (metric);
}
/* Get the module string value given a metric enum value.
*
* On error, NULL is returned.
* On success, the string module value is returned. */
char *
get_mtr_str (GSMetric metric) {
/* String modules to enumerated modules */
GEnum enum_metrics[] = {
{"MTRC_KEYMAP", MTRC_KEYMAP},
{"MTRC_ROOTMAP", MTRC_ROOTMAP},
{"MTRC_DATAMAP", MTRC_DATAMAP},
{"MTRC_UNIQMAP", MTRC_UNIQMAP},
{"MTRC_ROOT", MTRC_ROOT},
{"MTRC_HITS", MTRC_HITS},
{"MTRC_VISITORS", MTRC_VISITORS},
{"MTRC_BW", MTRC_BW},
{"MTRC_CUMTS", MTRC_CUMTS},
{"MTRC_MAXTS", MTRC_MAXTS},
{"MTRC_METHODS", MTRC_METHODS},
{"MTRC_PROTOCOLS", MTRC_PROTOCOLS},
{"MTRC_AGENTS", MTRC_AGENTS},
{"MTRC_METADATA", MTRC_METADATA},
{"MTRC_UNIQUE_KEYS", MTRC_UNIQUE_KEYS},
{"MTRC_AGENT_KEYS", MTRC_AGENT_KEYS},
{"MTRC_AGENT_VALS", MTRC_AGENT_VALS},
{"MTRC_CNT_VALID", MTRC_CNT_VALID},
{"MTRC_CNT_BW", MTRC_CNT_BW},
};
return enum2str (enum_metrics, ARRAY_SIZE (enum_metrics), metric);
}
/* Allocate space off the heap to store a uint32_t.
*
* On success, the newly allocated pointer is returned . */
uint32_t *
i322ptr (uint32_t val) {
uint32_t *ptr = xmalloc (sizeof (uint32_t));
*ptr = val;
return ptr;
}
/* Allocate space off the heap to store a uint64_t.
*
* On success, the newly allocated pointer is returned . */
uint64_t *
uint642ptr (uint64_t val) {
uint64_t *ptr = xmalloc (sizeof (uint64_t));
*ptr = val;
return ptr;
}
/* Set the module totals to calculate percentages. */
void
set_module_totals (GPercTotals * totals) {
totals->bw = ht_sum_bw ();
totals->hits = ht_sum_valid ();
totals->visitors = ht_get_size_uniqmap (VISITORS);
}
/* Set numeric metrics for each request given raw data.
*
* On success, numeric metrics are set into the given structure. */
void
set_data_metrics (GMetrics * ometrics, GMetrics ** nmetrics, GPercTotals totals) {
GMetrics *metrics;
/* determine percentages for certain fields */
float hits_perc = get_percentage (totals.hits, ometrics->hits);
float visitors_perc = get_percentage (totals.visitors, ometrics->visitors);
float bw_perc = get_percentage (totals.bw, ometrics->bw.nbw);
metrics = new_gmetrics ();
/* basic fields */
metrics->id = ometrics->id;
metrics->hits = ometrics->hits;
metrics->visitors = ometrics->visitors;
/* percentage fields */
metrics->hits_perc = hits_perc < 0 ? 0 : hits_perc;
metrics->bw_perc = bw_perc < 0 ? 0 : bw_perc;
metrics->visitors_perc = visitors_perc < 0 ? 0 : visitors_perc;
/* bandwidth field */
metrics->bw.nbw = ometrics->bw.nbw;
/* time served fields */
if (conf.serve_usecs && ometrics->hits > 0) {
metrics->avgts.nts = ometrics->avgts.nts;
metrics->cumts.nts = ometrics->cumts.nts;
metrics->maxts.nts = ometrics->maxts.nts;
}
/* method field */
if (conf.append_method && ometrics->method)
metrics->method = ometrics->method;
/* protocol field */
if (conf.append_protocol && ometrics->protocol)
metrics->protocol = ometrics->protocol;
/* data field */
metrics->data = ometrics->data;
*nmetrics = metrics;
}
/* Increment the overall bandwidth. */
static void
count_bw (int numdate, uint64_t resp_size) {
ht_inc_cnt_bw (numdate, resp_size);
}
/* Keep track of all invalid log strings. */
static void
count_invalid (GLog * glog, const char *line) {
glog->invalid++;
ht_inc_cnt_overall ("failed_requests", 1);
if (conf.invalid_requests_log) {
LOG_INVALID (("%s", line));
}
if (glog->items->errstr && glog->log_erridx < MAX_LOG_ERRORS) {
glog->errors[glog->log_erridx++] = xstrdup (glog->items->errstr);
}
}
/* Count down the number of invalids hits.
* Note: Upon performing a log test, invalid hits are counted, since
* no valid records were found, then we count down by the number of
* tests ran.
*/
void
uncount_invalid (GLog * glog) {
if (glog->invalid > conf.num_tests)
glog->invalid -= conf.num_tests;
else
glog->invalid = 0;
}
/* Count down the number of processed hits.
* Note: Upon performing a log test, processed hits are counted, since
* no valid records were found, then we count down by the number of
* tests ran.
*/
void
uncount_processed (GLog * glog) {
lock_spinner ();
if (glog->processed > conf.num_tests)
glog->processed -= conf.num_tests;
else
glog->processed = 0;
unlock_spinner ();
}
/* Keep track of all valid log strings. */
static void
count_valid (int numdate) {
lock_spinner ();
ht_inc_cnt_valid (numdate, 1);
unlock_spinner ();
}
/* Keep track of all valid and processed log strings. */
void
count_process (GLog * glog) {
lock_spinner ();
glog->processed++;
ht_inc_cnt_overall ("total_requests", 1);
unlock_spinner ();
}
void
count_process_and_invalid (GLog * glog, const char *line) {
count_process (glog);
count_invalid (glog, line);
}
/* Keep track of all excluded log strings (IPs).
*
* If IP not range, 1 is returned.
* If IP is excluded, 0 is returned. */
int
excluded_ip (GLogItem * logitem) {
if (conf.ignore_ip_idx && ip_in_range (logitem->host)) {
ht_inc_cnt_overall ("excluded_ip", 1);
return 0;
}
return 1;
}
/* A wrapper function to insert a data keymap string key.
*
* If the given key exists, its value is returned.
* On error, 0 is returned.
* On success the value of the key inserted is returned */
static int
insert_dkeymap (GModule module, GKeyData * kdata) {
return ht_insert_keymap (module, kdata->numdate, kdata->dhash, &kdata->cdnkey);
}
/* A wrapper function to insert a root keymap string key.
*
* If the given key exists, its value is returned.
* On error, 0 is returned.
* On success the value of the key inserted is returned */
static int
insert_rkeymap (GModule module, GKeyData * kdata) {
return ht_insert_keymap (module, kdata->numdate, kdata->rhash, &kdata->crnkey);
}
/* A wrapper function to insert a datamap uint32_t key and string value. */
static void
insert_data (GModule module, GKeyData * kdata) {
ht_insert_datamap (module, kdata->numdate, kdata->data_nkey, kdata->data, kdata->cdnkey);
}
/* A wrapper function to insert a uniqmap string key.
*
* If the given key exists, 0 is returned.
* On error, 0 is returned.
* On success the value of the key inserted is returned */
static int
insert_uniqmap (GModule module, GKeyData * kdata, uint32_t uniq_nkey) {
return ht_insert_uniqmap (module, kdata->numdate, kdata->data_nkey, uniq_nkey);
}
/* A wrapper function to insert a rootmap uint32_t key from the keymap
* store mapped to its string value. */
static void
insert_rootmap (GModule module, GKeyData * kdata) {
ht_insert_rootmap (module, kdata->numdate, kdata->root_nkey, kdata->root, kdata->crnkey);
}
/* A wrapper function to insert a data uint32_t key mapped to the
* corresponding uint32_t root key. */
static void
insert_root (GModule module, GKeyData * kdata) {
ht_insert_root (module, kdata->numdate, kdata->data_nkey, kdata->root_nkey, kdata->cdnkey,
kdata->crnkey);
}
/* A wrapper function to increase hits counter from an uint32_t key. */
static void
insert_hit (GModule module, GKeyData * kdata) {
ht_insert_hits (module, kdata->numdate, kdata->data_nkey, 1, kdata->cdnkey);
ht_insert_meta_data (module, kdata->numdate, "hits", 1);
}
/* A wrapper function to increase visitors counter from an uint32_t
* key. */
static void
insert_visitor (GModule module, GKeyData * kdata) {
ht_insert_visitor (module, kdata->numdate, kdata->data_nkey, 1, kdata->cdnkey);
ht_insert_meta_data (module, kdata->numdate, "visitors", 1);
}
/* A wrapper function to increases bandwidth counter from an uint32_t
* key. */
static void
insert_bw (GModule module, GKeyData * kdata, uint64_t size) {
ht_insert_bw (module, kdata->numdate, kdata->data_nkey, size, kdata->cdnkey);
ht_insert_meta_data (module, kdata->numdate, "bytes", size);
}
/* A wrapper call to increases cumulative time served counter
* from an uint32_t key. */
static void
insert_cumts (GModule module, GKeyData * kdata, uint64_t ts) {
ht_insert_cumts (module, kdata->numdate, kdata->data_nkey, ts, kdata->cdnkey);
ht_insert_meta_data (module, kdata->numdate, "cumts", ts);
}
/* A wrapper call to insert the maximum time served counter from
* an uint32_t key. */
static void
insert_maxts (GModule module, GKeyData * kdata, uint64_t ts) {
ht_insert_maxts (module, kdata->numdate, kdata->data_nkey, ts, kdata->cdnkey);
ht_insert_meta_data (module, kdata->numdate, "maxts", ts);
}
static void
insert_method (GModule module, GKeyData * kdata, const char *data) {
ht_insert_method (module, kdata->numdate, kdata->data_nkey, data ? data : "---",
kdata->cdnkey);
}
/* A wrapper call to insert a method given an uint32_t key and string
* value. */
static void
insert_protocol (GModule module, GKeyData * kdata, const char *data) {
ht_insert_protocol (module, kdata->numdate, kdata->data_nkey, data ? data : "---",
kdata->cdnkey);
}
/* A wrapper call to insert an agent for a hostname given an uint32_t
* key and uint32_t value. */
static void
insert_agent (GModule module, GKeyData * kdata, uint32_t agent_nkey) {
ht_insert_agent (module, kdata->numdate, kdata->data_nkey, agent_nkey);
}
/* The following generates a unique key to identity unique requests.
* The key is made out of the actual request, and if available, the
* method and the protocol. Note that for readability, doing a simple
* snprintf/sprintf should suffice, however, memcpy is the fastest
* solution
*
* On success the new unique request key is returned */
static char *
gen_unique_req_key (GLogItem * logitem) {
char *key = NULL;
size_t s1 = 0, s2 = 0, s3 = 0, nul = 1, sep = 0;
/* nothing to do */
if (!conf.append_method && !conf.append_protocol)
return xstrdup (logitem->req);
/* still nothing to do */
if (!logitem->method && !logitem->protocol)
return xstrdup (logitem->req);
s1 = strlen (logitem->req);
if (logitem->method && conf.append_method) {
s2 = strlen (logitem->method);
nul++;
}
if (logitem->protocol && conf.append_protocol) {
s3 = strlen (logitem->protocol);
nul++;
}
/* includes terminating null */
key = xcalloc (s1 + s2 + s3 + nul, sizeof (char));
/* append request */
memcpy (key, logitem->req, s1);
if (logitem->method && conf.append_method) {
key[s1] = '|';
sep++;
memcpy (key + s1 + sep, logitem->method, s2 + 1);
}
if (logitem->protocol && conf.append_protocol) {
key[s1 + s2 + sep] = '|';
sep++;
memcpy (key + s1 + s2 + sep, logitem->protocol, s3 + 1);
}
return key;
}
/* Append the query string to the request, and therefore, it modifies
* the original logitem->req */
static void
append_query_string (char **req, const char *qstr) {
char *r;
size_t s1, s2, qm = 0;
s1 = strlen (*req);
s2 = strlen (qstr);
/* add '?' between the URL and the query string */
if (*qstr != '?')
qm = 1;
r = xmalloc (s1 + s2 + qm + 1);
memcpy (r, *req, s1);
if (qm)
r[s1] = '?';
memcpy (r + s1 + qm, qstr, s2 + 1);
free (*req);
*req = r;
}
/* A wrapper to assign the given data key and the data item to the key
* data structure */
static void
get_kdata (GKeyData * kdata, const char *data_key, const char *data) {
/* inserted in datamap */
kdata->data = data;
/* inserted in keymap */
kdata->dhash = djb2 ((unsigned char *) data_key);
}
/* A wrapper to assign the given data key and the data item to the key
* data structure */
static void
get_kroot (GKeyData * kdata, const char *root_key, const char *root) {
/* inserted in datamap */
kdata->root = root;
/* inserted in keymap */
kdata->rhash = djb2 ((unsigned char *) root_key);
}
/* Generate a visitor's key given the date specificity. For instance,
* if the specificity if set to hours, then a generated key would
* look like: 03/Jan/2016:09 */
static void
set_spec_visitor_key (char **fdate, const char *ftime) {
size_t dlen = 0, tlen = 0;
char *key = NULL, *tkey = NULL, *pch = NULL;
tkey = xstrdup (ftime);
if (conf.date_spec_hr && (pch = strchr (tkey, ':')) && (pch - tkey) > 0)
*pch = '\0';
dlen = strlen (*fdate);
tlen = strlen (tkey);
key = xmalloc (dlen + tlen + 1);
memcpy (key, *fdate, dlen);
memcpy (key + dlen, tkey, tlen + 1);
free (*fdate);
free (tkey);
*fdate = key;
}
/* Generate a unique key for the visitors panel from the given logitem
* structure and assign it to the output key data structure.
*
* On error, or if no date is found, 1 is returned.
* On success, the date key is assigned to our key data structure.
*/
static int
gen_visitor_key (GKeyData * kdata, GLogItem * logitem) {
if (!logitem->date || !logitem->time)
return 1;
/* Append time specificity to date */
if (conf.date_spec_hr)
set_spec_visitor_key (&logitem->date, logitem->time);
get_kdata (kdata, logitem->date, logitem->date);
kdata->numdate = logitem->numdate;
return 0;
}
/* Generate a unique key for the requests panel from the given logitem
* structure and assign it to out key data structure.
*
* On success, the generated request key is assigned to our key data
* structure.
*/
static int
gen_req_key (GKeyData * kdata, GLogItem * logitem) {
if (!logitem->req)
return 1;
if (logitem->qstr)
append_query_string (&logitem->req, logitem->qstr);
logitem->req_key = gen_unique_req_key (logitem);
get_kdata (kdata, logitem->req_key, logitem->req);
kdata->numdate = logitem->numdate;
return 0;
}
/* A wrapper to generate a unique key for the request panel.
*
* On error, or if the request is static or a 404, 1 is returned.
* On success, the generated request key is assigned to our key data
* structure.
*/
static int
gen_request_key (GKeyData * kdata, GLogItem * logitem) {
if (!logitem->req || logitem->is_404 || logitem->is_static)
return 1;
return gen_req_key (kdata, logitem);
}
/* A wrapper to generate a unique key for the request panel.
*
* On error, or if the request is not a 404, 1 is returned.
* On success, the generated request key is assigned to our key data
* structure. */
static int
gen_404_key (GKeyData * kdata, GLogItem * logitem) {
if (logitem->req && logitem->is_404)
return gen_req_key (kdata, logitem);
return 1;
}
/* A wrapper to generate a unique key for the request panel.
*
* On error, or if the request is not a static request, 1 is returned.
* On success, the generated request key is assigned to our key data
* structure. */
static int
gen_static_request_key (GKeyData * kdata, GLogItem * logitem) {
if (logitem->req && logitem->is_static)
return gen_req_key (kdata, logitem);
return 1;
}
/* A wrapper to generate a unique key for the virtual host panel.
*
* On error, 1 is returned.
* On success, the generated vhost key is assigned to our key data
* structure. */
static int
gen_vhost_key (GKeyData * kdata, GLogItem * logitem) {
if (!logitem->vhost)
return 1;
get_kdata (kdata, logitem->vhost, logitem->vhost);
kdata->numdate = logitem->numdate;
return 0;
}
/* A wrapper to generate a unique key for the virtual host panel.
*
* On error, 1 is returned.
* On success, the generated userid key is assigned to our key data
* structure. */
static int
gen_remote_user_key (GKeyData * kdata, GLogItem * logitem) {
if (!logitem->userid)
return 1;
get_kdata (kdata, logitem->userid, logitem->userid);
kdata->numdate = logitem->numdate;
return 0;
}
/* A wrapper to generate a unique key for the cache status panel.
*
* On error, 1 is returned.
* On success, the generated cache status key is assigned to our key data
* structure. */
static int
gen_cache_status_key (GKeyData * kdata, GLogItem * logitem) {
if (!logitem->cache_status)
return 1;
get_kdata (kdata, logitem->cache_status, logitem->cache_status);
kdata->numdate = logitem->numdate;
return 0;
}
/* A wrapper to generate a unique key for the hosts panel.
*
* On error, 1 is returned.
* On success, the generated host key is assigned to our key data
* structure. */
static int
gen_host_key (GKeyData * kdata, GLogItem * logitem) {
if (!logitem->host)
return 1;
get_kdata (kdata, logitem->host, logitem->host);
kdata->numdate = logitem->numdate;
return 0;
}
/* Generate a browser unique key for the browser's panel given a user
* agent and assign the browser type/category as a root element.
*
* On error, 1 is returned.
* On success, the generated browser key is assigned to our key data
* structure. */
static int