From 2fee1eb6f7e9c2ee8683a57dfedf4d76a7d9c83a Mon Sep 17 00:00:00 2001 From: Jerome Lebleu Date: Tue, 17 Dec 2013 02:35:31 +0100 Subject: [PATCH] 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):