nagios library
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

status.py 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import argparse
  2. import json
  3. import os
  4. import requests
  5. import time
  6. DEBUG = False
  7. class colors:
  8. HEADER = '\033[95m'
  9. OKBLUE = '\033[94m'
  10. OKCYAN = '\033[96m'
  11. OKGREEN = '\033[92m'
  12. WARNING = '\033[93m'
  13. FAIL = '\033[91m'
  14. ENDC = '\033[0m'
  15. BOLD = '\033[1m'
  16. UNDERLINE = '\033[4m'
  17. def headline(color: bool) -> str:
  18. rv = ""
  19. if color:
  20. rv += colors.BOLD + colors.UNDERLINE
  21. rv += "NAGIOS Status:"
  22. if color:
  23. rv += colors.ENDC
  24. rv += "\n\n"
  25. return rv
  26. class nagios_service(dict):
  27. SID_OK = 2
  28. SID_WARNING = 4
  29. SID_ERROR = 16
  30. #
  31. SNAME_OK = "OKAY"
  32. SNAME_FLAPPING = "FLAPPING"
  33. SNAME_WARNING = "WARNING"
  34. SNAME_ERROR = "ERROR"
  35. SNAME_UNKNOWN = "UNKNOWN"
  36. TIMEFORMAT = "%d.%m.%Y %H:%M"
  37. def __init__(self, service_dict) -> None:
  38. super().__init__(service_dict)
  39. def is_problem(self):
  40. return self["status"] != self.SID_OK or self.flapping()
  41. def host_name(self):
  42. return self['host_name']
  43. def name(self):
  44. return self['description']
  45. def status(self):
  46. status = self["status"]
  47. default = self.SNAME_UNKNOWN
  48. if status == self.SID_OK and self.flapping():
  49. return self.SNAME_FLAPPING
  50. return {
  51. self.SID_OK: self.SNAME_OK,
  52. self.SID_WARNING: self.SNAME_WARNING,
  53. self.SID_ERROR: self.SNAME_ERROR
  54. }.get(status, default)
  55. def last_okay(self):
  56. return time.strftime(self.TIMEFORMAT, time.localtime(self["last_time_ok"] / 1000))
  57. def info(self):
  58. return self.get("plugin_output") or self.get("long_plugin_output")
  59. def flapping(self):
  60. return self["is_flapping"]
  61. def __color__(self):
  62. return {
  63. self.SNAME_OK: colors.OKGREEN,
  64. self.SNAME_FLAPPING: colors.OKBLUE,
  65. self.SNAME_WARNING: colors.WARNING,
  66. self.SNAME_ERROR: colors.FAIL,
  67. self.SNAME_UNKNOWN: colors.OKCYAN
  68. }.get(self.status())
  69. return colors.OKBLUE
  70. def __head__(self, color=False):
  71. rv = headline(color)
  72. rv += "+-----------------------------------------------------------------------------------------\n"
  73. rv += "| Host | Service Name | State | Last time Okay |\n"
  74. rv += "+---------------------------+-----------------+----------+------------------+-------------\n"
  75. return rv
  76. def __foot__(self):
  77. return "+---------------------------+-----------------+----------+------------------+-------------\n"
  78. def __str__(self, color=False):
  79. cols = []
  80. cols.append((self.host_name, 25, False))
  81. cols.append((self.name, 15, color))
  82. cols.append((self.status, 8, color))
  83. cols.append((self.last_okay, 16, color))
  84. cols.append((self.info, 0, False))
  85. #
  86. rv = ""
  87. for txt_method, l, c in cols:
  88. txt = txt_method()
  89. if len(rv) > 0:
  90. rv += " | "
  91. if c:
  92. if txt_method == self.last_okay:
  93. rv += colors.OKGREEN
  94. else:
  95. rv += self.__color__()
  96. if l > 0:
  97. rv += txt[:l]
  98. rv += " " * (l-len(txt))
  99. else:
  100. rv += txt
  101. if c:
  102. rv += colors.ENDC
  103. #
  104. rv = "| " + rv + "\n"
  105. if DEBUG:
  106. rv += json.dumps(self, indent=4, sort_keys=True)
  107. return rv
  108. class __nagios_status__(dict):
  109. def __init__(self) -> None:
  110. super().__init__()
  111. def all_problems(self):
  112. rv = __nagios_status__()
  113. for host in self:
  114. for service in self[host]:
  115. if service.is_problem():
  116. rv.__add_service__(host, service)
  117. return rv
  118. def __add_service__(self, host, service):
  119. if host not in self:
  120. self[host] = []
  121. self[host].append(nagios_service(service))
  122. def __str__(self, color=False) -> str:
  123. rv = ""
  124. for host in self:
  125. for service in self[host]:
  126. if len(rv) == 0:
  127. rv += service.__head__(color=color)
  128. rv += service.__str__(color=color)
  129. if len(rv) > 0:
  130. rv += service.__foot__()
  131. else:
  132. rv += headline(color)
  133. rv += "No monitorings to be reported."
  134. return rv + "\n"
  135. class nagios_status(__nagios_status__):
  136. URL = "nagios4/cgi-bin/statusjson.cgi?query=servicelist&details=true" # &servicestatus=warning"
  137. KEY_DATA = 'data'
  138. KEY_SERVICELIST = 'servicelist'
  139. def __init__(self, host, secure) -> None:
  140. super().__init__()
  141. #
  142. if secure:
  143. prefix = "https://"
  144. else:
  145. prefix = "http://"
  146. r = requests.get(os.path.join(prefix, host, self.URL))
  147. data = r.json()
  148. hosts = data.get(self.KEY_DATA, {}).get(self.KEY_SERVICELIST, {})
  149. for host in hosts:
  150. for service in hosts.get(host, {}):
  151. self.__add_service__(host, hosts.get(host).get(service))
  152. if __name__ == "__main__":
  153. parser = argparse.ArgumentParser(description="Returns the status of all or partial nagios monitorings.")
  154. parser.add_argument("hostname", help="The hostname and port of the nagios server (e.g. nagios:8080)")
  155. parser.add_argument("-a", "--all", action="store_true", default=False,
  156. help="print the status of all nagios monitorings. Default is all problems.")
  157. parser.add_argument("-s", "--secure", action="store_true", default=False, help="Enables secure connection (https)")
  158. parser.add_argument("-m", "--monochrome", action="store_true", default=False, help="No colored output")
  159. args = parser.parse_args()
  160. #
  161. try:
  162. s = nagios_status(args.hostname, args.secure)
  163. except requests.ConnectionError:
  164. print(headline(not args.monochrome) + "Can not connect to given host.")
  165. else:
  166. if args.all:
  167. print(s.__str__(color=not args.monochrome))
  168. else:
  169. print(s.all_problems().__str__(color=not args.monochrome))