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

# Source Code for Module Products.ZenModel.IpAddress

      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__ = """IpAddress
     15
     16  IpAddress represents a device residing on an IP network.
     17  """
     18
     19  import socket
     20  import logging
     21  log = logging.getLogger("zen.IpAddress")
     22
     23  #base classes for IpAddress
     24  from ManagedEntity import ManagedEntity
     25
     26
     27  from AccessControl import ClassSecurityInfo
     28  from Globals import DTMLFile
     29  from Globals import InitializeClass
     30  import zope.interface
     31
     32  from Products.ZenModel.interfaces import IIndexed
     33  from Products.ZenModel.Linkable import Layer3Linkable
     34  from Products.ZenRelations.RelSchema import *
     35  from Products.ZenUtils.IpUtil import *
     36  from Products.ZenModel.Exceptions import *
     37
     38 -def manage_addIpAddress(context, id, netmask=24, REQUEST = None):
     39      """make a IpAddress"""
     40      ip = IpAddress(id, netmask)
     41      context._setObject(ip.id, ip)
     42      if REQUEST is not None:
     43          REQUEST['RESPONSE'].redirect(context.absolute_url()
     44                                       +'/manage_main')
     45
     46
     47  addIpAddress = DTMLFile('dtml/addIpAddress',globals())
     48
     49
     50 -class IpAddress(ManagedEntity, Layer3Linkable):
     51      """IpAddress object"""
     52      zope.interface.implements(IIndexed)
     53
     54      event_key = portal_type = meta_type = 'IpAddress'
     55
     56      default_catalog = 'ipSearch'
     57
     58      _properties = (
     59          {'id':'netmask', 'type':'string', 'mode':'w', 'setter':'setNetmask'},
     60          {'id':'ptrName', 'type':'string', 'mode':'w'},
     61          )
     62      _relations = ManagedEntity._relations + (
     63          ("network", ToOne(ToManyCont,"Products.ZenModel.IpNetwork","ipaddresses")),
     64          ("interface", ToOne(ToMany,"Products.ZenModel.IpInterface","ipaddresses")),
     65          ("clientroutes", ToMany(ToOne,"Products.ZenModel.IpRouteEntry","nexthop")),
     66          )
     67
     68      factory_type_information = (
     69          {
     70              'id'             : 'IpAddress',
     71              'meta_type'      : 'IpAddress',
     72              'description'    : """Ip Address Class""",
     73              'icon'           : 'IpAddress_icon.gif',
     74              'product'        : 'ZenModel',
     75              'factory'        : 'manage_addIpAddress',
     76              'immediate_view' : 'viewIpAddressOverview',
     77              'actions'        :
     78              (
     79                  { 'id'            : 'overview'
     80                  , 'name'          : 'Overview'
     81                  , 'action'        : 'viewIpAddressOverview'
     82                  , 'permissions'   : ( "View", )
     83                  },
     84              )
     85            },
     86          )
     87
     88      security = ClassSecurityInfo()
     89
     90 -    def __init__(self, id, netmask=24):
     91          checkip(id)
     92          ManagedEntity.__init__(self, id)
     93          self._netmask = maskToBits(netmask)
     94          self.ptrName = None
     95          #self.setPtrName()
     96
     97
     98 -    def setPtrName(self):
     99          try:
    100              data = socket.gethostbyaddr(self.id)
    101              if data: self.ptrName = data[0]
    102          except socket.error, e:
    103              self.ptrName = ""
    104              log.warn("%s: %s", self.id, e)
    105
    106
    107      security.declareProtected('View', 'primarySortKey')
    108 -    def primarySortKey(self):
    109          """make sure that networks sort correctly"""
    110          return numbip(self.id)
    111
    112
    113 -    def setNetmask(self, value):
    114          self._netmask = maskToBits(value)
    115
    116 -    def _setPropValue(self, id, value):
    117          """override from PerpertyManager to handle checks and ip creation"""
    118          self._wrapperCheck(value)
    119          if id == 'netmask':
    120              self.setNetmask(value)
    121          else:
    122              setattr(self,id,value)
    123
    124
    125 -    def __getattr__(self, name):
    126          if name == 'netmask':
    127              return self._netmask
    128          else:
    129              raise AttributeError( name )
    130
    131
    132      security.declareProtected('Change Device', 'setIpAddress')
    133 -    def setIpAddress(self, ip):
    134          """set the ipaddress can be in the form 1.1.1.1/24 to also set mask"""
    135          iparray = ip.split("/")
    136          if len(iparray) > 1:
    137              ip = iparray[0]
    138              self._netmask = maskToBits(iparray[1])
    139          checkip(ip)
    140          aqself = self.primaryAq() #set aq path
    141          network = aqself.aq_parent
    142          netip = getnetstr(ip, network.netmask)
    143          if netip == network.id:
    144              network._renameObject(aqself.id, ip)
    145          else:
    146              raise WrongSubnetError(
    147                      "Ip %s is in a different subnet than %s" % (ip, self.id) )
    148
    149
    150
    151      security.declareProtected('View', 'getIp')
    152 -    def getIp(self):
    153          """return only the ip"""
    154          return self.id
    155
    156
    157      security.declareProtected('View', 'getIpAddress')
    158 -    def getIpAddress(self):
    159          """return the ip with its netmask in the form 1.1.1.1/24"""
    160          return self.id + "/" + str(self._netmask)
    161
    162
    163 -    def __str__(self):
    164          return self.getIpAddress()
    165
    166
    167      security.declareProtected('View', 'getInterfaceName')
    168 -    def getInterfaceName(self):
    169          if self.interface():
    170              return self.interface().name()
    171          return "No Interface"
    172
    173
    174      security.declareProtected('View', 'getNetworkName')
    175 -    def getNetworkName(self):
    176          if self.network():
    177              return self.network().getNetworkName()
    178          return "No Network"
    179
    180
    181      security.declareProtected('View', 'getNetworkUrl')
    182 -    def getNetworkUrl(self):
    183          if self.network():
    184              return self.network().absolute_url()
    185          return ""
    186
    187
    188      security.declareProtected('View', 'getDeviceUrl')
    189 -    def getDeviceUrl(self):
    190          """Get the primary url path of the device in which this ip
    191          is associated with.  If no device return url to the ip itself.
    192          """
    193          d = self.device()
    194          if d:
    195              return d.getPrimaryUrlPath()
    196          else:
    197              return self.getPrimaryUrlPath()
    198
    199
    200 -    def device(self):
    201          """Reuturn the device for this ip for DeviceResultInt"""
    202          int = self.interface()
    203          if int: return int.device()
    204          return None
    205
    206 -    def index_object(self):
    207          super(IpAddress, self).index_object()
    208          self.index_links()
    209
    210 -    def unindex_object(self):
    211          self.unindex_links()
    212          super(IpAddress, self).unindex_object()
    213
    214 -    def deviceId(self):
    215          """
    216          The device id, for indexing purposes.
    217          """
    218          d = self.device()
    219          if d: return d.id
    220          else: return None
    221
    222 -    def interfaceId(self):
    223          """
    224          The interface id, for indexing purposes.
    225          """
    226          i = self.interface()
    227          if i: return i.id
    228          else: return None
    229
    230 -    def ipAddressId(self):
    231          """
    232          The ipAddress id, for indexing purposes.
    233          """
    234          return self.getPrimaryId()
    235
    236 -    def networkId(self):
    237          """
    238          The network id, for indexing purposes.
    239          """
    240          n = self.network()
    241          if n: return n.getPrimaryId()
    242          else: return None
    243
    244  InitializeClass(IpAddress)
    245