-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
51 lines (37 loc) · 1.24 KB
/
server.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
# -*- coding: utf-8 -*-
from time import time
import signal
from daemonize import Daemonize
import gevent
from extensions.checks.base import MetaRegister
from settings import DEBUG, FOREGROUND, HOSTS, ERRORS_LIMIT, CHECK_TIME
from util.notify import Notify
def check(host):
notify = Notify()
host, check_type = host
errors = 0
start_time = None
while True:
service = MetaRegister.check_options[check_type]
result, response_time = service(host).check()
if result:
errors += 1
if not start_time:
start_time = time()
if errors == ERRORS_LIMIT:
notify.problem(host, check_type)
else:
if errors >= ERRORS_LIMIT:
error_time = round(time() - start_time)
notify.recovery(host, check_type, error_time)
errors = 0
start_time = None
if DEBUG:
print(host, check_type, errors, response_time)
gevent.sleep(CHECK_TIME)
def main():
gevent.signal(signal.SIGQUIT, gevent.kill)
gevent.joinall([gevent.spawn(check, host) for host in HOSTS])
pid = "/tmp/test.pid"
daemon = Daemonize(app="sysmon", pid=pid, action=main, foreground=FOREGROUND)
daemon.start()