Guest User

Untitled

a guest
Jul 16th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import os
  3. import re
  4. import subprocess
  5. import sys
  6.  
  7. import Globals
  8. import transaction
  9.  
  10. from Products.ZenUtils.ZenScriptBase import ZenScriptBase
  11.  
  12.  
  13. class CiscoProductImporter(ZenScriptBase):
  14. def __init__(self):
  15. super(CiscoProductImporter, self).__init__(connect=True)
  16.  
  17. def buildOptions(self):
  18. super(CiscoProductImporter, self).buildOptions()
  19.  
  20. self.parser.add_option(
  21. "--cisco-smi-mib",
  22. dest="cisco_smi_mib",
  23. default="CISCO-SMI.my",
  24. help="Path to CISCO-SMI.my file.")
  25.  
  26. self.parser.add_option(
  27. "--cisco-product-mib",
  28. dest="cisco_product_mib",
  29. default="CISCO-PRODUCTS-MIB.my",
  30. help="Path to CISCO-PRODUCTS-MIB file.")
  31.  
  32. def run(self):
  33. if not os.path.isfile(self.options.cisco_smi_mib):
  34. sys.exit("Can't open {}.".format(self.options_cisco_smi_mib))
  35.  
  36. if not os.path.isfile(self.options.cisco_product_mib):
  37. sys.exit("Can't open {}.".format(self.options.cisco_product_mib))
  38.  
  39. output = subprocess.check_output(
  40. "smidump -f identifiers {} {}".format(
  41. self.options.cisco_smi_mib,
  42. self.options.cisco_product_mib),
  43. shell=True)
  44.  
  45. product_match = re.compile(
  46. r'^CISCO-PRODUCTS-MIB\s+'
  47. r'(?P<name>[^\s]+)\s+'
  48. r'node\s+'
  49. r'(?P<oid>1\.3\.6\.1\.4\.1\.9\.1\.[\d]+)$').match
  50.  
  51. new, existing = 0, 0
  52.  
  53. for line in output.splitlines():
  54. match = product_match(line)
  55. if not match:
  56. continue
  57.  
  58. name = match.group("name")
  59. oid = ".{}".format(match.group("oid"))
  60.  
  61. product = self.dmd.Manufacturers.findProduct(oid)
  62. if product:
  63. existing += 1
  64. else:
  65. new += 1
  66. product = self.dmd.Manufacturers.createHardwareProduct(
  67. prodName=name,
  68. manufacturer="Cisco",
  69. productKey=oid)
  70.  
  71. transaction.commit()
  72. print "Created {} Cisco products. {} already existed.".format(
  73. new,
  74. existing)
  75.  
  76.  
  77. def main():
  78. importer = CiscoProductImporter()
  79. importer.run()
  80.  
  81.  
  82. if __name__ == '__main__':
  83. main()
Add Comment
Please, Sign In to add comment