Package Products :: Package Zuul :: Package routers :: Module mibs
[hide private]
[frames] | no frames]

Source Code for Module Products.Zuul.routers.mibs

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