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

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

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