Commit ea8d8dcc authored by Joanne Hugé's avatar Joanne Hugé

WIP

parent 16b27381
......@@ -104,7 +104,7 @@ md5sum = 5bf57a9074ea5b054d999789cbbe2c87
[cpu-temperature-promise]
_update_hash_filename_ = promise/check_cpu_temperature.py
md5sum = e8fc789eb7e7e3b054e921b0292c806f
md5sum = 107db0dc93bba6a90a0b328e625c2e5c
[interface-up-promise]
_update_hash_filename_ = promise/check_interface_up.py
......
import errno
import json
import logging
import os
import psutil
import socket
import time
from dateutil import parser
from zope.interface import implementer
from slapos.grid.promise import interface
from slapos.grid.promise.generic import GenericPromise
# Get all data in the last "interval" seconds from JSON log
def get_data_interval(log, interval):
class JSONRunPromise(GenericPromise):
def __init__(self, config):
self.__name = config.get('name', None)
self.__log_folder = config.get('log-folder', None)
super(JSONRunPromise, self).__init__(config)
self.__title = os.path.splitext(self.__name)[0]
self.log_file = os.path.join(self.__log_folder, '%s.json.log' % self.__title)
self.json_logger = logging.getLogger('json_logger')
self.json_logger.setLevel(logging.INFO)
handler = logging.FileHandler(self.log_file)
formatter = logging.Formatter('{"time": "%(asctime)s", "log_level": '\
'"%(levelname)s", "message": "%(message)s", "data": %(data)s}')
handler.setFormatter(formatter)
self.json_logger.addHandler(handler)
def get_data_interval_json_log(log, interval):
"""
Get all data in the last "interval" seconds from JSON log
Reads rotated logs too (XX.log, XX.log.1, XX.log.2, ...)
"""
log_number = 0
latest_timestamp = 0
......@@ -42,32 +58,19 @@ def get_data_interval(log, interval):
return data_list
f.seek(pos, os.SEEK_SET)
finally:
f.close()
f.close()
log_number += 1
@implementer(interface.IPromise)
class RunPromise(GenericPromise):
class RunPromise(JSONRunPromise):
def __init__(self, config):
self.__name = config.get('name', None)
self.__log_folder = config.get('log-folder', None)
super(RunPromise, self).__init__(config)
self.setPeriodicity(minute=2)
self.setPeriodicity(minute=2)
self.last_avg_computation_file = self.getConfig(
'last-avg-computation-file', '')
self.__title = os.path.splitext(self.__name)[0]
self.__log_file = os.path.join(self.__log_folder, '%s.json.log' % self.__title)
self.json_logger = logging.getLogger('json_logger')
self.json_logger.setLevel(logging.INFO)
handler = logging.FileHandler(self.__log_file)
formatter = logging.Formatter('{"time": "%(asctime)s", "log_level": '\
'"%(levelname)s", "message": "%(message)s", "data": %(data)s}')
handler.setFormatter(formatter)
self.json_logger.addHandler(handler)
'last-avg-computation-file', 'last_avg')
def sense(self):
......@@ -75,9 +78,16 @@ class RunPromise(GenericPromise):
max_spot_temp = float(self.getConfig('max-spot-temp', 90))
max_avg_temp = float(self.getConfig('max-avg-temp', 80))
avg_temp_duration = 60 * int(self.getConfig('avg-temp-duration', 5))
avg_temp_duration_sec = int(self.getConfig('avg-temp-duration-sec', 0))
if avg_temp_duration_sec:
avg_temp_duration = avg_temp_duration_sec
else:
avg_temp_duration = 60 * int(self.getConfig('avg-temp-duration', 5))
testing = self.getConfig('testing') == "True"
# For theia JHGD
#testing = True # JHGD
# Get current temperature
if testing:
from random import randint
......@@ -94,6 +104,7 @@ class RunPromise(GenericPromise):
data = json.dumps({'cpu_temperature': cpu_temp})
self.json_logger.info("Temperature data", extra={'data': data})
# TODO: promise should computer average only with logs between interval
# Computer average temperature
avg_computation_period = avg_temp_duration / 4
try:
......@@ -102,7 +113,7 @@ class RunPromise(GenericPromise):
t = 0
if (time.time() - t) > avg_computation_period:
open(self.last_avg_computation_file, 'w').close()
temp_list = get_data_interval(self.__log_file, avg_temp_duration)
temp_list = get_data_interval_json_log(self.log_file, avg_temp_duration)
if temp_list:
avg_temp = sum(map(lambda x: x['cpu_temperature'], temp_list)) / len(temp_list)
if avg_temp > max_avg_temp:
......
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