Skip to content

Instantly share code, notes, and snippets.

@cluther
Created July 16, 2018 22:42
Show Gist options
  • Save cluther/df6b12ae3a9b5a8cd88d6836475b884c to your computer and use it in GitHub Desktop.
Save cluther/df6b12ae3a9b5a8cd88d6836475b884c to your computer and use it in GitHub Desktop.
Import CISCO-PRODUCTS-MIB into Zenoss
#!/usr/bin/env python
import os
import re
import subprocess
import sys
import Globals
import transaction
from Products.ZenUtils.ZenScriptBase import ZenScriptBase
class CiscoProductImporter(ZenScriptBase):
def __init__(self):
super(CiscoProductImporter, self).__init__(connect=True)
def buildOptions(self):
super(CiscoProductImporter, self).buildOptions()
self.parser.add_option(
"--cisco-smi-mib",
dest="cisco_smi_mib",
default="CISCO-SMI.my",
help="Path to CISCO-SMI.my file.")
self.parser.add_option(
"--cisco-product-mib",
dest="cisco_product_mib",
default="CISCO-PRODUCTS-MIB.my",
help="Path to CISCO-PRODUCTS-MIB file.")
def run(self):
if not os.path.isfile(self.options.cisco_smi_mib):
sys.exit("Can't open {}.".format(self.options_cisco_smi_mib))
if not os.path.isfile(self.options.cisco_product_mib):
sys.exit("Can't open {}.".format(self.options.cisco_product_mib))
output = subprocess.check_output(
"smidump -f identifiers {} {}".format(
self.options.cisco_smi_mib,
self.options.cisco_product_mib),
shell=True)
product_match = re.compile(
r'^CISCO-PRODUCTS-MIB\s+'
r'(?P<name>[^\s]+)\s+'
r'node\s+'
r'(?P<oid>1\.3\.6\.1\.4\.1\.9\.1\.[\d]+)$').match
new, existing = 0, 0
for line in output.splitlines():
match = product_match(line)
if not match:
continue
name = match.group("name")
oid = ".{}".format(match.group("oid"))
product = self.dmd.Manufacturers.findProduct(oid)
if product:
existing += 1
else:
new += 1
product = self.dmd.Manufacturers.createHardwareProduct(
prodName=name,
manufacturer="Cisco",
productKey=oid)
transaction.commit()
print "Created {} Cisco products. {} already existed.".format(
new,
existing)
def main():
importer = CiscoProductImporter()
importer.run()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment