fail2ban.chart.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. # -*- coding: utf-8 -*-
  2. # Description: fail2ban log netdata python.d module
  3. # Author: ilyam8
  4. # SPDX-License-Identifier: GPL-3.0-or-later
  5. import os
  6. import re
  7. from collections import defaultdict
  8. from glob import glob
  9. from bases.FrameworkServices.LogService import LogService
  10. ORDER = [
  11. 'jails_failed_attempts',
  12. 'jails_bans',
  13. 'jails_banned_ips',
  14. ]
  15. def charts(jails):
  16. """
  17. Chart definitions creating
  18. """
  19. ch = {
  20. ORDER[0]: {
  21. 'options': [None, 'Failed attempts', 'attempts/s', 'failed attempts', 'fail2ban.failed_attempts', 'line'],
  22. 'lines': []
  23. },
  24. ORDER[1]: {
  25. 'options': [None, 'Bans', 'bans/s', 'bans', 'fail2ban.bans', 'line'],
  26. 'lines': []
  27. },
  28. ORDER[2]: {
  29. 'options': [None, 'Banned IP addresses (since the last restart of netdata)', 'ips', 'banned ips',
  30. 'fail2ban.banned_ips', 'line'],
  31. 'lines': []
  32. },
  33. }
  34. for jail in jails:
  35. dim = ['{0}_failed_attempts'.format(jail), jail, 'incremental']
  36. ch[ORDER[0]]['lines'].append(dim)
  37. dim = [jail, jail, 'incremental']
  38. ch[ORDER[1]]['lines'].append(dim)
  39. dim = ['{0}_in_jail'.format(jail), jail, 'absolute']
  40. ch[ORDER[2]]['lines'].append(dim)
  41. return ch
  42. RE_JAILS = re.compile(r'\[([a-zA-Z0-9_-]+)\][^\[\]]+?enabled\s+= +(true|yes|false|no)')
  43. ACTION_BAN = 'Ban'
  44. ACTION_UNBAN = 'Unban'
  45. ACTION_RESTORE_BAN = 'Restore Ban'
  46. ACTION_FOUND = 'Found'
  47. # Example:
  48. # 2018-09-12 11:45:58,727 fail2ban.actions[25029]: WARNING [ssh] Found 203.0.113.1
  49. # 2018-09-12 11:45:58,727 fail2ban.actions[25029]: WARNING [ssh] Ban 203.0.113.1
  50. # 2018-09-12 11:45:58,727 fail2ban.actions[25029]: WARNING [ssh] Restore Ban 203.0.113.1
  51. # 2018-09-12 11:45:53,715 fail2ban.actions[25029]: WARNING [ssh] Unban 203.0.113.1
  52. RE_DATA = re.compile(
  53. r'\[(?P<jail>[A-Za-z-_0-9]+)\] (?P<action>{0}|{1}|{2}|{3}) (?P<ip>[a-f0-9.:]+)'.format(
  54. ACTION_BAN, ACTION_UNBAN, ACTION_RESTORE_BAN, ACTION_FOUND
  55. )
  56. )
  57. DEFAULT_JAILS = [
  58. 'ssh',
  59. ]
  60. class Service(LogService):
  61. def __init__(self, configuration=None, name=None):
  62. LogService.__init__(self, configuration=configuration, name=name)
  63. self.order = ORDER
  64. self.definitions = dict()
  65. self.log_path = self.configuration.get('log_path', '/var/log/fail2ban.log')
  66. self.conf_path = self.configuration.get('conf_path', '/etc/fail2ban/jail.local')
  67. self.conf_dir = self.configuration.get('conf_dir', '/etc/fail2ban/jail.d/')
  68. self.exclude = self.configuration.get('exclude', str())
  69. self.monitoring_jails = list()
  70. self.banned_ips = defaultdict(set)
  71. self.data = dict()
  72. def check(self):
  73. """
  74. :return: bool
  75. """
  76. if not self.conf_path.endswith(('.conf', '.local')):
  77. self.error('{0} is a wrong conf path name, must be *.conf or *.local'.format(self.conf_path))
  78. return False
  79. if not os.access(self.log_path, os.R_OK):
  80. self.error('{0} is not readable'.format(self.log_path))
  81. return False
  82. if os.path.getsize(self.log_path) == 0:
  83. self.error('{0} is empty'.format(self.log_path))
  84. return False
  85. self.monitoring_jails = self.jails_auto_detection()
  86. for jail in self.monitoring_jails:
  87. self.data['{0}_failed_attempts'.format(jail)] = 0
  88. self.data[jail] = 0
  89. self.data['{0}_in_jail'.format(jail)] = 0
  90. self.definitions = charts(self.monitoring_jails)
  91. self.info('monitoring jails: {0}'.format(self.monitoring_jails))
  92. return True
  93. def get_data(self):
  94. """
  95. :return: dict
  96. """
  97. raw = self._get_raw_data()
  98. if not raw:
  99. return None if raw is None else self.data
  100. for row in raw:
  101. match = RE_DATA.search(row)
  102. if not match:
  103. continue
  104. match = match.groupdict()
  105. if match['jail'] not in self.monitoring_jails:
  106. continue
  107. jail, action, ip = match['jail'], match['action'], match['ip']
  108. if action == ACTION_FOUND:
  109. self.data['{0}_failed_attempts'.format(jail)] += 1
  110. elif action in (ACTION_BAN, ACTION_RESTORE_BAN):
  111. self.data[jail] += 1
  112. if ip not in self.banned_ips[jail]:
  113. self.banned_ips[jail].add(ip)
  114. self.data['{0}_in_jail'.format(jail)] += 1
  115. elif action == ACTION_UNBAN:
  116. if ip in self.banned_ips[jail]:
  117. self.banned_ips[jail].remove(ip)
  118. self.data['{0}_in_jail'.format(jail)] -= 1
  119. return self.data
  120. def get_files_from_dir(self, dir_path, suffix):
  121. """
  122. :return: list
  123. """
  124. if not os.path.isdir(dir_path):
  125. self.error('{0} is not a directory'.format(dir_path))
  126. return list()
  127. return glob('{0}/*.{1}'.format(self.conf_dir, suffix))
  128. def get_jails_from_file(self, file_path):
  129. """
  130. :return: list
  131. """
  132. if not os.access(file_path, os.R_OK):
  133. self.error('{0} is not readable or not exist'.format(file_path))
  134. return list()
  135. with open(file_path, 'rt') as f:
  136. lines = f.readlines()
  137. raw = ' '.join(line for line in lines if line.startswith(('[', 'enabled')))
  138. match = RE_JAILS.findall(raw)
  139. # Result: [('ssh', 'true'), ('dropbear', 'true'), ('pam-generic', 'true'), ...]
  140. if not match:
  141. self.debug('{0} parse failed'.format(file_path))
  142. return list()
  143. return match
  144. def jails_auto_detection(self):
  145. """
  146. :return: list
  147. Parses jail configuration files. Returns list of enabled jails.
  148. According man jail.conf parse order must be
  149. * jail.conf
  150. * jail.d/*.conf (in alphabetical order)
  151. * jail.local
  152. * jail.d/*.local (in alphabetical order)
  153. """
  154. jails_files, all_jails, active_jails = list(), list(), list()
  155. jails_files.append('{0}.conf'.format(self.conf_path.rsplit('.')[0]))
  156. jails_files.extend(self.get_files_from_dir(self.conf_dir, 'conf'))
  157. jails_files.append('{0}.local'.format(self.conf_path.rsplit('.')[0]))
  158. jails_files.extend(self.get_files_from_dir(self.conf_dir, 'local'))
  159. self.debug('config files to parse: {0}'.format(jails_files))
  160. for f in jails_files:
  161. all_jails.extend(self.get_jails_from_file(f))
  162. exclude = self.exclude.split()
  163. for name, status in all_jails:
  164. if name in exclude:
  165. continue
  166. if status in ('true', 'yes') and name not in active_jails:
  167. active_jails.append(name)
  168. elif status in ('false', 'no') and name in active_jails:
  169. active_jails.remove(name)
  170. return active_jails or DEFAULT_JAILS