dnac-hook.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (c) 2017-2023 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. import sys
  28. import json
  29. from sparker import MessageType # type: ignore
  30. import requests
  31. import html
  32. import re
  33. import os
  34. from requests.packages.urllib3.exceptions import InsecureRequestWarning # type: ignore
  35. requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
  36. import logging
  37. from cleu.config import Config as C # type: ignore
  38. DNAC_WEBHOOK = "https://webexapis.com/v1/webhooks/incoming/Y2lzY29zcGFyazovL3VzL1dFQkhPT0svZjAxZDhmNmUtODM3OS00Mzg3LWI4MDUtNzI0YzNjYzEyMzU2"
  39. HEADERS = {"Content-Type": "application/json"}
  40. if __name__ == "__main__":
  41. print("Content-type: application/json\r\n\r\n")
  42. output = sys.stdin.read()
  43. j = json.loads(output)
  44. logging.basicConfig(
  45. format="%(asctime)s - %(name)s - %(levelname)s : %(message)s", filename="/var/log/dnac-hook.log", level=logging.DEBUG
  46. )
  47. logging.debug(json.dumps(j, indent=4))
  48. # seen_issues[det["issueId"]] = det["issueMessage"]
  49. if "Assurance Issue Status" not in j["details"]:
  50. print("{}")
  51. mt = MessageType(MessageType.WARNING)
  52. verb = "has an"
  53. if j["details"]["Assurance Issue Status"] != "active":
  54. mt = MessageType(MessageType.GOOD)
  55. verb = "no longer has an"
  56. elif j["details"]["Assurance Issue Priority"] == "P1":
  57. mt = MessageType(MessageType.BAD)
  58. link = j["ciscoDnaEventLink"]
  59. link = html.unescape(link)
  60. link = re.sub(r"\<DNAC_IP_ADDRESS\>", os.environ["REMOTE_ADDR"], link)
  61. message = (
  62. f'{mt.value} Device **{j["details"]["Device"]}** {verb} <a href="{link}">issue</a> : {j["details"]["Assurance Issue Details"]}'
  63. )
  64. print(requests.post(DNAC_WEBHOOK, headers=HEADERS, json={"markdown": message}))