forked from allinurl/goaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.c
1234 lines (1072 loc) · 30.1 KB
/
output.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
/**
* output.c -- output to the standard output stream
* ______ ___
* / ____/___ / | _____________ __________
* / / __/ __ \/ /| |/ ___/ ___/ _ \/ ___/ ___/
* / /_/ / /_/ / ___ / /__/ /__/ __(__ |__ )
* \____/\____/_/ |_\___/\___/\___/____/____/
*
* The MIT License (MIT)
* Copyright (c) 2009-2016 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.
*/
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64
#include <errno.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "output.h"
#ifdef HAVE_LIBTOKYOCABINET
#include "tcabdb.h"
#else
#include "gkhash.h"
#endif
#include "error.h"
#include "gwsocket.h"
#include "json.h"
#include "settings.h"
#include "ui.h"
#include "util.h"
#include "xmalloc.h"
#include "tpls.h"
#include "bootstrapcss.h"
#include "facss.h"
#include "appcss.h"
#include "d3js.h"
#include "hoganjs.h"
#include "chartsjs.h"
#include "appjs.h"
static void hits_bw_plot (FILE * fp, GHTMLPlot plot, int sp);
static void hits_bw_req_plot (FILE * fp, GHTMLPlot plot, int sp);
static void hits_visitors_plot (FILE * fp, GHTMLPlot plot, int sp);
static void hits_visitors_req_plot (FILE * fp, GHTMLPlot plot, int sp);
static void print_metrics (FILE * fp, const GHTML * def, int sp);
static void print_host_metrics (FILE * fp, const GHTML * def, int sp);
/* *INDENT-OFF* */
static GHTML htmldef[] = {
{VISITORS, 1, print_metrics, {
{CHART_AREASPLINE, hits_visitors_plot, 1, 1} ,
{CHART_AREASPLINE, hits_bw_plot, 1, 1} ,
}},
{REQUESTS, 1, print_metrics, {
{CHART_VBAR, hits_visitors_req_plot, 0, 0},
{CHART_VBAR, hits_bw_req_plot, 0, 0},
}},
{REQUESTS_STATIC, 1, print_metrics, {
{CHART_VBAR, hits_visitors_req_plot, 0, 0},
{CHART_VBAR, hits_bw_req_plot, 0, 0},
}},
{NOT_FOUND, 1, print_metrics, {
{CHART_VBAR, hits_visitors_req_plot, 0, 0},
{CHART_VBAR, hits_bw_req_plot, 0, 0},
}},
{HOSTS, 1, print_host_metrics, {
{CHART_VBAR, hits_visitors_plot, 0, 0},
{CHART_VBAR, hits_bw_plot, 0, 0},
}},
{OS, 1, print_metrics, {
{CHART_VBAR, hits_visitors_plot, 0, 1},
{CHART_VBAR, hits_bw_plot, 0, 1},
}},
{BROWSERS, 1, print_metrics, {
{CHART_VBAR, hits_visitors_plot, 0, 1},
{CHART_VBAR, hits_bw_plot, 0, 1},
}},
{VISIT_TIMES, 1, print_metrics, {
{CHART_AREASPLINE, hits_visitors_plot, 0, 1},
{CHART_AREASPLINE, hits_bw_plot, 0, 1},
}},
{VIRTUAL_HOSTS, 1, print_metrics, {
{CHART_VBAR, hits_visitors_plot, 0, 0},
{CHART_VBAR, hits_bw_plot, 0, 0},
}},
{REFERRERS, 1, print_metrics, {
{CHART_VBAR, hits_visitors_plot, 0, 0},
{CHART_VBAR, hits_bw_plot, 0, 0},
}},
{REFERRING_SITES, 1, print_metrics, {
{CHART_VBAR, hits_visitors_plot, 0, 0},
{CHART_VBAR, hits_bw_plot, 0, 0},
}},
{KEYPHRASES, 1, print_metrics, {
{CHART_VBAR, hits_visitors_plot, 0, 0},
{CHART_VBAR, hits_bw_plot, 0, 0},
}},
{STATUS_CODES, 1, print_metrics, {
{CHART_VBAR, hits_visitors_plot, 0, 1},
{CHART_VBAR, hits_bw_plot, 0, 1},
}},
{REMOTE_USER, 1, print_metrics, {
{CHART_VBAR, hits_visitors_plot, 0, 0},
{CHART_VBAR, hits_bw_plot, 0, 0},
}},
#ifdef HAVE_GEOLOCATION
{GEO_LOCATION, 1, print_metrics, {
{CHART_VBAR, hits_visitors_plot, 0, 1},
{CHART_VBAR, hits_bw_plot, 0, 1},
}},
#endif
};
/* *INDENT-ON* */
/* number of new lines (applicable fields) */
static int nlines = 0;
/* Get the chart type for the JSON definition.
*
* On success, the chart type string is returned. */
static const char *
chart2str (GChartType type)
{
static const char *strings[] = { "null", "bar", "area-spline" };
return strings[type];
}
/* Get panel output data for the given module.
*
* If not found, NULL is returned.
* On success, panel data is returned . */
static GHTML *
panel_lookup (GModule module)
{
int i, num_panels = ARRAY_SIZE (htmldef);
for (i = 0; i < num_panels; i++) {
if (htmldef[i].module == module)
return &htmldef[i];
}
return NULL;
}
/* Sanitize output with html entities for special chars */
static void
clean_output (FILE * fp, char *s)
{
if (!s) {
LOG_DEBUG (("NULL data on clean_output.\n"));
return;
}
while (*s) {
switch (*s) {
case '\'':
fprintf (fp, "'");
break;
case '"':
fprintf (fp, """);
break;
case '&':
fprintf (fp, "&");
break;
case '<':
fprintf (fp, "<");
break;
case '>':
fprintf (fp, ">");
break;
case ' ':
fprintf (fp, " ");
break;
default:
fputc (*s, fp);
break;
}
s++;
}
}
/* Set the HTML document title and the generated date/time */
static void
print_html_title (FILE * fp)
{
const char *title =
conf.html_report_title ? conf.html_report_title : HTML_REPORT_TITLE;
fprintf (fp, "<title>");
clean_output (fp, (char *) title);
fprintf (fp, "</title>");
}
/* *INDENT-OFF* */
/* Output all the document head elements. */
static void
print_html_header (FILE * fp)
{
fprintf (fp,
"<!DOCTYPE html>"
"<html>"
"<head>"
"<meta charset='UTF-8' />"
"<meta http-equiv='X-UA-Compatible' content='IE=edge'>"
"<meta name='viewport' content='width=device-width, initial-scale=1'>"
"<meta name='robots' content='noindex, nofollow' />");
print_html_title (fp);
fprintf (fp, "<style>%s</style>", fa_css);
fprintf (fp, "<style>%s</style>", bootstrap_css);
fprintf (fp, "<style>%s</style>", app_css);
/* load custom CSS file, if any */
if (conf.html_custom_css)
fprintf (fp, "<link rel='stylesheet' href='%s'>", conf.html_custom_css);
fprintf (fp,
"</head>"
"<body>");
}
/* Output all structural elements of the HTML document. */
static void
print_html_body (FILE * fp, const char *now)
{
fprintf (fp,
"<nav class='hidden-xs hidden-sm hide'>"
"</nav>"
"<i class='spinner fa fa-circle-o-notch fa-spin fa-3x fa-fw'></i>"
"<div class='container hide'>"
"<div class='wrap-header'>"
"<div class='row row-offcanvas row-offcanvas-right'>"
"<div class='col-md-12'>"
"<div class='page-header clearfix'>"
"<div class='pull-right'>"
"<h4>"
"<span class='label label-info' style='display:%s'>"
"<span class='hidden-xs'>%s: </span>"
"<span class='last-updated'>%s</span>"
"</span>"
"</h4>"
"</div>"
"<h1>"
"<span class='hidden-xs hidden-sm'>"
"<i class='fa fa-tachometer'></i> %s"
"</span>"
"<span class='visible-xs visible-sm'>"
"<i class='fa fa-bars nav-minibars'></i>"
"<i class='fa fa-circle nav-ws-status mini'></i>"
"</span>"
"</h1>", conf.no_html_last_updated ? "none" : "block", INFO_LAST_UPDATED, now, T_DASH);
fprintf (fp,
"<div class='report-title'>%s</div>"
"</div>"
"<div class='wrap-general'></div>"
"</div>"
"</div>"
"</div>"
"<div class='wrap-panels'></div>"
"</div>", conf.html_report_title ? conf.html_report_title : "");
fprintf (fp, "%s", tpls);
}
/* Output all the document footer elements such as script and closing
* tags. */
static void
print_html_footer (FILE * fp)
{
fprintf (fp, "<script>%s</script>", d3_js);
fprintf (fp, "<script>%s</script>", hogan_js);
fprintf (fp, "<script>%s</script>", app_js);
fprintf (fp, "<script>%s</script>", charts_js);
/* load custom JS file, if any */
if (conf.html_custom_js)
fprintf (fp, "<script src='%s'></script>", conf.html_custom_js);
fprintf (fp, "</body>");
fprintf (fp, "</html>");
}
/* *INDENT-ON* */
static const GChartDef ChartDefStopper = { NULL, NULL };
/* Get the number of chart definitions per panel.
*
* The number of chart definitions is returned . */
static int
get_chartdef_cnt (GChart * chart)
{
GChartDef *def = chart->def;
while (memcmp (def, &ChartDefStopper, sizeof ChartDefStopper))
++def;
return def - chart->def;
}
/* Output the given JSON chart axis definition for the given panel. */
static void
print_d3_chart_def_axis (FILE * fp, GChart * chart, size_t cnt, int isp)
{
GChartDef *def = chart->def;
size_t j = 0;
for (j = 0; j < cnt; ++j) {
if (strchr (def[j].value, '[') != NULL)
fpskeyaval (fp, def[j].key, def[j].value, isp, j == cnt - 1);
else
fpskeysval (fp, def[j].key, def[j].value, isp, j == cnt - 1);
}
}
/* Output the given JSON chart definition for the given panel. */
static void
print_d3_chart_def (FILE * fp, GChart * chart, size_t n, int sp)
{
size_t i = 0, cnt = 0;
int isp = 0;
/* use tabs to prettify output */
if (conf.json_pretty_print)
isp = sp + 1;
for (i = 0; i < n; ++i) {
cnt = get_chartdef_cnt (chart + i);
fpopen_obj_attr (fp, chart[i].key, sp);
print_d3_chart_def_axis (fp, chart + i, cnt, isp);
fpclose_obj (fp, sp, (i == n - 1));
}
}
static void
print_plot_def (FILE * fp, const GHTMLPlot plot, GChart * chart, int n, int sp)
{
int isp = 0, iisp = 0;
/* use tabs to prettify output */
if (conf.json_pretty_print)
isp = sp + 1, iisp = sp + 2;
fpskeysval (fp, "className", plot.chart_key, isp, 0);
fpskeysval (fp, "label", plot.chart_lbl, isp, 0);
fpskeysval (fp, "chartType", chart2str (plot.chart_type), isp, 0);
fpskeyival (fp, "chartReverse", plot.chart_reverse, isp, 0);
fpskeyival (fp, "redrawOnExpand", plot.redraw_expand, isp, 0);
/* D3.js data */
fpopen_obj_attr (fp, "d3", isp);
/* print chart definitions */
print_d3_chart_def (fp, chart, n, iisp);
/* close D3 */
fpclose_obj (fp, isp, 1);
}
/* Output D3.js hits/visitors plot definitions. */
static void
hits_visitors_plot (FILE * fp, GHTMLPlot plot, int sp)
{
/* *INDENT-OFF* */
GChart def[] = {
{"y0", (GChartDef[]) {
{"key", "hits"}, {"label", MTRC_HITS_LBL}, ChartDefStopper
}},
{"y1", (GChartDef[]) {
{"key", "visitors"}, {"label", MTRC_VISITORS_LBL}, ChartDefStopper
}},
};
plot.chart_key = (char[]) {"hits-visitors"};
plot.chart_lbl = (char *) {HTML_PLOT_HITS_VIS};
/* *INDENT-ON* */
print_plot_def (fp, plot, def, ARRAY_SIZE (def), sp);
}
/* Output D3.js hits/visitors plot definitions. */
static void
hits_visitors_req_plot (FILE * fp, GHTMLPlot plot, int sp)
{
/* *INDENT-OFF* */
GChart def[] = {
{"x", (GChartDef[]) {
{"key", "[\"method\", \"data\", \"protocol\"]"}, ChartDefStopper
}},
{"y0", (GChartDef[]) {
{"key", "hits"}, {"label", MTRC_HITS_LBL}, ChartDefStopper
}},
{"y1", (GChartDef[]) {
{"key", "visitors"}, {"label", MTRC_VISITORS_LBL}, ChartDefStopper
}},
};
plot.chart_key = (char[]) {"hits-visitors"};
plot.chart_lbl = (char *) {HTML_PLOT_HITS_VIS};
/* *INDENT-ON* */
print_plot_def (fp, plot, def, ARRAY_SIZE (def), sp);
}
/* Output C3.js bandwidth plot definitions. */
static void
hits_bw_plot (FILE * fp, GHTMLPlot plot, int sp)
{
/* *INDENT-OFF* */
GChart def[] = {
{"y0", (GChartDef[]) {
{"key", "bytes"}, {"label", MTRC_BW_LBL}, {"format", "bytes"}, ChartDefStopper
}},
};
if (!conf.bandwidth)
return;
plot.chart_key = (char[]) {"bandwidth"};
plot.chart_lbl = (char *) {MTRC_BW_LBL};
/* *INDENT-ON* */
print_plot_def (fp, plot, def, ARRAY_SIZE (def), sp);
}
/* Output C3.js bandwidth plot definitions. */
static void
hits_bw_req_plot (FILE * fp, GHTMLPlot plot, int sp)
{
/* *INDENT-OFF* */
GChart def[] = {
{"x", (GChartDef[]) {
{"key", "[\"method\", \"protocol\", \"data\"]"}, ChartDefStopper
}},
{"y0", (GChartDef[]) {
{"key", "bytes"}, {"label", MTRC_BW_LBL}, {"format", "bytes"}, ChartDefStopper
}},
};
if (!conf.bandwidth)
return;
plot.chart_key = (char[]) {"bandwidth"};
plot.chart_lbl = (char *) {MTRC_BW_LBL};
/* *INDENT-ON* */
print_plot_def (fp, plot, def, ARRAY_SIZE (def), sp);
}
/* Output JSON data definitions. */
static void
print_json_data (FILE * fp, GLog * glog, GHolder * holder)
{
char *json = NULL;
if ((json = get_json (glog, holder, 1)) == NULL)
return;
fprintf (fp, "<script type='text/javascript'>");
fprintf (fp, "var json_data=%s", json);
fprintf (fp, "</script>");
free (json);
}
/* Output WebSocket connection definition. */
static void
print_conn_def (FILE * fp)
{
int sp = 0;
/* use tabs to prettify output */
if (conf.json_pretty_print)
sp += 1;
if (!conf.real_time_html)
return;
fprintf (fp, "<script type='text/javascript'>");
fprintf (fp, "var connection = ");
fpopen_obj (fp, sp);
fpskeysval (fp, "url", (conf.ws_url ? conf.ws_url : ""), sp, 0);
fpskeyival (fp, "port", (conf.port ? atoi (conf.port) : 7890), sp, 1);
fpclose_obj (fp, sp, 1);
fprintf (fp, "</script>");
}
/* Output JSON per panel metric definitions. */
static void
print_def_metric (FILE * fp, const GDefMetric def, int sp)
{
int isp = 0;
/* use tabs to prettify output */
if (conf.json_pretty_print)
isp = sp + 1;
if (def.cname)
fpskeysval (fp, "className", def.cname, isp, 0);
if (def.cwidth)
fpskeysval (fp, "colWidth", def.cwidth, isp, 0);
if (def.metakey)
fpskeysval (fp, "meta", def.metakey, isp, 0);
if (def.metatype)
fpskeysval (fp, "metaType", def.metatype, isp, 0);
if (def.metalbl)
fpskeysval (fp, "metaLabel", def.metalbl, isp, 0);
if (def.datatype)
fpskeysval (fp, "dataType", def.datatype, isp, 0);
if (def.datakey)
fpskeysval (fp, "key", def.datakey, isp, 0);
if (def.lbl)
fpskeysval (fp, "label", def.lbl, isp, 1);
}
/* Output JSON metric definition block. */
static void
print_def_block (FILE * fp, const GDefMetric def, int sp, int last)
{
fpopen_obj (fp, sp);
print_def_metric (fp, def, sp);
fpclose_obj (fp, sp, last);
}
/* Output JSON overall requests definition block. */
static void
print_def_overall_requests (FILE * fp, int sp)
{
GDefMetric def = {
.lbl = T_REQUESTS,
.datatype = "numeric",
.cname = "black"
};
fpopen_obj_attr (fp, OVERALL_REQ, sp);
print_def_metric (fp, def, sp);
fpclose_obj (fp, sp, 0);
}
/* Output JSON overall valid requests definition block. */
static void
print_def_overall_valid_reqs (FILE * fp, int sp)
{
GDefMetric def = {
.lbl = T_VALID,
.datatype = "numeric",
.cname = "green"
};
fpopen_obj_attr (fp, OVERALL_VALID, sp);
print_def_metric (fp, def, sp);
fpclose_obj (fp, sp, 0);
}
/* Output JSON overall invalid requests definition block. */
static void
print_def_overall_invalid_reqs (FILE * fp, int sp)
{
GDefMetric def = {
.lbl = T_FAILED,
.datatype = "numeric",
.cname = "red"
};
fpopen_obj_attr (fp, OVERALL_FAILED, sp);
print_def_metric (fp, def, sp);
fpclose_obj (fp, sp, 0);
}
/* Output JSON process time definition block. */
static void
print_def_overall_processed_time (FILE * fp, int sp)
{
GDefMetric def = {
.lbl = T_GEN_TIME,
.datatype = "secs",
.cname = "gray"
};
fpopen_obj_attr (fp, OVERALL_GENTIME, sp);
print_def_metric (fp, def, sp);
fpclose_obj (fp, sp, 0);
}
/* Output JSON overall visitors definition block. */
static void
print_def_overall_visitors (FILE * fp, int sp)
{
GDefMetric def = {
.lbl = T_UNIQUE_VISITORS,
.datatype = "numeric",
.cname = "blue"
};
fpopen_obj_attr (fp, OVERALL_VISITORS, sp);
print_def_metric (fp, def, sp);
fpclose_obj (fp, sp, 0);
}
/* Output JSON overall files definition block. */
static void
print_def_overall_files (FILE * fp, int sp)
{
GDefMetric def = {
.lbl = T_UNIQUE_FILES,
.datatype = "numeric",
};
fpopen_obj_attr (fp, OVERALL_FILES, sp);
print_def_metric (fp, def, sp);
fpclose_obj (fp, sp, 0);
}
/* Output JSON overall excluded requests definition block. */
static void
print_def_overall_excluded (FILE * fp, int sp)
{
GDefMetric def = {
.lbl = T_EXCLUDE_IP,
.datatype = "numeric",
};
fpopen_obj_attr (fp, OVERALL_EXCL_HITS, sp);
print_def_metric (fp, def, sp);
fpclose_obj (fp, sp, 0);
}
/* Output JSON overall referrers definition block. */
static void
print_def_overall_refs (FILE * fp, int sp)
{
GDefMetric def = {
.lbl = T_REFERRER,
.datatype = "numeric",
};
fpopen_obj_attr (fp, OVERALL_REF, sp);
print_def_metric (fp, def, sp);
fpclose_obj (fp, sp, 0);
}
/* Output JSON overall not found definition block. */
static void
print_def_overall_notfound (FILE * fp, int sp)
{
GDefMetric def = {
.lbl = T_UNIQUE404,
.datatype = "numeric",
};
fpopen_obj_attr (fp, OVERALL_NOTFOUND, sp);
print_def_metric (fp, def, sp);
fpclose_obj (fp, sp, 0);
}
/* Output JSON overall static files definition block. */
static void
print_def_overall_static_files (FILE * fp, int sp)
{
GDefMetric def = {
.lbl = T_STATIC_FILES,
.datatype = "numeric",
};
fpopen_obj_attr (fp, OVERALL_STATIC, sp);
print_def_metric (fp, def, sp);
fpclose_obj (fp, sp, 0);
}
/* Output JSON log size definition block. */
static void
print_def_overall_log_size (FILE * fp, int sp)
{
GDefMetric def = {
.lbl = T_LOG,
.datatype = "bytes",
};
fpopen_obj_attr (fp, OVERALL_LOGSIZE, sp);
print_def_metric (fp, def, sp);
fpclose_obj (fp, sp, 0);
}
/* Output JSON overall bandwidth definition block. */
static void
print_def_overall_bandwidth (FILE * fp, int sp)
{
GDefMetric def = {
.lbl = T_BW,
.datatype = "bytes",
};
fpopen_obj_attr (fp, OVERALL_BANDWIDTH, sp);
print_def_metric (fp, def, sp);
fpclose_obj (fp, sp, 1);
}
/* Output JSON hits definition block. */
static void
print_def_hits (FILE * fp, int sp)
{
GDefMetric def = {
.datakey = "hits",
.lbl = MTRC_HITS_LBL,
.datatype = "numeric",
.metakey = "count",
.cwidth = "12%",
};
print_def_block (fp, def, sp, 0);
}
/* Output JSON visitors definition block. */
static void
print_def_visitors (FILE * fp, int sp)
{
GDefMetric def = {
.datakey = "visitors",
.lbl = MTRC_VISITORS_LBL,
.datatype = "numeric",
.metakey = "count",
.cwidth = "12%",
};
print_def_block (fp, def, sp, 0);
}
/* Output JSON bandwidth definition block. */
static void
print_def_bw (FILE * fp, int sp)
{
GDefMetric def = {
.datakey = "bytes",
.lbl = MTRC_BW_LBL,
.datatype = "bytes",
.metakey = "count",
.cwidth = "12%",
};
if (!conf.bandwidth)
return;
print_def_block (fp, def, sp, 0);
}
/* Output JSON Avg. T.S. definition block. */
static void
print_def_avgts (FILE * fp, int sp)
{
GDefMetric def = {
.datakey = "avgts",
.lbl = MTRC_AVGTS_LBL,
.datatype = "utime",
.metakey = "avg",
.cwidth = "8%",
};
if (!conf.serve_usecs)
return;
print_def_block (fp, def, sp, 0);
}
/* Output JSON Cum. T.S. definition block. */
static void
print_def_cumts (FILE * fp, int sp)
{
GDefMetric def = {
.datakey = "cumts",
.lbl = MTRC_CUMTS_LBL,
.datatype = "utime",
.metakey = "count",
.cwidth = "8%",
};
if (!conf.serve_usecs)
return;
print_def_block (fp, def, sp, 0);
}
/* Output JSON Max. T.S. definition block. */
static void
print_def_maxts (FILE * fp, int sp)
{
GDefMetric def = {
.datakey = "maxts",
.lbl = MTRC_MAXTS_LBL,
.datatype = "utime",
.metakey = "count",
.cwidth = "8%",
};
if (!conf.serve_usecs)
return;
print_def_block (fp, def, sp, 0);
}
/* Output JSON method definition block. */
static void
print_def_method (FILE * fp, int sp)
{
GDefMetric def = {
.datakey = "method",
.lbl = MTRC_METHODS_LBL,
.datatype = "string",
.cwidth = "6%",
};
if (!conf.append_method)
return;
print_def_block (fp, def, sp, 0);
}
/* Output JSON protocol definition block. */
static void
print_def_protocol (FILE * fp, int sp)
{
GDefMetric def = {
.datakey = "protocol",
.lbl = MTRC_PROTOCOLS_LBL,
.datatype = "string",
.cwidth = "7%",
};
if (!conf.append_protocol)
return;
print_def_block (fp, def, sp, 0);
}
/* Output JSON city definition block. */
static void
print_def_city (FILE * fp, int sp)
{
GDefMetric def = {
.datakey = "city",
.lbl = MTRC_CITY_LBL,
.datatype = "string",
};
if (!conf.has_geocity)
return;
print_def_block (fp, def, sp, 0);
}
/* Output JSON country definition block. */
static void
print_def_country (FILE * fp, int sp)
{
GDefMetric def = {
.datakey = "country",
.lbl = MTRC_COUNTRY_LBL,
.datatype = "string",
};
if (!conf.has_geocountry)
return;
print_def_block (fp, def, sp, 0);
}
/* Output JSON hostname definition block. */
static void
print_def_hostname (FILE * fp, int sp)
{
GDefMetric def = {
.datakey = "hostname",
.lbl = MTRC_HOSTNAME_LBL,
.datatype = "string",
.cname = "light",
};
if (!conf.enable_html_resolver)
return;
print_def_block (fp, def, sp, 0);
}
/* Output JSON data definition block. */
static void
print_def_data (FILE * fp, GModule module, int sp)
{
GDefMetric def = {
.cname = "trunc",
.cwidth = "100%",
.datakey = "data",
.datatype = module == VISITORS ? "date" : "string",
.lbl = MTRC_DATA_LBL,
.metakey = "unique",
.metalbl = "Total",
.metatype = "numeric",
};
print_def_block (fp, def, sp, 1);
}
/* Get the number of plots for the given panel definition.
*
* The number of plots for the given panel is returned. */
static int
count_plot_fp (const GHTML * def)
{
int i = 0;
for (i = 0; def->chart[i].plot != 0; ++i);
return i;
}
/* Entry function to output JSON plot definition block. */
static void
print_def_plot (FILE * fp, const GHTML * def, int sp)
{
int i, isp = 0, n = count_plot_fp (def);
/* use tabs to prettify output */
if (conf.json_pretty_print)
isp = sp + 1;
fpopen_arr_attr (fp, "plot", sp);
for (i = 0; i < n; ++i) {
fpopen_obj (fp, isp);
def->chart[i].plot (fp, def->chart[i], isp);
fpclose_obj (fp, isp, (i == n - 1));
}
fpclose_arr (fp, sp, 0);
}
/* Output JSON host panel definitions. */
static void
print_host_metrics (FILE * fp, const GHTML * def, int sp)
{
const GOutput *output = output_lookup (def->module);
print_def_hits (fp, sp);
print_def_visitors (fp, sp);
print_def_bw (fp, sp);
print_def_avgts (fp, sp);
print_def_cumts (fp, sp);
print_def_maxts (fp, sp);
if (output->method)
print_def_method (fp, sp);
if (output->protocol)
print_def_protocol (fp, sp);
print_def_city (fp, sp);
print_def_country (fp, sp);
print_def_hostname (fp, sp);
print_def_data (fp, def->module, sp);
}
/* Output JSON panel definitions. */
static void
print_metrics (FILE * fp, const GHTML * def, int sp)
{
const GOutput *output = output_lookup (def->module);
print_def_hits (fp, sp);
print_def_visitors (fp, sp);
print_def_bw (fp, sp);
print_def_avgts (fp, sp);
print_def_cumts (fp, sp);
print_def_maxts (fp, sp);
if (output->method)
print_def_method (fp, sp);
if (output->protocol)
print_def_protocol (fp, sp);
print_def_data (fp, def->module, sp);
}
/* Entry point to output JSON metric definitions. */
static void
print_def_metrics (FILE * fp, const GHTML * def, int sp)
{
int isp = 0;
/* use tabs to prettify output */
if (conf.json_pretty_print)
isp = sp + 1;
/* open data metric data */
fpopen_arr_attr (fp, "items", sp);
/* definition metrics */
def->metrics (fp, def, isp);
/* close metrics block */
fpclose_arr (fp, sp, 1);
}