dhcp_scope_watch.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 sys
  27. import json
  28. from sparker import Sparker
  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. SPARK_TEAM = 'CL19 NOC Team'
  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. DHCP_SERVER = '10.100.253.9'
  42. def parse_result(out):
  43. matches = re.findall(r'([\w-]+=[^;]+);(?=\s|$)', out)
  44. res = {}
  45. for m in matches:
  46. if m == '':
  47. continue
  48. k, v = m.split('=')
  49. res[k] = v
  50. return res
  51. def get_results(scope):
  52. global DHCP_SERVER
  53. if scope != '100 Ok' and scope != '':
  54. proc = Popen(shlex.split(
  55. 'ssh -2 root@{} /root/nrcmd.sh -r scope {} getUtilization'.format(DHCP_SERVER, scope)), stdout=PIPE, stderr=PIPE)
  56. out, err = proc.communicate()
  57. if not re.search(r'^100', out):
  58. return None
  59. outd = parse_result(out)
  60. if 'active-dynamic' not in outd or 'total-dynamic' not in outd or 'free-dynamic' not in outd:
  61. return None
  62. util = (float(outd['active-dynamic']) /
  63. 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(
  70. 'ssh -2 root@{} /root/nrcmd.sh -r scope listnames'.format(DHCP_SERVER)), stdout=PIPE, stderr=PIPE)
  71. out, err = proc.communicate()
  72. if not re.search(r'^100', out):
  73. sys.exit(0)
  74. scopes = out.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. SPARK_TEAM, SPARK_ROOM, '**WARNING**: Scope **{0}** is now **{1:.2f}%** utilized ({2} of {3} free addresses remain); suppressing future alerts until resolved'.format(scope, stat['util'], stat['free-dynamic'], stat['total-dynamic']))
  99. else:
  100. curr_state[scope] = False
  101. if scope in prev_state and prev_state[scope]:
  102. spark.post_to_spark(SPARK_TEAM, SPARK_ROOM, '_INFO_: Scope **{0}** is now only **{1:.2f}%** utilized ({2} free addresses out of {3})'.format(
  103. scope, stat['util'], stat['free-dynamic'], stat['total-dynamic']))
  104. fd = open(CACHE_FILE, 'w')
  105. json.dump(curr_state, fd, indent=4)
  106. fd.close()
  107. fd = open(STATS_FILE, 'w')
  108. json.dump(stats, fd, indent=4)
  109. fd.close()