nat_limit_watch.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 print_function
  27. from future import standard_library
  28. standard_library.install_aliases()
  29. import paramiko
  30. import os
  31. from sparker import Sparker, MessageType
  32. import time
  33. from subprocess import Popen, PIPE, call
  34. import shlex
  35. import re
  36. import json
  37. import argparse
  38. import CLEUCreds
  39. import shutil
  40. from cleu.config import Config as C
  41. SPARK_ROOM = "Core Alarms"
  42. CACHE_FILE = "/home/jclarke/nat_limit.dat"
  43. def send_command(chan, command):
  44. chan.sendall(command + "\n")
  45. time.sleep(0.5)
  46. output = ""
  47. i = 0
  48. while i < 60:
  49. r = chan.recv(65535)
  50. if len(r) == 0:
  51. raise EOFError("Remote host has closed the connection")
  52. r = r.decode("utf-8", "ignore")
  53. output += r
  54. if re.search(r"[#>]$", r.strip()):
  55. break
  56. time.sleep(1)
  57. return output
  58. if __name__ == "__main__":
  59. prev_state = {}
  60. curr_state = {}
  61. spark = Sparker(token=CLEUCreds.SPARK_TOKEN)
  62. if os.path.exists(CACHE_FILE):
  63. fd = open(CACHE_FILE, "r")
  64. prev_state = json.load(fd)
  65. fd.close()
  66. ssh_client = paramiko.SSHClient()
  67. ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  68. routers = ["CORE1-EDGE", "CORE2-EDGE"]
  69. for router in routers:
  70. try:
  71. ssh_client.connect(
  72. router, username=CLEUCreds.NET_USER, password=CLEUCreds.NET_PASS, timeout=60, allow_agent=False, look_for_keys=False,
  73. )
  74. chan = ssh_client.invoke_shell()
  75. try:
  76. send_command(chan, "term length 0")
  77. send_command(chan, "term width 0")
  78. except:
  79. pass
  80. output = ""
  81. try:
  82. output = send_command(chan, "show ip nat limit all-host | inc [0-9] +[1-9][0-9]+[^0-9]+$")
  83. except Exception as ie:
  84. print("Failed to get NAT limit from {}: {}".format(router, ie))
  85. continue
  86. for line in output.split("\n"):
  87. m = re.search(r"^(\d+\.\d+\.\d+\.\d+)\s+\d+\s+\d+\s+(\d+)", line)
  88. if m:
  89. host = m.group(1)
  90. misses = m.group(2)
  91. if host not in prev_state:
  92. spark.post_to_spark(
  93. C.WEBEX_TEAM,
  94. SPARK_ROOM,
  95. "Host **{}** has exceeded its NAT connection limit **{}** times".format(host, misses),
  96. MessageType.BAD,
  97. )
  98. curr_state[host] = int(misses)
  99. for host, misses in list(prev_state.items()):
  100. if host not in curr_state:
  101. spark.post_to_spark(
  102. C.WEBEX_TEAM, SPARK_ROOM, "Host **{}** has aged out of the NAT limit exceeded table".format(host), MessageType.GOOD
  103. )
  104. except Exception as e:
  105. ssh_client.close()
  106. print("Failed to get NAT limits from {}: {}".format(router, e))
  107. continue
  108. ssh_client.close()
  109. fd = open(CACHE_FILE, "w")
  110. json.dump(curr_state, fd, indent=4)
  111. fd.close()