Package ZenModel :: Module zenmib
[hide private]
[frames] | no frames]

Source Code for Module ZenModel.zenmib

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2007, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 11  # 
 12  ########################################################################### 
 13  import sys 
 14  import os 
 15  import glob 
 16  from sets import Set 
 17  import pprint 
 18   
 19  import Globals 
 20  import transaction 
 21   
 22   
 23  from Products.ZenUtils.ZCmdBase import ZCmdBase 
 24  from Products.ZenUtils.Utils import zenPath 
 25   
 26  import re 
 27  DEFINITIONS=re.compile(r'([A-Za-z-0-9]+) +DEFINITIONS *::= *BEGIN') 
 28  DEPENDS=re.compile(r'FROM *([A-Za-z-0-9]+)') 
 29   
30 -def walk(*dirs):
31 for dir in dirs: 32 for dirname, _, filenames in os.walk(dir): 33 for f in filenames: 34 yield os.path.join(dirname, f)
35
36 -class DependencyMap:
37 - def __init__(self):
38 self.fileMap = {} 39 self.depMap = {}
40
41 - def add(self, filename, name, dependencies):
42 if not self.depMap.has_key(name): 43 self.fileMap[filename] = name 44 self.depMap[name] = (filename, dependencies)
45
46 - def getName(self, filename):
47 return self.fileMap.get(filename, None)
48
49 - def getDependencies(self, name):
50 return self.depMap.get(name, None)
51 52
53 -class zenmib(ZCmdBase):
54
55 - def parse(self, mibfile):
56 fp = open(mibfile) 57 mib = fp.read() 58 fp.close() 59 parts = mib.split('OBJECT IDENTIFIER', 1) 60 match = DEFINITIONS.search(parts[0]) 61 if not match: return None, [] 62 name = match.group(1) 63 depends = [] 64 start = match.end(0) 65 while 1: 66 match = DEPENDS.search(parts[0], start) 67 if not match: break 68 depends.append(match.group(1)) 69 start = match.end(0) 70 return name, depends
71 72
73 - def dependencies(self, filenames):
74 result = DependencyMap() 75 for filename in filenames: 76 defines, depends = self.parse(filename) 77 if defines == None: 78 self.log.info("Skipping file %s", filename) 79 else: 80 result.add(filename, defines, depends) 81 return result
82
83 - def generateDependenciesForSMIDump(self, filename, depMap):
84 deps = [] 85 name = depMap.getName(filename) 86 if not name: 87 return '' 88 def recurse(name): 89 fileAndDeps = depMap.getDependencies(name) 90 if not fileAndDeps: 91 self.log.info("Unable to find a file providing the MIB %s", 92 name) 93 return 94 f, d = fileAndDeps 95 if f and f not in deps: 96 deps.append(f) 97 for n in d: 98 recurse(n)
99 recurse(name) 100 if deps[1:]: 101 return ' -p "' + '" -p "'.join(deps[1:]) + '"' 102 return ''
103 104 MIB_MOD_ATTS = ('language', 'contact', 'description') 105
106 - def load1(self, mibs, mibname, depmap):
107 result = {} 108 self.log.debug("%s", mibname.split('/')[-1]) 109 dependencies = self.generateDependenciesForSMIDump(mibname, depmap) 110 dump = 'smidump -fpython %s "%s" 2>/dev/null' % (dependencies, mibname) 111 self.log.debug('running %s', dump) 112 exec os.popen(dump) in result 113 mib = result.get('MIB', None) 114 if mib: 115 modname = mib['moduleName'] 116 #mod = mibs.findMibModule(modname) 117 mod = None 118 if mod: 119 self.log.warn("skipping %s already loaded", modname) 120 return 121 mod = mibs.createMibModule(modname, self.options.path) 122 for key, val in mib[modname].items(): 123 if key in self.MIB_MOD_ATTS: 124 setattr(mod, key, val) 125 if mib.has_key('nodes'): 126 for name, values in mib['nodes'].items(): 127 mod.createMibNode(name, **values) 128 if mib.has_key('notifications'): 129 for name, values in mib['notifications'].items(): 130 mod.createMibNotification(name, **values) 131 self.log.info("Loaded mib %s", modname) 132 if not self.options.nocommit: transaction.commit() 133 else: 134 self.log.error("Failed to load mib: %s", mibname) 135 if self.options.debug: 136 msg = os.popen('smidump -fpython %s 2>&1' % mibname).read() 137 self.log.error("Error: %s", msg)
138
139 - def load(self):
140 141 smimibdir = zenPath('share/mibs') 142 ietf, iana, irtf, tubs, site = \ 143 map(lambda x: os.path.join(smimibdir, x), 144 'ietf iana irtf tubs site'.split()) 145 146 if len(self.args) > 0: 147 mibnames = self.args 148 depMap = self.dependencies(list(walk(ietf, iana, irtf, tubs)) 149 + mibnames) 150 else: 151 depMap = self.dependencies(walk(ietf, iana, irtf, tubs, site)) 152 mibnames = glob.glob(os.path.join(smimibdir, 'site', '*')) 153 154 mibs = self.dmd.Mibs 155 for mibname in mibnames: 156 try: 157 self.load1(mibs, mibname, depMap) 158 except (SystemExit, KeyboardInterrupt): raise 159 except Exception, ex: 160 self.log.exception("Failed to load mib: %s", mibname)
161 162
163 - def buildOptions(self):
164 ZCmdBase.buildOptions(self) 165 self.parser.add_option('--path', 166 dest='path',default="/", 167 help="path to load mib into") 168 self.parser.add_option('--nocommit', action='store_true', 169 dest='nocommit',default=False, 170 help="don't commit after loading") 171 self.parser.add_option('--debug', action='store_true', 172 dest='debug',default=False, 173 help="print diagnostic information")
174 175 176 if __name__ == '__main__': 177 import sys 178 zm = zenmib() 179 zm.load() 180