poll_tcam.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env 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 os
  27. import re
  28. import sys
  29. import time
  30. import json
  31. import paramiko
  32. import sparker
  33. from multiprocessing import Pool
  34. import traceback
  35. import CLEUCreds
  36. devices = ['CORE1-WA', 'CORE2-WA']
  37. TEAM_NAME = 'CL19 NOC Team'
  38. ROOM_NAME = 'Core Alarms'
  39. CACHE_FILE = '/home/jclarke/tcam_util.json'
  40. spark = None
  41. def exec_command(chan, cmd, dev):
  42. output = ''
  43. chan.send(cmd + '\n')
  44. time.sleep(1)
  45. output += chan.recv(65535)
  46. return output
  47. def get_results(dev, cache):
  48. global TEAM_NAME, ROOM_NAME, spark
  49. commands = [
  50. 'show platform hardware fed active fwd-asic resource tcam utilization']
  51. ssh_client = paramiko.SSHClient()
  52. ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  53. output = ''
  54. try:
  55. ssh_client.connect(dev, username=CLEUCreds.NET_USER, password=CLEUCreds.NET_PASS,
  56. timeout=5, allow_agent=False, look_for_keys=False)
  57. chan = ssh_client.invoke_shell()
  58. try:
  59. exec_command(chan, 'term width 0', dev)
  60. exec_command(chan, 'term length 0', dev)
  61. for cmd in commands:
  62. try:
  63. output = exec_command(chan, cmd, dev)
  64. except Exception as iie:
  65. sys.stderr.write(
  66. 'Failed to get result for {} from {}: {}\n'.format(cmd, dev, iie))
  67. traceback.print_exc()
  68. except Exception as ie:
  69. sys.stderr.write(
  70. 'Failed to setup SSH on {}: {}\n'.format(dev, ie))
  71. traceback.print_exc()
  72. except Exception as e:
  73. sys.stderr.write(
  74. 'Failed to connect to {}: {}\n'.format(dev, e))
  75. ssh_client.close()
  76. ready = False
  77. cache[dev] = {}
  78. for line in output.split('\n'):
  79. line = line.strip()
  80. if re.search(r'^-+$', line):
  81. ready = True
  82. continue
  83. if not ready:
  84. continue
  85. if len(line) == 0:
  86. break
  87. line = re.sub(r'\s+', ' ', line)
  88. elements = line.split(' ')
  89. used = elements[-1]
  90. max = elements[-2]
  91. metric = ' '.join(elements[0:-2])
  92. metric = metric.strip()
  93. m = re.search(r'(\d+)(/\d+)?', used)
  94. used = float(m.group(1))
  95. m = re.search(r'(\d+)(/\d+)?', max)
  96. max = float(m.group(1))
  97. perc = float(used / max) * 100.0
  98. # if metric == 'Directly or indirectly connected routes':
  99. # perc = 76.0
  100. if perc >= 75.0:
  101. msg = '**!!! DANGER DANGER DANGER DANGER !!!**<br>{} on {} is {}% used'.format(
  102. metric, dev, perc)
  103. spark.post_to_spark(TEAM_NAME, ROOM_NAME, msg)
  104. cache[dev][metric] = perc
  105. if __name__ == '__main__':
  106. spark = sparker.Sparker(token=CLEUCreds.SPARK_TOKEN)
  107. cache = {}
  108. for dev in devices:
  109. get_results(dev, cache)
  110. fd = open(CACHE_FILE, 'w')
  111. json.dump(cache, fd, indent=4)
  112. fd.close()