From e411b9410923d78675a9caf9eb725683f7e42481 Mon Sep 17 00:00:00 2001 From: Sergi Almacellas Abellana Date: Wed, 16 Dec 2015 00:12:51 +0100 Subject: [PATCH] Migrate to version 3.8 (grafted from eec3a3932591273a0465b141921b62c21f399a00) --- CHANGELOG | 1 + monitoring.py | 44 ++++++++++++++++-------- monitoring.xml | 22 +++++++----- tests/test_monitoring.py | 17 ++------- tryton.cfg | 2 +- view/scheduler_form.xml | 8 ++--- view/scheduler_list.xml | 4 +-- view/state_indicator_check_plan_list.xml | 3 +- view/state_list.xml | 3 +- view/state_type_form.xml | 2 -- view/state_type_list.xml | 1 - 11 files changed, 56 insertions(+), 51 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index f00efff..8b6a751 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,2 +1,3 @@ +Version 3.8.0 - 2015-12-09 Version 3.4.0 - 2014-11-03 * Initial release diff --git a/monitoring.py b/monitoring.py index cbb013a..19aa7ef 100644 --- a/monitoring.py +++ b/monitoring.py @@ -3,12 +3,13 @@ import sys import ssl import sql +from sql import Column import xmlrpclib from datetime import datetime import random import string import hashlib -from itertools import izip, chain, groupby +from itertools import izip, chain from decimal import Decimal import logging @@ -17,13 +18,13 @@ try: except ImportError: bcrypt = None -from trytond.tools import safe_eval from trytond.model import ModelSQL, ModelView, fields from trytond.pool import Pool, PoolMeta from trytond.pyson import Eval from trytond.config import config from trytond.rpc import RPC from trytond.transaction import Transaction +from trytond import backend __all__ = ['CheckType', 'ResultType', 'StateType', 'StateIndicator', @@ -61,7 +62,6 @@ class StateType(ModelSQL, ModelView): 'Monitoring State Value' __name__ = 'monitoring.state.type' name = fields.Char('Name', translate=True, required=True) - color = fields.Char('Color') class StateIndicator(ModelSQL, ModelView): @@ -104,9 +104,30 @@ class Scheduler(ModelSQL, ModelView): # It might make sense to create a parent and inherit values from the # parent similar to Nagios behaviour. name = fields.Char('Name', required=True, translate=True) - normal_check_interval = fields.Float('Normal Check Interval', required=True) + normal_check = fields.TimeDelta('Normal Check Interval', required=True) retries = fields.Integer('Retries', required=True) - retry_check_interval = fields.Float('Retry Check Interval', required=True) + retry_check = fields.TimeDelta('Retry Check Interval', required=True) + + @classmethod + def __register__(cls, module_name): + TableHandler = backend.get('TableHandler') + cursor = Transaction().cursor + table = TableHandler(cursor, cls, module_name) + sql_table = cls.__table__() + super(Scheduler, cls).__register__(module_name) + + for field in ('normal_check', 'retry_check'): + old_field = '%s_interval' + if table.column_exist(old_field): + cursor.execute(*sql_table.select( + sql_table.id, Column(sql_table, old_field))) + for id_, hours in cursor.fetchall(): + new_value = datetime.timedelta(hours=hours) + cursor.execute(*sql_table.update( + [Column(sql_table, field)], + [new_value], + where=sql_table.id == id_)) + table.drop_column(old_field) # TODO: We should probably create a scheduler queue @@ -164,7 +185,7 @@ class CheckPlan(ModelSQL, ModelView): state_type = None for line in indicator.lines: #ast.literal_eval(indicator.expression) - if safe_eval(line.expression, { + if eval(line.expression, { 'value': value, 'label': label, 'payload': payload, @@ -288,7 +309,7 @@ class CheckPlan(ModelSQL, ModelView): last_check = checks[0] delta = datetime.now() - last_check.timestamp - if (delta.seconds / 3600.0) >= plan.scheduler.normal_check_interval: + if delta >= plan.scheduler.normal_check: to_check.append(plan) cls.check(cls.browse([x.id for x in to_check])) @@ -328,13 +349,12 @@ class StateIndicatorCheckPlan(ModelSQL, ModelView): 'Monitoring Asset'), 'get_asset', searcher='search_asset') monitored_asset = fields.Function(fields.Many2One('asset', 'Monitored Asset'), 'get_asset', searcher='search_asset') - color = fields.Function(fields.Char('Color'), 'get_lasts') @classmethod def get_lasts(cls, records, names): res = {} for name in ('last_state', 'last_check', 'last_state_type', - 'last_state_value', 'color'): + 'last_state_value'): res[name] = dict([(x.id, None) for x in records]) @@ -368,8 +388,6 @@ class StateIndicatorCheckPlan(ModelSQL, ModelView): res['last_check'][mapping[state.id]] = state.check.id res['last_state_type'][mapping[state.id]] = state.state.id res['last_state_value'][mapping[state.id]] = state.value - res['color'][mapping[state.id]] = (state.state.color if state.state - else 'black') return res def get_asset(self, name): @@ -419,7 +437,6 @@ class State(ModelSQL, ModelView): monitored_asset = fields.Function(fields.Many2One('asset', 'Monitored Asset'), 'get_asset', searcher='search_asset') state = fields.Many2One('monitoring.state.type', 'State', required=True) - color = fields.Function(fields.Char('Color'), 'get_color') value = fields.Char('Value') label = fields.Char('Label') payload = fields.Text('Payload') @@ -436,9 +453,6 @@ class State(ModelSQL, ModelView): def search_asset(cls, name, clause): return [('check.%s' % name,) + tuple(clause[1:])] - def get_color(self, name): - return self.state.color if self.state else 'black' - class ResultInteger(ModelSQL, ModelView): 'Monitoring Result Integer' diff --git a/monitoring.xml b/monitoring.xml index 90cb805..c82a1d2 100644 --- a/monitoring.xml +++ b/monitoring.xml @@ -233,7 +233,7 @@ - monitoring.state.type-party.party + @@ -559,6 +559,13 @@ + + + + + + + @@ -600,7 +607,8 @@ States monitoring.state - [('monitoring_asset', 'in', Eval('active_ids'))] + form_relate @@ -611,7 +619,8 @@ Checks monitoring.check - [('plan', 'in', Eval('active_ids'))] + form_relate @@ -622,7 +631,8 @@ Checks monitoring.check - [('monitoring_asset', 'in', Eval('active_ids'))] + form_relate @@ -807,19 +817,15 @@ Ok - green Error - red Warning - orange Exception - purple diff --git a/tests/test_monitoring.py b/tests/test_monitoring.py index 584b669..b7056d1 100644 --- a/tests/test_monitoring.py +++ b/tests/test_monitoring.py @@ -1,24 +1,13 @@ -#!/usr/bin/env python # The COPYRIGHT file at the top level of this repository contains the full # copyright notices and license terms. import unittest import trytond.tests.test_tryton -from trytond.tests.test_tryton import test_view, test_depends +from trytond.tests.test_tryton import ModuleTestCase -class TestCase(unittest.TestCase): +class TestCase(ModuleTestCase): 'Test module' - - def setUp(self): - trytond.tests.test_tryton.install_module('monitoring') - - def test0005views(self): - 'Test views' - test_view('monitoring') - - def test0006depends(self): - 'Test depends' - test_depends() + module = 'monitoring' def suite(): diff --git a/tryton.cfg b/tryton.cfg index 60abb22..908d390 100644 --- a/tryton.cfg +++ b/tryton.cfg @@ -1,5 +1,5 @@ [tryton] -version=3.4.0 +version=3.8.0 depends: ir res diff --git a/view/scheduler_form.xml b/view/scheduler_form.xml index c2ce425..83ca990 100644 --- a/view/scheduler_form.xml +++ b/view/scheduler_form.xml @@ -4,10 +4,10 @@