poll_tcam.py 4.3 KB

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