Package Products :: Package ZenModel :: Module MibOrganizer |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenModel.MibOrganizer

      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 types
     15
     16  from Globals import DTMLFile
     17  from Globals import InitializeClass
     18  from AccessControl import ClassSecurityInfo
     19  from AccessControl import Permissions
     20  from Products.ZenModel.ZenossSecurity import *
     21
     22  from Products.ZenRelations.RelSchema import *
     23  from Products.ZenUtils.Search import makeCaseInsensitiveKeywordIndex
     24  from Products.ZenWidgets import messaging
     25
     26  from Organizer import Organizer
     27  from MibModule import MibModule
     28  from ZenPackable import ZenPackable
     29
     30 -def manage_addMibOrganizer(context, id, REQUEST = None):
     31      """make a device class"""
     32      sc = MibOrganizer(id)
     33      context._setObject(id, sc)
     34      sc = context._getOb(id)
     35      if REQUEST is not None:
     36          REQUEST['RESPONSE'].redirect(context.absolute_url() + '/manage_main')
     37
     38  addMibOrganizer = DTMLFile('dtml/addMibOrganizer',globals())
     39
     40
     41 -def _oid2name(mibSearch, oid, exactMatch=True, strip=False):
     42      """Return a name for an oid. This function is extracted out of the
     43      MibOrganizer class and takes mibSearch as a parameter to make it easier to
     44      unit test.
     45      """
     46      oid = oid.strip('.')
     47
     48      if exactMatch:
     49          brains = mibSearch(oid=oid)
     50          if len(brains) > 0:
     51              return brains[0].id
     52          else:
     53              return ""
     54
     55      oidlist = oid.split('.')
     56      for i in range(len(oidlist), 0, -1):
     57          brains = mibSearch(oid='.'.join(oidlist[:i]))
     58          if len(brains) < 1: continue
     59          if len(oidlist[i:]) > 0 and not strip:
     60              return "%s.%s" % (brains[0].id, '.'.join(oidlist[i:]))
     61          else:
     62              return brains[0].id
     63      return ""
     64
     65
     66 -class MibOrganizer(Organizer, ZenPackable):
     67      """
     68      DeviceOrganizer is the base class for device organizers.
     69      It has lots of methods for rolling up device statistics and information.
     70      """
     71      meta_type = "MibOrganizer"
     72      dmdRootName = "Mibs"
     73      default_catalog = 'mibSearch'
     74
     75      security = ClassSecurityInfo()
     76
     77      _relations = Organizer._relations + ZenPackable._relations + (
     78          ("mibs", ToManyCont(ToOne,"Products.ZenModel.MibModule","miborganizer")),
     79          )
     80
     81
     82      # Screen action bindings (and tab definitions)
     83      factory_type_information = (
     84          {
     85              'immediate_view' : 'mibOrganizerOverview',
     86              'actions'        :
     87              (
     88                  { 'id'            : 'overview'
     89                  , 'name'          : 'Overview'
     90                  , 'action'        : 'mibOrganizerOverview'
     91                  , 'permissions'   : ( Permissions.view, )
     92                  },
     93                  { 'id'            : 'viewHistory'
     94                  , 'name'          : 'Modifications'
     95                  , 'action'        : 'viewNewHistory'
     96                  , 'permissions'   : (ZEN_VIEW_MODIFICATIONS,)
     97                  },
     98              )
     99           },
    100          )
    101
    102
    103 -    def __init__(self, id=None, description=None):
    104          if not id: id = self.dmdRootName
    105          super(MibOrganizer, self).__init__(id, description)
    106          if self.id == self.dmdRootName:
    107              self.createCatalog()
    108
    109
    110 -    def oid2name(self, oid, exactMatch=True, strip=False):
    111          """Return a name for an oid.
    112          """
    113          return _oid2name(self.getDmdRoot("Mibs").mibSearch, oid,
    114              exactMatch, strip)
    115
    116
    117 -    def name2oid(self, name):
    118          """Return an oid based on a name in the form MIB::name.
    119          """
    120          brains = self.getDmdRoot("Mibs").mibSearch({'id': name})
    121          if len(brains) > 0: return brains[0].oid
    122          return ""
    123
    124
    125 -    def countClasses(self):
    126          """Count all mibs with in a MibOrganizer.
    127          """
    128          count = self.mibs.countObjects()
    129          for group in self.children():
    130              count += group.countClasses()
    131          return count
    132
    133
    134 -    def createMibModule(self, name, path="/"):
    135          """Create a MibModule
    136          """
    137          mibs = self.getDmdRoot(self.dmdRootName)
    138          mod = None
    139          if not mod:
    140              modorg = mibs.createOrganizer(path)
    141              mod = MibModule(name)
    142              modorg.mibs._setObject(mod.id, mod)
    143              mod = modorg.mibs._getOb(mod.id)
    144          return mod
    145
    146
    147 -    def manage_addMibModule(self, id, REQUEST=None):
    148          """Create a new service class in this Organizer.
    149          """
    150          mm = MibModule(id)
    151          self.mibs._setObject(id, mm)
    152          if REQUEST:
    153              messaging.IMessageSender(self).sendToBrowser(
    154                  'Mib Module Created',
    155                  'Mib module %s was created.' % id
    156              )
    157              return self.callZenScreen(REQUEST)
    158          else:
    159              return self.mibs._getOb(id)
    160
    161
    162 -    def removeMibModules(self, ids=None, REQUEST=None):
    163          """Remove MibModules from an EventClass.
    164          """
    165          if not ids: return self()
    166          if type(ids) == types.StringType: ids = (ids,)
    167          for id in ids:
    168              self.mibs._delObject(id)
    169          if REQUEST:
    170              messaging.IMessageSender(self).sendToBrowser(
    171                  'Mib Module Deleted',
    172                  'Mib modules deleted: %s' % ', '.join(ids)
    173              )
    174              return self()
    175
    176
    177 -    def moveMibModules(self, moveTarget, ids=None, REQUEST=None):
    178          """Move MibModules from this EventClass to moveTarget.
    179          """
    180          if not moveTarget or not ids: return self()
    181          if type(ids) == types.StringType: ids = (ids,)
    182          target = self.getChildMoveTarget(moveTarget)
    183          for id in ids:
    184              rec = self.mibs._getOb(id)
    185              rec._operation = 1 # moving object state
    186              self.mibs._delObject(id)
    187              target.mibs._setObject(id, rec)
    188          if REQUEST:
    189              messaging.IMessageSender(self).sendToBrowser(
    190                  'Mib Module Moved',
    191                  'Mib modules moved to %s.' % moveTarget
    192              )
    193              REQUEST['RESPONSE'].redirect(target.getPrimaryUrlPath())
    194
    195
    196 -    def reIndex(self):
    197          """Go through all devices in this tree and reindex them."""
    198          zcat = self._getOb(self.default_catalog)
    199          zcat.manage_catalogClear()
    200          for org in [self,] + self.getSubOrganizers():
    201              for mib in org.mibs():
    202                  for thing in mib.nodes() + mib.notifications():
    203                      thing.index_object()
    204
    205
    206 -    def createCatalog(self):
    207          """Create a catalog for mibs searching"""
    208          from Products.ZCatalog.ZCatalog import manage_addZCatalog
    209
    210          # XXX update to use ManagableIndex
    211          manage_addZCatalog(self, self.default_catalog, self.default_catalog)
    212          zcat = self._getOb(self.default_catalog)
    213          cat = zcat._catalog
    214          cat.addIndex('oid', makeCaseInsensitiveKeywordIndex('oid'))
    215          cat.addIndex('id', makeCaseInsensitiveKeywordIndex('id'))
    216          cat.addIndex('summary', makeCaseInsensitiveKeywordIndex('summary'))
    217          zcat.addColumn('getPrimaryId')
    218          zcat.addColumn('id')
    219          zcat.addColumn('oid')
    220
    221
    222
    223
    224  InitializeClass(MibOrganizer)
    225