|
4 | 4 | import time,os,sys,syslog,traceback,varnishapi |
5 | 5 |
|
6 | 6 | class SampleVarnishLog: |
7 | | - def execute(self,vap): |
| 7 | + def execute(self, vap): |
8 | 8 | #connect varnishapi |
9 | 9 | 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 | + } |
10 | 19 | while 1: |
11 | | - ret = self.vap.Dispatch(self.vapCallBack) |
| 20 | + ret = self.vap.Dispatch(self.vapLineCallBack,None,0,self.vapVxidCallBack,self.vapGroupCallBack) |
12 | 21 | if 0 >= ret: |
13 | 22 | time.sleep(0.5) |
14 | 23 |
|
15 | 24 |
|
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): |
17 | 37 | level = cbd['level'] |
18 | 38 | vxid = cbd['vxid'] |
19 | 39 | vxid_parent = cbd['vxid_parent'] |
20 | 40 | type = cbd['type'] |
| 41 | + trx_type = cbd['transaction_type'] |
21 | 42 | tag = cbd['tag'] |
22 | 43 | data = cbd['data'] |
23 | 44 | isbin = cbd['isbin'] |
24 | 45 | length = cbd['length'] |
25 | 46 | t_tag = vap.VSL_tags[tag] |
26 | 47 | 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 | + |
29 | 53 |
|
30 | 54 | def main(smp): |
31 | 55 | try: |
32 | 56 | arg = {} |
33 | | - arg["opt"] = ['-g','raw'] |
| 57 | + arg["opt"] = ['-g','session'] |
34 | 58 | vap = varnishapi.VarnishLog(**arg) |
35 | 59 | if vap.error: |
36 | 60 | print(vap.error) |
|
0 commit comments