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

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