Package Products :: Package ZenUtils :: Module FixIps |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenUtils.FixIps

     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  from Products.ZenUtils.Utils import getSubObjects
    12
    13  from Products.ZenUtils.ZCmdBase import ZCmdBase
    14  from transaction import get_transaction
    15
    16 -class FixIps(ZCmdBase):
    17
    18 -    def fixips(self):
    19          ips = getSubObjects(self.dmd, self.filter, self.decend)
    20          self.ccount = 0
    21          for ip in ips:
    22              self.log.debug("fixing ip %s" % ip.id)
    23              int = ip.interface()
    24              if int:
    25                  ip.removeRelation("interface")
    26                  ip.addRelation("interface", int)
    27                  self.ccount += 1
    28                  if (self.ccount >= self.options.commitCount
    29                      and not self.options.noCommit):
    30                      self.mycommit(ip)
    31          if self.options.noCommit:
    32              self.log.info("not commiting any changes")
    33          else:
    34              self.mycommit()
    35
    36
    37 -    def filter(self, obj):
    38          return obj.meta_type == "IpAddress"
    39
    40
    41 -    def decend(self, obj):
    42          return (obj.meta_type == "IpNetwork" or
    43              (obj.meta_type == "To Many Relationship" and
    44                  (obj.id == "subnetworks" or obj.id == "ipaddresses")))
    45
    46
    47 -    def mycommit(self, ip=None):
    48          if not ip:
    49              ipname = "all"
    50          else:
    51              ipname = ip.id
    52          self.log.info('commiting group of ips ending with %s' % ipname)
    53          trans = get_transaction()
    54          trans.note('FixIps reconnect ips')
    55          trans.commit()
    56          self.ccount = 0
    57
    58
    59 -    def buildOptions(self):
    60          ZCmdBase.buildOptions(self)
    61          self.parser.add_option('-x', '--commitCount',
    62                      dest='commitCount',
    63                      default=20,
    64                      type="int",
    65                      help='how many lines should be loaded before commit')
    66
    67          self.parser.add_option('-n', '--noCommit',
    68                      dest='noCommit',
    69                      action="store_true",
    70                      default=0,
    71                      help='Do not store changes to the Dmd (for debugging)')
    72
    73
    74  if __name__ == "__main__":
    75      fips = FixIps()
    76      fips.fixips()
    77