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