41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
|
""" monitor
|
||
|
|
||
|
Monitor weechat sessions running and their relay availability """
|
||
|
|
||
|
import re
|
||
|
import subprocess
|
||
|
from flask import Flask, request
|
||
|
import settings
|
||
|
|
||
|
LOGGER_NAME = __name__
|
||
|
|
||
|
app = Flask(__name__)
|
||
|
|
||
|
|
||
|
@app.route('/', methods=['GET'])
|
||
|
def home_view():
|
||
|
# We check whether weechat is running
|
||
|
url_regex = re.compile(
|
||
|
r'(?:https?://)?([^/:]*){}(?::[0-9]+)?/?'.format(
|
||
|
settings.URL_SUFFIX))
|
||
|
find_res = url_regex.match(request.base_url)
|
||
|
if find_res is None:
|
||
|
return 'Badly formed url\n', 400
|
||
|
user = find_res.group(1)
|
||
|
|
||
|
try:
|
||
|
output = subprocess.check_output(
|
||
|
['ps', '-u', user, '--format=cmd']).decode('utf-8')
|
||
|
lines = output.strip().split('\n')[1:]
|
||
|
for line in lines:
|
||
|
if line.strip() == settings.PS_COMMAND:
|
||
|
return 'OK\n', 200
|
||
|
except subprocess.CalledProcessError as exn:
|
||
|
return 'Internal error: {}\n'.format(exn), 500
|
||
|
return 'Weechat not running\n', 406
|
||
|
|
||
|
|
||
|
@app.before_first_request # FIXME this should be run on startup...
|
||
|
def startup_actions():
|
||
|
pass # Nothing needed for now
|