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

# Source Code for Module Products.ZenModel.RRDDataPoint

      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  __doc__="""RRDDataPoint
     15
     16  Defines attributes for how a datasource will be graphed
     17  and builds the nessesary DEF and CDEF statements for it.
     18
     19  $Id:$"""
     20
     21  __version__ = "$Revision:$"[11:-2]
     22
     23  import Globals
     24  from AccessControl import ClassSecurityInfo, Permissions
     25  from Products.ZenModel.ZenossSecurity import ZEN_VIEW, ZEN_MANAGE_DMD
     26
     27  from Products.ZenRelations.RelSchema import *
     28  from Products.ZenWidgets import messaging
     29
     30  from ZenModelRM import ZenModelRM
     31  from ZenPackable import ZenPackable
     32
     33  from Products.ZenUtils.Utils import unused
     34  from Products.ZenModel.RRDDataPointAlias import manage_addDataPointAlias
     35
     36 -def manage_addRRDDataPoint(context, id, REQUEST = None):
     37      """make a RRDDataPoint"""
     38      dp = RRDDataPoint(id)
     39      context._setObject(dp.id, dp)
     40      if REQUEST is not None:
     41          REQUEST['RESPONSE'].redirect(context.absolute_url()+'/manage_main')
     42
     43 -def getDataPointsByAliases( context, aliases ):
     44      """
     45      Retrieve the datapoint/alias pairs for the passed aliases.
     46      """
     47      if not aliases: return
     48      for brains in context.dmd.searchRRDTemplates():
     49          template = brains.getObject()
     50          for datasource in template.datasources():
     51              for datapoint in datasource.datapoints():
     52                  thisDatapointsAliases = dict(
     53                          [ ( dpAlias.id, dpAlias ) for dpAlias in
     54                             datapoint.aliases() ] )
     55                  found = False
     56                  foundAlias = None
     57                  for alias in aliases:
     58                      if thisDatapointsAliases.has_key( alias ):
     59                          found = True
     60                          yield thisDatapointsAliases[alias], datapoint
     61                  if alias == datapoint.id:
     62                      yield None, datapoint
     63
     64 -def getDataPointsByAlias( context, alias ):
     65      """
     66      Retrieve the datapoint/alias pairs for the passed alias.
     67      """
     68      return getDataPointsByAliases( context, [alias] )
     69
     70
     71  #addRRDDataPoint = DTMLFile('dtml/addRRDDataPoint',globals())
     72
     73  SEPARATOR = '_'
     74
     75 -def convertMethodParameter(value, type):
     76      __pychecker__='no-returnvalues'
     77      if type == "integer":
     78          return int(value)
     79      elif type == "string":
     80          return str(value)
     81      elif type == "float":
     82          return float(value)
     83      else:
     84          raise TypeError('Unsupported method parameter type: %s' % type)
     85
     86 -class RRDDataPointError(Exception): pass
     87
     88 -class RRDDataPoint(ZenModelRM, ZenPackable):
     89
     90      meta_type = 'RRDDataPoint'
     91
     92      rrdtypes = ('COUNTER', 'GAUGE', 'DERIVE', 'ABSOLUTE')
     93
     94      createCmd = ""
     95      rrdtype = 'GAUGE'
     96      isrow = True
     97      rrdmin = None
     98      rrdmax = None
     99
    100      ## These attributes can be removed post 2.1
    101      ## They should remain in 2.1 so the migrate script works correctly
    102      linetypes = ('', 'AREA', 'LINE')
    103      rpn = ""
    104      color = ""
    105      linetype = ''
    106      limit = -1
    107      format = '%5.2lf%s'
    108
    109
    110      _properties = (
    111          {'id':'rrdtype', 'type':'selection',
    112          'select_variable' : 'rrdtypes', 'mode':'w'},
    113          {'id':'createCmd', 'type':'text', 'mode':'w'},
    114          {'id':'isrow', 'type':'boolean', 'mode':'w'},
    115          {'id':'rrdmin', 'type':'string', 'mode':'w'},
    116          {'id':'rrdmax', '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