From 0778a8410cb2af431612f3a50cca6adf468d4460 Mon Sep 17 00:00:00 2001 From: adrien Date: Sun, 28 Oct 2012 15:55:35 +0100 Subject: [PATCH] add process check --- action_map.yml | 24 ++++++++++++++++++---- parse_args | 34 ++++++++++++++++++------------- yunohost_monitor.py | 49 ++++++++++++++++++++++++++++++++++----------- 3 files changed, 77 insertions(+), 30 deletions(-) diff --git a/action_map.yml b/action_map.yml index 9a1df261..568c68e0 100644 --- a/action_map.yml +++ b/action_map.yml @@ -384,11 +384,27 @@ monitor: full: --uptime help: Show Uptime action: store_true - -p: - full: --process - help: Show Process Account + process: + action_help: Check Process + arguments: + -e: + full: --enable + help: Enable process + -d: + full: --disable + help: Disable process + --start: + help: Start process + --stop: + help: Stop process + -c: + full: --check + help: Check process + -i: + full: --info + help: Process info action: store_true - + ############################# # Firewall # diff --git a/parse_args b/parse_args index 67e46b67..cb2827ed 100755 --- a/parse_args +++ b/parse_args @@ -24,6 +24,12 @@ import os import sys import argparse import gettext +try: + import yaml +except ImportError: + sys.stderr.write('Error: Yunohost CLI Require yaml lib\n') + sys.stderr.write('apt-get install python-yaml\n') + sys.exit(1) import yaml import json if not __debug__: @@ -41,11 +47,11 @@ except ImportError: def parse_dict(action_map): """ Turn action dictionnary to parser, subparsers and arguments - + Keyword arguments: action_map -- Multi-level dictionnary of categories/actions/arguments list - - Returns: + + Returns: Namespace of args """ @@ -63,7 +69,7 @@ def parse_dict(action_map): else: parsers['general'].add_argument(arg_name, **arg_params) - del action_map['general_arguments'] + del action_map['general_arguments'] # Split categories into subparsers for category, category_params in action_map.items(): @@ -77,8 +83,8 @@ def parse_dict(action_map): parsers[category + '_' + action] = subparsers_action[category].add_parser(action, help=action_params['action_help']) # Set the action s related function parsers[category + '_' + action].set_defaults( - func=str_to_func('yunohost_' + category + '.' - + category + '_' + action)) + func=str_to_func('yunohost_' + category + '.' + + category + '_' + action)) # Add arguments if 'arguments' in action_params: for arg_name, arg_params in action_params['arguments'].items(): @@ -91,24 +97,24 @@ def parse_dict(action_map): return parsers['general'].parse_args() - + def main(): - """ - Main instructions - + """ + Main instructions + Parse the action_dict and execute the action-specific function, then print json or pretty result if executed in a tty :) Returns: int -- 0 or error code - + """ with open('action_map.yml') as f: action_map = yaml.load(f) args = parse_dict(action_map) connections = connect_services(action_map) - + try: if connections: result = args.func(vars(args), connections) @@ -122,7 +128,7 @@ def main(): except YunoHostError, error: display_error(error) return error.code - else: + else: if os.isatty(1): pretty_print_dict(result) else: @@ -130,7 +136,7 @@ def main(): finally: disconnect_services(connections) - return 0 + return 0 if __name__ == '__main__': sys.exit(main()) diff --git a/yunohost_monitor.py b/yunohost_monitor.py index fb4398ad..c9137b69 100644 --- a/yunohost_monitor.py +++ b/yunohost_monitor.py @@ -10,7 +10,7 @@ except ImportError: sys.stderr.write('apt-get install python-psutil\n') sys.exit(1) from datetime import datetime, timedelta -#from psutil._compat import print_ +from yunohost import YunoHostError, win_msg, colorize, validate, get_required_args def bytes2human(n): symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y') @@ -27,16 +27,16 @@ def check_disk(): templ = "%s,%s/%s,%s,%s" for part in psutil.disk_partitions(all=False): usage = psutil.disk_usage(part.mountpoint) - print(templ % (part.mountpoint, + result = (templ % (part.mountpoint, bytes2human(usage.used), bytes2human(usage.total), bytes2human(usage.free), int(usage.percent))) + print result def check_cpu(): print psutil.cpu_percent(interval=1) - def check_memory(): print getattr(psutil.phymem_usage(), "percent") print getattr(psutil.virtmem_usage(), "percent") @@ -51,9 +51,8 @@ def ifconfig(): print 'MAC NOT FOUND!' def uptime(): - uptime = datetime.now() - datetime.fromtimestamp(psutil.BOOT_TIME) - print "Uptime: %s" % (str(uptime).split('.')[0]) - + uptimeres = (str(datetime.now() - datetime.fromtimestamp(psutil.BOOT_TIME)).split('.')[0]) + return uptimeres def processcount(): processcount = {'total': 0, 'running': 0, 'sleeping': 0} process_all = [proc for proc in psutil.process_iter()] @@ -82,10 +81,22 @@ def processcount(): process.append(self.__get_process_stats__(proc)) except Exception: pass - - print '%s, %s running, %s sleeping' % (str(processcount['total']), - str(processcount['running']), - str(processcount['sleeping'])) + +def process_enable(args): + print 'process_enable' + +def process_disable(args): + uptime = datetime.now() - datetime.fromtimestamp(psutil.BOOT_TIME) + print "Uptime: %s" % (str(uptime).split('.')[0]) + +def process_start(args): + print 'process_start' + +def process_stop(args): + print 'process_stop' + +def process_check(args): + print 'process_check' def monitor_info(args): @@ -96,8 +107,22 @@ def monitor_info(args): elif args['disk']: check_disk() elif args['ifconfig']: - ifconfig() + ifconfig() elif args['uptime']: uptime() - elif args['process']: + return { 'Uptime' : uptimeres } + +def monitor_process(args): + if args['enable']: + process_enable() + elif args['disable']: + process_disable() + elif args['start']: + process_start() + elif args['stop']: + process_stop() + elif args['check']: + process_check() + elif args['info']: processcount() + return { _("Total") : str(processcount['total']), _("Running") : str(processcount['running']), _("Sleeping") : str(processcount['sleeping']) }