Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 84 additions & 55 deletions src/varnishapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,6 @@



class VUT (Structure):
_fields_ = [
("magic" , c_uint), #unsigned magic;
("progname" , c_char_p), #const char *progname;
("d_opt" , c_int), #int d_opt;
("D_opt" , c_int), #int D_opt;
("g_arg" , c_int), #int g_arg;
("k_arg" , c_int), #int k_arg;
("n_arg" , c_char_p), #char *n_arg;
("P_arg" , c_char_p), #char *P_arg;
("q_arg" , c_char_p), #char *q_arg;
("r_arg" , c_char_p), #char *r_arg;
("t_arg" , c_char_p), #char *t_arg;
("vsl" , c_void_p), #struct VSL_data *vsl;
("vsm" , c_void_p), #struct vsm *vsm;
("vslq" , c_void_p), #struct VSLQ *vslq;
("sighup" , c_int), #int sighup;
("sigint" , c_int), #int sigint;
("sigusr1" , c_int), #int sigusr1;
("idle_f" , c_void_p), #VUT_cb_f *idle_f;
("sighup_f" , c_void_p), #VUT_cb_f *sighup_f;
("error_f" , c_void_p), #VUT_error_f *error_f;
("dispatch_f" , c_void_p), #VSLQ_dispatch_f *dispatch_f;
("dispatch_priv" , c_void_p) #void *dispatch_priv;
]

class VSC_level_desc(Structure):
_fields_ = [
("verbosity", c_uint), # unsigned verbosity;
Expand Down Expand Up @@ -382,8 +356,35 @@ def __init__(self):
# typedef int VUT_cb_f(struct VUT *);
VUT_cb_f = CFUNCTYPE(
c_int,
POINTER(VUT)
c_void_p #POINTER(VUT)
)

class VUT (Structure):
_fields_ = [
("magic" , c_uint), #unsigned magic;
("progname" , c_char_p), #const char *progname;
("d_opt" , c_int), #int d_opt;
("D_opt" , c_int), #int D_opt;
("g_arg" , c_int), #int g_arg;
("k_arg" , c_int), #int k_arg;
("n_arg" , c_char_p), #char *n_arg;
("P_arg" , c_char_p), #char *P_arg;
("q_arg" , c_char_p), #char *q_arg;
("r_arg" , c_char_p), #char *r_arg;
("t_arg" , c_char_p), #char *t_arg;
("vsl" , c_void_p), #struct VSL_data *vsl;
("vsm" , c_void_p), #struct vsm *vsm;
("vslq" , c_void_p), #struct VSLQ *vslq;
("sighup" , c_int), #int sighup;
("sigint" , c_int), #int sigint;
("sigusr1" , c_int), #int sigusr1;
("idle_f" , VUT_cb_f), #VUT_cb_f *idle_f;
("sighup_f" , VUT_cb_f), #VUT_cb_f *sighup_f;
("error_f" , c_void_p), #VUT_error_f *error_f;
("dispatch_f" , VSLQ_dispatch_f), #VSLQ_dispatch_f *dispatch_f;
("dispatch_priv" , c_void_p) #void *dispatch_priv;
]

class LIBVARNISHAPI10:
def __init__(self, lc):
self.lc = lc
Expand Down Expand Up @@ -1310,9 +1311,6 @@ 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 = []
Expand Down Expand Up @@ -1352,6 +1350,13 @@ def VSL_DATA(self, ptr, isbin=False):
data = string_at(ptr, length + 8)[8:-1].decode("utf8", "replace")
return data

class VarnishVSM(VarnishAPI):

def __init__(self, sopath='libvarnishapi.so.1'):
VarnishAPI.__init__(self, sopath)
self.vsm = self.lva.VSM_New()
self.d_opt = 0

def ArgDefault(self, op, arg):
if self.lva.apiversion >= 2.0:
if op == "n":
Expand Down Expand Up @@ -1387,10 +1392,43 @@ def Fini(self):
self.vsm = 0


class VarnishStat(VarnishAPI):
class VarnishVUT(VarnishAPI):
def __init__(self,
argc='VarnishVUTproc',
opt='',
sopath='libvarnishapi.so.1'):
VarnishAPI.__init__(self, sopath)
self.vopt_spec = vopt_spec()
self.vut = self.lva.VUT_Init(argc, 0, byref(cast('',c_char_p)), self.vopt_spec)
if len(opt) > 0:
self.__setArg(opt)
self.lva.VUT_Setup(self.vut)
self.vut[0].dispatch_f = VSLQ_dispatch_f(self._callBack)

def stop(self):
self.vut[0].sigint = 1

def run(self):
self.lva.VUT_Main(self.vut)
self.lva.VUT_Fini(self.vut)

def __setArg(self, opt):
# XXX args from vopt_spec
opts, args = getopt.getopt(opt, "bcCdx:X:r:q:N:n:I:i:g:k:t:T:")
error = 0
for o in opts:
op = o[0].lstrip('-')
arg = o[1].encode("utf8", "replace")
self.lva.VUT_Arg(self.vut, ord(op[0]), arg)

def _callBack(self, vsl, pt, fo):
# not implemented
return(0)

class VarnishStat(VarnishVSM):

def __init__(self, opt='', sopath='libvarnishapi.so.1'):
VarnishAPI.__init__(self, sopath)
VarnishVSM.__init__(self, sopath)
self.name = ''
if len(opt) > 0:
self.__setArg(opt)
Expand All @@ -1404,7 +1442,7 @@ def __init__(self, opt='', sopath='libvarnishapi.so.1'):
def Fini(self):
if self.lva.apiversion >= 2.0:
self.lva.VSC_Destroy(byref(cast(self.vsc, c_void_p)), self.vsm)
VarnishAPI.Fini(self)
VarnishVSM.Fini(self)

def __Setup20(self):
if self.lva.VSM_Attach(self.vsm, 2):
Expand Down Expand Up @@ -1434,7 +1472,7 @@ def __setArg(self, opt):

def __Arg(self, op, arg):
# default
i = VarnishAPI.ArgDefault(self, op, arg)
i = VarnishVSM.ArgDefault(self, op, arg)
if i is not None:
return(i)

Expand Down Expand Up @@ -1476,10 +1514,10 @@ def getStats(self):
return self._buf


class VarnishLog(VarnishAPI):
class VarnishLog(VarnishVSM):

def __init__(self, opt='', sopath='libvarnishapi.so.1', dataDecode=True):
VarnishAPI.__init__(self, sopath)
VarnishVSM.__init__(self, sopath)

self.vut = VSLUtil()
self.vsl = self.lva.VSL_New()
Expand All @@ -1489,9 +1527,14 @@ def __init__(self, opt='', sopath='libvarnishapi.so.1', dataDecode=True):
self.__r_arg = 0
self.name = ''
self.dataDecode = dataDecode
self._cb = None

if len(opt) > 0:
self.__setArg(opt)
if self.d_opt:
self.cursor_opt = self.defi.VSL_COPT_TAILSTOP | self.defi.VSL_COPT_BATCH
else:
self.cursor_opt = self.defi.VSL_COPT_TAIL | self.defi.VSL_COPT_BATCH

if self.lva.apiversion >= 2.0:
self.__Setup20()
Expand All @@ -1517,7 +1560,7 @@ def __setArg(self, opt):
return(1)

def __Arg(self, op, arg):
i = VarnishAPI.ArgDefault(self, op, arg)
i = VarnishVSM.ArgDefault(self, op, arg)
if i is not None:
return(i)

Expand Down Expand Up @@ -1568,13 +1611,7 @@ def __Setup20(self):
return(0)
#self.name = self.lva.VSM_Name(self.vsm)

if self.d_opt:
self.cursor_opt = self.defi.VSL_COPT_TAILSTOP | self.defi.VSL_COPT_BATCH
else:
self.cursor_opt = self.defi.VSL_COPT_TAIL | self.defi.VSL_COPT_BATCH

c = self.lva.VSL_CursorVSM(
self.vsl, self.vsm, self.cursor_opt)
c = self.lva.VSL_CursorVSM(self.vsl, self.vsm, self.cursor_opt)

self.lva.VSLQ_SetCursor(self.vslq, byref(cast(c, c_void_p)))
self.lva.VSL_ResetError(self.vsl)
Expand All @@ -1595,13 +1632,7 @@ def __Setup10(self):
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)
c = self.lva.VSL_CursorVSM(self.vsl, self.vsm, self.cursor_opt)

if not c:
self.error = "Can't open log (%s)" % self.lva.VSL_Error(self.vsl).decode("utf8", "replace")
Expand All @@ -1624,9 +1655,7 @@ def __Dispatch10(self, maxread):
if self.lva.VSM_Open(self.vsm):
self.lva.VSM_ResetError(self.vsm)
return(1)
c = self.lva.VSL_CursorVSM(
self.vsl, self.vsm,
self.defi.VSL_COPT_TAIL | self.defi.VSL_COPT_BATCH)
c = self.lva.VSL_CursorVSM(self.vsl, self.vsm, self.cursor_opt)
if not c:
self.lva.VSM_ResetError(self.vsm)
self.lva.VSM_Close(self.vsm)
Expand Down Expand Up @@ -1716,7 +1745,7 @@ def Fini(self):
if self.vsl:
self.lva.VSL_Delete(self.vsl)
self.vsl = 0
VarnishAPI.Fini(self)
VarnishVSM.Fini(self)

def __VSL_Arg(self, opt, arg='\0'):
return self.lva.VSL_Arg(self.vsl, ord(opt), arg)
Expand Down