Esempio n. 1
0
    def inner(driver):
        try:
            q = Query()

            yield q.connect(eventContext, 1, hostname, creds)
            driver.next()
            log.info("Query sent")
            yield q.query(query)
            result = driver.next()
            class_name = ''
            while 1:
                yield result.fetchSome()
                if not driver.next(): break
                for obj in driver.next():
                    props = [p for p in obj.__dict__.keys()
                             if not p.startswith('_')]
                    if obj._class_name != class_name:
                        class_name = obj._class_name
                        print obj._class_name
                        print repr(props)
                    d = {}
                    for p in props:
                        d[p]=getattr(obj,p)
                    print "-------------"
                    pprint.pprint(d)
                    #print repr([getattr(obj, p) for p in props])
            q.close()
        except Exception, ex:
            log.exception(ex)
Esempio n. 2
0
 def inner(driver):
     try:
         q = Query()
         yield q.connect(eventContext, hostname, creds)
         driver.next()
         yield q.notificationQuery(wql)
         result = driver.next()
         log.info("Query sent")
         while 1:
             printSize()
             try:
                 class_name = ''
                 while 1:
                     yield result.fetchSome(500)
                     if not driver.next(): break
                     log.info("Got %d items", len(driver.next()))
                     for obj in driver.next():
                         obj = obj.targetinstance
                         props = [p for p in obj.__dict__.keys()
                                  if not p.startswith('_')]
                         if obj._class_name != class_name:
                             class_name = obj._class_name
                             print obj._class_name
                             print repr(props)
                         print repr([getattr(obj, p) for p in props])
             except WError, ex:
                 log.exception(ex)
                 yield async_sleep(1)
                 driver.next()
         q.close()
Esempio n. 3
0
 def __init__(self, device, query):
     self.wmi = Query()
     self.device = device
     self.queryString = query
     self.enum = None
     self.busy = False
     self.closeRequested = False
     log.debug("Starting watcher on %s", device.id)
Esempio n. 4
0
 def connect(self):
     log.debug("connect to %s, user %r", self.host, self.user)
     if not self.user:
         log.warning("Windows login name is unset: "
                     "please specify zWinUser and "
                     "zWinPassword zProperties before adding devices.")
         raise BadCredentials("Username is empty")
     self._wmi = Query()
     creds = '%s%%%s' % (self.user, self.passwd)
     return self._wmi.connect(eventContext, self.device.id, self.host,
                              creds)
 def connect(self):
     from pysamba.twisted.reactor import eventContext
     args, kwargs = self.parseCS(self.cs)
     host = kwargs.get('host', 'localhost')
     user = kwargs.get('user', '')
     if not user:
         log.warning("Windows login name is unset: "
                     "please specify zWinUser and "
                     "zWinPassword zProperties before adding devices.")
         raise BadCredentials("Username is empty")
     password = kwargs.get('password', '')
     creds = '%s%%%s' % (user, password)
     namespace = kwargs.get('namespace', 'root/cimv2')
     log.debug("connect to %s, user %s", host, user)
     self.pool = Query()
     return self.pool.connect(eventContext, host, host, creds, namespace)
class wmiPool(asyncPool):

    poolType = 'pysamba'

    def add(self, pname, tname, task, results, host):
        sqlp = task[0]
        for query in self.queries:
            if query.sqlp != sqlp: continue
            query.add(pname, tname, task)
            return
        self.queries.append(wmiQuery(task[0], results, host))
        self.queries[-1].add(pname, tname, task)

    def connect(self):
        from pysamba.twisted.reactor import eventContext
        args, kwargs = self.parseCS(self.cs)
        host = kwargs.get('host', 'localhost')
        user = kwargs.get('user', '')
        if not user:
            log.warning("Windows login name is unset: "
                        "please specify zWinUser and "
                        "zWinPassword zProperties before adding devices.")
            raise BadCredentials("Username is empty")
        password = kwargs.get('password', '')
        creds = '%s%%%s' % (user, password)
        namespace = kwargs.get('namespace', 'root/cimv2')
        log.debug("connect to %s, user %s", host, user)
        self.pool = Query()
        return self.pool.connect(eventContext, host, host, creds, namespace)
class wmiPool(asyncPool):

    poolType = 'pysamba'

    def add(self, pname, tname, task, results, host):
        sqlp = task[0]
        for query in self.queries:
            if query.sqlp != sqlp: continue
            query.add(pname, tname, task)
            return
        self.queries.append(wmiQuery(task[0], results, host))
        self.queries[-1].add(pname, tname, task)

    def connect(self):
        from pysamba.twisted.reactor import eventContext
        args, kwargs = self.parseCS(self.cs)
        host = kwargs.get('host', 'localhost')
        user = kwargs.get('user', '')
        if not user:
            log.warning("Windows login name is unset: "
                        "please specify zWinUser and "
                        "zWinPassword zProperties before adding devices.")
            raise BadCredentials("Username is empty")
        password = kwargs.get('password', '')
        creds = '%s%%%s'%(user, password)
        namespace = kwargs.get('namespace', 'root/cimv2')
        log.debug("connect to %s, user %s", host, user)
        self.pool = Query()
        return self.pool.connect(eventContext, host, host, creds, namespace)
 def connect(self):
     log.debug("connect to %s, user %r", self.host, self.user)
     if not self.user:
         log.warning("Windows login name is unset: "
                     "please specify zWinUser and "
                     "zWinPassword zProperties before adding devices.")
         raise BadCredentials("Username is empty")
     self._wmi = Query()
     creds = '%s%%%s' % (self.user, self.passwd)
     return self._wmi.connect(eventContext, self.device.id, self.host, creds)
Esempio n. 9
0
 def inner(driver):
     try:
         q = Query()
         yield q.connect(eventContext, hostname, creds)
         driver.next()
         yield q.notificationQuery(wql)
         result = driver.next()
         log.info("Query sent")
         while 1:
             printSize()
             try:
                 class_name = ''
                 while 1:
                     yield result.fetchSome(500)
                     if not driver.next(): break
                     log.info("Got %d items", len(driver.next()))
                     for obj in driver.next():
                         obj = obj.targetinstance
                         props = [
                             p for p in obj.__dict__.keys()
                             if not p.startswith('_')
                         ]
                         if obj._class_name != class_name:
                             class_name = obj._class_name
                             print obj._class_name
                             print repr(props)
                         print repr([getattr(obj, p) for p in props])
             except WError, ex:
                 log.exception(ex)
                 yield async_sleep(1)
                 driver.next()
         q.close()
Esempio n. 10
0
    def inner(driver):
        try:
            q = Query()

            yield q.connect(eventContext, 1, hostname, creds)
            driver.next()
            log.info("Query sent")
            yield q.query(query)
            result = driver.next()
            class_name = ''
            while 1:
                yield result.fetchSome()
                if not driver.next(): break
                for obj in driver.next():
                    props = [
                        p for p in obj.__dict__.keys() if not p.startswith('_')
                    ]
                    if obj._class_name != class_name:
                        class_name = obj._class_name
                        print obj._class_name
                        print repr(props)
                    d = {}
                    for p in props:
                        d[p] = getattr(obj, p)
                    print "-------------"
                    pprint.pprint(d)
                    #print repr([getattr(obj, p) for p in props])
            q.close()
        except Exception, ex:
            log.exception(ex)
 def connect(self):
     from pysamba.twisted.reactor import eventContext
     args, kwargs = self.parseCS(self.cs)
     host = kwargs.get('host', 'localhost')
     user = kwargs.get('user', '')
     if not user:
         log.warning("Windows login name is unset: "
                     "please specify zWinUser and "
                     "zWinPassword zProperties before adding devices.")
         raise BadCredentials("Username is empty")
     password = kwargs.get('password', '')
     creds = '%s%%%s'%(user, password)
     namespace = kwargs.get('namespace', 'root/cimv2')
     log.debug("connect to %s, user %s", host, user)
     self.pool = Query()
     return self.pool.connect(eventContext, host, host, creds, namespace)
Esempio n. 12
0
class WMIClient(BaseClient):
    def __init__(self, device, datacollector=None, plugins=[]):
        BaseClient.__init__(self, device, datacollector)
        self.device = device
        self.host = device.id
        self._wmi = None
        if socket.getfqdn().lower() == device.id.lower():
            self.host = "."
            device.zWinUser = device.zWinPassword = ""
        elif device.manageIp is not None:
            self.host = device.manageIp
        self.name = device.id
        self.user = device.zWinUser
        self.passwd = device.zWinPassword
        self.datacollector = datacollector
        self.plugins = plugins
        self.results = []

    def connect(self):
        log.debug("connect to %s, user %r", self.host, self.user)
        if not self.user:
            log.warning("Windows login name is unset: "
                        "please specify zWinUser and "
                        "zWinPassword zProperties before adding devices.")
            raise BadCredentials("Username is empty")
        self._wmi = Query()
        creds = '%s%%%s' % (self.user, self.passwd)
        return self._wmi.connect(eventContext, self.device.id, self.host,
                                 creds)

    def close(self):
        if self._wmi:
            self._wmi.close()

    def query(self, queries):
        def inner(driver):
            try:
                queryResult = {}
                for tableName, query in queries.items():
                    query = query.replace("\\", "\\\\")
                    yield self._wmi.query(query)
                    result = driver.next()
                    queryResult[tableName] = []
                    while 1:
                        more = None
                        yield result.fetchSome()
                        try:
                            more = driver.next()
                        except WMIFailure, ex:
                            msg = 'Received %s from query: %s'

                            # Look for specific errors that should be equated
                            # to an empty result set.
                            if str(ex) in (
                                    "NT code 0x80041010",
                                    "WBEM_E_INVALID_CLASS",
                            ):
                                log.debug(msg % (ex, query))
                            else:
                                log.error(msg % (ex, query))
                                raise
                        if not more:
                            break
                        queryResult[tableName].extend(more)
                yield defer.succeed(queryResult)
                driver.next()
            except Exception, ex:
                log.debug("Exception collecting query: %s", str(ex))
                raise
class WMIClient(BaseClient):

    def __init__(self, device, datacollector=None, plugins=[]):
        BaseClient.__init__(self, device, datacollector)
        self.device = device
        self.host = device.id
        self._wmi = None
        if socket.getfqdn().lower() == device.id.lower(): 
            self.host = "."
            device.zWinUser = device.zWinPassword = ""
        elif device.manageIp is not None:
            self.host = device.manageIp
        self.name = device.id
        self.user = device.zWinUser
        self.passwd = device.zWinPassword
        self.datacollector = datacollector
        self.plugins = plugins
        self.results = []


    def connect(self):
        log.debug("connect to %s, user %r", self.host, self.user)
        if not self.user:
            log.warning("Windows login name is unset: "
                        "please specify zWinUser and "
                        "zWinPassword zProperties before adding devices.")
            raise BadCredentials("Username is empty")
        self._wmi = Query()
        creds = '%s%%%s' % (self.user, self.passwd)
        return self._wmi.connect(eventContext, self.device.id, self.host, creds)


    def close(self):
        if self._wmi:
            self._wmi.close()


    def query(self, queries):
        def inner(driver):
            try:
                queryResult = {}
                for tableName, query in queries.items():
                    query = query.replace ("\\", "\\\\")
                    yield self._wmi.query(query)
                    result = driver.next()
                    queryResult[tableName] = []
                    while 1:
                        more = None
                        yield result.fetchSome()
                        try:
                            more = driver.next()
                        except WMIFailure, ex:
                            msg = 'Received %s from query: %s'
                            
                            # Look for specific errors that should be equated
                            # to an empty result set.
                            if str(ex) in (
                                "NT code 0x80041010",
                                "WBEM_E_INVALID_CLASS",
                                ):
                                log.debug(msg % (ex, query))
                            else:
                                log.error(msg % (ex, query))
                                raise
                        if not more:
                            break
                        queryResult[tableName].extend(more)
                yield defer.succeed(queryResult)
                driver.next()
            except Exception, ex:
                log.debug("Exception collecting query: %s", str(ex))
                raise
Esempio n. 14
0
class Watcher(object):

    def __init__(self, device, query):
        self.wmi = Query()
        self.device = device
        self.queryString = query
        self.enum = None
        self.busy = False
        self.closeRequested = False
        log.debug("Starting watcher on %s", device.id)

    @defer.inlineCallbacks
    def connect(self):
        self.busy = True
        try:
            log.debug("connecting to %s", self.device.id)
            d = self.device

            yield self.wmi.connect(eventContext,
                                   d.id,
                                   d.manageIp,
                                   "%s%%%s" % (d.zWinUser, d.zWinPassword))

            log.debug("connected to %s sending query %s", self.device.id, self.queryString)
            self.enum = yield self.wmi.notificationQuery(self.queryString)
            log.debug("got query response from %s", self.device.id)
        finally:
            self.busy = False
            if self.closeRequested:
                self.close()

    @defer.inlineCallbacks
    def getEvents(self, timeout=0, chunkSize=10):
        assert self.enum
        self.busy = True
        log.debug("Fetching events for %s", self.device.id)
        result = []

        try:
            result = yield self.enum.fetchSome(timeoutMs=timeout, chunkSize=chunkSize)
        finally:
            self.busy = False
            if self.closeRequested:
                self.close()

        log.debug("Events fetched for %s", self.device.id)
        defer.returnValue(result)
    
    def close(self):
        if self.busy:
            log.debug("close requested on busy WMI Query for %s; deferring",
                           self.device.id)
            self.closeRequested = True
        elif self.wmi:
            log.debug("closing WMI Query for %s", self.device.id)
            self.wmi.close()
            self.wmi = None

    def __del__(self):
        log.debug("Watcher.__del__ called for %s, busy=%r closeRequested=%r",
                  self.device.id, self.busy, self.closeRequested)
        self.close()