Skip to content

Commit 9e5121a

Browse files
committed
update sample
1 parent 574c558 commit 9e5121a

1 file changed

Lines changed: 30 additions & 6 deletions

File tree

sample/sample_varnishlog.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,57 @@
44
import time,os,sys,syslog,traceback,varnishapi
55

66
class SampleVarnishLog:
7-
def execute(self,vap):
7+
def execute(self, vap):
88
#connect varnishapi
99
self.vap = vap
10+
self.headline = None
11+
self.buf = ""
12+
self.tnames = {
13+
vap.defi.VSL_t_unknown : 'unknown',
14+
vap.defi.VSL_t_sess : 'sess',
15+
vap.defi.VSL_t_req : 'req',
16+
vap.defi.VSL_t_bereq : 'bereq',
17+
vap.defi.VSL_t_raw : 'raw',
18+
}
1019
while 1:
11-
ret = self.vap.Dispatch(self.vapCallBack)
20+
ret = self.vap.Dispatch(self.vapLineCallBack,None,0,self.vapVxidCallBack,self.vapGroupCallBack)
1221
if 0 >= ret:
1322
time.sleep(0.5)
1423

1524

16-
def vapCallBack(self,vap,cbd,priv):
25+
def vapGroupCallBack(self,vap, priv):
26+
print("-"*100)
27+
def vapVxidCallBack(self,vap, priv):
28+
trx_type = self.headline['transaction_type']
29+
vxid = self.headline['vxid']
30+
level = self.headline['level']
31+
print("\n%s << %s >> %d" % ('*'*level,self.tnames[trx_type],vxid))
32+
print(self.buf.rstrip("\n"))
33+
self.headline = None
34+
self.buf = ""
35+
36+
def vapLineCallBack(self, vap, cbd, priv):
1737
level = cbd['level']
1838
vxid = cbd['vxid']
1939
vxid_parent = cbd['vxid_parent']
2040
type = cbd['type']
41+
trx_type = cbd['transaction_type']
2142
tag = cbd['tag']
2243
data = cbd['data']
2344
isbin = cbd['isbin']
2445
length = cbd['length']
2546
t_tag = vap.VSL_tags[tag]
2647
var = vap.vut.tag2VarName(t_tag,data)
27-
28-
print("level:%d vxid:%d vxid_parent:%d tag:%s var:%s type:%s data:%s (isbin=%d,len=%d)" % (level,vxid,vxid_parent,t_tag,var,type,data,isbin,length))
48+
if self.headline is None:
49+
self.headline = cbd
50+
51+
self.buf +="%s level:%d vxid:%d vxid_parent:%d tag:%s var:%s type:%s data:%s (isbin=%d,len=%d)\n" % ('-'*level,level,vxid,vxid_parent,t_tag,var,type,data,isbin,length)
52+
2953

3054
def main(smp):
3155
try:
3256
arg = {}
33-
arg["opt"] = ['-g','raw']
57+
arg["opt"] = ['-g','session']
3458
vap = varnishapi.VarnishLog(**arg)
3559
if vap.error:
3660
print(vap.error)

0 commit comments

Comments
 (0)