1
0
Fork 0
mirror of https://github.com/ThomasGsp/HyperProxmox.git synced 2025-02-15 04:32:15 +00:00
HyperProxmox/code/scripts/main/core/libs/logs.py

159 lines
6.2 KiB
Python
Raw Normal View History

2018-02-08 19:41:44 +00:00
import logging
import argparse
import sys
import http.client as http_client
import datetime
import re
2018-03-22 15:33:06 +00:00
import json
2018-04-12 16:29:34 +00:00
from sys import getsizeof
2018-02-08 19:41:44 +00:00
class StreamToLogger(object):
def __init__(self, logger, log_level=logging.INFO):
self.logger = logger
self.log_level = log_level
self.linebuf = ''
def write(self, buf):
for line in buf.rstrip().splitlines():
self.logger.log(self.log_level, line.rstrip())
2018-02-09 12:18:25 +00:00
"""
2018-02-08 19:41:44 +00:00
class VAction(argparse.Action):
def __call__(self, parser, args, values, option_string=None):
# print 'values: {v!r}'.format(v=values)
if values is None:
values = '1'
try:
values = int(values)
except ValueError:
values = values.count('v') + 1
setattr(args, self.dest, values)
2018-02-09 12:18:25 +00:00
"""
2018-02-08 19:41:44 +00:00
class Logger(object):
2018-02-09 12:18:25 +00:00
def __init__(self, generalconf):
self.debug = generalconf['logger']['debug']
self.logs_dir = generalconf['logger']['logs_dir']
self.debug_level = generalconf['logger']['debug_level']
2018-02-08 19:41:44 +00:00
self.stdout_logger = None
def run(self):
# HTTP Debug (verbose !!)
if self.debug is True:
# Logging configuration INFO, DEBUG, ERROR
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s:%(levelname)s:%(name)s:%(threadName)s:%(message)s',
2018-02-09 12:18:25 +00:00
filename="{0}/debug.log".format(self.logs_dir),
2018-02-08 19:41:44 +00:00
filemode='a'
)
http_client.HTTPConnection.debuglevel = 1
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.propagate = True
2018-02-09 12:18:25 +00:00
sl = StreamToLogger(self.stdout_logger, logging.DEBUG)
2018-02-08 19:41:44 +00:00
sys.stdout = sl
else:
# Logging configuration INFO, DEBUG, ERROR
logging.basicConfig(
level=logging.ERROR,
format='%(asctime)s:%(levelname)s:%(name)s:%(threadName)s:%(message)s',
2018-02-09 12:18:25 +00:00
filename="{0}/errors.log".format(self.logs_dir),
2018-02-08 19:41:44 +00:00
filemode='a'
)
loginlv = ["INFO", "WARNING", "ERROR", "CRITICAL", "DEBUG"]
for lv in loginlv:
self.stdout_logger = logging.getLogger(lv)
sl = StreamToLogger(self.stdout_logger, logging.INFO)
sys.stdout = sl
return self.stdout_logger
2018-02-19 11:46:54 +00:00
def logsout(self, node, errortxt):
2018-02-08 19:41:44 +00:00
now = datetime.datetime.now()
date = now.strftime("%Y-%m-%d %H:%M")
errortxt = re.sub(r"ticket\?(.*?) ", "ticket?username=***YOUR_USER***&password=***PWD***", errortxt)
error = "[{date} -- {node}] : {error} \n".format(date=date, node=node, error=errortxt)
2018-02-09 12:18:25 +00:00
errorlog = open("{0}/proxmox.log".format(self.logs_dir), "ab")
2018-02-08 19:41:44 +00:00
errorlog.write(error.encode('utf-8'))
errorlog.close()
2018-03-22 15:33:06 +00:00
class Logger2:
def __init__(self, loggerconf):
self.logs_dir = loggerconf['logs_dir']
self.log_level = int(loggerconf['logs_level'])
2018-04-12 16:29:34 +00:00
self.bulk = loggerconf['bulk_write']
self.bulk_size = loggerconf['bulk_size']
2018-04-12 16:37:48 +00:00
self.currenttext_proxmox = ""
self.currenttext_hyperproxmox = ""
self.currenttext_python = ""
self.currenttext_others = ""
2018-03-22 15:33:06 +00:00
def write(self, json_text):
switcher = {
1: "INFO",
2: "WARNING",
3: "ERROR",
4: "CRITICAL",
5: "DEBUG"
}
lKeyC = [key for key, value in switcher.items() if '"{val}"'.format(val=value) == json.dumps(json_text["result"])][0]
if lKeyC >= self.log_level and lKeyC <= 4:
now = datetime.datetime.now()
date = now.strftime("%Y-%m-%d %H:%M")
2018-04-12 16:29:34 +00:00
newtext = re.sub(r"ticket\?(.*?) ", "ticket?username=***YOUR_USER***&password=***PWD***", json_text["value"])
2018-03-22 15:33:06 +00:00
try:
info = "[{0}] [{1}] [{2}]".format(json_text["result"], json_text["type"], json_text["target"])
except BaseException:
info = "[{0}] [{1}]".format(json_text["result"], json_text["type"])
2018-04-12 16:29:34 +00:00
newtext = "[{date}] {info} : {text} \n".format(date=date, info=info, text=newtext)
2018-04-12 16:37:48 +00:00
try:
if json_text["type"] == "PROXMOX":
self.currenttext_proxmox = self.currenttext_proxmox + newtext
if getsizeof(self.currenttext_proxmox) > 800 or self.bulk == 0 or self.log_level == 6:
2018-04-12 16:29:34 +00:00
errorlog = open("{0}/proxmox.log".format(self.logs_dir), "ab")
2018-04-12 16:37:48 +00:00
errorlog.write(self.currenttext_proxmox.encode('utf-8'))
self.currenttext_proxmox = ""
errorlog.close()
elif json_text["type"] == "HYPERPROXMOX":
self.currenttext_hyperproxmox = self.currenttext_hyperproxmox + newtext
if getsizeof(self.currenttext_hyperproxmox) > 800 or self.bulk == 0 or self.log_level == 6:
2018-04-12 16:29:34 +00:00
errorlog = open("{0}/hyperproxmox.log".format(self.logs_dir), "ab")
2018-04-12 16:37:48 +00:00
errorlog.write(self.currenttext_hyperproxmox.encode('utf-8'))
self.currenttext_hyperproxmox = ""
errorlog.close()
elif json_text["type"] == "PYTHON":
self.currenttext_python = self.currenttext_python + newtext
if getsizeof(self.currenttext_python) > 800 or self.bulk == 0 or self.log_level == 6:
2018-04-12 16:29:34 +00:00
errorlog = open("{0}/python.log".format(self.logs_dir), "ab")
2018-04-12 16:37:48 +00:00
errorlog.write(self.currenttext_python.encode('utf-8'))
self.currenttext_python = ""
errorlog.close()
else:
self.currenttext_others = self.currenttext_others + newtext
if getsizeof(self.currenttext_others) > 800 or self.bulk == 0 or self.log_level == 6:
2018-04-12 16:29:34 +00:00
errorlog = open("{0}/others.log".format(self.logs_dir), "ab")
2018-04-12 16:37:48 +00:00
errorlog.write(self.currenttext_others.encode('utf-8'))
self.currenttext_others = ""
errorlog.close()
except BaseException:
print("Cannot write on {0}, please check permissions.".format(self.logs_dir))
exit(1)