initial commit
This commit is contained in:
parent
be416c80ca
commit
2bcd14006c
17 changed files with 473 additions and 0 deletions
BIN
app/lib/__pycache__/agent_checker.cpython-310.pyc
Normal file
BIN
app/lib/__pycache__/agent_checker.cpython-310.pyc
Normal file
Binary file not shown.
BIN
app/lib/__pycache__/configuration.cpython-310.pyc
Normal file
BIN
app/lib/__pycache__/configuration.cpython-310.pyc
Normal file
Binary file not shown.
BIN
app/lib/__pycache__/logger.cpython-310.pyc
Normal file
BIN
app/lib/__pycache__/logger.cpython-310.pyc
Normal file
Binary file not shown.
BIN
app/lib/__pycache__/server_checker.cpython-310.pyc
Normal file
BIN
app/lib/__pycache__/server_checker.cpython-310.pyc
Normal file
Binary file not shown.
95
app/lib/agent_checker.py
Normal file
95
app/lib/agent_checker.py
Normal file
|
@ -0,0 +1,95 @@
|
|||
#!/usr/bin/env python
|
||||
import time
|
||||
from threading import Timer
|
||||
from subprocess import run
|
||||
from lib.logger import logging
|
||||
|
||||
log = logging.getLogger('checker')
|
||||
|
||||
|
||||
class Check:
|
||||
def run_check(self):
|
||||
self.last_exec_start = time.asctime()
|
||||
log.debug(f'start command {self.command} at {self.last_exec_start}')
|
||||
try:
|
||||
runcheck = run(self.command, capture_output=True)
|
||||
|
||||
# for nagios checks split text and perfdata
|
||||
perfdata = None
|
||||
if self.nagios_check:
|
||||
parts = runcheck.stdout.decode('utf-8').split('|')
|
||||
output_text = parts[0]
|
||||
perfdata = parts[1]
|
||||
else:
|
||||
output_text = runcheck.stdout.decode('utf-8')
|
||||
|
||||
self.output = {
|
||||
"rc": runcheck.returncode,
|
||||
"stdout": runcheck.stdout.decode('utf-8'),
|
||||
"stderr": runcheck.stderr.decode('utf-8'),
|
||||
"output_text": output_text
|
||||
}
|
||||
if perfdata:
|
||||
self.output["perfdata"] = perfdata
|
||||
|
||||
if runcheck.returncode == 0:
|
||||
self.state = "OK"
|
||||
elif runcheck.returncode == 1:
|
||||
self.state = "WARNING"
|
||||
else:
|
||||
self.state = "CRITICAL"
|
||||
self.last_exec_finish = time.asctime()
|
||||
log.debug(f'finished command {self.command} at {self.last_exec_start}')
|
||||
|
||||
except:
|
||||
log.error(f'error trying to execute {self.command}')
|
||||
self.state = "CRITICAL"
|
||||
|
||||
self.timer = Timer(interval=self.interval, function=self.run_check)
|
||||
self.timer.daemon = True
|
||||
self.timer.start()
|
||||
|
||||
def __init__(self, configuration, check):
|
||||
defaults = configuration.get('defaults')
|
||||
self.name = check['name']
|
||||
self.command = check['command']
|
||||
self.nagios_check = check.get('nagios_check', False)
|
||||
self.interval = check.get('interval', defaults.get('interval', 300))
|
||||
|
||||
# pre define variables for check output
|
||||
self.timer = None
|
||||
self.state = None
|
||||
self.output = {}
|
||||
self.last_exec_finish = None
|
||||
self.last_exec_start = None
|
||||
|
||||
self.run_check()
|
||||
|
||||
def get_values(self):
|
||||
values = {
|
||||
'name': self.name,
|
||||
'command': self.command,
|
||||
'last_exec_start': self.last_exec_start,
|
||||
'last_exec_finish': self.last_exec_finish,
|
||||
'output': self.output,
|
||||
'state': self.state
|
||||
}
|
||||
return values
|
||||
|
||||
|
||||
class Checker:
|
||||
checks = []
|
||||
|
||||
def __init__(self, configuration):
|
||||
for check in configuration['checks']:
|
||||
log.debug(f"create check {check['name']}")
|
||||
self.checks.append(
|
||||
Check(
|
||||
check=check,
|
||||
configuration=configuration))
|
||||
|
||||
def show_data(self):
|
||||
check_values = []
|
||||
for check in self.checks:
|
||||
check_values.append(check.get_values())
|
||||
return check_values
|
27
app/lib/configuration.py
Normal file
27
app/lib/configuration.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
#!/usr/bin/env python
|
||||
from yaml import safe_load
|
||||
from pathlib import Path
|
||||
from lib.logger import logging
|
||||
from sys import exit
|
||||
|
||||
log = logging.getLogger('config')
|
||||
|
||||
|
||||
def configuration(prefix: str):
|
||||
try:
|
||||
filename = f'{prefix}.yml'
|
||||
|
||||
if not Path(filename).is_file():
|
||||
filename = f'{prefix}.example.yml'
|
||||
log.warning(f'config file not found - using {filename}')
|
||||
|
||||
configfile = open(filename, 'r')
|
||||
config = safe_load(configfile)
|
||||
configfile.close()
|
||||
log.info('configuration loaded successfully')
|
||||
return config
|
||||
|
||||
except Exception:
|
||||
log.error(msg='unable to load configuration')
|
||||
exit(2)
|
||||
|
7
app/lib/logger.py
Normal file
7
app/lib/logger.py
Normal file
|
@ -0,0 +1,7 @@
|
|||
#!/usr/bin/env python
|
||||
import logging
|
||||
|
||||
logging.basicConfig(
|
||||
format='%(asctime)s %(name)-8s %(levelname)-8s %(message)s',
|
||||
level=logging.DEBUG)
|
||||
|
88
app/lib/server_checker.py
Normal file
88
app/lib/server_checker.py
Normal file
|
@ -0,0 +1,88 @@
|
|||
import time
|
||||
|
||||
from lib.logger import logging
|
||||
from threading import Timer
|
||||
from requests import get
|
||||
log = logging.getLogger('checker')
|
||||
|
||||
|
||||
class CheckServer:
|
||||
def fetch_server(self):
|
||||
self.last_request_started = time.asctime()
|
||||
log.debug(f'try to fetch data from {self.name}')
|
||||
try:
|
||||
r = get(
|
||||
self.url,
|
||||
auth=('monitoring', self.password),
|
||||
timeout=self.timeout
|
||||
)
|
||||
|
||||
if r.status_code == 200:
|
||||
self.check_results = r.json()
|
||||
self.server_conn_result = "OK"
|
||||
elif 400 < r.status_code < 404:
|
||||
self.server_conn_result = "FORBIDDEN"
|
||||
elif r.status_code == 404:
|
||||
self.server_conn_result = "NOT FOUND"
|
||||
else:
|
||||
self.server_conn_result = f"Server Error: HTTP {r.status_code}"
|
||||
self.last_request_finished = time.asctime()
|
||||
|
||||
except ConnectionError:
|
||||
log.error(f'error connecting to {self.name}')
|
||||
self.server_conn_result = "UNREACHABLE"
|
||||
|
||||
except:
|
||||
log.error("something else went wrong")
|
||||
self.server_conn_result = "UNREACHABLE"
|
||||
|
||||
self.timer = Timer(interval=self.interval, function=self.fetch_server)
|
||||
self.timer.daemon = True
|
||||
self.timer.start()
|
||||
|
||||
def __init__(self, server, configuration):
|
||||
defaults = configuration.get('defaults')
|
||||
self.url = server['url']
|
||||
self.name = server['name']
|
||||
self.interval = server.get('interval', defaults.get('interval'))
|
||||
self.password = server.get('password', defaults.get('password'))
|
||||
self.timeout = server.get('timeout', defaults.get('timeout', 10))
|
||||
|
||||
# initialize status variables
|
||||
self.timer = None
|
||||
self.last_request_started = None
|
||||
self.last_request_finished = None
|
||||
self.check_results = {}
|
||||
self.server_conn_result = "UNCHECKED"
|
||||
|
||||
self.fetch_server()
|
||||
|
||||
def get_values(self):
|
||||
values = {
|
||||
"name": self.name,
|
||||
"url": self.url,
|
||||
"server_conn_result": self.server_conn_result,
|
||||
"last_request_started": self.last_request_started,
|
||||
"last_request_finished": self.last_request_finished,
|
||||
"check_results": self.check_results.get('check_results')
|
||||
}
|
||||
return values
|
||||
|
||||
|
||||
class ServerChecker:
|
||||
servers = []
|
||||
|
||||
def __init__(self, configuration):
|
||||
servers = configuration.get('servers')
|
||||
for server in servers:
|
||||
log.debug(f"Monitoring {server.get('name')}")
|
||||
self.servers.append(CheckServer(
|
||||
server=server,
|
||||
configuration=configuration
|
||||
))
|
||||
|
||||
def get_data(self):
|
||||
server_values = []
|
||||
for server in self.servers:
|
||||
server_values.append(server.get_values())
|
||||
return server_values
|
Loading…
Add table
Add a link
Reference in a new issue