Package Products :: Package ZenRelations :: Module ImportDevices
[hide private]
[frames] | no frames]

Source Code for Module Products.ZenRelations.ImportDevices

  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 or (at your 
  8  # option) any later version as published by the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 11  # 
 12  ########################################################################### 
 13   
 14  __doc__ = """ImportRM 
 15   
 16  Import RelationshipManager objects into a Zope database 
 17   
 18  """ 
 19   
 20  import sys 
 21  import urllib2 
 22  import transaction 
 23  from urlparse import urlparse 
 24  from xml.dom.minidom import parse 
 25   
 26  import Globals 
 27  from Products.ZenUtils.ZCmdBase import ZCmdBase 
 28   
 29  from Products.ZenRelations.Exceptions import * 
 30   
 31   
32 -class ImportDevices(ZCmdBase):
33
34 - def getDevicePath(self, device):
35 36 def _getParentDevClass(node): 37 ancestor = node.parentNode 38 while ancestor.getAttribute('class') != 'DeviceClass': 39 ancestor = ancestor.parentNode 40 return ancestor
41 42 ancestor = _getParentDevClass(device) 43 path = [] 44 while ancestor.getAttribute('id') != '/zport/dmd/Devices': 45 path.append(ancestor.getAttribute('id')) 46 ancestor = _getParentDevClass(ancestor) 47 path.reverse() 48 return '/' + '/'.join(path)
49 56 d = dict( 57 systemPaths = [], 58 groupPaths = [], 59 performanceMonitor = '', 60 locationPath = '' 61 ) 62 tomanys = device.getElementsByTagName('tomany') 63 toones = device.getElementsByTagName('toone') 64 for tomany in tomanys: 65 id = tomany.getAttribute('id') 66 links = tomany.getElementsByTagName('link') 67 if id == 'systems': 68 for link in links: 69 d['systemPaths'].append(parse_objid(link.getAttribute('objid'))) 70 elif id == 'groups': 71 for link in links: 72 d['groupPaths'].append(parse_objid(link.getAttribute('objid'))) 73 for toone in toones: 74 id = toone.getAttribute('id') 75 objid = toone.getAttribute('objid') 76 if id=='perfServer': d['performanceMonitor'] = parse_objid(objid, 77 True) 78 elif id=='location': d['locationPath'] = parse_objid(objid) 79 return d 80 81
82 - def handleDevices(self):
83 devs = self.doc.getElementsByTagName('object') 84 for dev in devs: 85 if dev.getAttribute('class') != 'Device': continue 86 device = { 87 'deviceName' : dev.getAttribute('id').encode('ascii'), 88 'devicePath' : self.getDevicePath(dev).encode('ascii') 89 } 90 device.update(self.processLinks(dev)) 91 print "Loading %s into %s..." % (device['deviceName'], 92 device['devicePath']) 93 self.dmd.DeviceLoader.loadDevice(**device)
94 95
96 - def buildOptions(self):
97 """basic options setup sub classes can add more options here""" 98 ZCmdBase.buildOptions(self) 99 100 self.parser.add_option('-i', '--infile', 101 dest="infile", 102 help="Input file for import. The default is stdin") 103 print "Build option infile" 104 105 self.parser.add_option('-x', '--commitCount', 106 dest='commitCount', 107 default=20, 108 type="int", 109 help='How many lines should be loaded before a database commit') 110 111 self.parser.add_option('--noindex', 112 dest='noindex',action="store_true",default=False, 113 help='Do not try to index data that was just loaded') 114 115 self.parser.add_option('-n', '--noCommit', 116 dest='noCommit', 117 action="store_true", 118 default=0, 119 help='Do not store changes to the Dmd (for debugging)')
120 121
122 - def loadObjectFromXML(self, objstack=None, xmlfile=''):
123 """This method can be used to load data for the root of Zenoss (default 124 behavior) or it can be used to operate on a specific point in the 125 Zenoss hierarchy (ZODB). 126 127 Upon loading the XML file to be processed, the content of the XML file 128 is handled (processed) by the methods in this class. 129 """ 130 from Products.ZenUtils.Utils import unused 131 unused(objstack) 132 if xmlfile: 133 # check to see if we're getting the XML from a URL ... 134 schema, host, path, null, null, null = urlparse(xmlfile) 135 if schema and host: 136 self.infile = urllib2.urlopen(xmlfile) 137 # ... or from a file on the file system 138 else: 139 self.infile = open(xmlfile) 140 elif self.options.infile: 141 self.infile = open(self.options.infile) 142 else: 143 self.infile = sys.stdin 144 self.doc = parse(self.infile) 145 self.handleDevices() 146 self.doc.unlink() 147 self.infile.close()
148
149 - def loadDatabase(self):
150 """The default behavior of loadObjectFromXML() will be to use the Zope 151 app object, and thus operatate on the whole of Zenoss. 152 """ 153 self.loadObjectFromXML()
154
155 - def commit(self):
156 trans = transaction.get() 157 trans.note('Import from file %s using %s' 158 % (self.options.infile, self.__class__.__name__)) 159 trans.commit()
160 161 162 if __name__ == '__main__': 163 im = ImportDevices() 164 im.loadDatabase() 165