tool-to-observium.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/local/bin/python2
  2. #
  3. # Copyright (c) 2017-2019 Joe Clarke <jclarke@cisco.com>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. # 1. Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. # SUCH DAMAGE.
  26. import requests
  27. from requests.packages.urllib3.exceptions import InsecureRequestWarning
  28. requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
  29. import json
  30. import sys
  31. import time
  32. import os
  33. from subprocess import Popen, PIPE, call
  34. import shlex
  35. from sparker import Sparker
  36. import pprint
  37. import re
  38. import socket
  39. import CLEUCreds
  40. MONITORING = 'cl-monitoring.ciscolive.network'
  41. CACHE_FILE = '/home/jclarke/monitored_devs.json'
  42. TOOL = 'tool.ciscolive.network'
  43. def get_devs():
  44. global TOOL
  45. url = "http://{}/get/switches/json".format(TOOL)
  46. devs = []
  47. response = requests.request('GET', url)
  48. code = 200
  49. code = response.status_code
  50. if code == 200:
  51. devs = json.loads(response.text)
  52. return devs
  53. def delete_device(dev):
  54. global MONITORING
  55. res = call(shlex.split(
  56. 'ssh -2 {} /usr/local/www/observium/delete_device.php {} rrd'.format(MONITORING, dev)))
  57. return res
  58. if __name__ == '__main__':
  59. devs = {}
  60. force = False
  61. changed_devs = False
  62. if len(sys.argv) == 2:
  63. if sys.argv[1] == '-f':
  64. force = True
  65. try:
  66. fd = open(CACHE_FILE, 'r')
  67. devs = json.load(fd)
  68. fd.close()
  69. except:
  70. pass
  71. tdevs = get_devs()
  72. i = 0
  73. for tdev in tdevs:
  74. i += 1
  75. if not tdev['Reachable']:
  76. continue
  77. if re.search(r'.*CORE.*', tdev['Hostname'], flags=re.I) or re.search(r'.*MDF.*', tdev['Hostname'], flags=re.I):
  78. continue
  79. if tdev['AssetTag'] in devs.keys() and devs[tdev['AssetTag']] != tdev['Hostname']:
  80. print('=== Deleting device {} from Observium ({} / {}) ==='.format(
  81. tdev['Hostname'], i, len(tdevs)))
  82. res = delete_device(devs[tdev['AssetTag']])
  83. if res != 0:
  84. print(
  85. '\n\n***WARNING: Failed to remove Observium device for {}'.format(devs[tdev['AssetTag']]))
  86. print('=== DONE. ===')
  87. changed_devs = True
  88. del devs[tdev['AssetTag']]
  89. print('=== Adding DNS record for {} > {} ({} / {}) ==='.format(
  90. tdev['Hostname'], tdev['IPAddress'], i, len(tdevs)))
  91. res = call(shlex.split(
  92. '/home/jclarke/update_dns.py --ip {} --host {}'.format(tdev['IPAddress'], tdev['Hostname'])))
  93. if res != 0:
  94. print('\n\n***ERROR: Failed to add DNS record')
  95. continue
  96. print('=== DONE. ===')
  97. if tdev['AssetTag'] not in devs.keys() or force:
  98. if force:
  99. print('=== Deleting device {} from Observium ({} / {}) ==='.format(
  100. tdev['Hostname'], i, len(tdevs)))
  101. res = delete_device(tdev['Hostname'])
  102. if res != 0:
  103. print(
  104. '\n\n***WARNING: Failed to remove Observium device {}'.format(tdev['Hostname']))
  105. print('=== DONE. ===')
  106. time.sleep(3)
  107. print('=== Adding device {} to Observium ({} / {}) ==='.format(
  108. tdev['Hostname'], i, len(tdevs)))
  109. res = call(shlex.split(
  110. 'ssh -2 {} /usr/local/www/observium/add_device.php {} ap v3 CLEUR {} {} sha des'.format(MONITORING, tdev['Hostname'], CLEUCreds.SNMP_TOOL_AUTH_PASS, CLEUCreds.SNMP_TOOL_PRIV_PASS)), stdout=PIPE, stderr=PIPE)
  111. if res != 0:
  112. print(
  113. '\n\n***ERROR: Failed to add {} to Observium'.format(tdev['Hostname']))
  114. continue
  115. print('=== DONE. ===')
  116. changed_devs = True
  117. devs[tdev['AssetTag']] = tdev['Hostname']
  118. if changed_devs:
  119. fd = open(CACHE_FILE, 'w')
  120. json.dump(devs, fd)
  121. fd.close()