forked from allinurl/goaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goaccess.c
1705 lines (1444 loc) · 43.2 KB
/
goaccess.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
/**
* goaccess.c -- main log analyzer
* ______ ___
* / ____/___ / | _____________ __________
* / / __/ __ \/ /| |/ ___/ ___/ _ \/ ___/ ___/
* / /_/ / /_/ / ___ / /__/ /__/ __(__ |__ )
* \____/\____/_/ |_\___/\___/\___/____/____/
*
* The MIT License (MIT)
* Copyright (c) 2009-2024 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 <assert.h>
#include <ctype.h>
#include <errno.h>
#include <locale.h>
#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <fcntl.h>
#include <grp.h>
#include <pthread.h>
#include <pwd.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <inttypes.h>
#include "gkhash.h"
#ifdef HAVE_GEOLOCATION
#include "geoip1.h"
#endif
#include "browsers.h"
#include "csv.h"
#include "error.h"
#include "gdashboard.h"
#include "gdns.h"
#include "gholder.h"
#include "goaccess.h"
#include "gwsocket.h"
#include "json.h"
#include "options.h"
#include "output.h"
#include "util.h"
#include "websocket.h"
#include "xmalloc.h"
GConf conf = {
.append_method = 1,
.append_protocol = 1,
.chunk_size = 1024,
.hl_header = 1,
.jobs = 1,
.num_tests = 10,
};
/* Loading/Spinner */
GSpinner *parsing_spinner;
/* active reverse dns flag */
int active_gdns = 0;
/* WebSocket server - writer and reader threads */
static GWSWriter *gwswriter;
static GWSReader *gwsreader;
/* Dashboard data structure */
static GDash *dash;
/* Data holder structure */
static GHolder *holder;
/* Old signal mask */
static sigset_t oldset;
/* Curses windows */
static WINDOW *header_win, *main_win;
static int main_win_height = 0;
/* *INDENT-OFF* */
static GScroll gscroll = {
{
{0, 0}, /* VISITORS { scroll, offset} */
{0, 0}, /* REQUESTS { scroll, offset} */
{0, 0}, /* REQUESTS_STATIC { scroll, offset} */
{0, 0}, /* NOT_FOUND { scroll, offset} */
{0, 0}, /* HOSTS { scroll, offset} */
{0, 0}, /* OS { scroll, offset} */
{0, 0}, /* BROWSERS { scroll, offset} */
{0, 0}, /* VISIT_TIMES { scroll, offset} */
{0, 0}, /* VIRTUAL_HOSTS { scroll, offset} */
{0, 0}, /* REFERRERS { scroll, offset} */
{0, 0}, /* REFERRING_SITES { scroll, offset} */
{0, 0}, /* KEYPHRASES { scroll, offset} */
{0, 0}, /* STATUS_CODES { scroll, offset} */
{0, 0}, /* REMOTE_USER { scroll, offset} */
{0, 0}, /* CACHE_STATUS { scroll, offset} */
#ifdef HAVE_GEOLOCATION
{0, 0}, /* GEO_LOCATION { scroll, offset} */
{0, 0}, /* ASN { scroll, offset} */
#endif
{0, 0}, /* MIME_TYPE { scroll, offset} */
{0, 0}, /* TLS_TYPE { scroll, offset} */
},
0, /* current module */
0, /* main dashboard scroll */
0, /* expanded flag */
};
/* *INDENT-ON* */
/* Free malloc'd holder */
static void
house_keeping_holder (void) {
/* REVERSE DNS THREAD */
pthread_mutex_lock (&gdns_thread.mutex);
/* kill dns pthread */
active_gdns = 0;
/* clear holder structure */
free_holder (&holder);
/* clear reverse dns queue */
gdns_free_queue ();
/* clear the whole storage */
free_storage ();
pthread_mutex_unlock (&gdns_thread.mutex);
}
/* Free malloc'd data across the whole program */
static void
house_keeping (void) {
house_keeping_holder ();
/* DASHBOARD */
if (dash && !conf.output_stdout) {
free_dashboard (dash);
reset_find ();
}
/* GEOLOCATION */
#ifdef HAVE_GEOLOCATION
geoip_free ();
#endif
/* INVALID REQUESTS */
if (conf.invalid_requests_log) {
LOG_DEBUG (("Closing invalid requests log.\n"));
invalid_log_close ();
}
/* UNKNOWNS */
if (conf.unknowns_log) {
LOG_DEBUG (("Closing unknowns log.\n"));
unknowns_log_close ();
}
/* CONFIGURATION */
free_formats ();
free_browsers_hash ();
if (conf.debug_log) {
LOG_DEBUG (("Bye.\n"));
dbg_log_close ();
}
if (conf.fifo_in)
free ((char *) conf.fifo_in);
if (conf.fifo_out)
free ((char *) conf.fifo_out);
/* clear spinner */
free (parsing_spinner);
/* free colors */
free_color_lists ();
/* free cmd arguments */
free_cmd_args ();
/* WebSocket writer */
free (gwswriter);
/* WebSocket reader */
free (gwsreader);
}
static void
cleanup (int ret) {
/* done, restore tty modes and reset terminal into
* non-visual mode */
if (!conf.output_stdout)
endwin ();
if (!conf.no_progress)
fprintf (stdout, "Cleaning up resources...\n");
/* unable to process valid data */
if (ret)
output_logerrors ();
house_keeping ();
}
/* Drop permissions to the user specified. */
static void
drop_permissions (void) {
struct passwd *pw;
errno = 0;
if ((pw = getpwnam (conf.username)) == NULL) {
if (errno == 0)
FATAL ("No such user %s", conf.username);
FATAL ("Unable to retrieve user %s: %s", conf.username, strerror (errno));
}
if (setgroups (1, &pw->pw_gid) == -1)
FATAL ("setgroups: %s", strerror (errno));
if (setgid (pw->pw_gid) == -1)
FATAL ("setgid: %s", strerror (errno));
if (setuid (pw->pw_uid) == -1)
FATAL ("setuid: %s", strerror (errno));
}
/* Open the pidfile whose name is specified in the given path and write
* the daemonized given pid. */
static void
write_pid_file (const char *path, pid_t pid) {
FILE *pidfile;
if (!path)
return;
if ((pidfile = fopen (path, "w"))) {
fprintf (pidfile, "%d", pid);
fclose (pidfile);
} else {
FATAL ("Unable to open the specified pid file. %s", strerror (errno));
}
}
/* Set GoAccess to run as a daemon */
static void
daemonize (void) {
pid_t pid, sid;
int fd;
/* Clone ourselves to make a child */
pid = fork ();
if (pid < 0)
exit (EXIT_FAILURE);
if (pid > 0) {
write_pid_file (conf.pidfile, pid);
printf ("Daemonized GoAccess: %d\n", pid);
exit (EXIT_SUCCESS);
}
umask (0);
/* attempt to create our own process group */
sid = setsid ();
if (sid < 0) {
LOG_DEBUG (("Unable to setsid: %s.\n", strerror (errno)));
exit (EXIT_FAILURE);
}
/* set the working directory to the root directory.
* requires the user to specify absolute paths */
if (chdir ("/") < 0) {
LOG_DEBUG (("Unable to set chdir: %s.\n", strerror (errno)));
exit (EXIT_FAILURE);
}
/* redirect fd's 0,1,2 to /dev/null */
/* Note that the user will need to use --debug-file for log output */
if ((fd = open ("/dev/null", O_RDWR, 0)) == -1) {
LOG_DEBUG (("Unable to open /dev/null: %s.\n", strerror (errno)));
exit (EXIT_FAILURE);
}
dup2 (fd, STDIN_FILENO);
dup2 (fd, STDOUT_FILENO);
dup2 (fd, STDERR_FILENO);
if (fd > STDERR_FILENO) {
close (fd);
}
}
/* Extract data from the given module hash structure and allocate +
* load data from the hash table into an instance of GHolder */
static void
allocate_holder_by_module (GModule module) {
GRawData *raw_data;
/* extract data from the corresponding hash table */
raw_data = parse_raw_data (module);
if (!raw_data) {
LOG_DEBUG (("raw data is NULL for module: %d.\n", module));
return;
}
load_holder_data (raw_data, holder + module, module, module_sort[module]);
}
/* Iterate over all modules/panels and extract data from hash
* structures and load it into an instance of GHolder */
static void
allocate_holder (void) {
size_t idx = 0;
holder = new_gholder (TOTAL_MODULES);
FOREACH_MODULE (idx, module_list) {
allocate_holder_by_module (module_list[idx]);
}
}
/* Extract data from the modules GHolder structure and load it into
* the terminal dashboard */
static void
allocate_data_by_module (GModule module, int col_data) {
int size = 0, max_choices = get_max_choices ();
dash->module[module].head = module_to_head (module);
dash->module[module].desc = module_to_desc (module);
size = holder[module].idx;
if (gscroll.expanded && module == gscroll.current) {
size = size > max_choices ? max_choices : holder[module].idx;
} else {
size = holder[module].idx > col_data ? col_data : holder[module].idx;
}
dash->module[module].alloc_data = size; /* data allocated */
dash->module[module].ht_size = holder[module].ht_size; /* hash table size */
dash->module[module].idx_data = 0;
dash->module[module].pos_y = 0;
if (gscroll.expanded && module == gscroll.current)
dash->module[module].dash_size = DASH_EXPANDED;
else
dash->module[module].dash_size = DASH_COLLAPSED;
dash->total_alloc += dash->module[module].dash_size;
pthread_mutex_lock (&gdns_thread.mutex);
load_data_to_dash (&holder[module], dash, module, &gscroll);
pthread_mutex_unlock (&gdns_thread.mutex);
}
/* Iterate over all modules/panels and extract data from GHolder
* structure and load it into the terminal dashboard */
static void
allocate_data (void) {
GModule module;
int col_data = get_num_collapsed_data_rows ();
size_t idx = 0;
dash = new_gdash ();
FOREACH_MODULE (idx, module_list) {
module = module_list[idx];
allocate_data_by_module (module, col_data);
}
}
static void
clean_stdscrn (void) {
int row, col;
getmaxyx (stdscr, row, col);
draw_header (stdscr, "", "%s", row - 1, 0, col, color_default);
}
/* A wrapper to render all windows within the dashboard. */
static void
render_screens (uint32_t offset) {
GColors *color = get_color (COLOR_DEFAULT);
int row, col;
char time_str_buf[32];
getmaxyx (stdscr, row, col);
term_size (main_win, &main_win_height);
generate_time ();
strftime (time_str_buf, sizeof (time_str_buf), "%d/%b/%Y:%T", &now_tm);
draw_header (stdscr, "", "%s", row - 1, 0, col, color_default);
wattron (stdscr, color->attr | COLOR_PAIR (color->pair->idx));
mvaddstr (row - 1, 1, T_HELP_ENTER);
mvprintw (row - 1, col / 2 - 10, "%" PRIu32 "/r - %s", offset, time_str_buf);
mvaddstr (row - 1, col - 6 - strlen (T_QUIT), T_QUIT);
mvprintw (row - 1, col - 5, "%s", GO_VERSION);
wattroff (stdscr, color->attr | COLOR_PAIR (color->pair->idx));
refresh ();
/* call general stats header */
display_general (header_win, holder);
wrefresh (header_win);
/* display active label based on current module */
update_active_module (header_win, gscroll.current);
display_content (main_win, dash, &gscroll);
}
/* Collapse the current expanded module */
static int
collapse_current_module (void) {
if (!gscroll.expanded)
return 1;
gscroll.expanded = 0;
reset_scroll_offsets (&gscroll);
free_dashboard (dash);
allocate_data ();
return 0;
}
/* Display message at the bottom of the terminal dashboard that panel
* is disabled */
static void
disabled_panel_msg (GModule module) {
const char *lbl = module_to_label (module);
int row, col;
getmaxyx (stdscr, row, col);
draw_header (stdscr, lbl, ERR_PANEL_DISABLED, row - 1, 0, col, color_error);
}
/* Set the current module/panel */
static int
set_module_to (GScroll *scrll, GModule module) {
if (get_module_index (module) == -1) {
disabled_panel_msg (module);
return 1;
}
/* scroll to panel */
if (!conf.no_tab_scroll)
gscroll.dash = get_module_index (module) * DASH_COLLAPSED;
/* reset expanded module */
collapse_current_module ();
scrll->current = module;
return 0;
}
/* Scroll expanded module or terminal dashboard to the top */
static void
scroll_to_first_line (void) {
if (!gscroll.expanded)
gscroll.dash = 0;
else {
gscroll.module[gscroll.current].scroll = 0;
gscroll.module[gscroll.current].offset = 0;
}
}
/* Scroll expanded module or terminal dashboard to the last row */
static void
scroll_to_last_line (void) {
int exp_size = get_num_expanded_data_rows ();
int scrll = 0, offset = 0;
if (!gscroll.expanded)
gscroll.dash = dash->total_alloc - main_win_height;
else {
scrll = dash->module[gscroll.current].idx_data - 1;
if (scrll >= exp_size && scrll >= offset + exp_size)
offset = scrll < exp_size - 1 ? 0 : scrll - exp_size + 1;
gscroll.module[gscroll.current].scroll = scrll;
gscroll.module[gscroll.current].offset = offset;
}
}
/* Load the user-agent window given the selected IP */
static void
load_ip_agent_list (void) {
int type_ip = 0;
/* make sure we have a valid IP */
int sel = gscroll.module[gscroll.current].scroll;
GDashData item = { 0 };
if (dash->module[HOSTS].holder_size == 0)
return;
item = dash->module[HOSTS].data[sel];
if (!invalid_ipaddr (item.metrics->data, &type_ip))
load_agent_list (main_win, item.metrics->data);
}
/* Expand the selected module */
static void
expand_current_module (void) {
if (gscroll.expanded && gscroll.current == HOSTS) {
load_ip_agent_list ();
return;
}
/* expanded, nothing to do... */
if (gscroll.expanded)
return;
reset_scroll_offsets (&gscroll);
gscroll.expanded = 1;
free_holder_by_module (&holder, gscroll.current);
free_dashboard (dash);
allocate_holder_by_module (gscroll.current);
allocate_data ();
}
/* Expand the clicked module/panel given the Y event coordinate. */
static int
expand_module_from_ypos (int y) {
/* ignore header/footer clicks */
if (y < MAX_HEIGHT_HEADER || y == LINES - 1)
return 1;
if (set_module_from_mouse_event (&gscroll, dash, y))
return 1;
reset_scroll_offsets (&gscroll);
gscroll.expanded = 1;
free_holder_by_module (&holder, gscroll.current);
free_dashboard (dash);
allocate_holder_by_module (gscroll.current);
allocate_data ();
return 0;
}
/* Expand the clicked module/panel */
static int
expand_on_mouse_click (void) {
int ok_mouse;
MEVENT event;
ok_mouse = getmouse (&event);
if (!conf.mouse_support || ok_mouse != OK)
return 1;
if (event.bstate & BUTTON1_CLICKED)
return expand_module_from_ypos (event.y);
return 1;
}
/* Scroll down expanded module to the last row */
static void
scroll_down_expanded_module (void) {
int exp_size = get_num_expanded_data_rows ();
int *scroll_ptr, *offset_ptr;
scroll_ptr = &gscroll.module[gscroll.current].scroll;
offset_ptr = &gscroll.module[gscroll.current].offset;
if (!gscroll.expanded)
return;
if (*scroll_ptr >= dash->module[gscroll.current].idx_data - 1)
return;
++(*scroll_ptr);
if (*scroll_ptr >= exp_size && *scroll_ptr >= *offset_ptr + exp_size)
++(*offset_ptr);
}
/* Scroll up expanded module */
static void
scroll_up_expanded_module (void) {
int *scroll_ptr, *offset_ptr;
scroll_ptr = &gscroll.module[gscroll.current].scroll;
offset_ptr = &gscroll.module[gscroll.current].offset;
if (!gscroll.expanded)
return;
if (*scroll_ptr <= 0)
return;
--(*scroll_ptr);
if (*scroll_ptr < *offset_ptr)
--(*offset_ptr);
}
/* Scroll up terminal dashboard */
static void
scroll_up_dashboard (void) {
gscroll.dash--;
}
/* Page up expanded module */
static void
page_up_module (void) {
int exp_size = get_num_expanded_data_rows ();
int *scroll_ptr, *offset_ptr;
scroll_ptr = &gscroll.module[gscroll.current].scroll;
offset_ptr = &gscroll.module[gscroll.current].offset;
if (!gscroll.expanded)
return;
/* decrease scroll and offset by exp_size */
*scroll_ptr -= exp_size;
if (*scroll_ptr < 0)
*scroll_ptr = 0;
if (*scroll_ptr < *offset_ptr)
*offset_ptr -= exp_size;
if (*offset_ptr <= 0)
*offset_ptr = 0;
}
/* Page down expanded module */
static void
page_down_module (void) {
int exp_size = get_num_expanded_data_rows ();
int *scroll_ptr, *offset_ptr;
scroll_ptr = &gscroll.module[gscroll.current].scroll;
offset_ptr = &gscroll.module[gscroll.current].offset;
if (!gscroll.expanded)
return;
*scroll_ptr += exp_size;
if (*scroll_ptr >= dash->module[gscroll.current].idx_data - 1)
*scroll_ptr = dash->module[gscroll.current].idx_data - 1;
if (*scroll_ptr >= exp_size && *scroll_ptr >= *offset_ptr + exp_size)
*offset_ptr += exp_size;
if (*offset_ptr + exp_size >= dash->module[gscroll.current].idx_data - 1)
*offset_ptr = dash->module[gscroll.current].idx_data - exp_size;
if (*scroll_ptr < exp_size - 1)
*offset_ptr = 0;
}
/* Create a new find dialog window and render it. Upon closing the
* window, dashboard is refreshed. */
static int
render_search_dialog (int search) {
if (render_find_dialog (main_win, &gscroll))
return 1;
pthread_mutex_lock (&gdns_thread.mutex);
search = perform_next_find (holder, &gscroll);
pthread_mutex_unlock (&gdns_thread.mutex);
if (search != 0)
return 1;
free_dashboard (dash);
allocate_data ();
return 0;
}
/* Search for the next occurrence within the dashboard structure */
static int
search_next_match (int search) {
pthread_mutex_lock (&gdns_thread.mutex);
search = perform_next_find (holder, &gscroll);
pthread_mutex_unlock (&gdns_thread.mutex);
if (search != 0)
return 1;
free_dashboard (dash);
allocate_data ();
return 0;
}
/* Update holder structure and dashboard screen */
static void
tail_term (void) {
pthread_mutex_lock (&gdns_thread.mutex);
free_holder (&holder);
pthread_cond_broadcast (&gdns_thread.not_empty);
pthread_mutex_unlock (&gdns_thread.mutex);
free_dashboard (dash);
allocate_holder ();
allocate_data ();
term_size (main_win, &main_win_height);
}
static void
tail_html (void) {
char *json = NULL;
pthread_mutex_lock (&gdns_thread.mutex);
free_holder (&holder);
pthread_cond_broadcast (&gdns_thread.not_empty);
pthread_mutex_unlock (&gdns_thread.mutex);
allocate_holder ();
pthread_mutex_lock (&gdns_thread.mutex);
json = get_json (holder, 1);
pthread_mutex_unlock (&gdns_thread.mutex);
if (json == NULL)
return;
pthread_mutex_lock (&gwswriter->mutex);
broadcast_holder (gwswriter->fd, json, strlen (json));
pthread_mutex_unlock (&gwswriter->mutex);
free (json);
}
/* Fast-forward latest JSON data when client connection is opened. */
static void
fast_forward_client (int listener) {
char *json = NULL;
pthread_mutex_lock (&gdns_thread.mutex);
json = get_json (holder, 1);
pthread_mutex_unlock (&gdns_thread.mutex);
if (json == NULL)
return;
pthread_mutex_lock (&gwswriter->mutex);
send_holder_to_client (gwswriter->fd, listener, json, strlen (json));
pthread_mutex_unlock (&gwswriter->mutex);
free (json);
}
/* Start reading data coming from the client side through the
* WebSocket server. */
void
read_client (void *ptr_data) {
GWSReader *reader = (GWSReader *) ptr_data;
/* check we have a fifo for reading */
if (reader->fd == -1)
return;
pthread_mutex_lock (&reader->mutex);
set_self_pipe (reader->self_pipe);
pthread_mutex_unlock (&reader->mutex);
while (1) {
/* poll(2) will block */
if (read_fifo (reader, fast_forward_client))
break;
}
close (reader->fd);
}
/* Parse tailed lines */
static void
parse_tail_follow (GLog *glog, FILE *fp) {
GLogItem *logitem = NULL;
#ifdef WITH_GETLINE
char *buf = NULL;
#else
char buf[LINE_BUFFER] = { 0 };
#endif
glog->bytes = 0;
#ifdef WITH_GETLINE
while ((buf = fgetline (fp)) != NULL) {
#else
while (fgets (buf, LINE_BUFFER, fp) != NULL) {
#endif
pthread_mutex_lock (&gdns_thread.mutex);
if ((parse_line (glog, buf, 0, &logitem)) == 0 && logitem != NULL)
process_log (logitem);
if (logitem != NULL) {
free_glog (logitem);
logitem = NULL;
}
pthread_mutex_unlock (&gdns_thread.mutex);
glog->bytes += strlen (buf);
#ifdef WITH_GETLINE
free (buf);
#endif
/* If the ingress rate is greater than MAX_BATCH_LINES,
* then we break and allow to re-render the UI */
if (++glog->read % MAX_BATCH_LINES == 0)
break;
}
}
static void
verify_inode (FILE *fp, GLog *glog) {
struct stat fdstat;
if (stat (glog->props.filename, &fdstat) == -1)
FATAL ("Unable to stat the specified log file '%s'. %s", glog->props.filename,
strerror (errno));
glog->props.size = fdstat.st_size;
/* Either the log got smaller, probably was truncated so start reading from 0
* and reset snippet.
* If the log changed its inode, more likely the log was rotated, so we set
* the initial snippet for the new log for future iterations */
if (fdstat.st_ino != glog->props.inode || glog->snippet[0] == '\0' || 0 == glog->props.size) {
glog->length = glog->bytes = 0;
set_initial_persisted_data (glog, fp, glog->props.filename);
}
glog->props.inode = fdstat.st_ino;
}
/* Process appended log data
*
* If nothing changed, 0 is returned.
* If log file changed, 1 is returned. */
static int
perform_tail_follow (GLog *glog) {
FILE *fp = NULL;
char buf[READ_BYTES + 1] = { 0 };
uint16_t len = 0;
uint64_t length = 0;
if (glog->props.filename[0] == '-' && glog->props.filename[1] == '\0') {
parse_tail_follow (glog, glog->pipe);
/* did we read something from the pipe? */
if (0 == glog->bytes)
return 0;
glog->length += glog->bytes;
goto out;
}
length = file_size (glog->props.filename);
/* file hasn't changed */
/* ###NOTE: This assumes the log file being read can be of smaller size, e.g.,
* rotated/truncated file or larger when data is appended */
if (length == glog->length)
return 0;
if (!(fp = fopen (glog->props.filename, "r")))
FATAL ("Unable to read the specified log file '%s'. %s", glog->props.filename,
strerror (errno));
verify_inode (fp, glog);
len = MIN (glog->snippetlen, length);
/* This is not ideal, but maybe the only reliable way to know if the
* current log looks different than our first read/parse */
if ((fread (buf, len, 1, fp)) != 1 && ferror (fp))
FATAL ("Unable to fread the specified log file '%s'", glog->props.filename);
/* For the case where the log got larger since the last iteration, we attempt
* to compare the first READ_BYTES against the READ_BYTES we had since the last
* parse. If it's different, then it means the file may got truncated but grew
* faster than the last iteration (odd, but possible), so we read from 0* */
if (glog->snippet[0] != '\0' && buf[0] != '\0' && memcmp (glog->snippet, buf, len) != 0)
glog->length = glog->bytes = 0;
if (!fseeko (fp, glog->length, SEEK_SET))
parse_tail_follow (glog, fp);
fclose (fp);
glog->length += glog->bytes;
/* insert the inode of the file parsed and the last line parsed */
if (glog->props.inode) {
glog->lp.line = glog->read;
glog->lp.size = glog->props.size;
ht_insert_last_parse (glog->props.inode, glog->lp);
}
out:
return 1;
}
/* Loop over and perform a follow for the given logs */
static void
tail_loop_html (Logs *logs) {
struct timespec refresh = {
.tv_sec = conf.html_refresh ? conf.html_refresh : HTML_REFRESH,
.tv_nsec = 0,
};
int i = 0, ret = 0;
while (1) {
if (conf.stop_processing)
break;
for (i = 0, ret = 0; i < logs->size; ++i)
ret |= perform_tail_follow (&logs->glog[i]); /* 0.2 secs */
if (1 == ret)
tail_html ();
if (nanosleep (&refresh, NULL) == -1 && errno != EINTR)
FATAL ("nanosleep: %s", strerror (errno));
}
}
/* Entry point to start processing the HTML output */
static void
process_html (Logs *logs, const char *filename) {
/* render report */
pthread_mutex_lock (&gdns_thread.mutex);
output_html (holder, filename);
pthread_mutex_unlock (&gdns_thread.mutex);
/* not real time? */
if (!conf.real_time_html)
return;
/* ignore loading from disk */
if (logs->load_from_disk_only)
return;
pthread_mutex_lock (&gwswriter->mutex);
gwswriter->fd = open_fifoin ();
pthread_mutex_unlock (&gwswriter->mutex);
/* open fifo for write */
if (gwswriter->fd == -1)
return;
set_ready_state ();
tail_loop_html (logs);
close (gwswriter->fd);
}
/* Iterate over available panels and advance the panel pointer. */
static int
next_module (void) {
int next = -1;
if ((next = get_next_module (gscroll.current)) == -1)
return 1;
gscroll.current = next;
if (!conf.no_tab_scroll)
gscroll.dash = get_module_index (gscroll.current) * DASH_COLLAPSED;
return 0;
}
/* Iterate over available panels and rewind the panel pointer. */
static int
previous_module (void) {
int prev = -1;
if ((prev = get_prev_module (gscroll.current)) == -1)
return 1;
gscroll.current = prev;
if (!conf.no_tab_scroll)
gscroll.dash = get_module_index (gscroll.current) * DASH_COLLAPSED;
return 0;
}
/* Perform several curses operations upon resizing the terminal. */
static void
window_resize (void) {
endwin ();
refresh ();
werase (header_win);
werase (main_win);
werase (stdscr);
term_size (main_win, &main_win_height);
refresh ();
}
/* Create a new sort dialog window and render it. Upon closing the
* window, dashboard is refreshed. */
static void
render_sort_dialog (void) {
load_sort_win (main_win, gscroll.current, &module_sort[gscroll.current]);
pthread_mutex_lock (&gdns_thread.mutex);
free_holder (&holder);
pthread_cond_broadcast (&gdns_thread.not_empty);
pthread_mutex_unlock (&gdns_thread.mutex);
free_dashboard (dash);
allocate_holder ();
allocate_data ();
}
static void