1
2
3
4
5
6
7
8
9
10
11 """
12 Operations for MIBs.
13
14 Available at: /zport/dmd/mib_router
15 """
16
17 import logging
18 from Products.ZenUtils.Ext import DirectResponse
19 from Products.Zuul.routers import TreeRouter
20 from Products.Zuul.decorators import require
21 from Products.Zuul.interfaces import IInfo
22 from Products.Zuul.form.interfaces import IFormBuilder
23 from Products import Zuul
24 from Products.ZenMessaging.audit import audit
25
26 log = logging.getLogger('zen.MibRouter')
30 """
31 A JSON/ExtDirect interface to operations on MIBs
32 """
33
35 self.api = Zuul.getFacade('mibs')
36 self.context = context
37 self.request = request
38 super(MibRouter, self).__init__(context, request)
39
42
43 - def getTree(self, id='/zport/dmd/Mibs'):
44 """
45 Returns the tree structure of an organizer hierarchy. Default tree
46 root is MIBs.
47
48 @type id: string
49 @param id: (optional) Id of the root node of the tree to be
50 returned (default: '/zport/dmd/Mibs')
51 @rtype: [dictionary]
52 @return: Object representing the tree
53 """
54 tree = self.api.getTree(id)
55 data = Zuul.marshal(tree)
56 return [data]
57
59 """
60 Returns the tree structure of an organizer hierarchy, only including
61 organizers.
62
63 @type id: string
64 @param id: Id of the root node of the tree to be returned
65 @rtype: [dictionary]
66 @return: Object representing the organizer tree
67 """
68 tree = self.api.getOrganizerTree(id)
69 data = Zuul.marshal(tree)
70 return [data]
71
72 @require('Manage DMD')
73 - def addNode(self, contextUid='', id='', type=''):
74 """
75 Add an organizer or new blank MIB.
76
77 @type contextUid: string
78 @param contextUid: Context to attach new node
79 @type id: string
80 @param id: Id of the new orgainzer or blank MIB
81 @type type: string
82 @param type: Type of new node. Can be 'organizer' or 'MIB'
83 @rtype: DirectResponse
84 @return: B{Properties}:
85 - tree: ([dictionary]) Object representing the new tree
86 """
87
88
89 if not Zuul.checkPermission('Manage DMD'):
90 return DirectResponse.fail("You don't have permission to execute this command", sticky=False)
91
92
93 nodeType = type
94 if nodeType not in ['organizer', 'MIB']:
95 return DirectResponse.fail('Not creating "%s"' % nodeType)
96
97 try:
98 if nodeType == 'organizer':
99 uid = contextUid + '/' + id
100 maoUid = uid.replace('/zport/dmd', '')
101 self.context.dmd.Mibs.manage_addOrganizer(maoUid)
102 self.context.dmd.restrictedTraverse(uid)
103 audit('UI.Organizer.Add', uid)
104 else:
105 container = self.context.dmd.restrictedTraverse(contextUid)
106 container.manage_addMibModule(id)
107 audit('UI.Mib.Add', contextUid + '/' + id)
108
109 return DirectResponse.succeed(tree=self.getTree())
110 except Exception, e:
111 return DirectResponse.exception(e)
112
113 - def addMIB(self, package, organizer='/'):
114 """
115 Add a new MIB by URL or local file.
116
117 @type package: string
118 @param package: URL or local file path to MIB file
119 @type organizer: string
120 @param organizer: ID of the organizer to add MIB to
121 @rtype: DirectResponse
122 @return: B{Properties}:
123 - jobId: (string) ID of the add MIB job
124 """
125
126
127 if not Zuul.checkPermission('Manage DMD'):
128 return DirectResponse.fail("You don't have permission to execute this command", sticky=False)
129
130 facade = self._getFacade()
131 jobrecord = facade.addMibPackage(package, organizer)
132 if jobrecord:
133 audit('UI.Mib.AddFromPackage', mibpackage=package, organizer=organizer)
134 return DirectResponse.succeed(new_jobs=Zuul.marshal([jobrecord],
135 keys=('uuid', 'description', 'started')))
136 else:
137 return DirectResponse.fail("Failed to add MIB package %s" % package)
138
139 @require('Manage DMD')
141 """
142 Remove an organizer or MIB.
143
144 @type uid: string
145 @param uid: UID of organizer or MIB to remove
146 @rtype: DirectResponse
147 @return: B{Properties}:
148 - tree: ([dictionary]) Object representing the new tree
149 """
150
151
152 if not Zuul.checkPermission('Manage DMD'):
153 return DirectResponse.fail("You don't have permission to execute this command", sticky=False)
154
155 represented = self.context.dmd.restrictedTraverse(uid)
156 organizer = represented.getParentNode()
157 if represented.meta_type == 'MibOrganizer':
158 organizer.manage_deleteOrganizer(represented.id)
159 audit('UI.Organizer.Delete', represented.id)
160 else:
161 organizer.removeMibModules(ids=represented.id)
162 mibUids = represented.id
163 if isinstance(mibUids, basestring):
164 mibUids = (mibUids,)
165 for mibUid in mibUids:
166 audit('UI.Mib.Remove', mibUid)
167 return DirectResponse.succeed(tree=self.getTree())
168
169 @require('Manage DMD')
171 """
172 Move an organizer or MIB from one organizer to another.
173
174 @type uids: [string]
175 @param uids: UIDs of organizers and MIBs to move
176 @type target: string
177 @param target: UID of the organizer to move to
178 @rtype: DirectResponse
179 @return: B{Properties}:
180 - data: (dictionary) Object representing the new parent organizer
181 """
182
183
184 if not Zuul.checkPermission('Manage DMD'):
185 return DirectResponse.fail("You don't have permission to execute this command", sticky=False)
186
187 parent = self.api.moveMibs(uids, target)
188 parent = IInfo(parent)
189 for uid in uids:
190 audit('UI.Mib.Move', uid, target=target)
191 return DirectResponse.succeed(data=Zuul.marshal(parent))
192
193 - def getInfo(self, uid, useFieldSets=True):
194 """
195 Get the properties of a MIB
196
197 @type uid: string
198 @param uid: Unique identifier of a MIB
199 @type useFieldSets: boolean
200 @param useFieldSets: True to return a fieldset version of the info form
201 (default: True)
202 @rtype: DirectResponse
203 @return: B{Properties}
204 - data: (dictionary) Object representing a MIB's properties
205 - form: (dictionary) Object representing an edit form for a MIB's
206 properties
207 """
208 facade = self._getFacade()
209 info = facade.getInfo(uid)
210 form = IFormBuilder(info).render(fieldsets=useFieldSets)
211 return DirectResponse(success=True, data=Zuul.marshal(info), form=form)
212
214 """
215 Set attributes on a MIB.
216 This method accepts any keyword argument for the property that you wish
217 to set. The only required property is "uid".
218
219 @type uid: string
220 @keyword uid: Unique identifier of a MIB
221 @rtype: DirectResponse
222 @return: B{Properties}
223 - data: (dictionary) Object representing a MIB's new properties
224 """
225
226
227 if not Zuul.checkPermission('Manage DMD'):
228 return DirectResponse.fail("You don't have permission to execute this command", sticky=False)
229
230 uid = data['uid']
231 del data['uid']
232 facade = self._getFacade()
233 info = facade.setInfo(uid, data)
234 audit('UI.Mib.Edit', uid, data_=data)
235 return DirectResponse.succeed(data=Zuul.marshal(info))
236
238
239
240 if not Zuul.checkPermission('Manage DMD'):
241 return DirectResponse.fail("You don't have permission to execute this command", sticky=False)
242
243 self.api.addOidMapping(uid, id, oid, nodetype)
244 audit('UI.Mib.AddOidMapping', uid, id=id, oid=oid, nodetype=nodetype)
245 return DirectResponse.succeed()
246
247 - def addTrap(self, uid, id, oid, nodetype='notification'):
248
249
250 if not Zuul.checkPermission('Manage DMD'):
251 return DirectResponse.fail("You don't have permission to execute this command", sticky=False)
252
253 self.api.addTrap(uid, id, oid, nodetype)
254 audit('UI.Mib.AddTrap', uid, id=id, oid=oid, nodetype=nodetype)
255 return DirectResponse.succeed()
256
258
259
260 if not Zuul.checkPermission('Manage DMD'):
261 return DirectResponse.fail("You don't have permission to execute this command", sticky=False)
262
263 if uid.find('/nodes/') == -1:
264 return DirectResponse.fail('"%s" does not appear to refer to an OID Mapping' % uid)
265 mibUid, mappingId = uid.split('/nodes/')
266 self.api.deleteOidMapping(mibUid, mappingId)
267 audit('UI.Mib.DeleteOidMapping', mibUid, mapping=mappingId)
268 return DirectResponse.succeed()
269
271
272
273 if not Zuul.checkPermission('Manage DMD'):
274 return DirectResponse.fail("You don't have permission to execute this command", sticky=False)
275
276 if uid.find('/notifications/') == -1:
277 return DirectResponse.fail('"%s" does not appear to refer to a trap' % uid)
278 mibUid, trapId = uid.split('/notifications/')
279 self.api.deleteTrap(mibUid, trapId)
280 audit('UI.Mib.DeleteTrap', mibUid, trap=trapId)
281 return DirectResponse.succeed()
282
283 - def getOidMappings(self, uid, dir='ASC', sort='name', start=0, page=None, limit=256):
284 count, nodes = self.api.getMibNodes(uid=uid, dir=dir, sort=sort,
285 start=start, limit=limit, relation='nodes')
286 return {'count': count, 'data': Zuul.marshal(nodes)}
287
288 - def getTraps(self, uid, dir='ASC', sort='name', start=0, page=None, limit=256):
289 count, nodes = self.api.getMibNodes(uid=uid, dir=dir, sort=sort,
290 start=start, limit=limit, relation='notifications')
291 return {'count': count, 'data': Zuul.marshal(nodes)}
292
294 """
295 A MIB node is a regular OID (ie you can hit it with snmpwalk)
296 """
297 if id is None:
298 return []
299 tree = self.api.getMibNodeTree(id)
300 if tree is None:
301 return []
302 data = Zuul.marshal(tree)
303 return [data]
304
306 """
307 A MIB trap node is an OID received from a trap
308 """
309 if id is None:
310 return []
311 tree = self.api.getMibTrapTree(id)
312 if tree is None:
313 return []
314 data = Zuul.marshal(tree)
315 return [data]
316