log notifications

This commit is contained in:
2018-09-11 21:02:07 +03:00
parent 0fbb8d0780
commit b29f3790c1
3 changed files with 74 additions and 17 deletions

View File

@@ -14,23 +14,10 @@ from utils.utils import *
from utils.crypt import * from utils.crypt import *
__FLEES_VERSION__ = "20180821.0" __FLEES_VERSION__ = "20180911.0"
app = Flask(__name__) app = Flask(__name__)
app.config.from_object(__name__) app.config.from_object(__name__)
# Read config from json ! config_values = read_config(app)
config_values = json.load(open(os.getenv('FLEES_CONFIG'),'rt'))
app.config['PUBLIC_URL'] = config_values['public_url']
app.config['SITE_NAME'] = config_values['site_name']
app.config['UPLOAD_FOLDER'] = config_values['data_folder']
app.config['SHARES_FILE'] = config_values['shares_file']
app.config['ZIP_FOLDER'] = config_values['zip_folder']
app.config['MAX_ZIP_SIZE'] = config_values['max_zip_size'] # megabytes
app.config['DATE_FORMAT'] = config_values['date_format']
app.config['UID'] = config_values['uid']
app.config['GID'] = config_values['gid']
app.config['DEBUG'] = config_values['debug']
app.config['VERSION_FOLDER'] = config_values['version_folder']
app.config['SESSION_COOKIE_NAME'] = secure_filename(config_values['site_name'])
if 'notifier' in config_values: if 'notifier' in config_values:
if len(config_values['notifier']) > 0: if len(config_values['notifier']) > 0:
notifier_config = config_values['notifier'].split(":") notifier_config = config_values['notifier'].split(":")
@@ -52,6 +39,7 @@ def before_request():
g.version = __FLEES_VERSION__ g.version = __FLEES_VERSION__
g.site_name = app.config['SITE_NAME'] g.site_name = app.config['SITE_NAME']
g.max_zip_size = app.config['MAX_ZIP_SIZE'] g.max_zip_size = app.config['MAX_ZIP_SIZE']
g.logger = app.config['LOGGER']
@app.route("/") @app.route("/")
@@ -581,6 +569,10 @@ def script_client(name = None, token = None):
@app.route('/script/flip/<name>/<token>', methods=['GET']) @app.route('/script/flip/<name>/<token>', methods=['GET'])
@app.route('/script/flip', methods=['GET']) @app.route('/script/flip', methods=['GET'])
def script_flip(name = "", token = ""): def script_flip(name = "", token = ""):
notify({
'share': name,
'operation': 'script_flip'
})
return render_template( return render_template(
"flip", "flip",
name = name, name = name,
@@ -870,6 +862,15 @@ def makedirs_rights(path):
def notify(msg): def notify(msg):
""" Log notifier messages, and run the custom notifier """
address = request.environ['REMOTE_ADDR']
if 'HTTP_X_FORWARDED_FOR' in request.environ:
address += ':' + request.environ['HTTP_X_FORWARDED_FOR']
log_msg = "%s: %s"%(
address,
', '.join(["%s:%s"%( k, msg[k] ) for k in sorted(msg)])
)
g.logger.info(log_msg)
if 'notifier' in app.config: if 'notifier' in app.config:
msg['environment'] = request.environ msg['environment'] = request.environ
app.config['notifier'].notify(msg) app.config['notifier'].notify(msg)

View File

@@ -3,10 +3,16 @@ from datetime import datetime
from flask import current_app as app from flask import current_app as app
import requests import requests
import re import re
import json
try: try:
import magic import magic
except ImportError: except ImportError:
pass pass
try:
from werkzeug.utils import secure_filename
except ImportError:
pass
try: try:
from urllib.request import pathname2url from urllib.request import pathname2url
from urllib.request import urlparse from urllib.request import urlparse
@@ -14,6 +20,33 @@ except ImportError:
from urllib import pathname2url from urllib import pathname2url
from urlparse import urlparse from urlparse import urlparse
class Logger:
def __init__(self, filename):
self.filename = filename
self.init()
def info(self, message):
self.write("INFO", str(message))
def init(self):
self.info("Service started")
def warning(self, message):
self.write("WARNING", str(message))
def write(self, level, message):
with open(self.filename, 'at') as fp:
fp.write("%s\t%s\t%s\n"%(
datetime.now().isoformat(),
level,
message
))
fp.flush()
def download_url(url, filename): def download_url(url, filename):
try: try:
@@ -186,6 +219,28 @@ def path2url(path):
return pathname2url(path) return pathname2url(path)
def read_config(app):
# Read config from json
config_values = json.load(open(os.getenv('FLEES_CONFIG'),'rt'))
app.config['PUBLIC_URL'] = config_values['public_url']
app.config['SITE_NAME'] = config_values['site_name']
app.config['UPLOAD_FOLDER'] = config_values['data_folder']
app.config['SHARES_FILE'] = config_values['shares_file']
if 'log_file' in config_values:
app.config['LOG_FILE'] = config_values['log_file']
else:
app.config['LOG_FILE'] = os.path.join(app.config['UPLOAD_FOLDER'], 'flees.log')
app.config['LOGGER'] = Logger(app.config['LOG_FILE'])
app.config['ZIP_FOLDER'] = config_values['zip_folder']
app.config['MAX_ZIP_SIZE'] = config_values['max_zip_size'] # megabytes
app.config['DATE_FORMAT'] = config_values['date_format']
app.config['UID'] = config_values['uid']
app.config['GID'] = config_values['gid']
app.config['DEBUG'] = config_values['debug']
app.config['VERSION_FOLDER'] = config_values['version_folder']
app.config['SESSION_COOKIE_NAME'] = secure_filename(config_values['site_name'])
return config_values
def safe_name(s): def safe_name(s):
return safe_string(s, "-_") return safe_string(s, "-_")

View File

@@ -19,6 +19,7 @@
"__do_not_edit": "most likely you will not change anything after this line", "__do_not_edit": "most likely you will not change anything after this line",
"data_folder": "data", "data_folder": "data",
"shares_file": "data/shares.json", "shares_file": "data/shares.json",
"log_file": "data/flees.log",
"version_folder": "_version", "version_folder": "_version",
"zip_folder": "data/.zip", "zip_folder": "data/.zip",
"date_format": "%Y-%m-%d %H:%M", "date_format": "%Y-%m-%d %H:%M",