-
Notifications
You must be signed in to change notification settings - Fork 225
/
Copy pathreqlog.c
3354 lines (2976 loc) · 103 KB
/
reqlog.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
/*
Copyright 2015, 2022 Bloomberg Finance L.P.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
* Advanced/Smart/Too-clever-for-its-own-good logging module for comdb2.
*
* The aims here are:-
* - minimal impact on speed when logging is all turned off.
* - ability to log specific events (e.g. requests from certain sourcs,
* certain types of requests, requests that fail in certain ways etc)
* - ability to log to act.log or a file.
* - unified interface for sql and tagged requests.
* - free beer.
*
* To make this as fast as possible and accessible in deeply nested routines
* each thread has a request logging object associated with it whose memory
* is recycled between requests.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <bdb_api.h>
#include <alloca.h>
#include <str0.h>
#include <rtcpu.h>
#include <list.h>
#include <segstr.h>
#include <plhash_glue.h>
#include <memory_sync.h>
#include <epochlib.h>
#include <crc32c.h>
#include "comdb2.h"
#include "sqloffload.h"
#include "osqlblockproc.h"
#include "cdb2_constants.h"
#include "comdb2_atomic.h"
#include "intern_strings.h"
#include "util.h"
#include "tohex.h"
#include "logmsg.h"
#include "reqlog.h"
#include "comdb2uuid.h"
#include "strbuf.h"
#include "roll_file.h"
#include "eventlog.h"
#include "reqlog_int.h"
#include "sp.h"
#include <tohex.h>
#include "string_ref.h"
/* The normal case is for there to be no rules, just a long request threshold
* which takes some default action on long requests. If you want anything
* different then you add rules and you have to lock around the list. */
static int long_request_ms = 2000;
static struct output *long_request_out = NULL;
static pthread_mutex_t rules_mutex;
static LISTC_T(struct logrule) rules;
static LISTC_T(struct output) outputs;
static int reqlog_init_off = 0;
static struct output *default_out;
int diffstat_thresh = 60; /* every sixty seconds */
static struct output *stat_request_out = NULL;
/* These global lockless variables define what we will log for all requests
* just in case the request meets all the criteria to trigger a rule. */
static int master_event_mask = 0;
static int master_all_requests = 0;
static struct list master_opcode_list = {0};
static struct list master_opcode_inv_list = {0};
static int master_table_rules = 0;
static char master_stmts[NUMSTMTS][MAXSTMT + 1];
static int master_num_stmts = 0;
int reqltruncate = 0;
/* sometimes you have to debug the debugger */
static int verbose = 0;
/* stolen from sltdbt.c */
static int long_reqs = 0;
static int norm_reqs = 0;
/* for the sqldbgtrace message trap */
int sqldbgflag = 0;
/* Log long running requests this frequently (in seconds) */
int gbl_longreq_log_freq_sec = 60;
static void log_all_events(struct reqlogger *logger, struct output *out);
extern int is_stored_proc(struct sqlclntstate*);
inline void sltdbt_get_stats(int *n_reqs, int *l_reqs)
{
*n_reqs = norm_reqs;
*l_reqs = long_reqs;
norm_reqs = long_reqs = 0;
}
/* maintain a logging trace prefix with a stack structure */
static void prefix_init(struct prefix_type *p)
{
p->pos = 0;
p->stack_pos = 0;
p->prefix[0] = '\0';
}
static void prefix_push(struct prefix_type *p, const char *prefix, int len)
{
if (p->stack_pos < MAX_PREFIXES) {
p->stack[p->stack_pos] = p->pos;
if (len + p->pos >= sizeof(p->prefix)) {
len = (sizeof(p->prefix) - 1) - p->pos;
}
memcpy(p->prefix + p->pos, prefix, len);
p->pos += len;
p->prefix[p->pos] = '\0';
}
p->stack_pos++;
}
static void prefix_pop(struct prefix_type *p)
{
p->stack_pos--;
if (p->stack_pos < 0) {
p->stack_pos = 0;
p->pos = 0;
logmsg(LOGMSG_ERROR, "%s: stack pos went -ve!\n", __func__);
} else if (p->stack_pos < MAX_PREFIXES) {
p->pos = p->stack[p->stack_pos];
}
p->prefix[p->pos] = '\0';
}
static void prefix_pop_all(struct prefix_type *p)
{
p->stack_pos = 0;
p->pos = 0;
p->prefix[p->pos] = '\0';
}
/* This should be called under lock unless the output is the default
* output (act.log). */
static void flushdump(struct reqlogger *logger, struct output *out)
{
if (logger->dumplinepos > 0) {
struct iovec iov[5];
int niov = 0;
int append_duration = 0;
char durstr[16];
if (!out) {
out = default_out;
append_duration = 1;
}
if (out->use_time_prefix && out != default_out) {
int now = comdb2_time_epoch();
if (now != out->lasttime) {
time_t timet = (time_t)now;
struct tm tm;
out->lasttime = now;
localtime_r(&timet, &tm);
int rc = snprintf(out->timeprefix, sizeof(out->timeprefix),
"%02d/%02d %02d:%02d:%02d: ", tm.tm_mon + 1,
tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
if (rc >= sizeof(out->timeprefix)) {
snprintf(out->timeprefix, sizeof(out->timeprefix),
"truncated-time");
}
}
iov[niov].iov_base = out->timeprefix;
iov[niov].iov_len = sizeof(out->timeprefix) - 1;
niov++;
}
if (logger->prefix.pos > 0) {
iov[niov].iov_base = logger->prefix.prefix;
iov[niov].iov_len = logger->prefix.pos;
niov++;
}
iov[niov].iov_base = logger->dumpline;
iov[niov].iov_len = logger->dumplinepos;
niov++;
if (append_duration) {
iov[niov].iov_base = durstr;
iov[niov].iov_len =
snprintf(durstr, sizeof(durstr), " TIME +%d",
U2M(comdb2_time_epochus() - logger->startus));
niov++;
}
iov[niov].iov_base = "\n";
iov[niov].iov_len = 1;
niov++;
if (out == default_out) {
for (int i = 0; i < niov; i++)
logmsg(LOGMSG_USER, "%.*s", (int)iov[i].iov_len,
(char *)iov[i].iov_base);
} else {
ssize_t rc = writev(out->fd, iov, niov);
if (rc == -1) {
logmsg(LOGMSG_USER, "%s:%d writev returns rc=%zd\n", __FILE__,
__LINE__, rc);
}
}
logger->dumplinepos = 0;
}
}
static void dump(struct reqlogger *logger, struct output *out, const char *s,
int len)
{
int ii;
for (ii = 0; ii < len;) {
if (logger->dumplinepos >= sizeof(logger->dumpline)) {
flushdump(logger, out);
continue;
}
if (s[ii] == '\n') {
flushdump(logger, out);
} else {
logger->dumpline[(logger->dumplinepos)++] = s[ii];
}
ii++;
}
}
/* if we provide here an output bigger than 256, we used to print
the stack in the log file;
in this new attempt, we're providing a slow path in which we
malloc/free a buffer large enough to fit the size and preserve
the output string (and its termination)
*/
static void dumpf(struct reqlogger *logger, struct output *out, const char *fmt,
...)
{
va_list args;
char buf[256], *buf_slow = NULL;
int len;
va_start(args, fmt);
len = vsnprintf(buf, sizeof(buf), fmt, args);
if (len >= sizeof(buf)) {
buf_slow = malloc(len + 1); /* with the trailing 0 */
if (!buf_slow) {
/* no memory, or asking for too much;
i am gonna assume we are providing a new line */
buf[sizeof(buf) - 1] = '\n';
len = sizeof(buf);
} else {
len = vsnprintf(buf_slow, len + 1, fmt, args);
}
}
va_end(args);
dump(logger, out, (buf_slow) ? buf_slow : buf,
len); /* don't dump the terminating \0 */
if (buf_slow) free(buf_slow);
}
static void init_range(struct range *range)
{
range->from = -1;
range->to = -1;
}
static void init_dblrange(struct dblrange *dblrange)
{
dblrange->from = -1;
dblrange->to = -1;
}
static int add_list(struct list *list, int value, unsigned inv)
{
int ii;
inv = inv ? ~0 : 0;
if (inv != list->inv) {
list->num = 0;
list->inv = inv;
}
for (ii = 0; ii < list->num; ii++) {
if (list->list[ii] == value) {
return 0;
}
}
if (list->num >= LIST_MAX) {
return -1;
}
list->list[list->num] = value;
list->num++;
return 0;
}
/* see if value matches criteria of the list */
static int check_list(const struct list *list, int value)
{
unsigned ii;
if (list->num == 0) {
/* empty list matches all values */
return 1;
}
for (ii = 0; ii < list->num && ii < LIST_MAX; ii++) {
if (list->list[ii] == value) {
return !list->inv;
}
}
return list->inv;
}
static void printlist(FILE *fh, const struct list *list,
const char *(*item2a)(int value))
{
int ii;
if (list->inv) {
logmsgf(LOGMSG_USER, fh, "not in ");
} else {
logmsgf(LOGMSG_USER, fh, "in ");
}
for (ii = 0; ii < list->num; ii++) {
if (ii > 0) {
logmsgf(LOGMSG_USER, fh, ", ");
}
logmsgf(LOGMSG_USER, fh, "%d", list->list[ii]);
if (item2a) {
logmsgf(LOGMSG_USER, fh, " (%s)", item2a(list->list[ii]));
}
}
}
/* get and reference a file output */
static struct output *get_output_ll(const char *filename)
{
struct output *out;
int fd, len;
LISTC_FOR_EACH(&outputs, out, linkv)
{
if (strcmp(out->filename, filename) == 0) {
out->refcount++;
return out;
}
}
fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0666);
if (fd == -1) {
logmsg(LOGMSG_ERROR, "error opening '%s' for logging: %d %s\n",
filename, errno, strerror(errno));
default_out->refcount++;
return default_out;
}
len = strlen(filename);
out = calloc(offsetof(struct output, filename) + len + 1, 1);
if (!out) {
logmsg(LOGMSG_ERROR, "%s: out of memory\n", __func__);
Close(fd);
default_out->refcount++;
return default_out;
}
logmsg(LOGMSG_INFO, "opened request log file %s\n", filename);
memcpy(out->filename, filename, len + 1);
out->use_time_prefix = 1;
out->refcount = 1;
out->fd = fd;
listc_atl(&outputs, out);
Pthread_mutex_init(&out->mutex, NULL);
return out;
}
/* dereference a file output */
static void deref_output_ll(struct output *out)
{
out->refcount--;
if (out->refcount <= 0 && out->fd > 2) {
Close(out->fd);
logmsg(LOGMSG_INFO, "closed request log file %s\n", out->filename);
listc_rfl(&outputs, out);
free(out);
}
}
/* should be called while you hold the rules_mutex */
static struct logrule *new_rule_ll(const char *name)
{
struct logrule *rule;
rule = calloc(sizeof(struct logrule), 1);
if (!rule) {
logmsg(LOGMSG_ERROR, "%s: calloc failed\n", __func__);
return NULL;
}
strncpy0(rule->name, name, sizeof(rule->name));
init_range(&rule->duration);
init_range(&rule->retries);
init_dblrange(&rule->sql_cost);
init_range(&rule->sql_rows);
rule->out = default_out;
rule->out->refcount++;
listc_abl(&rules, rule);
return rule;
}
static void del_rule_ll(struct logrule *rule)
{
if (rule) {
listc_rfl(&rules, rule);
deref_output_ll(rule->out);
free(rule);
}
}
static const char *rangestr(const struct range *range, char *buf, size_t buflen)
{
if (range->from >= 0 && range->to >= 0) {
snprintf(buf, buflen, "%d..%d", range->from, range->to);
} else if (range->from >= 0) {
snprintf(buf, buflen, ">=%d", range->from);
} else if (range->to >= 0) {
snprintf(buf, buflen, "<=%d", range->to);
} else {
strncpy0(buf, "<no constraint>", buflen);
}
return buf;
}
static const char *dblrangestr(const struct dblrange *range, char *buf,
size_t buflen)
{
if (range->from >= 0 && range->to >= 0) {
snprintf(buf, buflen, "%f..%f", range->from, range->to);
} else if (range->from >= 0) {
snprintf(buf, buflen, ">=%f", range->from);
} else if (range->to >= 0) {
snprintf(buf, buflen, "<=%f", range->to);
} else {
strncpy0(buf, "<no constraint>", buflen);
}
return buf;
}
static void printrule(struct logrule *rule, FILE *fh, const char *p)
{
char b[32];
logmsgf(LOGMSG_USER, fh, "%sRULE '%s'", p, rule->name);
if (!rule->active) logmsgf(LOGMSG_USER, fh, " (INACTIVE)");
logmsgf(LOGMSG_USER, fh, "\n");
if (rule->count)
logmsgf(LOGMSG_USER, fh, "%s Log next %d requests where:\n", p,
rule->count);
else
logmsgf(LOGMSG_USER, fh, "%s Log all requests where:\n", p);
if (rule->duration.from >= 0 || rule->duration.to >= 0)
logmsgf(LOGMSG_USER, fh, "%s duration %s msec\n", p,
rangestr(&rule->duration, b, sizeof(b)));
if (rule->retries.from >= 0 || rule->retries.to >= 0)
logmsgf(LOGMSG_USER, fh, "%s retries %s\n", p,
rangestr(&rule->retries, b, sizeof(b)));
if (rule->vreplays.from >= 0 || rule->vreplays.to >= 0)
logmsgf(LOGMSG_USER, fh, "%s verify replays %s\n", p,
rangestr(&rule->vreplays, b, sizeof(b)));
if (rule->sql_cost.from >= 0 || rule->sql_cost.to >= 0)
logmsgf(LOGMSG_USER, fh, "%s SQL cost %s\n", p,
dblrangestr(&rule->sql_cost, b, sizeof(b)));
if (rule->sql_rows.from >= 0 || rule->sql_rows.to >= 0)
logmsgf(LOGMSG_USER, fh, "%s SQL rows %s\n", p,
rangestr(&rule->sql_rows, b, sizeof(b)));
if (rule->rc_list.num > 0) {
logmsgf(LOGMSG_USER, fh, "%s rcode is ", p);
printlist(fh, &rule->rc_list, NULL);
logmsgf(LOGMSG_USER, fh, "\n");
}
if (rule->opcode_list.num > 0) {
logmsgf(LOGMSG_USER, fh, "%s opcode is ", p);
printlist(fh, &rule->opcode_list, req2a);
logmsgf(LOGMSG_USER, fh, "\n");
}
if (rule->tablename[0]) {
logmsgf(LOGMSG_USER, fh, "%s touches table '%s'\n", p,
rule->tablename);
}
if (rule->stmt[0]) {
logmsgf(LOGMSG_USER, fh, "%s sql statement like '%%%s%%'\n", p,
rule->stmt);
}
if (rule->event_mask & REQL_TRACE)
logmsgf(LOGMSG_USER, fh, "%s Logging detailed trace\n", p);
if (rule->event_mask & REQL_RESULTS)
logmsgf(LOGMSG_USER, fh, "%s Logging query results\n", p);
logmsgf(LOGMSG_USER, fh, "%s Log to %s\n", p, rule->out->filename);
}
/* scan all the rules and setup our master settings that define what we log
* for each request. We want to log as little as possible to be fast, but
* we have to make sure that we log enough so that if a request matches some
* of our criteria we can catch it. */
static void scanrules_ll(void)
{
int ii, rc;
int table_rules = 0;
struct logrule *rule;
unsigned event_mask = 0;
int log_all_reqs = 0;
master_all_requests = 1;
master_num_stmts = 0;
bzero(&master_opcode_list, sizeof(master_opcode_list));
bzero(&master_opcode_inv_list, sizeof(master_opcode_inv_list));
bzero(master_stmts, NUMSTMTS * (MAXSTMT + 1));
LISTC_FOR_EACH(&rules, rule, linkv)
{
/* ignore inactive rules */
if (!rule->active) {
continue;
}
/* if the rule doesn't have any criteria that can be tested before the
* request starts, then we definateky have to log for all requests */
if (rule->opcode_list.num == 0 && rule->stmt[0] == 0) {
log_all_reqs = 1;
}
/* if the rule has opcode criteria then build a list of opcodes to
* allow through */
for (ii = 0; ii < rule->opcode_list.num; ii++) {
if (rule->opcode_list.inv) {
rc = add_list(&master_opcode_inv_list,
rule->opcode_list.list[ii], 1);
} else {
rc = add_list(&master_opcode_list, rule->opcode_list.list[ii],
0);
}
if (rc != 0) {
log_all_reqs = 1;
}
}
/* if the rule has table name criteria then we must track tables used */
if (rule->tablename[0]) {
table_rules = 1;
}
/* if the rule has sql stmt criteria then add it to the list */
if (rule->stmt[0]) {
if (master_num_stmts == NUMSTMTS) {
log_all_reqs = 1;
} else {
strncpy(master_stmts[master_num_stmts], rule->stmt, MAXSTMT);
master_num_stmts++;
}
}
event_mask |= rule->event_mask;
}
master_event_mask = event_mask;
master_table_rules = table_rules;
master_all_requests = log_all_reqs;
if (verbose) {
logmsg(LOGMSG_USER, "%s: master_event_mask=0x%x\n", __func__,
master_event_mask);
logmsg(LOGMSG_USER, "%s: master_table_rules=%d\n", __func__,
master_table_rules);
logmsg(LOGMSG_USER, "%s: master_all_requests=%d\n", __func__,
master_all_requests);
logmsg(LOGMSG_USER, "%s: master_opcode_inv_list: ", __func__);
printlist(stdout, &master_opcode_inv_list, NULL);
logmsg(LOGMSG_USER, "\n");
logmsg(LOGMSG_USER, "%s: master_opcode_list: ", __func__);
printlist(stdout, &master_opcode_list, NULL);
logmsg(LOGMSG_USER, "\n");
for (ii = 0; ii < master_num_stmts; ii++) {
logmsg(LOGMSG_USER, "master_stmts[%d] = '%s'\n", ii,
master_stmts[ii]);
}
}
}
int reqlog_init(const char *dbname)
{
struct output *out;
char *filename;
Pthread_mutex_init(&rules_mutex, NULL);
listc_init(&rules, offsetof(struct logrule, linkv));
listc_init(&outputs, offsetof(struct output, linkv));
out = calloc(sizeof(struct output) + strlen("<stdout>") + 1, 1);
if (!out) {
logmsg(LOGMSG_ERROR, "%s:calloc failed\n", __func__);
return -1;
}
strcpy(out->filename, "<stdout>");
out->fd = 2;
out->refcount = 1;
default_out = out;
listc_atl(&outputs, out);
Pthread_mutex_init(&out->mutex, NULL);
filename = comdb2_location("logs", "%s.longreqs", dbname);
long_request_out = get_output_ll(filename);
free(filename);
filename = comdb2_location("logs", "%s.statreqs", dbname);
stat_request_out = get_output_ll(filename);
free(filename);
eventlog_init();
scanrules_ll();
return 0;
}
static const char *help_text[] = {
"Request logging framework commands",
"reql off - request logging turn off for performance",
"reql longrequest # - set long request threshold in msec",
"reql longsqlrequest # - set long SQL request threshold in msec",
"reql longreqfile <filename> - set file to log long requests in",
"reql diffstat # - set diff stat threshold in sec",
"reql truncate # - set request truncation",
"reql stat - status, print rules",
"reql events - event logging",
" Subcommands for events: ",
" on - turn on event logging",
" off - turn off event logging",
" roll - roll over log file",
" keep N - keep N log files",
" detailed on/off - turn on/off detailed mode (ex. sql bound param)",
" rollat N - roll when log file size larger than N MB",
" every N - log only every Nth event, 0 logs all",
" verbose on/off - turn on/off verbose mode",
" dir <dir> - set custom directory for event log files\n",
" file <file> - set log file to custom location\n",
" flush - flush log file to disk",
"reql [rulename] ... - add/modify rules. The default rule is '0'.",
" Valid rule names begin with a digit or '.'.",
" General commands:", " delete - delete named rule",
" go - start logging with rule",
" stop - stop logging with this rule",
" Specify criteria:",
" opcode [!]# - log regular requests with opcode [other than] #",
" rc [!]# - log requests with rcode [other than] #",
" ms <range> - log requests within a range of msecs",
" retries <range> - log requests with that many retries",
" cost <range> - log SQL requests with the given cost",
" rows <range> - log SQL requests with the given row count",
" table <name> - log requests that touch given table",
" stmt 'sql stmt' - log requests where sql contains that text",
" vreplays <range> - log requests with given number of verify "
"replays",
" Specify what to log:", " trace - log detailed trace",
" results - log query results",
" cnt # - log up to # before removing rule",
" Specify where to log:",
" file <filename> - log to filename rather than stdout",
" stdout - log to stdout",
"<range> is a range specification. valid range specifications are:-",
" #+ - match any number >=#",
" #- - match any number <=#",
" #..# - match anything between the two numbers "
"inclusive",
"<filename> must be a filename or the keyword '<stdout>'", NULL};
void reqlog_help(void)
{
int ii;
for (ii = 0; help_text[ii]; ii++) {
logmsg(LOGMSG_USER, "%s\n", help_text[ii]);
}
}
/* parse a range specification */
static int parse_range_tok(struct range *range, char *tok, int ltok)
{
if (ltok > 0) {
if (tok[ltok - 1] == '-') {
range->from = -1;
range->to = toknum(tok, ltok - 1);
return 0;
} else if (tok[ltok - 1] == '+') {
range->from = toknum(tok, ltok - 1);
range->to = -1;
return 0;
} else {
int ii;
for (ii = 0; ii < ltok - 1; ii++) {
if (tok[ii] == '.' && tok[ii + 1] == '.') {
int end;
for (end = ii + 2; end < ltok && tok[end] == '.'; end++)
;
range->from = toknum(tok, ii);
range->to = toknum(tok + end, ltok - end);
return 0;
}
}
}
}
logmsg(LOGMSG_ERROR, "bad range specification '%*.*s'\n", ltok, ltok, tok);
return -1;
}
static int parse_dblrange_tok(struct dblrange *dblrange, char *tok, int ltok)
{
struct range range;
int rc;
rc = parse_range_tok(&range, tok, ltok);
if (rc) return rc;
dblrange->from = range.from;
dblrange->to = range.to;
return 0;
}
static void tokquoted(char *line, int lline, int *st, char *buf, size_t bufsz)
{
int stage = 0;
char quote = 0;
if (bufsz == 0) {
return;
}
while (bufsz > 0 && *st < lline) {
char ch = line[*st];
switch (stage) {
case 0:
/* scan for start */
if (ch == '\'' || ch == '"') {
quote = ch;
stage = 2;
break;
} else if (isspace(ch)) {
break;
}
stage = 1;
/* fall through; found first character of line */
case 1:
/* unquoted text, scan for next whitespace */
if (isspace(ch)) {
goto end;
}
*buf = ch;
buf++;
bufsz--;
break;
case 2:
/* quoted text */
if (ch == quote) {
if ((*st) + 1 < lline && buf[(*st) + 1] == ch) {
(*st)++;
} else {
(*st)++;
goto end;
}
}
*buf = ch;
buf++;
bufsz--;
break;
}
(*st)++;
}
end:
if (bufsz == 0) {
bufsz--;
buf--;
}
*buf = 0;
}
void reqlog_process_message(char *line, int st, int lline)
{
char *tok;
int ltok;
tok = segtok(line, lline, &st, <ok);
if (tokcmp(tok, ltok, "off") == 0) {
logmsg(LOGMSG_USER, "Turn off Request logging\n");
reqlog_init_off = 1;
} else if (tokcmp(tok, ltok, "longrequest") == 0) {
tok = segtok(line, lline, &st, <ok);
long_request_ms = toknum(tok, ltok);
logmsg(LOGMSG_USER, "Long request threshold now %d msec\n",
long_request_ms);
} else if (tokcmp(tok, ltok, "longsqlrequest") == 0) {
tok = segtok(line, lline, &st, <ok);
gbl_sql_time_threshold = toknum(tok, ltok);
logmsg(LOGMSG_USER, "Long SQL request threshold now %d msec\n",
gbl_sql_time_threshold);
} else if (tokcmp(tok, ltok, "longreqfile") == 0) {
char filename[128];
struct output *out;
tok = segtok(line, lline, &st, <ok);
tokcpy0(tok, ltok, filename, sizeof(filename));
out = get_output_ll(filename);
if (out) {
deref_output_ll(long_request_out);
long_request_out = out;
}
} else if (tokcmp(tok, ltok, "diffstat") == 0) {
tok = segtok(line, lline, &st, <ok);
if (ltok == 0) {
reqlog_help();
} else {
reqlog_set_diffstat_thresh(toknum(tok, ltok));
}
} else if (tokcmp(tok, ltok, "truncate") == 0) {
tok = segtok(line, lline, &st, <ok);
if (ltok == 0) {
reqlog_help();
} else {
reqlog_set_truncate(toknum(tok, ltok));
}
} else if (tokcmp(tok, ltok, "stat") == 0) {
reqlog_stat();
} else if (tokcmp(tok, ltok, "help") == 0) {
reqlog_help();
} else if (tokcmp(tok, ltok, "vbon") == 0) {
verbose = 1;
} else if (tokcmp(tok, ltok, "vbof") == 0) {
verbose = 0;
} else if (ltok == 0) {
logmsg(LOGMSG_ERROR, "huh?\n");
} else if (tokcmp(tok, ltok, "events") == 0) {
eventlog_process_message(line, lline, &st);
} else {
char rulename[32];
struct logrule *rule;
if (isdigit(tok[0]) || tok[0] == '.') {
tokcpy0(tok, ltok, rulename, sizeof(rulename));
tok = segtok(line, lline, &st, <ok);
} else {
strncpy0(rulename, "0", sizeof(rulename));
}
if (verbose) {
logmsg(LOGMSG_USER, "rulename='%s'\n", rulename);
}
Pthread_mutex_lock(&rules_mutex);
LISTC_FOR_EACH(&rules, rule, linkv)
{
if (strcmp(rulename, rule->name) == 0) {
break;
}
}
if (!rule) {
rule = new_rule_ll(rulename);
if (!rule) {
logmsg(LOGMSG_ERROR, "error creating new rule %s\n", rulename);
Pthread_mutex_unlock(&rules_mutex);
return;
}
}
while (ltok > 0) {
if (tokcmp(tok, ltok, "go") == 0) {
rule->active = 1;
} else if (tokcmp(tok, ltok, "stop") == 0) {
rule->active = 0;
} else if (tokcmp(tok, ltok, "delete") == 0) {
del_rule_ll(rule);
rule = NULL;
logmsg(LOGMSG_USER, "Rule deleted\n");
break;
} else if (tokcmp(tok, ltok, "cnt") == 0) {
tok = segtok(line, lline, &st, <ok);
rule->count = toknum(tok, ltok);
} else if (tokcmp(tok, ltok, "file") == 0) {
char filename[128];
struct output *out;
tok = segtok(line, lline, &st, <ok);
tokcpy0(tok, ltok, filename, sizeof(filename));
out = get_output_ll(filename);
if (out) {
deref_output_ll(rule->out);
rule->out = out;
}
} else if (tokcmp(tok, ltok, "stdout") == 0) {
struct output *out;
out = default_out;
out->refcount++;
deref_output_ll(rule->out);
rule->out = out;
} else if (tokcmp(tok, ltok, "ms") == 0) {
tok = segtok(line, lline, &st, <ok);
parse_range_tok(&rule->duration, tok, ltok);
} else if (tokcmp(tok, ltok, "retries") == 0) {
tok = segtok(line, lline, &st, <ok);
parse_range_tok(&rule->retries, tok, ltok);
} else if (tokcmp(tok, ltok, "vreplays") == 0) {
tok = segtok(line, lline, &st, <ok);
parse_range_tok(&rule->vreplays, tok, ltok);
} else if (tokcmp(tok, ltok, "cost") == 0) {
tok = segtok(line, lline, &st, <ok);
parse_dblrange_tok(&rule->sql_cost, tok, ltok);
} else if (tokcmp(tok, ltok, "rows") == 0) {
tok = segtok(line, lline, &st, <ok);
parse_range_tok(&rule->sql_rows, tok, ltok);
} else if (tokcmp(tok, ltok, "sql") == 0) {
add_list(&rule->opcode_list, OP_SQL, 0);
} else if (tokcmp(tok, ltok, "stmt") == 0) {
tokquoted(line, lline, &st, rule->stmt, sizeof(rule->stmt));
} else if (tokcmp(tok, ltok, "opcode") == 0) {
int opcode;
char opname[32];
int inv = 0;
tok = segtok(line, lline, &st, <ok);
if (ltok > 0 && tok[0] == '!') {
tok++;
ltok--;
inv = 1;
}
tokcpy0(tok, ltok, opname, sizeof(opname));
opcode = a2req(opname);
if (opcode >= 0 && opcode < MAXTYPCNT) {
add_list(&rule->opcode_list, opcode, inv);
}
} else if (tokcmp(tok, ltok, "rc") == 0) {
int rc;
int inv = 0;
tok = segtok(line, lline, &st, <ok);
if (ltok > 0 && tok[0] == '!') {
tok++;
ltok--;
inv = 1;
}
rc = toknum(tok, ltok);
add_list(&rule->rc_list, rc, inv);
} else if (tokcmp(tok, ltok, "table") == 0) {
tok = segtok(line, lline, &st, <ok);
tokcpy0(tok, ltok, rule->tablename, sizeof(rule->tablename));
} else if (tokcmp(tok, ltok, "trace") == 0) {
rule->event_mask |= REQL_TRACE;
} else if (tokcmp(tok, ltok, "results") == 0) {
rule->event_mask |= REQL_RESULTS;
} else {
logmsg(LOGMSG_ERROR, "unknown rule command <%*.*s>\n", ltok,
ltok, tok);
}
tok = segtok(line, lline, &st, <ok);
}
if (rule) {
printrule(rule, stdout, "");
}
scanrules_ll();
Pthread_mutex_unlock(&rules_mutex);
}
}
void reqlog_stat(void)
{
struct logrule *rule;
struct output *out;
logmsg(LOGMSG_USER, "Long request threshold : %d msec (%dmsec for SQL)\n",
long_request_ms, gbl_sql_time_threshold);
logmsg(LOGMSG_USER, "Long request log file : %s\n",
long_request_out->filename);
logmsg(LOGMSG_USER, "diffstat threshold : %d s\n", diffstat_thresh);
logmsg(LOGMSG_USER, "diffstat log file : %s\n",
stat_request_out->filename);
logmsg(LOGMSG_USER, "request truncation : %s\n",
reqltruncate ? "enabled" : "disabled");
logmsg(LOGMSG_USER, "SQL cost thresholds :\n");
logmsg(LOGMSG_USER, " error : ");
if (gbl_sql_cost_error_threshold == -1)
logmsg(LOGMSG_USER, "not set\n");
else
logmsg(LOGMSG_USER, "%f\n", gbl_sql_cost_error_threshold);
Pthread_mutex_lock(&rules_mutex);
logmsg(LOGMSG_USER, "%d rules currently active\n", rules.count);
LISTC_FOR_EACH(&rules, rule, linkv)
{
printrule(rule, stdout, "");
}
LISTC_FOR_EACH(&outputs, out, linkv)
{
logmsg(LOGMSG_USER, "Output file open: %s\n", out->filename);
}
eventlog_status();
Pthread_mutex_unlock(&rules_mutex);
}