From 4add88a5a005dadacc13d1b764e8a0c44218f06e Mon Sep 17 00:00:00 2001 From: root Date: Sun, 14 Oct 2012 11:56:57 +0200 Subject: [PATCH] Add monitor --- parse_args | 38 ++++++++++++++++++++++++++---- yunohost_monitor.py | 56 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 yunohost_monitor.py diff --git a/parse_args b/parse_args index d63da352..38d66b8b 100755 --- a/parse_args +++ b/parse_args @@ -411,8 +411,35 @@ action_dict = { }, 'monitor' : { 'category_help' : _("Monitoring functions"), - 'actions' : {} - }, + 'actions' : { + 'info': { + 'action_help' : _("Check System"), + 'connections' : ['ldap'], + 'arguments' : { + '-m' : { + 'full' : '--memory', + 'help' : _("Check Memory"), + 'action' : 'store_true', + }, + '-c' : { + 'full' : '--cpu', + 'help' : _("Check CPU"), + 'action' : 'store_true', + }, + '-d' : { + 'full' : '--disk', + 'help' : _("Check Disk"), + 'action' : 'store_true', + }, + '-i' : { + 'full' : '--ifconfig', + 'help' : _("Ifconfig"), + 'action' : 'store_true', + }, + } + }, + } + }, 'tools' : { 'category_help' : _("Specific tools"), 'actions' : {} @@ -459,9 +486,10 @@ def parse_dict(action_dict): if 'actions' in category_params: for action, action_params in category_params['actions'].items(): # Connections settings - for connection in connections_available: - if connection in action_params['connections']: - connections_enabled.append(connection) + if 'connections' in action_params: + for connection in connections_available: + if connection in action_params['connections']: + connections_enabled.append(connection) if 'action_help' not in action_params: action_params['action_help'] = '' parsers[category + '_' + action] = subparsers_action[category].add_parser(action, help=action_params['action_help']) # Set the action s related function diff --git a/yunohost_monitor.py b/yunohost_monitor.py new file mode 100644 index 00000000..9fdaafbe --- /dev/null +++ b/yunohost_monitor.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- + +import os +import sys +import subprocess +import psutil +from psutil._compat import print_ + +def bytes2human(n): + symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y') + prefix = {} + for i, s in enumerate(symbols): + prefix[s] = 1 << (i+1)*10 + for s in reversed(symbols): + if n >= prefix[s]: + value = float(n) / prefix[s] + return '%.1f%s' % (value, s) + return "%sB" % n + +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, + bytes2human(usage.used), + bytes2human(usage.total), + bytes2human(usage.free), + int(usage.percent))) + +def check_cpu(): + print psutil.cpu_percent(interval=1) + + +def check_memory(): + print getattr(psutil.phymem_usage(), "percent") + print getattr(psutil.virtmem_usage(), "percent") + +def ifconfig(): + output = subprocess.Popen(['ifconfig'], stdout=subprocess.PIPE).communicate()[0] + if 'HWaddr' in output: + mac = output[(output.find('HWaddr')+7):(output.find('HWaddr')+24)] + ip = output[(output.find('Bcast')-15):(output.find('inet')+22)] + print 'MAC: ' + mac + ' IP: ' +ip + else: + return 'MAC NOT FOUND!' + + +def monitor_info(args, connections): + if args['memory'] == True: + check_memory() + elif args['cpu'] == True: + check_cpu() + elif args['disk'] == True: + check_disk() + elif args['ifconfig'] == True: + ifconfig()