From 1f2ff22a98ca3ac865cad9e7e2a75041757fbb7b Mon Sep 17 00:00:00 2001 From: Jerome Lebleu Date: Tue, 17 Dec 2013 00:26:27 +0100 Subject: [PATCH 1/4] Accept string as services name --- yunohost_service.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/yunohost_service.py b/yunohost_service.py index f4e83a2b..cc9467bf 100644 --- a/yunohost_service.py +++ b/yunohost_service.py @@ -38,6 +38,8 @@ def service_start(names): names -- Services name to start """ + if isinstance(names, str): + names = [names] for name in names: if _run_service_command('start', name): win_msg(_("'%s' service started") % name) @@ -53,6 +55,8 @@ def service_stop(names): name -- Services name to stop """ + if isinstance(names, str): + names = [names] for name in names: if _run_service_command('stop', name): win_msg(_("'%s' service stopped") % name) @@ -68,6 +72,8 @@ def service_enable(names): names -- Services name to enable """ + if isinstance(names, str): + names = [names] for name in names: if _run_service_command('enable', name): win_msg(_("'%s' service enabled") % name) @@ -83,6 +89,8 @@ def service_disable(names): names -- Services name to disable """ + if isinstance(names, str): + names = [names] for name in names: if _run_service_command('disable', name): win_msg(_("'%s' service disabled") % name) @@ -90,7 +98,7 @@ def service_disable(names): raise YunoHostError(1, _("Service disabling failed for '%s'") % name) -def service_status(names=None): +def service_status(names=[]): """ Show status information about one or more services (all by default) @@ -102,7 +110,9 @@ def service_status(names=None): check_names = True result = {} - if names is None or len(names) == 0: + if isinstance(names, str): + names = [names] + elif len(names) == 0: names = services.keys() check_names = False @@ -141,6 +151,8 @@ def service_status(names=None): else: result[name]['loaded'] = _("not-found") + if len(names) == 1: + return result[names[0]] return result From 6b12c90e88b6395d74c94f02b7d1dfb0b6b675ee Mon Sep 17 00:00:00 2001 From: Jerome Lebleu Date: Tue, 17 Dec 2013 01:42:49 +0100 Subject: [PATCH 2/4] Fix mean calculation --- yunohost_monitor.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/yunohost_monitor.py b/yunohost_monitor.py index 9fe84555..059eeb2c 100644 --- a/yunohost_monitor.py +++ b/yunohost_monitor.py @@ -563,8 +563,12 @@ def _calculate_stats_mean(stats): if isinstance(v, dict): s[k] = _mean(v, t, ts) elif isinstance(v, list): - nums = [ float(x * t[i]) for i, x in enumerate(v) ] - s[k] = sum(nums) / float(ts) + try: + nums = [ float(x * t[i]) for i, x in enumerate(v) ] + except: + pass + else: + s[k] = sum(nums) / float(ts) return s stats = _mean(stats, timestamp, t_sum) From 2fee1eb6f7e9c2ee8683a57dfedf4d76a7d9c83a Mon Sep 17 00:00:00 2001 From: Jerome Lebleu Date: Tue, 17 Dec 2013 02:35:31 +0100 Subject: [PATCH 3/4] Implement monitoring management --- action_map.yml | 13 ++++++++ yunohost_monitor.py | 72 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/action_map.yml b/action_map.yml index 9a0313ed..ddb3dff2 100644 --- a/action_map.yml +++ b/action_map.yml @@ -561,6 +561,19 @@ monitor: - week - month + ### monitor_enable() + enable: + action_help: Enable server monitoring + arguments: + -n: + full: --no-stats + help: Disable monitoring statistics + action: store_true + + ### monitor_disable() + disable: + action_help: Disable server monitoring + ############################# # Service # diff --git a/yunohost_monitor.py b/yunohost_monitor.py index 059eeb2c..05d07eb1 100644 --- a/yunohost_monitor.py +++ b/yunohost_monitor.py @@ -34,10 +34,13 @@ import os.path import cPickle as pickle from urllib import urlopen from datetime import datetime, timedelta -from yunohost import YunoHostError +from yunohost import YunoHostError, win_msg +from yunohost_service import (service_enable, service_disable, + service_start, service_stop, service_status) -glances_uri = 'http://127.0.0.1:61209' -stats_path = '/var/lib/yunohost/stats' +glances_uri = 'http://127.0.0.1:61209' +stats_path = '/var/lib/yunohost/stats' +crontab_path = '/etc/cron.d/yunohost-monitor' def monitor_disk(units=None, mountpoint=None, human_readable=False): """ @@ -336,6 +339,60 @@ def monitor_show_stats(period, date=None): return result +def monitor_enable(no_stats=False): + """ + Enable server monitoring + + Keyword argument: + no_stats -- Disable monitoring statistics + + """ + glances = service_status('glances') + if glances['status'] != 'running': + service_start('glances') + if glances['loaded'] != 'enabled': + try: + service_enable('glances') + except: + # TODO: log error + pass + + # Install crontab + if not no_stats: + cmd = 'yunohost monitor update-stats' + rules = ('*/5 * * * * root %(cmd)s day --no-ldap >> /dev/null\n' + \ + '0 * * * * root %(cmd)s week --no-ldap >> /dev/null\n' + \ + '* */4 * * * root %(cmd)s month --no-ldap >> /dev/null') % {'cmd': cmd} + os.system("touch %s" % crontab_path) + os.system("echo '%s' >%s" % (rules, crontab_path)) + + win_msg(_("Server monitoring enabled")) + + +def monitor_disable(): + """ + Disable server monitoring + + """ + glances = service_status('glances') + if glances['status'] != 'inactive': + service_stop('glances') + if glances['loaded'] != 'disabled': + try: + service_disable('glances') + except: + # TODO: log error + pass + + # Remove crontab + try: + os.remove(crontab_path) + except: + pass + + win_msg(_("Server monitoring disabled")) + + def _get_glances_api(): """ Retrieve Glances API running on the local server @@ -345,10 +402,13 @@ def _get_glances_api(): p = xmlrpclib.ServerProxy(glances_uri) p.system.methodHelp('getAll') except (xmlrpclib.ProtocolError, IOError): - # TODO: Try to start Glances service - raise YunoHostError(1, _("Connection to Glances server failed")) + pass + else: + return p - return p + if service_status('glances')['status'] != 'running': + raise YunoHostError(1, _("Monitoring is disabled")) + raise YunoHostError(1, _("Connection to Glances server failed")) def _extract_inet(string, skip_netmask=False): From bee3d2bcc9dad855db8826b2e9c74ca36b3512bb Mon Sep 17 00:00:00 2001 From: Jerome Lebleu Date: Tue, 17 Dec 2013 16:30:21 +0100 Subject: [PATCH 4/4] Update YunoHost versioning --- action_map.yml | 2 +- yunohost | 19 ------------------- yunohost.py | 29 ++++++++++++++++++++++++++--- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/action_map.yml b/action_map.yml index ddb3dff2..4d565418 100644 --- a/action_map.yml +++ b/action_map.yml @@ -39,7 +39,7 @@ general_arguments: full: --version help: Display YunoHost version action: version - version: YunoHost 2.0 beta1 + version: YunoHost %version% ############################# # User # diff --git a/yunohost b/yunohost index e9933f84..5eebbe9b 100755 --- a/yunohost +++ b/yunohost @@ -1,25 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -__credits__ = """ - Copyright (C) 2012 YunoHost - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program; if not, see http://www.gnu.org/licenses - """ -__author__ = 'Kload ' -__version__ = '2.0 beta1' - import os import sys import argparse diff --git a/yunohost.py b/yunohost.py index 543b0d17..1bacf28f 100644 --- a/yunohost.py +++ b/yunohost.py @@ -22,6 +22,26 @@ """ YunoHost core classes & functions """ + +__credits__ = """ + Copyright (C) 2012 YunoHost + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program; if not, see http://www.gnu.org/licenses + """ +__author__ = 'Kload ' +__version__ = '2.0-beta3' + import os import sys try: @@ -468,10 +488,13 @@ def parse_dict(action_map): # Add general arguments for arg_name, arg_params in action_map['general_arguments'].items(): + if 'version' in arg_params: + v = arg_params['version'] + arg_params['version'] = v.replace('%version%', __version__) if 'full' in arg_params: - arg_names = [arg_name, arg_params['full']] - arg_fullname = arg_params['full'] - del arg_params['full'] + arg_names = [arg_name, arg_params['full']] + arg_fullname = arg_params['full'] + del arg_params['full'] else: arg_names = [arg_name] parsers['general'].add_argument(*arg_names, **arg_params)