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 nodeType = type
89 if nodeType not in ['organizer', 'MIB']:
90 return DirectResponse.fail('Not creating "%s"' % nodeType)
91
92 try:
93 if nodeType == 'organizer':
94 uid = contextUid + '/' + id
95 maoUid = uid.replace('/zport/dmd', '')
96 self.context.dmd.Mibs.manage_addOrganizer(maoUid)
97 self.context.dmd.restrictedTraverse(uid)
98 audit('UI.Organizer.Add', uid)
99 else:
100 container = self.context.dmd.restrictedTraverse(contextUid)
101 container.manage_addMibModule(id)
102 audit('UI.Mib.Add', contextUid + '/' + id)
103
104 return DirectResponse.succeed(tree=self.getTree())
105 except Exception, e:
106 return DirectResponse.exception(e)
107
108 - def addMIB(self, package, organizer='/'):
109 """
110 Add a new MIB by URL or local file.
111
112 @type package: string
113 @param package: URL or local file path to MIB file
114 @type organizer: string
115 @param organizer: ID of the organizer to add MIB to
116 @rtype: DirectResponse
117 @return: B{Properties}:
118 - jobId: (string) ID of the add MIB job
119 """
120 facade = self._getFacade()
121 jobrecord = facade.addMibPackage(package, organizer)
122 if jobrecord:
123 audit('UI.Mib.AddFromPackage', mibpackage=package, organizer=organizer)
124 return DirectResponse.succeed(new_jobs=Zuul.marshal([jobrecord],
125 keys=('uuid', 'description', 'started')))
126 else:
127 return DirectResponse.fail("Failed to add MIB package %s" % package)
128
129 @require('Manage DMD')
131 """
132 Remove an organizer or MIB.
133
134 @type uid: string
135 @param uid: UID of organizer or MIB to remove
136 @rtype: DirectResponse
137 @return: B{Properties}:
138 - tree: ([dictionary]) Object representing the new tree
139 """
140 represented = self.context.dmd.restrictedTraverse(uid)
141 organizer = represented.getParentNode()
142 if represented.meta_type == 'MibOrganizer':
143 organizer.manage_deleteOrganizer(represented.id)
144 audit('UI.Organizer.Delete', represented.id)
145 else:
146 organizer.removeMibModules(ids=represented.id)
147 mibUids = represented.id
148 if isinstance(mibUids, basestring):
149 mibUids = (mibUids,)
150 for mibUid in mibUids:
151 audit('UI.Mib.Remove', mibUid)
152 return DirectResponse.succeed(tree=self.getTree())
153
154 @require('Manage DMD')
156 """
157 Move an organizer or MIB from one organizer to another.
158
159 @type uids: [string]
160 @param uids: UIDs of organizers and MIBs to move
161 @type target: string
162 @param target: UID of the organizer to move to
163 @rtype: DirectResponse
164 @return: B{Properties}:
165 - data: (dictionary) Object representing the new parent organizer
166 """
167 parent = self.api.moveMibs(uids, target)
168 parent = IInfo(parent)
169 for uid in uids:
170 audit('UI.Mib.Move', uid, target=target)
171 return DirectResponse.succeed(data=Zuul.marshal(parent))
172
173 - def getInfo(self, uid, useFieldSets=True):
174 """
175 Get the properties of a MIB
176
177 @type uid: string
178 @param uid: Unique identifier of a MIB
179 @type useFieldSets: boolean
180 @param useFieldSets: True to return a fieldset version of the info form
181 (default: True)
182 @rtype: DirectResponse
183 @return: B{Properties}
184 - data: (dictionary) Object representing a MIB's properties
185 - form: (dictionary) Object representing an edit form for a MIB's
186 properties
187 """
188 facade = self._getFacade()
189 info = facade.getInfo(uid)
190 form = IFormBuilder(info).render(fieldsets=useFieldSets)
191 return DirectResponse(success=True, data=Zuul.marshal(info), form=form)
192
194 """
195 Set attributes on a MIB.
196 This method accepts any keyword argument for the property that you wish
197 to set. The only required property is "uid".
198
199 @type uid: string
200 @keyword uid: Unique identifier of a MIB
201 @rtype: DirectResponse
202 @return: B{Properties}
203 - data: (dictionary) Object representing a MIB's new properties
204 """
205 uid = data['uid']
206 del data['uid']
207 facade = self._getFacade()
208 info = facade.setInfo(uid, data)
209 audit('UI.Mib.Edit', uid, data_=data)
210 return DirectResponse.succeed(data=Zuul.marshal(info))
211
213 self.api.addOidMapping(uid, id, oid, nodetype)
214 audit('UI.Mib.AddOidMapping', uid, id=id, oid=oid, nodetype=nodetype)
215 return DirectResponse.succeed()
216
217 - def addTrap(self, uid, id, oid, nodetype='notification'):
218 self.api.addTrap(uid, id, oid, nodetype)
219 audit('UI.Mib.AddTrap', uid, id=id, oid=oid, nodetype=nodetype)
220 return DirectResponse.succeed()
221
223 if uid.find('/nodes/') == -1:
224 return DirectResponse.fail('"%s" does not appear to refer to an OID Mapping' % uid)
225 mibUid, mappingId = uid.split('/nodes/')
226 self.api.deleteOidMapping(mibUid, mappingId)
227 audit('UI.Mib.DeleteOidMapping', mibUid, mapping=mappingId)
228 return DirectResponse.succeed()
229
231 if uid.find('/notifications/') == -1:
232 return DirectResponse.fail('"%s" does not appear to refer to a trap' % uid)
233 mibUid, trapId = uid.split('/notifications/')
234 self.api.deleteTrap(mibUid, trapId)
235 audit('UI.Mib.DeleteTrap', mibUid, trap=trapId)
236 return DirectResponse.succeed()
237
238 - def getOidMappings(self, uid, dir='ASC', sort='name', start=0, page=None, limit=256):
239 count, nodes = self.api.getMibNodes(uid=uid, dir=dir, sort=sort,
240 start=start, limit=limit, relation='nodes')
241 return {'count': count, 'data': Zuul.marshal(nodes)}
242
243 - def getTraps(self, uid, dir='ASC', sort='name', start=0, page=None, limit=256):
244 count, nodes = self.api.getMibNodes(uid=uid, dir=dir, sort=sort,
245 start=start, limit=limit, relation='notifications')
246 return {'count': count, 'data': Zuul.marshal(nodes)}
247
249 """
250 A MIB node is a regular OID (ie you can hit it with snmpwalk)
251 """
252 if id is None:
253 return []
254 tree = self.api.getMibNodeTree(id)
255 if tree is None:
256 return []
257 data = Zuul.marshal(tree)
258 return [data]
259
261 """
262 A MIB trap node is an OID received from a trap
263 """
264 if id is None:
265 return []
266 tree = self.api.getMibTrapTree(id)
267 if tree is None:
268 return []
269 data = Zuul.marshal(tree)
270 return [data]
271