dhcp_scope_watch.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. import sys
  27. import json
  28. from sparker import Sparker, MessageType
  29. from subprocess import Popen, PIPE
  30. import re
  31. import shlex
  32. import requests
  33. import os
  34. from multiprocessing import Pool
  35. import CLEUCreds
  36. from cleu.config import Config as C
  37. SPARK_ROOM = "DHCP Scope Alarms"
  38. THRESHOLD = "75"
  39. CACHE_FILE = "/home/jclarke/dhcp_scope.dat"
  40. STATS_FILE = "/home/jclarke/dhcp_scope_stats.dat"
  41. def parse_result(out):
  42. matches = re.findall(r"([\w-]+=[^;]+);(?=\s|$)", out)
  43. res = {}
  44. for m in matches:
  45. if m == "":
  46. continue
  47. k, v = m.split("=")
  48. res[k] = v
  49. return res
  50. def get_results(scope):
  51. global DHCP_SERVER
  52. if scope != "100 Ok" and scope != "":
  53. proc = Popen(
  54. shlex.split("ssh -2 root@{} /root/nrcmd.sh -r scope {} getUtilization".format(C.DHCP_SERVER, scope)), stdout=PIPE, stderr=PIPE
  55. )
  56. out, err = proc.communicate()
  57. outs = out.decode("utf-8")
  58. if not re.search(r"^100", outs):
  59. return None
  60. outd = parse_result(outs)
  61. if "active-dynamic" not in outd or "total-dynamic" not in outd or "free-dynamic" not in outd:
  62. return None
  63. util = (float(outd["active-dynamic"]) / float(outd["total-dynamic"])) * 100.0
  64. # print('Util for {0} is {1:.2f}% utilized'.format(scope, util))
  65. return (scope, {"util": util, "active-dynamic": outd["active-dynamic"], "total-dynamic": outd["total-dynamic"]})
  66. def get_metrics(pool):
  67. global DHCP_SERVER
  68. response = {}
  69. proc = Popen(shlex.split("ssh -2 root@{} /root/nrcmd.sh -r scope listnames".format(C.DHCP_SERVER)), stdout=PIPE, stderr=PIPE)
  70. out, err = proc.communicate()
  71. outs = out.decode("utf-8")
  72. if not re.search(r"^100", outs):
  73. sys.exit(0)
  74. scopes = outs.split("\n")
  75. results = [pool.apply_async(get_results, [s]) for s in scopes]
  76. for res in results:
  77. retval = res.get()
  78. if retval is not None:
  79. response[retval[0]] = retval[1]
  80. return response
  81. if __name__ == "__main__":
  82. prev_state = {}
  83. curr_state = {}
  84. stats = {}
  85. spark = Sparker(token=CLEUCreds.SPARK_TOKEN)
  86. if os.path.exists(CACHE_FILE):
  87. fd = open(CACHE_FILE, "r")
  88. prev_state = json.load(fd)
  89. fd.close()
  90. pool = Pool(20)
  91. metrics = get_metrics(pool)
  92. for scope, stat in metrics.items():
  93. stats[scope] = {"perc": stat["util"]}
  94. if stat["util"] >= float(THRESHOLD):
  95. curr_state[scope] = True
  96. if scope not in prev_state or (scope in prev_state and not prev_state[scope]):
  97. spark.post_to_spark(
  98. C.WEBEX_TAM,
  99. SPARK_ROOM,
  100. "Scope **{0}** is now **{1:.2f}%** utilized ({2} of {3} free addresses remain); suppressing future alerts until resolved".format(
  101. scope, stat["util"], stat["free-dynamic"], stat["total-dynamic"]
  102. ),
  103. MessageType.WARNING,
  104. )
  105. else:
  106. curr_state[scope] = False
  107. if scope in prev_state and prev_state[scope]:
  108. spark.post_to_spark(
  109. C.WEBEX_TEAM,
  110. SPARK_ROOM,
  111. "Scope **{0}** is now only **{1:.2f}%** utilized ({2} free addresses out of {3})".format(
  112. scope, stat["util"], stat["free-dynamic"], stat["total-dynamic"]
  113. ),
  114. MessageType.GOOD,
  115. )
  116. fd = open(CACHE_FILE, "w")
  117. json.dump(curr_state, fd, indent=4)
  118. fd.close()
  119. fd = open(STATS_FILE, "w")
  120. json.dump(stats, fd, indent=4)
  121. fd.close()