/
fail2ban.py
executable file
·122 lines (108 loc) · 3.81 KB
/
fail2ban.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/python
#
# Fail2Ban is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Fail2Ban is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Fail2Ban; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
#
# Fail2ban jail state fetcher for collectd
# Author: Antti Jaakkola
# Email: annttu@annttu.fi
# based on fail2ban-client code
#
import sys, string, os, logging
import socket
import collectd
# Inserts our own modules path first in the list
# fix for bug #343821
sys.path.insert(1, "/usr/share/fail2ban")
# Now we can import our modules
from client.csocket import CSocket
from client.configurator import Configurator
# Gets the instance of the logger.
logSys = logging.getLogger("fail2ban.client")
##
#
# @todo This class needs cleanup.
class Fail2banClient:
def __init__(self):
self.__stream = None
self.__configurator = Configurator()
self.__conf = dict()
self.__conf["conf"] = "/etc/fail2ban"
self.__conf["socket"] = None
logSys.setLevel(logging.WARNING)
stdout = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(levelname)-6s %(message)s')
stdout.setFormatter(formatter)
logSys.addHandler(stdout)
self.__configurator.setBaseDir(self.__conf["conf"])
self.__configurator.readEarly()
socket = self.__configurator.getEarlyOptions()
if self.__conf["socket"] == None:
self.__conf["socket"] = socket["socket"]
logSys.info("Using socket file " + self.__conf["socket"])
def __processCmd(self, jail = "", listjails = False, showRet = True):
cmd = []
if listjails is True:
cmd.append(['status'])
elif jail != "":
cmd.append(['status', jail])
for c in cmd:
retval = False
try:
logSys.debug("%s" % c)
client = CSocket(self.__conf["socket"])
ret = client.send(c)
if ret[0] == 0:
if listjails is False:
retval = ret[1][1][1][0][1]
else:
retval = [i.strip() for i in ret[1][1][1].split(",")]
if showRet:
logSys.debug("OK : " + `ret[1]`)
logSys.debug("retval: %s" % retval)
return retval
else:
logSys.debug("NOK: " + `ret[1].args`)
logSys.debug(ret[1])
return
except socket.error:
if showRet:
logSys.error("Unable to contact server. Is it running?")
return
except Exception, e:
if showRet:
logSys.error(e)
return
def get_banned(self, jail):
return self.__processCmd(jail)
def list_jails(self):
return self.__processCmd(listjails = True)
class ServerExecutionException(Exception):
pass
client = None
def init():
global client
client = Fail2banClient()
return True
def read(data=None):
global client
jails = client.list_jails()
for jail in jails:
v1 = collectd.Values(type='gauge', interval=10)
v1.plugin='fail2ban-jails-' + jail
v1.dispatch(values=[client.get_banned(jail)])
collectd.register_read(read)
collectd.register_init(init)