-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.py
executable file
·105 lines (96 loc) · 2.92 KB
/
agent.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
#!/usr/bin/env python3
"""
A script to run monitoring checks on a system and
push the result to uptime-kuma instance.
"""
from argparse import ArgumentParser
import sys
import logging
import shlex
import subprocess
import requests
import yaml
def create_parser():
"""
create a parser with ArgumentParser to parse arguments
"""
parser = ArgumentParser()
parser.add_argument(
'-c','--config-file',
help="configuration file for agent",
dest="config_file",
required=True
)
return parser
args = create_parser().parse_args()
logging.basicConfig(
level=logging.ERROR,
format="%(asctime)s - %(levelname)s - %(message)s",
datefmt="%d/%m/%y %H:%M:%S",
)
def load_configuration(config_file):
"""
load the configuration file
"""
try:
with open(config_file, 'r', encoding = 'utf-8') as file:
try:
configuration = yaml.safe_load(file)
except yaml.scanner.ScannerError:
logging.error("Cant't read yml file")
sys.exit(2)
except OSError as err:
logging.error("%s", err)
sys.exit(2)
return configuration
def execute_check(chk):
"""
execute a configuration given check and return a message
and a status as result
"""
with subprocess.Popen(
shlex.split(chk),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
) as process:
out, err = process.communicate()
if process.returncode == 0:
if 'nagios' in chk:
mes = out.decode('utf-8').split('|', maxsplit = 1)[0].replace(' ','+').replace(';','') # pylint: disable=line-too-long
sta = 'up'
else:
mes = out.decode('utf-8')
sta = 'up'
if process.returncode >= 1 and process.returncode <= 3:
if 'nagios' in chk:
mes = out.decode('utf-8').split('|', maxsplit = 1)[0].replace(' ','+').replace(';','') # pylint: disable=line-too-long
sta = 'down'
else:
mes = out.decode('utf-8')
sta = 'down'
if process.returncode >= 3:
logging.error("%s", out.decode('utf-8'))
logging.error("%s", err.decode('utf-8'))
mes = 'UNKNOWN'
sta = 'down'
return mes, sta
def push_event(msg, status, token, url):
"""
push event to uptime-kuma monitoring instance
"""
url = 'https://' + url + '/api/push/' + token + '?status=' + status + '&msg=' + msg
try:
requests.get(url, timeout=10)
except TimeoutError:
logging.error("Timeout for url: %s", url)
except ConnectionError:
logging.error("Can't connect to url: %s", url)
config = load_configuration(config_file=args.config_file)
for check in config['checks']:
message, state = execute_check(chk = check['command'])
push_event(
msg = message,
status = state,
token = check['token'],
url = config['url']
)