Commit 5f477994 authored by Joanne Hugé's avatar Joanne Hugé

promise/plugin: rearrange and rename methods

parent 38f7e51b
...@@ -10,10 +10,11 @@ from slapos.grid.promise import interface ...@@ -10,10 +10,11 @@ from slapos.grid.promise import interface
@implementer(interface.IPromise) @implementer(interface.IPromise)
class RunPromise(JSONPromise): class RunPromise(JSONPromise):
def __init__(self, config): def __init__(self, config):
super(RunPromise, self).__init__(config) super(RunPromise, self).__init__(config)
self.setPeriodicity(float(self.getConfig('frequency', 2))) self.setPeriodicity(float(self.getConfig('frequency', 2)))
self.avg_flag_file = self.getConfig('last-avg-computation-file', 'last_avg') self.avg_flag_file = self.getConfig('avg-flag-file', 'last_avg')
self.max_spot_temp = float(self.getConfig('max-spot-temp', 90)) # °C self.max_spot_temp = float(self.getConfig('max-spot-temp', 90)) # °C
self.max_avg_temp = float(self.getConfig('max-avg-temp', 80)) # °C self.max_avg_temp = float(self.getConfig('max-avg-temp', 80)) # °C
self.avg_temp_duration = int(self.getConfig('avg-temp-duration', 600)) # secondes self.avg_temp_duration = int(self.getConfig('avg-temp-duration', 600)) # secondes
...@@ -50,7 +51,7 @@ class RunPromise(JSONPromise): ...@@ -50,7 +51,7 @@ class RunPromise(JSONPromise):
t = 0 t = 0
if (time.time() - t) > avg_computation_period: if (time.time() - t) > avg_computation_period:
open(self.avg_flag_file, 'w').close() open(self.avg_flag_file, 'w').close()
temp_list = self.getJsonLogDataInterval(self.avg_temp_duration) temp_list = self.get_json_log_data_interval(self.avg_temp_duration)
if temp_list: if temp_list:
avg_temp = sum(x['cpu_temperature'] for x in temp_list) / len(temp_list) avg_temp = sum(x['cpu_temperature'] for x in temp_list) / len(temp_list)
if avg_temp > self.max_avg_temp: if avg_temp > self.max_avg_temp:
...@@ -75,7 +76,6 @@ class RunPromise(JSONPromise): ...@@ -75,7 +76,6 @@ class RunPromise(JSONPromise):
""" """
return self._test(result_count=1, failure_amount=1) return self._test(result_count=1, failure_amount=1)
def anomaly(self): def anomaly(self):
""" """
Called after sense() if the instance has finished converging. Called after sense() if the instance has finished converging.
......
...@@ -39,7 +39,7 @@ class RunPromise(JSONPromise): ...@@ -39,7 +39,7 @@ class RunPromise(JSONPromise):
# can be heavy in computation # can be heavy in computation
if (time.time() - t) > self.transit_period / 4: if (time.time() - t) > self.transit_period / 4:
open(self.last_transit_file, 'w').close() open(self.last_transit_file, 'w').close()
temp_list = self.getJsonLogDataInterval(self.transit_period) temp_list = self.get_json_log_data_interval(self.transit_period)
if temp_list: if temp_list:
# If no previous data in log # If no previous data in log
if len(temp_list) == 1: if len(temp_list) == 1:
......
...@@ -43,7 +43,7 @@ class RunPromise(JSONPromise): ...@@ -43,7 +43,7 @@ class RunPromise(JSONPromise):
# Get last available RAM from log file since avg_ram_period / 4 # Get last available RAM from log file since avg_ram_period / 4
if (time.time() - t) > self.avg_ram_period / 4: if (time.time() - t) > self.avg_ram_period / 4:
open(self.last_avg_ram_file, 'w').close() open(self.last_avg_ram_file, 'w').close()
temp_list = self.getJsonLogDataInterval(self.avg_ram_period) temp_list = self.get_json_log_data_interval(self.avg_ram_period)
if temp_list: if temp_list:
avg_ram = sum(map(lambda x: x['available_ram'], temp_list)) / len(temp_list) avg_ram = sum(map(lambda x: x['available_ram'], temp_list)) / len(temp_list)
if avg_ram < self.min_avg_ram: if avg_ram < self.min_avg_ram:
......
...@@ -38,37 +38,14 @@ def iter_logrotate_file_handle(path, mode='r'): ...@@ -38,37 +38,14 @@ def iter_logrotate_file_handle(path, mode='r'):
except OSError: except OSError:
break break
def get_json_log_data_interval(json_log_file, interval):
class JSONPromise(GenericPromise):
def __init__(self, config):
self.__name = config.get('name', None)
self.__log_folder = config.get('log-folder', None)
super(JSONPromise, self).__init__(config)
json_log_name = os.path.splitext(self.__name)[0] + '.json.log'
self.__json_log_file = os.path.join(self.__log_folder, json_log_name)
self.json_logger = self.__makeJsonLogger(self.__json_log_file)
def __makeJsonLogger(self, json_log_file):
logger = logging.getLogger('json-logger')
logger.setLevel(logging.INFO)
handler = logging.FileHandler(json_log_file)
formatter = logging.Formatter(
'{"time": "%(asctime)s", "log_level": "%(levelname)s"'
', "message": "%(message)s", "data": %(data)s}'
)
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
def getJsonLogDataInterval(self, interval):
""" """
Get all data in the last "interval" seconds from JSON log Get all data in the last "interval" seconds from JSON log
Reads rotated logs too (XX.log, XX.log.1, XX.log.2, ...) Reads rotated logs too (XX.log, XX.log.1, XX.log.2, ...)
""" """
current_time = datetime.now() current_time = datetime.now()
data_list = [] data_list = []
for f in iter_logrotate_file_handle(self.__json_log_file, 'rb'): for f in iter_logrotate_file_handle(json_log_file, 'rb'):
for line in iter_reverse_lines(f): for line in iter_reverse_lines(f):
l = json.loads(line.decode().replace("'", '"')) l = json.loads(line.decode().replace("'", '"'))
timestamp = dateparser.parse(l['time']) timestamp = dateparser.parse(l['time'])
...@@ -77,26 +54,49 @@ class JSONPromise(GenericPromise): ...@@ -77,26 +54,49 @@ class JSONPromise(GenericPromise):
data_list.append(l['data']) data_list.append(l['data'])
return data_list return data_list
def getJsonLogLatestTimestamp(log): def get_json_log_latest_timestamp(json_log_file):
""" """
Get latest timestamp from JSON log Get latest timestamp from JSON log
Reads rotated logs too (XX.log, XX.log.1, XX.log.2, ...) Reads rotated logs too (XX.log, XX.log.1, XX.log.2, ...)
""" """
for f in iter_logrotate_file_handle(self.__json_log_file, 'rb'): for f in iter_logrotate_file_handle(json_log_file, 'rb'):
for line in iter_reverse_lines(f): for line in iter_reverse_lines(f):
l = json.loads(line.decode().replace("'", '"')) l = json.loads(line.decode().replace("'", '"'))
return dateparser.parse(l['time']) return dateparser.parse(l['time']).timestamp()
return 0 return 0
from dateutil import parser
from slapos.grid.promise.generic import GenericPromise class JSONPromise(GenericPromise):
def __init__(self, config):
self.__name = config.get('name', None)
self.__log_folder = config.get('log-folder', None)
super(JSONPromise, self).__init__(config)
json_log_name = os.path.splitext(self.__name)[0] + '.json.log'
self.__json_log_file = os.path.join(self.__log_folder, json_log_name)
self.json_logger = self.__make_json_logger(self.__json_log_file)
def __make_json_logger(self, json_log_file):
logger = logging.getLogger('json-logger')
logger.setLevel(logging.INFO)
handler = logging.FileHandler(json_log_file)
formatter = logging.Formatter(
'{"time": "%(asctime)s", "log_level": "%(levelname)s"'
', "message": "%(message)s", "data": %(data)s}'
)
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
def get_json_log_data_interval(self, interval):
return get_json_log_data_interval(self.__json_log_file, interval)
def tail_file(file_path, line_count=10): def tail_file(file_path, line_count=10):
""" """
Returns the last lines of file. Returns the last lines of file.
""" """
line_list = [] line_list = []
with open(file_path) as f: with open(file_path, 'rb') as f:
BUFSIZ = 1024 BUFSIZ = 1024
f.seek(0, 2) f.seek(0, 2)
bytes = f.tell() bytes = f.tell()
...@@ -106,11 +106,11 @@ def tail_file(file_path, line_count=10): ...@@ -106,11 +106,11 @@ def tail_file(file_path, line_count=10):
if bytes - BUFSIZ > 0: if bytes - BUFSIZ > 0:
# Seek back one whole BUFSIZ # Seek back one whole BUFSIZ
f.seek(block * BUFSIZ, 2) f.seek(block * BUFSIZ, 2)
line_list.insert(0, f.read(BUFSIZ)) line_list.insert(0, f.read(BUFSIZ).decode())
else: else:
f.seek(0, 0) f.seek(0, 0)
# only read what was not read # only read what was not read
line_list.insert(0, f.read(bytes)) line_list.insert(0, f.read(bytes).decode())
line_len = line_list[0].count('\n') line_len = line_list[0].count('\n')
size -= line_len size -= line_len
bytes -= BUFSIZ bytes -= BUFSIZ
......
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
############################################################################## ##############################################################################
import mock import mock
import os
import time import time
from slapos.grid.promise import PromiseError from slapos.grid.promise import PromiseError
from slapos.promise.plugin.check_cpu_temperature import RunPromise from slapos.promise.plugin.check_cpu_temperature import RunPromise
...@@ -74,7 +73,6 @@ class TestCheckCpuTemperature(TestPromisePluginMixin): ...@@ -74,7 +73,6 @@ class TestCheckCpuTemperature(TestPromisePluginMixin):
}) })
self.assertEqual(message, self.runPromise({'coretemp': [[0, 90]]})) self.assertEqual(message, self.runPromise({'coretemp': [[0, 90]]}))
def test_avg_critical(self): def test_avg_critical(self):
message = "Average temperature over the last 1s reached threshold: 45.0 °C (threshold is 40.0 °C)" message = "Average temperature over the last 1s reached threshold: 45.0 °C (threshold is 40.0 °C)"
self.writePromise(**{ self.writePromise(**{
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment