-
Notifications
You must be signed in to change notification settings - Fork 11
/
varnishapi.py
1096 lines (920 loc) · 33.3 KB
/
varnishapi.py
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
# coding: utf-8
# Copyright (c) 2013-2016 Shohei Tanaka(@xcir)
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
# https://github.com/xcir/python-varnishapi
# v50.20
from ctypes import *
import getopt
import time
class VSC_level_desc(Structure):
_fields_ = [
("verbosity", c_uint), # unsigned verbosity;
("label", c_char_p), # const char *label; /* label */
("sdesc", c_char_p), # const char *sdesc; /* short description */
("ldesc", c_char_p), # const char *ldesc; /* long description */
]
class VSC_type_desc(Structure):
_fields_ = [
("label", c_char_p), # const char *label; /* label */
("sdesc", c_char_p), # const char *sdesc; /* short description */
("ldesc", c_char_p), # const char *ldesc; /* long description */
]
class VSM_fantom(Structure):
_fields_ = [
("chunk", c_void_p), # struct VSM_chunk *chunk;
("b", c_void_p), # void *b; /* first byte of payload */
("e", c_void_p), # void *e; /* first byte past payload */
# ("priv", c_uint), #uintptr_t priv; /* VSM private */
("priv", c_void_p), # uintptr_t priv; /* VSM private */
("_class", c_char * 8), # char class[VSM_MARKER_LEN];
("type", c_char * 8), # char type[VSM_MARKER_LEN];
("ident", c_char * 128), # char ident[VSM_IDENT_LEN];
]
class VSC_section(Structure):
_fields_ = [
("type", c_char_p), # const char *type;
("ident", c_char_p), # const char *ident;
("desc", POINTER(VSC_type_desc)), # const struct VSC_type_desc *desc;
("fantom", POINTER(VSM_fantom)), # struct VSM_fantom *fantom;
]
class VSC_desc(Structure):
_fields_ = [
# const char *name; /* field name */
("name", c_char_p),
# const char *fmt; /* field format ("uint64_t") */
("fmt", c_char_p),
# int flag; /* 'c' = counter, 'g' = gauge */
("flag", c_int),
# const char *sdesc; /* short description */
("sdesc", c_char_p),
# const char *ldesc; /* long description */
("ldesc", c_char_p),
# const struct VSC_level_desc *level;
("level", POINTER(VSC_level_desc)),
]
class VSC_point(Structure):
_fields_ = [
# const struct VSC_desc *desc; /* point description */
("desc", POINTER(VSC_desc)),
# ("ptr", c_void_p), #const volatile void *ptr; /* field value */
# const volatile void *ptr; /* field value */
("ptr", POINTER(c_ulonglong)),
# const struct VSC_section *section;
("section", POINTER(VSC_section)),
]
# typedef int VSC_iter_f(void *priv, const struct VSC_point *const pt);
VSC_iter_f = CFUNCTYPE(
c_int,
c_void_p,
POINTER(VSC_point)
)
# typedef void VSL_tagfind_f(int tag, void *priv);
VSL_tagfind_f = CFUNCTYPE(
c_int,
c_void_p
)
#
class VSLC_ptr(Structure):
_fields_ = [
# const uint32_t *ptr; /* Record pointer */
("ptr", POINTER(c_uint32)),
# unsigned priv;
("priv", c_uint),
]
class VSL_cursor(Structure):
_fields_ = [
("rec", VSLC_ptr), # struct VSLC_ptr rec;
("priv_tbl", c_void_p), # const void *priv_tbl;
("priv_data", c_void_p), # void *priv_data;
]
class VSL_transaction(Structure):
_fields_ = [
("level", c_uint), # unsigned level;
("vxid", c_int32), # int32_t vxid;
("vxid_parent", c_int32), # int32_t vxid_parent;
("type", c_int), # enum VSL_transaction_e type;
("reason", c_int), # enum VSL_reason_e reason;
("c", POINTER(VSL_cursor)), # struct VSL_cursor *c;
]
class VTAILQ_HEAD(Structure):
_fields_ = [
# struct type *vtqh_first; /* first element */
("vtqh_first", c_void_p),
# struct type **vtqh_last; /* addr of last next element */
("vtqh_last", POINTER(c_void_p)),
]
class vbitmap(Structure):
_fields_ = [
("bits", c_void_p), # VBITMAP_TYPE *bits;
("nbits", c_uint), # unsigned nbits;
]
class vsb(Structure):
_fields_ = [
# unsigned magic;
("magic", c_uint),
# char *s_buf; /* storage buffer */
("s_buf", c_char_p),
# int s_error; /* current error code */
("s_error", c_int),
# ssize_t s_size; /* size of storage buffer */
# ("s_size", c_ssize_t),
# ssize_t s_len; /* current length of string */
# ("s_len", c_ssize_t),
# ssize_t s_size; /* size of storage buffer */
("s_size", c_long),
# ssize_t s_len; /* current length of string */
("s_len", c_long),
# int s_flags; /* flags */
("s_flags", c_int),
]
class VSL_data(Structure):
_fields_ = [
("magic", c_uint), # unsigned magic;
("diag", POINTER(vsb)), # struct vsb *diag;
("flags", c_uint), # unsigned flags;
("vbm_select", POINTER(vbitmap)), # struct vbitmap *vbm_select;
("vbm_supress", POINTER(vbitmap)), # struct vbitmap *vbm_supress;
("vslf_select", VTAILQ_HEAD), # vslf_list vslf_select;
("vslf_suppress", VTAILQ_HEAD), # vslf_list vslf_suppress;
("b_opt", c_int), # int b_opt;
("c_opt", c_int), # int c_opt;
("C_opt", c_int), # int C_opt;
("L_opt", c_int), # int L_opt;
("T_opt", c_double), # double T_opt;
("v_opt", c_int), # int v_opt;
]
# typedef int VSLQ_dispatch_f(struct VSL_data *vsl, struct VSL_transaction
# * const trans[], void *priv);
VSLQ_dispatch_f = CFUNCTYPE(
c_int,
POINTER(VSL_data),
POINTER(POINTER(VSL_transaction)),
c_void_p
)
class VarnishAPIDefine40:
def __init__(self):
self.VSL_COPT_TAIL = (1 << 0)
self.VSL_COPT_BATCH = (1 << 1)
self.VSL_COPT_TAILSTOP = (1 << 2)
self.SLT_F_BINARY = (1 << 1)
'''
//////////////////////////////
enum VSL_transaction_e {
VSL_t_unknown,
VSL_t_sess,
VSL_t_req,
VSL_t_bereq,
VSL_t_raw,
VSL_t__MAX,
};
'''
self.VSL_t_unknown = 0
self.VSL_t_sess = 1
self.VSL_t_req = 2
self.VSL_t_bereq = 3
self.VSL_t_raw = 4
self.VSL_t__MAX = 5
'''
//////////////////////////////
enum VSL_reason_e {
VSL_r_unknown,
VSL_r_http_1,
VSL_r_rxreq,
VSL_r_esi,
VSL_r_restart,
VSL_r_pass,
VSL_r_fetch,
VSL_r_bgfetch,
VSL_r_pipe,
VSL_r__MAX,
};
'''
self.VSL_r_unknown = 0
self.VSL_r_http_1 = 1
self.VSL_r_rxreq = 2
self.VSL_r_esi = 3
self.VSL_r_restart = 4
self.VSL_r_pass = 5
self.VSL_r_fetch = 6
self.VSL_r_bgfetch = 7
self.VSL_r_pipe = 8
self.VSL_r__MAX = 9
'''
//////////////////////////////
enum VSM_valid_e {
VSM_invalid,
VSM_valid,
VSM_similar,
};
'''
self.VSM_invalid = 0
self.VSM_valid = 1
self.VSM_similar = 2
'''
//////////////////////////////
enum vas_e {
VAS_WRONG,
VAS_MISSING,
VAS_ASSERT,
VAS_INCOMPLETE,
VAS_VCL,
};
'''
self.VAS_WRONG = 0
self.VAS_MISSING = 1
self.VAS_ASSERT = 2
self.VAS_INCOMPLETE = 3
self.VAS_VCL = 4
class LIBVARNISHAPI:
def __init__(self, lib):
#Check libvarnishapi version
if hasattr(lib, "VUT_Init"):
self.apiversion = 1.5
elif hasattr(lib, "VSM_IsOpen"):
self.apiversion = 1.4
else:
self.apiversion = 1.3
#LIBVARNISHAPI_1.0
#VSM_New;
self.VSM_New = lib.VSM_New
self.VSM_New.restype = c_void_p
#VSM_Diag; (undefined symbol at 4.0/4.1/5.0)
#VSM_n_Arg;
self.VSM_n_Arg = lib.VSM_n_Arg
self.VSM_n_Arg.restype = c_int
self.VSM_n_Arg.argtypes = [c_void_p, c_char_p]
#VSM_Name;
self.VSM_Name = lib.VSM_Name
self.VSM_Name.restype = c_char_p
self.VSM_Name.argtypes = [c_void_p]
#VSM_Delete;
self.VSM_Delete = lib.VSM_Delete
self.VSM_Delete.argtypes = [c_void_p]
#VSM_Open;
self.VSM_Open = lib.VSM_Open
self.VSM_Open.restype = c_int
self.VSM_Open.argtypes = [c_void_p]
#VSM_ReOpen; (undefined symbol at 4.0/4.1/5.0)
#VSM_Seq; (undefined symbol at 4.0/4.1/5.0)
#VSM_Head; (undefined symbol at 4.0/4.1/5.0)
#VSM_Find_Chunk; (undefined symbol at 4.0/4.1/5.0)
#VSM_Close;
self.VSM_Close = lib.VSM_Close
self.VSM_Close.argtypes = [c_void_p]
#VSM_iter0; (undefined symbol at 4.0/4.1/5.0)
#VSM_intern; (undefined symbol at 4.0/4.1/5.0)
#
#VSC_Setup; (undefined symbol at 4.0/4.1/5.0)
#VSC_Arg;
self.VSC_Arg = lib.VSC_Arg
self.VSC_Arg.restype = c_int
self.VSC_Arg.argtypes = [c_void_p, c_int, c_char_p]
#VSC_Open; (undefined symbol at 4.0/4.1/5.0)
#VSC_Main;
self.VSC_Main = lib.VSC_Main
self.VSC_Main.restype = c_void_p
self.VSC_Main.argtypes = [c_void_p, c_void_p]
#VSC_Iter;
self.VSC_Iter = lib.VSC_Iter
self.VSC_Iter.argtypes = [c_void_p, c_void_p, VSC_iter_f, c_void_p]
#
#VSL_Setup; (private func at 5.0)
#VSL_Open; (undefined symbol at 4.0/4.1/5.0)
#VSL_Arg;
self.VSL_Arg = lib.VSL_Arg
self.VSL_Arg.restype = c_int
self.VSL_Arg.argtypes = [c_void_p, c_int, c_char_p]
#VSL_H_Print; (undefined symbol at 4.0/4.1/5.0)
#VSL_Select; (undefined symbol at 4.0/4.1/5.0)
#VSL_NonBlocking; (undefined symbol at 4.0/4.1/5.0)
#VSL_Dispatch; (undefined symbol at 4.0/4.1/5.0)
#VSL_NextLog; (undefined symbol at 4.0/4.1/5.0)
#VSL_Matched; (undefined symbol at 4.0/4.1/5.0)
#
#VCLI_WriteResult;
#VCLI_ReadResult;
#VCLI_AuthResponse;
#
## Variables
#VSL_tags;
#LIBVARNISHAPI_1.1
# Functions:
#VSL_Name2Tag;
self.VSL_Name2Tag = lib.VSL_Name2Tag
self.VSL_Name2Tag.restype = c_int
self.VSL_Name2Tag.argtypes = [c_char_p, c_int]
#LIBVARNISHAPI_1.2
# Functions:
#VSL_NextSLT; (undefined symbol at 4.0/4.1/5.0)
#VSM_Error;
self.VSM_Error = lib.VSM_Error
self.VSM_Error.restype = c_char_p
self.VSM_Error.argtypes = [c_void_p]
#VSM_Get;
self.VSM_Get = lib.VSM_Get
self.VSM_Get.argtypes = [c_void_p, c_void_p, c_char_p, c_char_p, c_char_p]
#LIBVARNISHAPI_1.3
#VSM_Abandoned;
self.VSM_Abandoned = lib.VSM_Abandoned
self.VSM_Abandoned.argtypes = [c_void_p]
#VSM_ResetError;
self.VSM_ResetError = lib.VSM_ResetError
self.VSM_ResetError.argtypes = [c_void_p]
#VSM_StillValid;
self.VSM_StillValid = lib.VSM_StillValid
self.VSM_StillValid.argtypes = [c_void_p, c_void_p]
#VSC_Mgt;
self.VSC_Mgt = lib.VSC_Mgt
self.VSC_Mgt.restype = c_void_p
self.VSC_Mgt.argtypes = [c_void_p, c_void_p]
#VSC_LevelDesc;
self.VSC_LevelDesc = lib.VSC_LevelDesc
self.VSC_LevelDesc.restype = c_void_p
self.VSC_LevelDesc.argtypes = [c_uint]
#VSL_New;
self.VSL_New = lib.VSL_New
self.VSL_New.restype = c_void_p
#VSL_Delete;
self.VSL_Delete = lib.VSL_Delete
self.VSL_Delete.argtypes = [c_void_p]
#VSL_Error;
self.VSL_Error = lib.VSL_Error
self.VSL_Error.restype = c_char_p
self.VSL_Error.argtypes = [c_void_p]
#VSL_ResetError;
self.VSL_ResetError = lib.VSL_ResetError
self.VSL_ResetError.argtypes = [c_void_p]
#VSL_CursorVSM;
self.VSL_CursorVSM = lib.VSL_CursorVSM
self.VSL_CursorVSM.restype = POINTER(VSL_cursor)
self.VSL_CursorVSM.argtypes = [c_void_p, c_void_p, c_uint]
#VSL_CursorFile;
self.VSL_CursorFile = lib.VSL_CursorFile
self.VSL_CursorFile.restype = POINTER(VSL_cursor)
self.VSL_CursorFile.argtypes = [c_void_p, c_char_p, c_uint]
#VSL_DeleteCursor;
self.VSL_DeleteCursor = lib.VSL_DeleteCursor
self.VSL_DeleteCursor.argtypes = [c_void_p]
#VSL_Next;
self.VSL_Next = lib.VSL_Next
self.VSL_Next.restype = c_int
self.VSL_Next.argtypes = [POINTER(VSL_cursor)]
#VSL_Match;
self.VSL_Match = lib.VSL_Match
self.VSL_Match.restype = c_int
self.VSL_Match.argtypes = [c_void_p, POINTER(VSL_cursor)]
#VSL_Print;
self.VSL_Print = lib.VSL_Print
self.VSL_Print.argtypes = [c_void_p, c_void_p, c_void_p]
#VSL_PrintTerse;
self.VSL_PrintTerse = lib.VSL_PrintTerse
self.VSL_PrintTerse.argtypes = [c_void_p, c_void_p, c_void_p]
#VSL_PrintAll;
self.VSL_PrintAll = lib.VSL_PrintAll
self.VSL_PrintAll.argtypes = [c_void_p, c_void_p, c_void_p]
#VSL_PrintTransactions;
self.VSL_PrintTransactions = lib.VSL_PrintTransactions
self.VSL_PrintTransactions.argtypes = [c_void_p, POINTER(POINTER(VSL_transaction)), c_void_p]
#VSL_WriteOpen;
self.VSL_WriteOpen = lib.VSL_WriteOpen
self.VSL_WriteOpen.restype = c_void_p
self.VSL_WriteOpen.argtypes = [c_void_p, c_char_p, c_int, c_int]
#VSL_Write;
self.VSL_Write = lib.VSL_Write
self.VSL_Write.argtypes = [c_void_p, c_void_p, c_void_p]
#VSL_WriteAll;
self.VSL_WriteAll = lib.VSL_WriteAll
self.VSL_WriteAll.argtypes = [c_void_p, c_void_p, c_void_p]
#VSL_WriteTransactions;
self.VSL_WriteTransactions = lib.VSL_WriteTransactions
self.VSL_WriteTransactions.argtypes = [c_void_p, POINTER(POINTER(VSL_transaction)), c_void_p]
#VSLQ_New;
self.VSLQ_New = lib.VSLQ_New
self.VSLQ_New.restype = c_void_p
self.VSLQ_New.argtypes = [c_void_p, POINTER(POINTER(VSL_cursor)), c_int, c_char_p]
#VSLQ_Delete;
self.VSLQ_Delete = lib.VSLQ_Delete
self.VSLQ_Delete.argtypes = [POINTER(c_void_p)]
#VSLQ_Dispatch;
self.VSLQ_Dispatch = lib.VSLQ_Dispatch
self.VSLQ_Dispatch.restype = c_int
self.VSLQ_Dispatch.argtypes = [c_void_p, VSLQ_dispatch_f, c_void_p]
#VSLQ_Flush;
self.VSLQ_Flush = lib.VSLQ_Flush
self.VSLQ_Flush.restype = c_int
self.VSLQ_Flush.argtypes = [c_void_p, VSLQ_dispatch_f, c_void_p]
#VSLQ_Name2Grouping;
self.VSLQ_Name2Grouping = lib.VSLQ_Name2Grouping
self.VSLQ_Name2Grouping.restype = c_int
self.VSLQ_Name2Grouping.argtypes = [c_char_p, c_int]
#VSL_Glob2Tags;
self.VSL_Glob2Tags = lib.VSL_Glob2Tags
self.VSL_Glob2Tags.argtypes = [c_char_p, c_int, VSL_tagfind_f, c_void_p]
#VSL_List2Tags;
self.VSL_List2Tags = lib.VSL_List2Tags
self.VSL_List2Tags.argtypes = [c_char_p, c_int, VSL_tagfind_f, c_void_p]
#VSM_N_Arg;
self.VSM_N_Arg = lib.VSM_N_Arg
self.VSM_N_Arg.restype = c_int
self.VSM_N_Arg.argtypes = [c_void_p, c_char_p]
#VSL_Check;
self.VSL_Check = lib.VSL_Check
self.VSL_Check.argtypes = [c_void_p, c_void_p]
#VSL_ResetCursor;
self.VSL_ResetCursor = lib.VSL_ResetCursor
self.VSL_ResetCursor.argtypes = [c_void_p]
## Variables:
#VSLQ_grouping;
#VSL_tagflags;
#LIBVARNISHAPI_1.4
if self.apiversion < 1.4:
return
#VNUM;
#VSLQ_SetCursor;
self.VSLQ_SetCursor = lib.VSLQ_SetCursor
self.VSLQ_SetCursor.argtypes = [c_void_p, POINTER(c_void_p)]
#VSM_IsOpen;
self.VSM_IsOpen = lib.VSM_IsOpen
self.VSM_IsOpen.argtypes = [c_void_p]
#LIBVARNISHAPI_1.5
if self.apiversion < 1.5:
return
#VUT_Error;
self.VUT_Error = lib.VUT_Error
self.VUT_Error.argtypes = [c_int, c_char_p]
#VUT_g_Arg;
self.VUT_g_Arg = lib.VUT_g_Arg
self.VUT_g_Arg.argtypes = [c_char_p]
#VUT_Arg;
self.VUT_Arg = lib.VUT_Arg
self.VUT_Arg.argtypes = [c_int, c_char_p]
#VUT_Setup;
self.VUT_Setup = lib.VUT_Setup
#VUT_Init;
self.VUT_Init = lib.VUT_Init
self.VUT_Init.argtypes = [c_char_p]
#VUT_Fini;
self.VUT_Fini = lib.VUT_Fini
#VUT_Main;
self.VUT_Main = lib.VUT_Main
#VUT;
#VTIM_mono;
self.VTIM_mono = lib.VTIM_mono
self.VTIM_mono.restype = c_double
#VTIM_real;
self.VTIM_real = lib.VTIM_real
self.VTIM_real.restype = c_double
#VTIM_sleep;
self.VTIM_sleep = lib.VTIM_sleep
self.VTIM_sleep.argtypes = [c_double]
#VSB_new;
self.VSB_new = lib.VSB_new
self.VSB_new.restype = c_void_p
self.VSB_new.argtypes = [c_void_p, c_char_p, c_int]
#VSB_destroy;
self.VSB_destroy = lib.VSB_destroy
self.VSB_destroy.argtypes = [POINTER(c_void_p)]
#VSB_error;
self.VSB_error = lib.VSB_error
self.VSB_error.argtypes = [c_void_p]
#VSB_cat;
self.VSB_cat = lib.VSB_cat
self.VSB_cat.argtypes = [c_void_p, c_char_p]
#VSB_putc;
self.VSB_putc = lib.VSB_putc
self.VSB_putc.argtypes = [c_void_p, c_int]
#VSB_printf;
self.VSB_printf = lib.VSB_printf
self.VSB_printf.argtypes = [c_void_p, c_char_p]
#VSB_clear;
self.VSB_clear = lib.VSB_clear
self.VSB_clear.argtypes = [c_void_p]
#VSB_finish;
self.VSB_clear = lib.VSB_clear
self.VSB_clear.argtypes = [c_void_p]
#VSB_len;
self.VSB_len = lib.VSB_len
self.VSB_len.restype = c_long
self.VSB_len.argtypes = [c_void_p]
#VSB_data;
self.VSB_data = lib.VSB_data
self.VSB_data.restype = c_char_p
self.VSB_data.argtypes = [c_void_p]
#VAS_Fail;
self.VAS_Fail = lib.VAS_Fail
self.VAS_Fail.argtypes = [c_char_p, c_char_p, c_int, c_char_p, c_int]
#VCS_Message;
self.VCS_Message = lib.VCS_Message
self.VCS_Message.argtypes = [c_char_p]
class VSLUtil:
def tag2Var(self, tag, data):
ret = {'key': '', 'val': '', 'vkey': ''}
if tag not in self.__tags:
return ret
r = self.__tags[tag]
ret['vkey'] = r.split(' ', 1)[-1].split('.', 1)[0]
if r == '':
return ret
elif r[-1:] == '.':
spl = data.split(': ', 1)
ret['key'] = r + spl[0].rstrip(': ')
ret['val'] = ''
if len(spl) > 1:
ret['val'] = spl[1]
else:
ret['key'] = r
ret['val'] = data
return (ret)
def tag2VarName(self, tag, data):
return self.tag2Var(tag, data)['key']
__tags = {
'Debug': '',
'Error': '',
'CLI': '',
'SessOpen': '',
'SessClose': '',
'BackendOpen': '', # Change key count at varnish41(4->6)
'BackendStart': '', # 4.1.3~
'BackendReuse': '',
'BackendClose': '',
'HttpGarbage': '',
'Backend': '',
'Length': '',
'FetchError': '',
'BogoHeader': '',
'LostHeader': '',
'TTL': '',
'Fetch_Body': '',
'VCL_acl': '',
'VCL_call': '',
'VCL_trace': '',
'VCL_return': '',
'ReqStart': 'client.ip',
'Hit': '',
'HitPass': '',
'ExpBan': '',
'ExpKill': '',
'WorkThread': '',
'ESI_xmlerror': '',
'Hash': '', # Change log data type(str->bin)
'Backend_health': '',
'VCL_Log': '',
'VCL_Error': '',
'Gzip': '',
'Link': '',
'Begin': '',
'End': '',
'VSL': '',
'Storage': '',
'Timestamp': '',
'ReqAcct': '',
'ESI_BodyBytes': '', # Only Varnish40X
'PipeAcct': '',
'BereqAcct': '',
'ReqMethod': 'req.method',
'ReqURL': 'req.url',
'ReqProtocol': 'req.proto',
'ReqStatus': '',
'ReqReason': '',
'ReqHeader': 'req.http.',
'ReqUnset': 'unset req.http.',
'ReqLost': '',
'RespMethod': '',
'RespURL': '',
'RespProtocol': 'resp.proto',
'RespStatus': 'resp.status',
'RespReason': 'resp.reason',
'RespHeader': 'resp.http.',
'RespUnset': 'unset resp.http.',
'RespLost': '',
'BereqMethod': 'bereq.method',
'BereqURL': 'bereq.url',
'BereqProtocol': 'bereq.proto',
'BereqStatus': '',
'BereqReason': '',
'BereqHeader': 'bereq.http.',
'BereqUnset': 'unset bereq.http.',
'BereqLost': '',
'BerespMethod': '',
'BerespURL': '',
'BerespProtocol': 'beresp.proto',
'BerespStatus': 'beresp.status',
'BerespReason': 'beresp.reason',
'BerespHeader': 'beresp.http.',
'BerespUnset': 'unset beresp.http.',
'BerespLost': '',
'ObjMethod': '',
'ObjURL': '',
'ObjProtocol': 'obj.proto',
'ObjStatus': 'obj.status',
'ObjReason': 'obj.reason',
'ObjHeader': 'obj.http.',
'ObjUnset': 'unset obj.http.',
'ObjLost': '',
'Proxy': '', # Only Varnish41x
'ProxyGarbage': '', # Only Varnish41x
'VfpAcct': '', # Only Varnish41x
'Witness': '', # Only Varnish41x
'H2RxHdr': '', # Only Varnish50x
'H2RxBody': '', # Only Varnish50x
'H2TxHdr': '', # Only Varnish50x
'H2TxBody': '', # Only Varnish50x
}
class VarnishAPI:
def __init__(self, sopath='libvarnishapi.so.1'):
self.lib = cdll[sopath]
self.lva = LIBVARNISHAPI(self.lib)
self.defi = VarnishAPIDefine40()
self._cb = None
self.vsm = self.lva.VSM_New()
self.d_opt = 0
VSLTAGS = c_char_p * 256
self.VSL_tags = []
self.VSL_tags_rev = {}
tmp = VSLTAGS.in_dll(self.lib, "VSL_tags")
for i in range(0, 255):
if tmp[i] is None:
self.VSL_tags.append(None)
else:
key = tmp[i].decode("utf8", "replace")
self.VSL_tags.append(key)
self.VSL_tags_rev[key] = i
VSLTAGFLAGS = c_uint * 256
self.VSL_tagflags = []
tmp = VSLTAGFLAGS.in_dll(self.lib, "VSL_tagflags")
for i in range(0, 255):
self.VSL_tagflags.append(tmp[i])
VSLQGROUPING = c_char_p * 4
self.VSLQ_grouping = []
tmp = VSLQGROUPING.in_dll(self.lib, "VSLQ_grouping")
for i in range(0, 3):
self.VSLQ_grouping.append(tmp[i])
self.error = ''
def VSL_TAG(self, ptr):
tag = ptr[0] >> 24
return tag
def VSL_DATA(self, ptr, isbin=False):
length = ptr[0] & 0xffff
if isbin:
data = string_at(ptr, length + 8)[8:]
else:
data = string_at(ptr, length + 8)[8:-1].decode("utf8", "replace")
return data
def ArgDefault(self, op, arg):
if op == "n":
# Set Varnish instance name.
i = self.lva.VSM_n_Arg(self.vsm, arg)
if i <= 0:
self.error = "%s" % self.lva.VSM_Error(self.vsm).rstrip()
return(i)
elif op == "N":
# Set VSM file.
i = self.lva.VSM_N_Arg(self.vsm, arg)
if i <= 0:
self.error = "%s" % self.lva.VSM_Error(self.vsm).rstrip()
return(i)
self.d_opt = 1
return(None)
class VarnishStat(VarnishAPI):
def __init__(self, opt='', sopath='libvarnishapi.so.1'):
VarnishAPI.__init__(self, sopath)
self.name = ''
if len(opt) > 0:
self.__setArg(opt)
if self.lva.VSM_Open(self.vsm):
self.error = "Can't open VSM file (%s)" % self.lva.VSM_Error(
self.vsm).rstrip()
else:
self.name = self.lva.VSM_Name(self.vsm)
def __setArg(self, opt):
opts, args = getopt.getopt(opt, "N:n:")
error = 0
for o in opts:
op = o[0].lstrip('-')
arg = o[1]
self.__Arg(op, arg.encode("utf8", "replace"))
if error:
self.error = error
return(0)
return(1)
def __Arg(self, op, arg):
# default
i = VarnishAPI.ArgDefault(self, op, arg)
if i < 0:
return(i)
def _getstat(self, priv, pt):
if not bool(pt):
return(0)
val = pt[0].ptr[0]
sec = pt[0].section
key = ''
type = sec[0].fantom[0].type.decode("utf8", "replace")
ident = sec[0].fantom[0].ident.decode("utf8", "replace")
if type != '':
key += type + '.'
if ident != '':
key += ident + '.'
key += pt[0].desc[0].name.decode("utf8", "replace")
self._buf[key] = {'val': val, 'desc': pt[0].desc[0].sdesc.decode("utf8", "replace")}
return(0)
def getStats(self):
self._buf = {}
self.lva.VSC_Iter(self.vsm, None, VSC_iter_f(self._getstat), None)
return self._buf
def Fini(self):
if self.vsm:
self.lva.VSM_Delete(self.vsm)
self.vsm = 0
class VarnishLog(VarnishAPI):
def __init__(self, opt='', sopath='libvarnishapi.so.1', dataDecode=True):
VarnishAPI.__init__(self, sopath)
self.vut = VSLUtil()
self.vsl = self.lva.VSL_New()
self.vslq = None
self.__g_arg = 0
self.__q_arg = None
self.__r_arg = 0
self.name = ''
self.dataDecode = dataDecode
if len(opt) > 0:
self.__setArg(opt)
self.__Setup()
def __setArg(self, opt):
opts, args = getopt.getopt(opt, "bcCdx:X:r:q:N:n:I:i:g:")
error = 0
for o in opts:
op = o[0].lstrip('-')
arg = o[1]
self.__Arg(op, arg.encode("utf8", "replace"))
# Check
if self.__r_arg and self.vsm:
error = "Can't have both -n and -r options"
if error:
self.error = error
return(0)
return(1)
def __Arg(self, op, arg):
i = VarnishAPI.ArgDefault(self, op, arg)
if i is not None:
return(i)
if op == "d":
# Set log cursor at the head.
self.d_opt = 1
elif op == "g":
# Specify the grouping.
self.__g_arg = self.__VSLQ_Name2Grouping(arg)
if self.__g_arg == -2:
self.error = "Ambiguous grouping type: %s" % (arg)
return(self.__g_arg)
elif self.__g_arg < 0:
self.error = "Unknown grouping type: %s" % (arg)
return(self.__g_arg)
# elif op == "P":
# Not support PID(-P) option.
elif op == "q":
# VSL-query
self.__q_arg = arg
elif op == "r":
# Read log from the binary file.
self.__r_arg = arg
else:
# default
i = self.__VSL_Arg(op, arg)
if i < 0:
self.error = "%s" % self.lva.VSL_Error(self.vsl).decode("utf8", "replace")
return(i)
def __Setup(self):
if self.__r_arg:
c = self.lva.VSL_CursorFile(self.vsl, self.__r_arg, 0)
else:
if self.lva.VSM_Open(self.vsm):
self.error = "Can't open VSM file (%s)" % self.lva.VSM_Error(
self.vsm).decode("utf8", "replace").rstrip()
return(0)
self.name = self.lva.VSM_Name(self.vsm)
if self.d_opt:
tail = self.defi.VSL_COPT_TAILSTOP
else:
tail = self.defi.VSL_COPT_TAIL
c = self.lva.VSL_CursorVSM(
self.vsl, self.vsm, tail | self.defi.VSL_COPT_BATCH)
if not c:
self.error = "Can't open log (%s)" % self.lva.VSL_Error(self.vsl).decode("utf8", "replace")
return(0)
# query
self.vslq = self.lva.VSLQ_New(self.vsl, c, self.__g_arg, self.__q_arg)
if not self.vslq:
self.error = "Query expression error:\n%s" % self.lva.VSL_Error(
self.vsl).decode("utf8", "replace")
return(0)
return(1)
def __cbMain(self, cb, priv=None):
self._cb = cb
self._priv = priv