Package DataCollector :: Module PortscanClient
[hide private]
[frames] | no frames]

Source Code for Module DataCollector.PortscanClient

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2007, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 11  # 
 12  ########################################################################### 
 13   
 14  import logging 
 15   
 16  from twisted.internet import reactor, error 
 17  from twisted.python import failure 
 18   
 19  import Globals 
 20   
 21  from Products.ZenUtils import PortScan 
 22   
 23  log = logging.getLogger("zen.PortscanClient") 
 24   
25 -class PortscanClient(object):
26
27 - def __init__(self, hostname, ipaddr, options=None, device=None, 28 datacollector=None, plugins=[]):
29 self.hostname = hostname 30 self.device = device 31 self.options = options 32 self.datacollector = datacollector 33 self.plugins = plugins 34 self.results = [] 35 36 maxPort = getattr(device,'zIpServiceMapMaxPort', 1024) 37 self.portRange = (1, maxPort) 38 39 #self.portList = getattr(device,'zPortscanPortList', []) 40 self.portList = [] 41 42 if self.portList: 43 kwds = {'portList': self.portList} 44 else: 45 kwds = {'portRange': self.portRange} 46 kwds.update(dict(timeout=self.options.portscantimeout)) 47 self.scanner = PortScan.Scanner(ipaddr, **kwds)
48
49 - def run(self):
50 """ 51 Start portscan collection. 52 """ 53 for plugin in self.plugins: 54 pluginName = plugin.name() 55 log.debug("sending queries for plugin %s", pluginName) 56 d = self.scanner.prepare() 57 d.addCallback(self._cbScanComplete, pluginName) 58 d.addErrback(self._ebScanError, pluginName)
59
60 - def _cbScanComplete(self, results, pluginName):
61 log.debug("received plugin:%s getOids", pluginName) 62 self.clientFinished(pluginName)
63
64 - def _ebScanError( self, err, pluginName):
65 """Handle an error generated by one of our requests. 66 """ 67 self.clientFinished(pluginName) 68 log.debug('device %s plugin %s %s', self.hostname, pluginName, err) 69 if isinstance(err, failure.Failure): 70 actualError = err.value 71 trace = err.getTraceback() 72 else: 73 actualError = err 74 trace = log.getException(err) 75 log.error( 76 """device %s plugin %s unexpected error: %s""", 77 self.hostname, pluginName, trace, 78 )
79
80 - def getResults(self):
81 """ 82 ZenUtils.PortScan records open ports in a list that are the 83 values for a key in a dict where the key is the IP address 84 scanned. 85 86 For example, if we scan host 10.0.4.55 and ports 22 and 80 are 87 open, getSuccesses() will return the following: 88 89 {'10.0.4.55': [22, 80]} 90 """ 91 return self.results
92
93 - def clientFinished(self, pluginName):
94 """ 95 Tell the datacollector that we are all done. 96 """ 97 log.info("portscan client finished collection for %s" % self.hostname) 98 # ApplyDataMap.processClient() expect an iterable with two 99 # elements: the plugin name and the results, so we set this 100 # here. 101 data = (pluginName, self.scanner.getSuccesses()) 102 self.results.append(data) 103 if self.datacollector: 104 self.datacollector.clientFinished(self) 105 else: 106 reactor.stop()
107
108 -def buildOptions(parser=None, usage=None):
109 "build options list that both telnet and ssh use" 110 111 if not usage: 112 usage = "%prog [options] hostname[:port] oids" 113 114 if not parser: 115 from optparse import OptionParser 116 parser = OptionParser(usage=usage)
117 # XXX this function may need options later, so we'll keep this here 118 # as a reminder for now 119 #parser.add_option('--snmpCommunity', 120 # dest='snmpCommunity', 121 # default=defaultSnmpCommunity, 122 # help='Snmp Community string') 123 124 if __name__ == "__main__": 125 import sys 126 sys.path.extend([ 127 '/usr/local/zenoss/Products/DataCollector/plugins', 128 ]) 129 import pprint 130 from zenoss.portscan import IpServiceMap 131 132 logging.basicConfig() 133 log = logging.getLogger() 134 log.setLevel(20) 135 ipmap = IpServiceMap.IpServiceMap() 136 psc = PortscanClient("localhost", '127.0.0.1', plugins=[ipmap,]) 137 psc.run() 138 reactor.run() 139 pprint.pprint(psc.getResults()) 140