Package Products :: Package ZenHub :: Package services :: Module SnmpTrapConfig |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenHub.services.SnmpTrapConfig

      1  ##############################################################################
      2  #
      3  # Copyright (C) Zenoss, Inc. 2011, all rights reserved.
      4  #
      5  # This content is made available according to terms specified in
      6  # License.zenoss under the directory where your Zenoss product is installed.
      7  #
      8  ##############################################################################
      9
     10
     11  __doc__ = '''SnmpTrapConfig
     12
     13  Provides configuration for an OID translation service.
     14  '''
     15
     16  import logging
     17  log = logging.getLogger('zen.HubService.SnmpTrapConfig')
     18
     19  import Globals
     20
     21  from twisted.spread import pb
     22  from twisted.internet import reactor, defer
     23
     24  from Products.ZenCollector.services.config import CollectorConfigService
     25  from Products.ZenHub.zodb import onUpdate, onDelete
     26
     27  from Products.ZenModel.Device import Device
     28  from Products.ZenModel.DeviceClass import DeviceClass
     29  from Products.ZenModel.MibBase import MibBase
     30  from Products.Zuul.interfaces import ICatalogTool
     31
     32  SNMPV3_USER_ZPROPS = ["zSnmpEngineId",
     33                        "zSnmpSecurityName",
     34                        "zSnmpAuthType",
     35                        "zSnmpAuthPassword",
     36                        "zSnmpPrivType",
     37                        "zSnmpPrivPassword",
     38                       ]
     39
     40 -class FakeDevice(object):
     41      id = 'MIB payload'
     42
     43 -class User(pb.Copyable, pb.RemoteCopy):
     44      version = None
     45      engine_id = None
     46      username = None
     47      authentication_type = None # MD5 or SHA
     48      authentication_passphrase = None
     49      privacy_protocol = None # DES or AES
     50      privacy_passphrase = None
     51 -    def __str__(self):
     52          fmt = "<User(version={0.version},engine_id={0.engine_id},username={0.username},authentication_type={0.authentication_type},privacy_protocol={0.privacy_protocol})>"
     53          return fmt.format(self)
     54  pb.setUnjellyableForClass(User, User)
     55
     56 -class SnmpTrapConfig(CollectorConfigService):
     57
     58 -    def _filterDevices(self, deviceList):
     59          return [ FakeDevice() ]
     60
     61 -    def _createDeviceProxy(self, device):
     62          proxy = CollectorConfigService._createDeviceProxy(self, device)
     63          proxy.configCycleInterval = 3600
     64          proxy.name = "SNMP Trap Configuration"
     65          proxy.device = device.id
     66
     67          # Gather all OID -> Name mappings from /Mibs catalog
     68          proxy.oidMap = dict(
     69                         (b.oid, b.id) for b in self.dmd.Mibs.mibSearch() if b.oid
     70                         )
     71
     72          return proxy
     73
     74      @defer.inlineCallbacks
     75 -    def _create_user(self, obj):
     76
     77          # if v3 and has at least one v3 user property, then we want to create a user
     78          if obj.getProperty("zSnmpVer", None) == "v3":
     79              has_user = any(obj.hasProperty(zprop) for zprop in SNMPV3_USER_ZPROPS)
     80          else:
     81              has_user = False
     82
     83          if has_user:
     84              # only send v3 users that have at least one local zProp defined
     85              user = User()
     86              user.version = int(obj.zSnmpVer[1])
     87              user.engine_id = obj.zSnmpEngineId
     88              user.username = obj.zSnmpSecurityName
     89              user.authentication_type = obj.zSnmpAuthType
     90              user.authentication_passphrase = obj.zSnmpAuthPassword
     91              user.privacy_protocol = obj.zSnmpPrivType
     92              user.privacy_passphrase = obj.zSnmpPrivPassword
     93              for listener in self.listeners:
     94                  yield listener.callRemote('createUser', user)
     95          else:
     96              # give way in the reactor loop while walking all users
     97              d = defer.Deferred()
     98              reactor.callLater(0, d.callback, None)
     99              yield d
    100
    101 -    def remote_createAllUsers(self):
    102          cat = ICatalogTool(self.dmd)
    103          brains = cat.search(("Products.ZenModel.Device.Device", "Products.ZenModel.DeviceClass.DeviceClass"))
    104          for brain in brains:
    105              device = brain.getObject()
    106              self._create_user(device)
    107
    108      @onUpdate(DeviceClass)
    109 -    def deviceClassChanged(self, device, event):
    110          self._create_user(device)
    111
    112      @onUpdate(Device)
    113 -    def deviceChanged(self, device, event):
    114          self._create_user(device)
    115
    116      @onUpdate(MibBase)
    117 -    def mibsChanged(self, device, event):
    118          for listener in self.listeners:
    119              listener.callRemote('notifyConfigChanged')
    120
    121      @onDelete(MibBase)
    122 -    def mibsDeleted(self, device, event):
    123          for listener in self.listeners:
    124              listener.callRemote('notifyConfigChanged')
    125
    126
    127  if __name__ == '__main__':
    128      from pprint import pprint
    129      from Products.ZenHub.ServiceTester import ServiceTester
    130
    131 -    class TrapTester(ServiceTester):
    132 -        def buildOptions(self):
    133              ServiceTester.buildOptions(self)
    134              self.parser.add_option('--resolve', dest='request',
    135                                 help="Specify a specific OID or name to map to the name or OID.")
    136              self.parser.add_option('--exactMatch', dest='exactMatch',
    137                                     action='store_true', default=True,
    138                                 help="When resolving to name, use an exact match")
    139              self.parser.add_option('--fuzzyMatch', dest='exactMatch',
    140                                     action='store_false',
    141                                 help="When resolving to name, don't use an exact match")
    142              self.parser.add_option('--list', dest='list',
    143                                     action='store_true', default=False,
    144                                 help="List all OIDs?")
    145
    146 -        def resolve(self):
    147              name = self.dmd.Mibs.oid2name(self.options.request,
    148                                            exactMatch=self.options.exactMatch)
    149              if name:
    150                  log.info("\t%s => %s", self.options.request, name)
    151
    152              oid = self.dmd.Mibs.name2oid(self.options.request)
    153              if oid:
    154                  log.info("\t%s => %s", self.options.request, oid)
    155
    156 -        def list(self):
    157              dev = FakeDevice()
    158              proxy = self.service._createDeviceProxy(dev)
    159              pprint(proxy.oidMap)
    160
    161 -        def printer(self, config):
    162              print "\t%s => %s" % (config.id, config.oidMap)
    163
    164 -        def run(self):
    165              if self.options.request:
    166                  self.resolve()
    167              elif self.options.list:
    168                  self.list()
    169              else:
    170                  self.showDeviceInfo()
    171
    172      tester = TrapTester(SnmpTrapConfig)
    173      tester.run()
    174