1
2
3
4
5
6
7
8
9
10
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
33
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
51 def parse_objid(objid, last=False):
52 if last: objid = objid.split('/')[-1]
53 else: objid = '/'.join(objid.split('/')[4:])
54 objid = objid.encode('ascii')
55 return objid
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
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
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
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
134 schema, host, path, null, null, null = urlparse(xmlfile)
135 if schema and host:
136 self.infile = urllib2.urlopen(xmlfile)
137
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
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
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