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

# Source Code for Module Products.ZenModel.RRDDataPoint

      1  ##############################################################################
      2  #
      3  # Copyright (C) Zenoss, Inc. 2007, 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__="""RRDDataPoint
     12
     13  Defines attributes for how a datasource will be graphed
     14  and builds the nessesary DEF and CDEF statements for it.
     15
     16  """
     17
     18  import logging
     19  log = logging.getLogger('zen.RRDDatapoint')
     20
     21  import Globals
     22  from AccessControl import ClassSecurityInfo, Permissions
     23  from Products.ZenModel.ZenossSecurity import ZEN_VIEW, ZEN_MANAGE_DMD
     24  from Products.ZenRelations.RelSchema import *
     25  from Products.ZenWidgets import messaging
     26
     27  from ZenModelRM import ZenModelRM
     28  from ZenPackable import ZenPackable
     29
     30  from Products.ZenUtils.Utils import unused, getDisplayType
     31  from Products.ZenUtils.deprecated import deprecated
     32  from Products.ZenModel.RRDDataPointAlias import manage_addDataPointAlias
     33
     34  @deprecated
     35 -def manage_addRRDDataPoint(context, id, REQUEST = None):
     36      """make a RRDDataPoint"""
     37      dp = RRDDataPoint(id)
     38      context._setObject(dp.id, dp)
     39      if REQUEST is not None:
     40          REQUEST['RESPONSE'].redirect(context.absolute_url()+'/manage_main')
     41
     42 -def getDataPointsByAliases( context, aliases ):
     43      """
     44      Retrieve the datapoint/alias pairs for the passed aliases.
     45      """
     46      if not aliases: return
     47      for brains in context.dmd.searchRRDTemplates():
     48          template = brains.getObject()
     49          for datasource in template.datasources():
     50              if not hasattr(datasource, 'datapoints'):
     51                  log.error('The datasource %s on template %s is broken -- skipping',
     52                            datasource, template.getPrimaryUrlPath())
     53                  continue
     54              for datapoint in datasource.datapoints():
     55                  thisDatapointsAliases = dict((dpAlias.id, dpAlias)
     56                                                  for dpAlias in datapoint.aliases())
     57                  for alias in aliases:
     58                      if alias in thisDatapointsAliases:
     59                          yield thisDatapointsAliases[alias], datapoint
     60                      if alias == datapoint.id:
     61                          yield None, datapoint
     62
     63 -def getDataPointsByAlias( context, alias ):
     64      """
     65      Retrieve the datapoint/alias pairs for the passed alias.
     66      """
     67      return getDataPointsByAliases( context, [alias] )
     68
     69
     70  #addRRDDataPoint = DTMLFile('dtml/addRRDDataPoint',globals())
     71
     72  SEPARATOR = '_'
     73
     74 -def convertMethodParameter(value, type):
     75      __pychecker__='no-returnvalues'
     76      if type == "integer":
     77          return int(value)
     78      elif type == "string":
     79          return str(value)
     80      elif type == "float":
     81          return float(value)
     82      else:
     83          raise TypeError('Unsupported method parameter type: %s' % type)
     84
     85 -class RRDDataPointError(Exception): pass
     86
     87 -class RRDDataPoint(ZenModelRM, ZenPackable):
     88
     89      meta_type = 'RRDDataPoint'
     90
     91      rrdtypes = ('COUNTER', 'GAUGE', 'DERIVE', 'ABSOLUTE')
     92
     93      createCmd = ""
     94      rrdtype = 'GAUGE'
     95      isrow = True
     96      rrdmin = None
     97      rrdmax = None
     98
     99      ## These attributes can be removed post 2.1
    100      ## They should remain in 2.1 so the migrate script works correctly
    101      linetypes = ('', 'AREA', 'LINE')
    102      rpn = ""
    103      color = ""
    104      linetype = ''
    105      limit = -1
    106      format = '%5.2lf%s'
    107
    108
    109      _properties = (
    110          {'id':'rrdtype', 'type':'selection',
    111          'select_variable' : 'rrdtypes', 'mode':'w'},
    112          {'id':'createCmd', 'type':'text', 'mode':'w'},
    113          {'id':'isrow', 'type':'boolean', 'mode':'w'},
    114          {'id':'rrdmin', 'type':'string', 'mode':'w'},
    115          {'id':'rrdmax', 'type':'string', 'mode':'w'},
    116          {'id':'description', 'type':'string', 'mode':'w'},
    117          )
    118
    119
    120      _relations = ZenPackable._relations + (
    121          ("datasource", ToOne(ToManyCont,"Products.ZenModel.RRDDataSource","datapoints")),
    122          ("aliases", ToManyCont(ToOne, "Products.ZenModel.RRDDataPointAlias","datapoint"))
    123          )
    124
    125      # Screen action bindings (and tab definitions)
    126      factory_type_information = (
    127      {
    128          'immediate_view' : 'editRRDDataPoint',
    129          'actions'        :
    130          (
    131              { 'id'            : 'edit'
    132              , 'name'          : 'Data Point'
    133              , 'action'        : 'editRRDDataPoint'
    134              , 'permissions'   : ( Permissions.view, )
    135              },
    136          )
    137      },
    138      )
    139
    140      security = ClassSecurityInfo()
    141
    142
    143 -    def breadCrumbs(self, terminator='dmd'):
    144          """Return the breadcrumb links for this object add ActionRules list.
    145          [('url','id'), ...]
    146          """
    147          from RRDTemplate import crumbspath
    148          crumbs = super(RRDDataPoint, self).breadCrumbs(terminator)
    149          return crumbspath(self.rrdTemplate(), crumbs, -3)
    150
    151
    152 -    def name(self):
    153          """Include the data source name in our name,
    154          useful for lists of DataPoints"""
    155          return '%s%c%s' % (self.datasource().id, SEPARATOR, self.id)
    156
    157
    158 -    def getRRDCreateCommand(self, performanceConf):
    159          """Get the create command.
    160          Return '' for the default from performanceConf"""
    161          unused(performanceConf)
    162          if self.createCmd:
    163              return self.createCmd
    164          return ''
    165
    166
    167 -    def addAlias(self, id, formula=None):
    168          """
    169          Add a new alias to this datapoint
    170          """
    171          manage_addDataPointAlias( self, id, formula )
    172
    173
    174 -    def hasAlias(self, aliasId):
    175          """
    176          Whether this datapoint has an alias of this id
    177          """
    178          return hasattr( self.aliases, aliasId )
    179
    180
    181 -    def removeAlias(self, aliasId):
    182          """
    183          Remove any alias with the given id
    184          """
    185          if self.hasAlias( aliasId ):
    186              self.aliases._delObject( aliasId )
    187
    188
    189 -    def getAliasNames(self):
    190          """
    191          Return all the ids of this datapoint's aliases
    192          """
    193          return [ alias.id for alias in self.aliases() ]
    194
    195
    196      security.declareProtected(ZEN_MANAGE_DMD, 'manage_addDataPointAlias')
    197 -    def manage_addDataPointAlias(self, id, formula, REQUEST=None ):
    198          """
    199          Add an alias to this datapoint
    200          """
    201          alias = manage_addDataPointAlias( self, id, formula )
    202          if REQUEST:
    203              return self.callZenScreen(REQUEST)
    204          return alias
    205
    206
    207      security.declareProtected(ZEN_MANAGE_DMD, 'manage_removeDataPointAliases')
    208 -    def manage_removeDataPointAliases(self, ids=(), REQUEST=None ):
    209          """
    210          Remove aliases from this datapoint
    211          """
    212          for id in ids:
    213              self.removeAlias( id )
    214          if REQUEST:
    215              return self.callZenScreen(REQUEST)
    216
    217
    218      security.declareProtected(ZEN_MANAGE_DMD, 'zmanage_editProperties')
    219 -    def zmanage_editProperties(self, REQUEST=None, redirect=False):
    220          """Edit a ZenModel object and return its proper page template
    221          """
    222          unused(redirect)
    223          if REQUEST:
    224              msgs = []
    225              for optional in 'rrdmin', 'rrdmax':
    226                  v = REQUEST.form.get(optional, None)
    227                  if v:
    228                      try:
    229                          REQUEST.form[optional] = long(v)
    230                      except ValueError:
    231                          msgs.append('Unable to convert "%s" to a number' % v)
    232              msgs = ', '.join(msgs)
    233              if msgs:
    234                  messaging.IMessageSender(self).sendToBrowser(
    235                      'Properties Saved',
    236                      msgs[0].capitalize() + msgs[1:]
    237                  )
    238                  return self.callZenScreen(REQUEST, False)
    239
    240          return ZenModelRM.zmanage_editProperties(self, REQUEST)
    241