Skip to content

Commit

Permalink
fix merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
epuzanov committed Sep 13, 2012
2 parents b8106f0 + ad77c1f commit 0d391b4
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 17 deletions.
15 changes: 12 additions & 3 deletions ZenPacks/community/SQLDataSource/SQLClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
Gets performance data over python DB-API.
$Id: SQLClient.py,v 3.11 2012/08/25 23:01:36 egor Exp $"""
$Id: SQLClient.py,v 3.10 2012/08/25 22:46:58 egor Exp $"""

__version__ = "$Revision: 3.11 $"[11:-2]
__version__ = "$Revision: 3.10 $"[11:-2]

if __name__ == "__main__":
import pysamba.twisted.reactor
import logging
log = logging.getLogger("zen.SQLClient")

Expand Down Expand Up @@ -257,7 +259,14 @@ def _connect(self, task):
self._connection.close()
self._connection = None
self._cs = task.connectionString
self._connection = adbapiClient(task.connectionString)
if task.connectionString.startswith("'pywmidb'"):
try:
from pysambaClient import pysambaClient
self._connection = pysambaClient(task.connectionString)
except ImportError:
self._connection = adbapiClient(task.connectionString)
else:
self._connection = adbapiClient(task.connectionString)
return defer.maybeDeferred(self._connection.connect)

def _connected(self, connection, task):
Expand Down
2 changes: 1 addition & 1 deletion ZenPacks/community/SQLDataSource/SQLPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ def collect(self, device, log):
cl = SQLClient(device, datacollector=self, plugins=[self])
setattr(cl, 'deferred', deferred)
cl.run()
return deferred
return deferred
12 changes: 7 additions & 5 deletions ZenPacks/community/SQLDataSource/lib/pywbemdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#***************************************************************************

__author__ = "Egor Puzanov"
__version__ = '2.2.0'
__version__ = '2.2.1'

import socket
from xml.sax import xmlreader, handler, make_parser, SAXParseException
Expand Down Expand Up @@ -218,8 +218,8 @@ def Binary(string):
# compliant with DB SIG 2.0
apilevel = '2.0'

# module and connections may be shared
threadsafety = 3
# module may be shared
threadsafety = 1

# this module use extended python format codes
paramstyle = 'qmark'
Expand Down Expand Up @@ -624,9 +624,9 @@ def __init__(self, *args, **kwargs):
else:
self._connection = httplib.HTTPConnection(**conkwargs)
if hasattr(self._connection, 'timeout'):
self._connection.timeout = float(kwargs.get('timeout', 60))
self._connection.timeout = float(kwargs.get('timeout', 120))
else:
self._timeout = float(kwargs.get('timeout', 60))
self._timeout = float(kwargs.get('timeout', 120))

def _wbem_request(self, data, parser=None):
"""Send XML data over HTTP to the specified url. Return the
Expand Down Expand Up @@ -697,6 +697,8 @@ def close(self):
if self._connection is not None:
if getattr(self._connection, 'sock', None):
self._connection.sock.shutdown(socket.SHUT_RDWR)
self._connection.sock.close()
self._connection.sock = None
if hasattr(self._connection, 'close'):
self._connection.close()
self._connection = None
Expand Down
12 changes: 7 additions & 5 deletions ZenPacks/community/SQLDataSource/lib/pywsmandb.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#***************************************************************************

__author__ = "Egor Puzanov"
__version__ = '2.2.0'
__version__ = '2.2.1'

import socket
from xml.sax import xmlreader, handler, make_parser, SAXParseException
Expand Down Expand Up @@ -249,8 +249,8 @@ def Binary(string):
# compliant with DB SIG 2.0
apilevel = '2.0'

# module and connections may be shared
threadsafety = 3
# module may be shared
threadsafety = 1

# this module use extended python format codes
paramstyle = 'qmark'
Expand Down Expand Up @@ -727,9 +727,9 @@ def __init__(self, *args, **kwargs):
else:
self._connection = httplib.HTTPConnection(**conkwargs)
if hasattr(self._connection, 'timeout'):
self._connection.timeout = float(kwargs.get('timeout', 60))
self._connection.timeout = float(kwargs.get('timeout', 120))
else:
self._timeout = float(kwargs.get('timeout', 60))
self._timeout = float(kwargs.get('timeout', 120))
self._wsm_vendor = ''
self._fltr={'WQL':WQL_FILTER_TMPL,
'CQL':CQL_FILTER_TMPL,
Expand Down Expand Up @@ -822,6 +822,8 @@ def close(self):
if self._connection is not None:
if getattr(self._connection, 'sock', None):
self._connection.sock.shutdown(socket.SHUT_RDWR)
self._connection.sock.close()
self._connection.sock = None
if hasattr(self._connection, 'close'):
self._connection.close()
self._connection = None
Expand Down
224 changes: 224 additions & 0 deletions ZenPacks/community/SQLDataSource/pysambaClient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
################################################################################
#
# This program is part of the SQLDataSource Zenpack for Zenoss.
# Copyright (C) 2012 Egor Puzanov.
#
# This program can be used under the GNU General Public License version 2
# You can find full information here: http://www.zenoss.com/oss
#
################################################################################

__doc__="""pysambaClient
Gets performance data over WMI.
$Id: pysambaClient.py,v1.1 2012/08/08 19:15:52 egor Exp $"""

__version__ = "$Revision: 1.1 $"[11:-2]

from pysamba.library import *
from pysamba.wbem.wbem import *
from pysamba.wbem.Query import Query, QueryResult
from pysamba.twisted.callback import Callback, WMIFailure
from pysamba.twisted.reactor import eventContext

from twisted.internet import defer

import logging
log = logging.getLogger("zen.pysambaClient")

from datetime import datetime, timedelta
import re
DTPAT = re.compile(r'^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})\.(\d{6})([+|-]\d{3})')
TDPAT = re.compile(r'^(\d{8})(\d{2})(\d{2})(\d{2})\.(\d{6})')

if not getattr(WbemQualifier, "_fields_", None):
WbemQualifier._fields_ = [
('name', CIMSTRING),
('flavors', uint8_t),
('cimtype', uint32_t),
('value', CIMVAR),
]

def convertArray(arr):
if not arr: return None
return [arr.contents.item[i] for i in range(arr.contents.count)]

def convert(v, typeval):
if typeval == CIM_SINT8: return v.v_sint8
if typeval == CIM_UINT8: return v.v_uint8
if typeval == CIM_SINT16: return v.v_sint16
if typeval == CIM_UINT16: return v.v_uint16
if typeval == CIM_SINT32: return v.v_sint32
if typeval == CIM_UINT32: return v.v_uint32
if typeval == CIM_SINT64: return v.v_sint64
if typeval == CIM_UINT64: return v.v_sint64
if typeval == CIM_REAL32: return float(v.v_uint32)
if typeval == CIM_REAL64: return float(v.v_uint64)
if typeval == CIM_BOOLEAN: return bool(v.v_boolean)
if typeval == CIM_DATETIME:
dt = str(v.v_datetime)
s = DTPAT.match(dt)
if s is not None:
tt = map(int, s.groups(0))
return datetime(*tt[:7]) #+ timedelta(minutes=tt[7])
s = TDPAT.match(dt)
if s is None: return str(dt)
try: return timedelta(**dict(zip(
('days','hours','minutes','microseconds'),map(int, s.groups(0)))))
except Exception: return v.v_string or ""
if typeval == CIM_STRING:
return v.v_string or ""
if typeval == CIM_REFERENCE:
if not v.v_string.startswith(r'\\'): return v.v_string
return v.v_string.split(':', 1)[-1]
if typeval == CIM_CHAR16:
return v.v_string.decode('utf16')
if typeval == CIM_OBJECT:
return v.v_string or ""
if typeval == CIM_ARR_SINT8: return convertArray(v.a_sint8)
if typeval == CIM_ARR_UINT8: return convertArray(v.a_uint8)
if typeval == CIM_ARR_SINT16: return convertArray(v.a_sint16)
if typeval == CIM_ARR_UINT16: return convertArray(v.a_uint16)
if typeval == CIM_ARR_SINT32: return convertArray(v.a_sint32)
if typeval == CIM_ARR_UINT32: return convertArray(v.a_uint32)
if typeval == CIM_ARR_SINT64: return convertArray(v.a_sint64)
if typeval == CIM_ARR_UINT64: return convertArray(v.a_uint64)
if typeval == CIM_ARR_REAL32: return convertArray(v.a_real32)
if typeval == CIM_ARR_REAL64: return convertArray(v.a_real64)
if typeval == CIM_ARR_BOOLEAN: return convertArray(v.a_boolean)
if typeval == CIM_ARR_STRING: return convertArray(v.a_string)
if typeval == CIM_ARR_DATETIME:
return convertArray(v.contents.a_datetime)
if typeval == CIM_ARR_REFERENCE:
return convertArray(v.contents.a_reference)
return "Unsupported"

def wbemInstanceToPython(obj, qualifiers=False):
klass = obj.contents.obj_class.contents
inst = obj.contents.instance.contents
result = {}
kb = []
result['__class'] = klass.__CLASS
result['__namespace'] = obj.contents.__NAMESPACE.replace("\\", '/')
for j in range(klass.__PROPERTY_COUNT):
prop = klass.properties[j]
value = convert(inst.data[j], prop.desc.contents.cimtype & CIM_TYPEMASK)
if qualifiers:
for qi in range(prop.desc.contents.qualifiers.count):
q = prop.desc.contents.qualifiers.item[qi].contents
if q.name in ['key'] and convert(q.value, q.cimtype) == True:
kb.append("%s=%s"%(prop.name,
type(value) is str and '"%s"'%value or value))
if prop.name:
result[prop.name.lower()] = value
if qualifiers:
result['__path'] = "%s.%s"%(klass.__CLASS, ",".join(kb))
return result

def fetchSome(obj, timeoutMs=-1, chunkSize=10, qualifiers=False):

assert obj.pEnum

ctx = library.IEnumWbemClassObject_SmartNext_send(
obj.pEnum, None, timeoutMs, chunkSize
)

cback = Callback()
ctx.contents.async.fn = cback.callback

def fetch(results):
count = uint32_t()
objs = (POINTER(WbemClassObject)*chunkSize)()
result = library.IEnumWbemClassObject_SmartNext_recv(
ctx, obj.ctx, objs, byref(count)
)

WERR_CHECK(result, obj._deviceId, "Retrieve result data.")

result = []
for i in range(count.value):
result.append(wbemInstanceToPython(objs[i], qualifiers))
library.talloc_free(objs[i])
return result

d = cback.deferred
d.addCallback(fetch)
return d

def parseConnectionString(cs='', options={}):
try: args, kwargs = eval('(lambda *args,**kwargs:(args,kwargs))(%s)'%cs)
except:
args = []
kwargs = {}
for arg in cs.split(','):
try:
if arg.strip().startswith("'"):
arg = arg.strip("' ")
raise
var, val = arg.strip().split('=', 1)
if val.startswith('\'') or val.startswith('"'):
kwargs[var.strip()] = val.strip('\'" ')
elif val.lower() == 'true':
kwargs[var.strip()] = True
elif val.lower() == 'false':
kwargs[var.strip()] = False
else:
kwargs[var.strip()] = float(val.strip())
except: args.append(arg)
kwargs.update(options)
return args, kwargs

class pysambaClient(object):

def __init__(self, cs):
"""
@type cs: string
@param cs: connection string
"""
self.cs = cs
self._wmi = None
self.ready = None

def connect(self):
args, kwargs = parseConnectionString(self.cs)
lkwargs = dict((k.lower(), v) for k,v in kwargs.iteritems())
host = lkwargs.get("host") or ""
user = lkwargs.get("user") or ""
namespace = lkwargs.get("namespace") or "root\\cimv2"
log.debug("connect to %s, user %r", host, user)
if not user:
log.warning("Windows login name is unset: "
"please specify zWinUser and "
"zWinPassword zProperties before adding devices.")
return defer.fail("Username is empty")
self._wmi = Query()
creds = '%s%%%s' % (user, lkwargs.get("password", ""))
return self._wmi.connect(eventContext, host, host, creds, namespace)

def close(self):
if self._wmi:
self._wmi.close()
self._wmi.pWS = None
self._wmi = None
self.ready = None

def _fetchResults(self, results, timeout, qualifiers, result=None, rows=[]):
if not results:
return defer.succeed(rows)
elif isinstance(results, QueryResult):
rows = []
result = results
else:
rows.extend(results)
d = fetchSome(result, timeoutMs=timeout, qualifiers=qualifiers)
d.addCallback(self._fetchResults, timeout, qualifiers, result, rows)
return d

def query(self, task):
timeout = task.timeout * 1000
qualifiers = task.ds == ''
rows = []
d = self._wmi.query(task.sqlp)
d.addCallback(self._fetchResults, timeout, qualifiers, rows)
return d
5 changes: 3 additions & 2 deletions ZenPacks/community/SQLDataSource/zenperfsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
Run SQL Queries periodically and stores it results in RRD files.
$Id: zenperfsql.py,v 3.9 2012/08/25 23:03:05 egor Exp $"""
$Id: zenperfsql.py,v 3.8 2012/08/16 23:21:42 egor Exp $"""

__version__ = "$Revision: 3.9 $"[11:-2]
__version__ = "$Revision: 3.8 $"[11:-2]

import pysamba.twisted.reactor
import time
from datetime import datetime, timedelta
import logging
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# or saved. Do not modify them directly here.
# NB: PACKAGES is deprecated
NAME = "ZenPacks.community.SQLDataSource"
VERSION = "2.2.8"
VERSION = "2.3"
AUTHOR = "Egor Puzanov"
LICENSE = ""
NAMESPACE_PACKAGES = ['ZenPacks', 'ZenPacks.community']
Expand Down

0 comments on commit 0d391b4

Please sign in to comment.