Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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()
Add Comment
Please, Sign In to add comment