1
2
3
4
5
6
7
8
9
10
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
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
38 self.fileMap = {}
39 self.depMap = {}
40
41 - def add(self, filename, name, dependencies):
45
48
51
52
54
55 - def parse(self, mibfile):
71
72
82
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
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
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
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