Skip to content

Commit 2d82427

Browse files
committed
Linting & added pylint
1 parent ea5cce0 commit 2d82427

File tree

2 files changed

+81
-44
lines changed

2 files changed

+81
-44
lines changed

.github/workflows/pylint.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
name: pylint
3+
on: [push, pull_request]
4+
5+
jobs:
6+
Linting:
7+
runs-on: ubuntu-latest
8+
name: Executes pylint
9+
steps:
10+
- name: Checkout
11+
uses: actions/checkout@v1
12+
13+
- name: Lint
14+
uses: gabriel-milan/action-pylint@v1
15+
with:
16+
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} # GitHub token
17+
path: "./*.py" # Glob pattern for files to lint
18+
fail: true # Fail the action if pylint errors are found

agent.py

Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
#!/usr/bin/env python3
2+
"""
3+
A script to run monitoring checks on a system and
4+
push the result to uptime-kuma instance.
5+
"""
26

37
from argparse import ArgumentParser
8+
import sys
49
import logging
5-
import shlex, subprocess
10+
import shlex
11+
import subprocess
612
import requests
713
import yaml
8-
import sys
914

1015
def create_parser():
16+
"""
17+
create a parser with ArgumentParser to parse arguments
18+
"""
1119
parser = ArgumentParser()
1220
parser.add_argument(
1321
'-c','--config-file',
@@ -25,62 +33,73 @@ def create_parser():
2533
datefmt="%d/%m/%y %H:%M:%S",
2634
)
2735

28-
def execute_check(check):
29-
p = subprocess.Popen(
30-
shlex.split(check),
36+
def load_configuration(config_file):
37+
"""
38+
load the configuration file
39+
"""
40+
try:
41+
with open(config_file, 'r', encoding = 'utf-8') as file:
42+
try:
43+
configuration = yaml.safe_load(file)
44+
except yaml.scanner.ScannerError:
45+
logging.error("Cant't read yml file")
46+
sys.exit(2)
47+
except OSError as err:
48+
logging.error("%s", err)
49+
sys.exit(2)
50+
return configuration
51+
52+
def execute_check(chk):
53+
"""
54+
execute a configuration given check and return a message
55+
and a status as result
56+
"""
57+
with subprocess.Popen(
58+
shlex.split(chk),
3159
stdout=subprocess.PIPE,
3260
stderr=subprocess.PIPE
33-
)
34-
out, err = p.communicate()
35-
if p.returncode == 0:
36-
if 'nagios' in check:
37-
msg = out.decode('utf-8').split('|')[0].replace(' ','+').replace(';','')
38-
status = 'up'
61+
) as process:
62+
out, err = process.communicate()
63+
if process.returncode == 0:
64+
if 'nagios' in chk:
65+
mes = out.decode('utf-8').split('|', maxsplit = 1)[0].replace(' ','+').replace(';','') # pylint: disable=line-too-long
66+
sta = 'up'
3967
else:
40-
msg = out.decode('utf-8')
41-
status = 'up'
42-
if p.returncode >= 1 and p.returncode <= 3:
43-
if 'nagios' in check:
44-
msg = out.decode('utf-8').split('|')[0].replace(' ','+').replace(';','')
45-
status = 'down'
68+
mes = out.decode('utf-8')
69+
sta = 'up'
70+
if process.returncode >= 1 and process.returncode <= 3:
71+
if 'nagios' in chk:
72+
mes = out.decode('utf-8').split('|', maxsplit = 1)[0].replace(' ','+').replace(';','') # pylint: disable=line-too-long
73+
sta = 'down'
4674
else:
47-
msg = out.decode('utf-8')
48-
status = 'down'
49-
if p.returncode >= 3:
50-
logging.error(f"{out.decode('utf-8')}")
51-
msg = 'UNKNOWN'
52-
status = 'down'
53-
return msg, status
75+
mes = out.decode('utf-8')
76+
sta = 'down'
77+
if process.returncode >= 3:
78+
logging.error("%s", out.decode('utf-8'))
79+
logging.error("%s", err.decode('utf-8'))
80+
mes = 'UNKNOWN'
81+
sta = 'down'
82+
return mes, sta
5483

55-
def push_notification(msg, status, token, url):
84+
def push_event(msg, status, token, url):
85+
"""
86+
push event to uptime-kuma monitoring instance
87+
"""
5688
url = 'https://' + url + '/api/push/' + token + '?status=' + status + '&msg=' + msg
5789
try:
5890
requests.get(url, timeout=10)
5991
except TimeoutError:
60-
logging.error(f"Timeout for url: {url}")
92+
logging.error("Timeout for url: %s", url)
6193
except ConnectionError:
62-
logging.error(f"Can't connect to url: {url}")
63-
64-
def load_configuration(config_file):
65-
try:
66-
with open(config_file, 'r') as f:
67-
try:
68-
config = yaml.safe_load(f)
69-
except yaml.scanner.ScannerError:
70-
logging.error(f"Cant't read yml file")
71-
sys.exit(2)
72-
except OSError as err:
73-
logging.error(f"{err}")
74-
sys.exit(2)
75-
return config
94+
logging.error("Can't connect to url: %s", url)
7695

7796
config = load_configuration(config_file=args.config_file)
7897

7998
for check in config['checks']:
80-
msg, status = execute_check(check = check['command'])
81-
push_notification(
82-
msg = msg,
83-
status = status,
99+
message, state = execute_check(chk = check['command'])
100+
push_event(
101+
msg = message,
102+
status = state,
84103
token = check['token'],
85104
url = config['url']
86105
)

0 commit comments

Comments
 (0)