Skip to content

Commit ed1c91f

Browse files
authored
Merge pull request #4737 from zenoss/backport/ZEN-35390.7x
ZEN-35390: Adding and downloading MIBs takes around 20 minutes
2 parents ec309fb + 49b29e4 commit ed1c91f

File tree

6 files changed

+56
-21
lines changed

6 files changed

+56
-21
lines changed

src/Products/ZenModel/MibOrganizer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ def handleUploadedFile(self, REQUEST):
253253
savedMIBPath,
254254
"--path=%s" % mypath,
255255
"--mibdepsdir=%s" % zenPath(_pathToMIB),
256+
"--inMainProcess",
256257
]
257258
return self.dmd.JobManager.addJob(
258259
SubprocessJob,

src/Products/ZenModel/tests/test_zenmib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def test_MIBFileProcessor(self, mockSMIDumpTool):
8282
processor = MIBFileProcessor(log, self.dmd, options, [source])
8383
processor.run()
8484

85-
mockTool.run.assert_called_with(expectedMIBFile)
85+
mockTool.run.assert_called_with(False, expectedMIBFile)
8686
self._checkResults()
8787

8888
def test_DumpFileProcessor(self):

src/Products/ZenModel/zenmib.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,11 @@ def __init__(self, log, dmd, options, mibfiles):
114114
self._mibsdir = options.mibsdir
115115
self._mibfiles = mibfiles
116116

117-
def run(self):
117+
def run(self, in_main_process=False):
118+
"""
119+
By default, we execute command in the sub process.
120+
In case of zenjob usage we explicitly pass --inMainProcess cli arg for zenmib
121+
"""
118122
mibfiles = self._getMIBFiles()
119123
paths = [zenPath("share", "mibs"), self._mibdepsdir]
120124

@@ -130,7 +134,7 @@ def run(self):
130134
tool = SMIDumpTool(config=cfg)
131135
# returns string containing all the MIB definitions found
132136
# in the provided set of MIBFile objects.
133-
dump = tool.run(*mibfiles)
137+
dump = tool.run(in_main_process, *mibfiles)
134138
# for defn in dump.definitions:
135139
# print defn
136140
if dump:
@@ -207,7 +211,7 @@ def run(self):
207211
self.log, self.dmd, self.options, self.args
208212
)
209213

210-
processor.run()
214+
processor.run(in_main_process=self.options.in_main_process)
211215

212216
if not self.options.nocommit:
213217
transaction.commit()
@@ -316,6 +320,13 @@ def buildOptions(self):
316320
default=False,
317321
help="Remove zeros found in the middle of the OID",
318322
)
323+
self.parser.add_option(
324+
"--inMainProcess",
325+
dest="in_main_process",
326+
action="store_true",
327+
default=False,
328+
help="Run the nested linux command in a current process (default: False)",
329+
)
319330

320331

321332
def main():

src/Products/ZenUtils/mib/smidump.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import uuid
2+
import os
13
import logging
24
import re
35

@@ -216,7 +218,7 @@ def __init__(self, config=None):
216218
if config:
217219
self._cmd.extend(["--config", config.filename])
218220

219-
def run(self, *mibfiles):
221+
def run(self, in_main_process=False, *mibfiles):
220222
"""Takes the given MIBFile objects and returns a string containing
221223
the output of the 'smidump -f python' command. If multiple MIBFiles
222224
are given, the returned string will contain the MIB definitions from
@@ -227,14 +229,35 @@ def run(self, *mibfiles):
227229
if log.getEffectiveLevel() <= logging.DEBUG:
228230
log.debug("Executing command: %s", ' '.join(cmd))
229231

230-
process = Popen(cmd, stdout=PIPE, stderr=PIPE, close_fds=True)
231-
output, error = process.communicate() # blocks until done
232-
rc = process.poll()
232+
error = ""
233+
if not in_main_process:
234+
process = Popen(cmd, stdout=PIPE, stderr=PIPE, close_fds=False)
235+
output, error = process.communicate() # blocks until done
236+
rc = process.poll()
237+
else:
238+
unique_id = uuid.uuid4().hex
239+
output_file = "/tmp/mib_command_output_%s.txt" % unique_id
240+
error_file = "/tmp/mib_command_error_%s.txt" % unique_id
241+
cmd.extend([">", output_file, "2>", error_file])
242+
243+
try:
244+
with open(error_file, 'r') as ef:
245+
error = ef.read()
246+
except (OSError, IOError):
247+
pass # No errors while running the command
248+
else:
249+
os.system("rm -f " + error_file)
250+
251+
rc = os.system(" ".join(cmd))
252+
253+
with open(output_file, 'r') as f:
254+
output = f.read()
255+
os.system("rm -f " + output_file)
233256

234257
if rc != 0:
235258
raise RuntimeError("smidump failed:\n" + error.strip())
236259

237-
if log.getEffectiveLevel() <= logging.DEBUG:
260+
if log.getEffectiveLevel() <= logging.DEBUG and error:
238261
for line in (ln.strip() for ln in error.split("\n")):
239262
if line:
240263
log.warn("[smidump] %s", line)

src/Products/ZenUtils/mib/tests/test_smidump.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,10 @@ def test_cmdline_no_config(self):
190190
]
191191

192192
tool = SMIDumpTool()
193-
dump = tool.run(mibfile)
193+
dump = tool.run(False, mibfile)
194194
self.assertIsInstance(dump, SMIDump)
195195
self.mock_popen.assert_called_once_with(
196-
expected_cmd, stdout=PIPE, stderr=PIPE, close_fds=True
196+
expected_cmd, stdout=PIPE, stderr=PIPE, close_fds=False
197197
)
198198

199199
@mock.patch("Products.ZenUtils.mib.tests.test_smidump.SMIConfigFile")
@@ -217,11 +217,11 @@ def test_cmdline_with_config(self, mockConfig):
217217

218218
with mockConfig as cfg:
219219
tool = SMIDumpTool(config=cfg)
220-
dump = tool.run(mibfile)
220+
dump = tool.run(False, mibfile)
221221

222222
self.assertIsInstance(dump, SMIDump)
223223
self.mock_popen.assert_called_once_with(
224-
expected_cmd, stdout=PIPE, stderr=PIPE, close_fds=True
224+
expected_cmd, stdout=PIPE, stderr=PIPE, close_fds=False
225225
)
226226

227227
def test_cmdline_n_files(self):
@@ -238,10 +238,10 @@ def test_cmdline_n_files(self):
238238
]
239239

240240
tool = SMIDumpTool()
241-
dump = tool.run(mibfile1, mibfile2)
241+
dump = tool.run(False, mibfile1, mibfile2)
242242
self.assertIsInstance(dump, SMIDump)
243243
self.mock_popen.assert_called_once_with(
244-
expected_cmd, stdout=PIPE, stderr=PIPE, close_fds=True
244+
expected_cmd, stdout=PIPE, stderr=PIPE, close_fds=False
245245
)
246246

247247
def test_cmdline_one_file_n_modules(self):
@@ -257,10 +257,10 @@ def test_cmdline_one_file_n_modules(self):
257257
expected_cmd.extend(mibfile.module_names[:-1])
258258

259259
tool = SMIDumpTool()
260-
dump = tool.run(mibfile)
260+
dump = tool.run(False,mibfile)
261261
self.assertIsInstance(dump, SMIDump)
262262
self.mock_popen.assert_called_once_with(
263-
expected_cmd, stdout=PIPE, stderr=PIPE, close_fds=True
263+
expected_cmd, stdout=PIPE, stderr=PIPE, close_fds=False
264264
)
265265

266266
def test_cmdline_n_files_n_modules(self):
@@ -285,10 +285,10 @@ def test_cmdline_n_files_n_modules(self):
285285
]
286286

287287
tool = SMIDumpTool()
288-
dump = tool.run(mockMibFile1, mockMibFile2)
288+
dump = tool.run(False, mockMibFile1, mockMibFile2)
289289
self.assertIsInstance(dump, SMIDump)
290290
self.mock_popen.assert_called_once_with(
291-
expected_cmd, stdout=PIPE, stderr=PIPE, close_fds=True
291+
expected_cmd, stdout=PIPE, stderr=PIPE, close_fds=False
292292
)
293293

294294
def test_cmd_error(self):
@@ -301,5 +301,5 @@ def test_cmd_error(self):
301301

302302
tool = SMIDumpTool()
303303
with self.assertRaises(RuntimeError) as ex:
304-
tool.run(mockMibFile)
304+
tool.run(False, mockMibFile)
305305
self.assertMultiLineEqual(str(ex.exception), "smidump failed:\nboom")

src/Products/Zuul/facades/mibfacade.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def getMibBaseNodeTree(self, id, relation='nodes'):
169169
@info
170170
def addMibPackage(self, package, organizer):
171171
args = [binPath('zenmib'), 'run', package,
172-
'--path=%s' % organizer]
172+
'--path=%s' % organizer, "--inMainProcess"]
173173
jobStatus = self._dmd.JobManager.addJob(
174174
SubprocessJob,
175175
description="Add MIB package %s" % package,

0 commit comments

Comments
 (0)