-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfail2ban-subscriber.py
executable file
·59 lines (50 loc) · 1.86 KB
/
fail2ban-subscriber.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
#!/usr/bin/env python3
# for configuration, please refer to fail2ban-cluster.conf
import zmq
import sys
import time
import signal
import syslog
from configparsing import ConfigParsing
from daemon import daemon
from subscriber import Subscriber
global subscriberconfig
subscriberconfig = ConfigParsing().Section(section='subscriber')
syslog.openlog(logoption=syslog.LOG_PID, facility=syslog.LOG_AUTH)
class f2bSubscriberDaemon(daemon):
def __sigTERMhandler(self, signum, frame):
syslog.syslog("fail2ban-zmq-tools Subscriber: Caught signal %d.\
Initiating shutdown..." % signum)
self.quit()
def run(self):
syslog.syslog("fail2ban-zmq-tools Subscriber starting")
signal.signal(signal.SIGTERM, self.__sigTERMhandler)
signal.signal(signal.SIGINT, self.__sigTERMhandler)
self.subscriber = Subscriber(subscriberconfig=subscriberconfig)
self.subscriber.start()
syslog.syslog("fail2ban-zmq-tools Subscriber running.\
Main process waiting for termination signal.\
Threads working.")
signal.pause()
syslog.syslog("fail2ban-zmq-tools Subscriber exiting.")
def quit(self):
signal.signal(signal.SIGTERM, signal.SIG_IGN)
syslog.syslog("fail2ban-zmq-tools Subscriber: Stopping threads...")
self.subscriber.join()
if __name__ == "__main__":
pidfile = subscriberconfig['pidfile']
Daemon = f2bSubscriberDaemon(pidfile)
if len(sys.argv) == 2:
if 'start' == sys.argv[1]:
Daemon.start()
elif 'stop' == sys.argv[1]:
Daemon.stop()
elif 'restart' == sys.argv[1]:
Daemon.restart()
else:
print("Unknown command")
sys.exit(2)
sys.exit(0)
else:
print("Usage: %s start|stop|restart" % sys.argv[0])
sys.exit(2)