From 2caab6a31ddeb3cab7b4db4bc4eb3c80e63ae795 Mon Sep 17 00:00:00 2001 From: Adrien Beudin Date: Tue, 16 Oct 2012 14:56:54 +0200 Subject: [PATCH 1/2] add try import ldap and psutils --- appconftest/bash/yunohost | 35 +++-------------------------------- appconftest/bash/yunohost_cli | 33 +++++++++++++++++++++++++++++++++ parse_args | 2 +- yunohost.py | 7 ++++++- yunohost_monitor.py | 7 ++++++- 5 files changed, 49 insertions(+), 35 deletions(-) mode change 100644 => 100755 appconftest/bash/yunohost create mode 100644 appconftest/bash/yunohost_cli diff --git a/appconftest/bash/yunohost b/appconftest/bash/yunohost old mode 100644 new mode 100755 index 2bcb6963..1990fe71 --- a/appconftest/bash/yunohost +++ b/appconftest/bash/yunohost @@ -1,33 +1,4 @@ -# yuno-repo(1) completion +#!/bin/sh -_yunohost() -{ -local argc cur prev opts -COMPREPLY=() - -argc=${COMP_CWORD} -cur="${COMP_WORDS[argc]}" -prev="${COMP_WORDS[argc-1]}" -opts=$(yunohost2 -h | grep usage | awk -F"{" '{print $2}' | awk -F"}" '{print $1}' | tr ',' ' ') - -if [[ $argc = 1 ]]; -then - COMPREPLY=( $(compgen -W "$opts --help" -- $cur ) ) -fi - -if [[ "$prev" != "--help" ]]; -then - if [[ $argc = 2 ]]; - then - opts2=$(yunohost2 $prev -h | grep usage | awk -F"{" '{print $2}' | awk -F"}" '{print $1}' | tr ',' ' ') - COMPREPLY=( $(compgen -W "$opts2 --help" -- $cur ) ) - elif [[ $argc = 3 ]]; - then - COMPREPLY=( $(compgen -W "--help" $cur ) ) - fi -else - COMPREPLY=() -fi - -} -complete -F _yunohost yunohost +cd /usr/share/pyshared/yunohost/ +python parse_args $@ diff --git a/appconftest/bash/yunohost_cli b/appconftest/bash/yunohost_cli new file mode 100644 index 00000000..c924ea79 --- /dev/null +++ b/appconftest/bash/yunohost_cli @@ -0,0 +1,33 @@ +# yunohost(1) completion + +_yunohost_cli() +{ +local argc cur prev opts +COMPREPLY=() + +argc=${COMP_CWORD} +cur="${COMP_WORDS[argc]}" +prev="${COMP_WORDS[argc-1]}" +opts=$(yunohost -h | grep usage | awk -F"{" '{print $2}' | awk -F"}" '{print $1}' | tr ',' ' ') + +if [[ $argc = 1 ]]; +then + COMPREPLY=( $(compgen -W "$opts --help" -- $cur ) ) +fi + +if [[ "$prev" != "--help" ]]; +then + if [[ $argc = 2 ]]; + then + opts2=$(yunohost $prev -h | grep usage | awk -F"{" '{print $2}' | awk -F"}" '{print $1}' | tr ',' ' ') + COMPREPLY=( $(compgen -W "$opts2 --help" -- $cur ) ) + elif [[ $argc = 3 ]]; + then + COMPREPLY=( $(compgen -W "--help" $cur ) ) + fi +else + COMPREPLY=() +fi + +} +complete -F _yunohost_cli yunohost diff --git a/parse_args b/parse_args index f46d02d4..abeadd6a 100755 --- a/parse_args +++ b/parse_args @@ -33,7 +33,7 @@ gettext.install('YunoHost') try: from yunohost import YunoHostError, YunoHostLDAP, str_to_func, colorize, pretty_print_dict, display_error, connect_services, disconnect_services except ImportError: - sys.stderr.write('Require YunoHost lib') + sys.stderr.write('Error: Yunohost CLI Require YunoHost lib\n') sys.exit(1) """ diff --git a/yunohost.py b/yunohost.py index 227ee3f4..90a9c12b 100644 --- a/yunohost.py +++ b/yunohost.py @@ -2,7 +2,12 @@ import os import sys -import ldap +try: + import ldap +except ImportError: + sys.stderr.write('Error: Yunohost CLI Require LDAP lib\n') + sys.stderr.write('apt-get install python-ldap\n') + sys.exit(1) import ldap.modlist as modlist import json import re diff --git a/yunohost_monitor.py b/yunohost_monitor.py index 65712062..ed38d3f0 100644 --- a/yunohost_monitor.py +++ b/yunohost_monitor.py @@ -3,7 +3,12 @@ import os import sys import subprocess -import psutil +try: + import psutil +except ImportError: + sys.stderr.write('Error: Yunohost CLI Require psutil\n') + sys.stderr.write('apt-get install python-psutil\n') + sys.exit(1) from datetime import datetime, timedelta #from psutil._compat import print_ From da0f59d2542aad483a7ac509ff96b9c4d2950646 Mon Sep 17 00:00:00 2001 From: Adrien Beudin Date: Tue, 16 Oct 2012 17:52:39 +0200 Subject: [PATCH 2/2] add process count --- parse_args | 5 +++++ yunohost_monitor.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/parse_args b/parse_args index abeadd6a..6feebbb8 100755 --- a/parse_args +++ b/parse_args @@ -469,6 +469,11 @@ action_map = { 'full' : '--uptime', 'help' : _("Show Uptime"), 'action' : 'store_true', + }, + '-p' : { + 'full' : '--process', + 'help' : _("Show Process Account"), + 'action' : 'store_true', }, } }, diff --git a/yunohost_monitor.py b/yunohost_monitor.py index ed38d3f0..fb4398ad 100644 --- a/yunohost_monitor.py +++ b/yunohost_monitor.py @@ -54,6 +54,40 @@ def uptime(): uptime = datetime.now() - datetime.fromtimestamp(psutil.BOOT_TIME) print "Uptime: %s" % (str(uptime).split('.')[0]) +def processcount(): + processcount = {'total': 0, 'running': 0, 'sleeping': 0} + process_all = [proc for proc in psutil.process_iter()] + for proc in process_all: + try: + if not proc.is_running(): + try: + process_all.remove(proc) + except Exception: + pass + except psutil.error.NoSuchProcess: + try: + self.process_all.remove(proc) + except Exception: + pass + else: + try: + processcount[str(proc.status)] += 1 + except psutil.error.NoSuchProcess: + pass + except KeyError: + processcount[str(proc.status)] = 1 + finally: + processcount['total'] += 1 + try: + 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 monitor_info(args): if args['memory']: check_memory() @@ -65,3 +99,5 @@ def monitor_info(args): ifconfig() elif args['uptime']: uptime() + elif args['process']: + processcount()