Skip to content

Commit

Permalink
fix SQLClient, add support for PostgreSQL to pyisqldb
Browse files Browse the repository at this point in the history
  • Loading branch information
epuzanov committed Oct 27, 2011
1 parent 4ad2c38 commit e6696ef
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 31 deletions.
27 changes: 15 additions & 12 deletions ZenPacks/community/SQLDataSource/SQLClient.py
Expand Up @@ -12,9 +12,9 @@
Gets performance data over python DB API.
$Id: SQLClient.py,v 2.9 2011/10/26 16:09:22 egor Exp $"""
$Id: SQLClient.py,v 2.10 2011/10/27 16:54:14 egor Exp $"""

__version__ = "$Revision: 2.9 $"[11:-2]
__version__ = "$Revision: 2.10 $"[11:-2]

import Globals
from Products.ZenUtils.Utils import zenPath
Expand Down Expand Up @@ -56,11 +56,11 @@ def _filename(device):

class Query(object):

def __init__(self, sqlp, client):
def __init__(self, sqlp, results):
self.sql = ''
self.sqlp = sqlp
self.resMaps = {}
self.results = client.results
self.results = results


def add(self, pname, tname, task):
Expand Down Expand Up @@ -238,7 +238,7 @@ def __init__(self, device=None, datacollector=None, plugins=[]):
self.device = device
self.datacollector = datacollector
self.plugins = plugins
self.results = {}
self.results = []


def __del__(self):
Expand All @@ -247,11 +247,12 @@ def __del__(self):

def close(self):
del self.plugins[:]
self.results.clear()
del self.results[:]


def query(self, tasks={}, sync=False):
queue = []
results = {}
for tname, task in tasks.iteritems():
if type(tname) is tuple: pname, tname = tname
else: pname = None
Expand All @@ -263,23 +264,25 @@ def query(self, tasks={}, sync=False):
if sync: queue.append(syncPool(task[2]))
elif 'pywmidb' in task[2]: queue.append(wmiPool(task[2]))
else: queue.append(Pool(task[2]))
queue[poolid].add(pname, tname, task, self)
self.results.setdefault(pname, {})[tname] = []
results.setdefault(pname, {})[tname] = []
queue[poolid].add(pname, tname, task, results)
if sync:
for pool in queue:
pool.run()
return self.results.get(None, {})
return results.pop(None, results)
def inner(driver):
for pool in queue:
yield pool.run()
driver.next()
yield defer.succeed(self.results)
yield defer.succeed(results.pop(None, results))
driver.next()
return drive(inner)


def run(self):
def finish(result):
def finish(results):
for pl in self.plugins:
self.results.append((pl, results.pop(pl.name(), {})))
if self.datacollector:
self.datacollector.clientFinished(self)
else:
Expand All @@ -297,7 +300,7 @@ def finish(result):
def getResults(self):
"""Return data for this client
"""
return [(pl, self.results.get(pl.name(), {})) for pl in self.plugins]
return self.results



Expand Down
9 changes: 4 additions & 5 deletions ZenPacks/community/SQLDataSource/WMIQuery.py
Expand Up @@ -12,9 +12,9 @@
Gets WMI performance data.
$Id: WMIQuery.py,v 1.1 2011/10/26 16:10:15 egor Exp $"""
$Id: WMIQuery.py,v 1.2 2011/10/27 16:54:34 egor Exp $"""

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

from pysamba.twisted.callback import WMIFailure
from pysamba.wbem.Query import Query
Expand All @@ -34,11 +34,11 @@ class BadCredentials(Exception): pass

class wmiQuery(object):

def __init__(self, sqlp, client):
def __init__(self, sqlp, results):
self.sql = ''
self.sqlp = sqlp
self.resMaps = {}
self.results = client.results
self.results = results


def add(self, pname, tname, task):
Expand Down Expand Up @@ -69,7 +69,6 @@ def parseError(self, err):
def parseResult(self, instances):
for instance in instances:
for kbKey, kbVal in self.resMaps.iteritems():
cNames=set([k.upper() for k in kbVal.values()[0][0][1].keys()])
kIdx = []
for kb in kbKey:
kbV = getattr(instance, kb, '')
Expand Down
10 changes: 5 additions & 5 deletions ZenPacks/community/SQLDataSource/lib/pyisqldb.py
Expand Up @@ -20,7 +20,7 @@
#***************************************************************************

__author__ = "Egor Puzanov"
__version__ = '1.0.3'
__version__ = '1.0.4'

from string import upper, strip
import datetime
Expand Down Expand Up @@ -297,16 +297,16 @@ def __init__(self, *args, **kwargs):
isqlcmd = 'iusql'
kwargs.update(dict(map(strip, i.split('=')) for i in (
args and args[0] or kwargs.pop('cs', '')).split(';') if '=' in i))
kwargs['DRIVER'] = kwargs.get('DRIVER', 'None').strip('{}')
for k in kwargs.keys():
ku = k.upper()
if ku in ('UID', 'USER'): uid = kwargs.pop(k)
elif ku in ('PWD', 'PASSWORD'): pwd = kwargs.pop(k)
elif ku in ('DSN', 'FILEDSN'): dsn = kwargs.pop(k)
elif ku in ('HOST', 'SERVER'): kwargs['servername'] = kwargs[k]
elif ku in ('SERVER', 'HOST') and kwargs['DRIVER'] == 'PostgreSQL':
kwargs['servername'] = kwargs.pop(k)
elif ku == 'ANSI':
ansi = kwargs.pop(k)
if ansi.upper() == 'TRUE': isqlcmd = 'isql'
kwargs['DRIVER'] = kwargs.get('DRIVER', 'None').strip('{}')
isqlcmd=str(kwargs.pop(k)).upper()=='TRUE' and 'isql' or 'iusql'
if not dsn:
import md5
newcs = ';'.join(('%s = %s' %o for o in kwargs.iteritems()))
Expand Down
14 changes: 5 additions & 9 deletions ZenPacks/community/SQLDataSource/zenperfsql.py
Expand Up @@ -12,9 +12,9 @@
PB daemon-izable base class for creating sql collectors
$Id: zenperfsql.py,v 2.6 2011/10/26 16:01:05 egor Exp $"""
$Id: zenperfsql.py,v 2.7 2011/10/27 16:55:04 egor Exp $"""

__version__ = "$Revision: 2.6 $"[11:-2]
__version__ = "$Revision: 2.7 $"[11:-2]

import logging
import pysamba.twisted.reactor
Expand All @@ -41,7 +41,7 @@
TaskStates
from Products.ZenEvents.ZenEventClasses import Error, Clear
from Products.ZenUtils.observable import ObservableMixin
from SQLClient import SQLClient, SQLPlugin
from SQLClient import SQLClient

# We retrieve our configuration data remotely via a Twisted PerspectiveBroker
# connection. To do so, we need to import the class that will be used by the
Expand Down Expand Up @@ -314,9 +314,6 @@ def _collectSuccessful(self, results={}):
"""
self.state = ZenPerfSqlTask.STATE_SQLC_PROCESS

try: results = self._sqlc.getResults()[0][1]
except: pass

log.debug("Successful collection from %s [%s], results=%s",
self._devId, self._manageIp, results)
if not results: return None
Expand Down Expand Up @@ -387,9 +384,8 @@ def doTask(self):

self.state = ZenPerfSqlTask.STATE_SQLC_QUERY

self._sqlc = SQLClient( self._taskConfig, self,
[SQLPlugin(self._taskConfig.queries.copy())])
d = self._sqlc.run()
self._sqlc = SQLClient()
d = self._sqlc.query(self._taskConfig.queries.copy())
d.addCallbacks(self._collectSuccessful, self._failure)

# returning a Deferred will keep the framework from assuming the task
Expand Down

0 comments on commit e6696ef

Please sign in to comment.