mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
merging
This commit is contained in:
commit
0367811b6e
3 changed files with 76 additions and 29 deletions
|
@ -384,11 +384,27 @@ monitor:
|
||||||
full: --uptime
|
full: --uptime
|
||||||
help: Show Uptime
|
help: Show Uptime
|
||||||
action: store_true
|
action: store_true
|
||||||
-p:
|
process:
|
||||||
full: --process
|
action_help: Check Process
|
||||||
help: Show Process Account
|
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
|
action: store_true
|
||||||
|
|
||||||
|
|
||||||
#############################
|
#############################
|
||||||
# Firewall #
|
# Firewall #
|
||||||
|
|
32
parse_args
32
parse_args
|
@ -24,6 +24,12 @@ import os
|
||||||
import sys
|
import sys
|
||||||
import argparse
|
import argparse
|
||||||
import gettext
|
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 yaml
|
||||||
import json
|
import json
|
||||||
if not __debug__:
|
if not __debug__:
|
||||||
|
@ -41,11 +47,11 @@ except ImportError:
|
||||||
def parse_dict(action_map):
|
def parse_dict(action_map):
|
||||||
"""
|
"""
|
||||||
Turn action dictionnary to parser, subparsers and arguments
|
Turn action dictionnary to parser, subparsers and arguments
|
||||||
|
|
||||||
Keyword arguments:
|
Keyword arguments:
|
||||||
action_map -- Multi-level dictionnary of categories/actions/arguments list
|
action_map -- Multi-level dictionnary of categories/actions/arguments list
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Namespace of args
|
Namespace of args
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
@ -63,7 +69,7 @@ def parse_dict(action_map):
|
||||||
else:
|
else:
|
||||||
parsers['general'].add_argument(arg_name, **arg_params)
|
parsers['general'].add_argument(arg_name, **arg_params)
|
||||||
|
|
||||||
del action_map['general_arguments']
|
del action_map['general_arguments']
|
||||||
|
|
||||||
# Split categories into subparsers
|
# Split categories into subparsers
|
||||||
for category, category_params in action_map.items():
|
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'])
|
parsers[category + '_' + action] = subparsers_action[category].add_parser(action, help=action_params['action_help'])
|
||||||
# Set the action s related function
|
# Set the action s related function
|
||||||
parsers[category + '_' + action].set_defaults(
|
parsers[category + '_' + action].set_defaults(
|
||||||
func=str_to_func('yunohost_' + category + '.'
|
func=str_to_func('yunohost_' + category + '.'
|
||||||
+ category + '_' + action))
|
+ category + '_' + action))
|
||||||
# Add arguments
|
# Add arguments
|
||||||
if 'arguments' in action_params:
|
if 'arguments' in action_params:
|
||||||
for arg_name, arg_params in action_params['arguments'].items():
|
for arg_name, arg_params in action_params['arguments'].items():
|
||||||
|
@ -91,24 +97,24 @@ def parse_dict(action_map):
|
||||||
|
|
||||||
return parsers['general'].parse_args()
|
return parsers['general'].parse_args()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""
|
"""
|
||||||
Main instructions
|
Main instructions
|
||||||
|
|
||||||
Parse the action_dict and execute the action-specific function,
|
Parse the action_dict and execute the action-specific function,
|
||||||
then print json or pretty result if executed in a tty :)
|
then print json or pretty result if executed in a tty :)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
int -- 0 or error code
|
int -- 0 or error code
|
||||||
|
|
||||||
"""
|
"""
|
||||||
with open('action_map.yml') as f:
|
with open('action_map.yml') as f:
|
||||||
action_map = yaml.load(f)
|
action_map = yaml.load(f)
|
||||||
|
|
||||||
args = parse_dict(action_map)
|
args = parse_dict(action_map)
|
||||||
connections = connect_services(action_map)
|
connections = connect_services(action_map)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if connections:
|
if connections:
|
||||||
result = args.func(vars(args), connections)
|
result = args.func(vars(args), connections)
|
||||||
|
@ -132,7 +138,7 @@ def main():
|
||||||
finally:
|
finally:
|
||||||
disconnect_services(connections)
|
disconnect_services(connections)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
|
|
@ -10,7 +10,7 @@ except ImportError:
|
||||||
sys.stderr.write('apt-get install python-psutil\n')
|
sys.stderr.write('apt-get install python-psutil\n')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
#from psutil._compat import print_
|
from yunohost import YunoHostError, win_msg, colorize, validate, get_required_args
|
||||||
|
|
||||||
def bytes2human(n):
|
def bytes2human(n):
|
||||||
symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
|
symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
|
||||||
|
@ -27,16 +27,16 @@ def check_disk():
|
||||||
templ = "%s,%s/%s,%s,%s"
|
templ = "%s,%s/%s,%s,%s"
|
||||||
for part in psutil.disk_partitions(all=False):
|
for part in psutil.disk_partitions(all=False):
|
||||||
usage = psutil.disk_usage(part.mountpoint)
|
usage = psutil.disk_usage(part.mountpoint)
|
||||||
print(templ % (part.mountpoint,
|
result = (templ % (part.mountpoint,
|
||||||
bytes2human(usage.used),
|
bytes2human(usage.used),
|
||||||
bytes2human(usage.total),
|
bytes2human(usage.total),
|
||||||
bytes2human(usage.free),
|
bytes2human(usage.free),
|
||||||
int(usage.percent)))
|
int(usage.percent)))
|
||||||
|
print result
|
||||||
|
|
||||||
def check_cpu():
|
def check_cpu():
|
||||||
print psutil.cpu_percent(interval=1)
|
print psutil.cpu_percent(interval=1)
|
||||||
|
|
||||||
|
|
||||||
def check_memory():
|
def check_memory():
|
||||||
print getattr(psutil.phymem_usage(), "percent")
|
print getattr(psutil.phymem_usage(), "percent")
|
||||||
print getattr(psutil.virtmem_usage(), "percent")
|
print getattr(psutil.virtmem_usage(), "percent")
|
||||||
|
@ -51,9 +51,8 @@ def ifconfig():
|
||||||
print 'MAC NOT FOUND!'
|
print 'MAC NOT FOUND!'
|
||||||
|
|
||||||
def uptime():
|
def uptime():
|
||||||
uptime = datetime.now() - datetime.fromtimestamp(psutil.BOOT_TIME)
|
uptimeres = (str(datetime.now() - datetime.fromtimestamp(psutil.BOOT_TIME)).split('.')[0])
|
||||||
print "Uptime: %s" % (str(uptime).split('.')[0])
|
return uptimeres
|
||||||
|
|
||||||
def processcount():
|
def processcount():
|
||||||
processcount = {'total': 0, 'running': 0, 'sleeping': 0}
|
processcount = {'total': 0, 'running': 0, 'sleeping': 0}
|
||||||
process_all = [proc for proc in psutil.process_iter()]
|
process_all = [proc for proc in psutil.process_iter()]
|
||||||
|
@ -82,10 +81,22 @@ def processcount():
|
||||||
process.append(self.__get_process_stats__(proc))
|
process.append(self.__get_process_stats__(proc))
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
print '%s, %s running, %s sleeping' % (str(processcount['total']),
|
def process_enable(args):
|
||||||
str(processcount['running']),
|
print 'process_enable'
|
||||||
str(processcount['sleeping']))
|
|
||||||
|
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):
|
def monitor_info(args):
|
||||||
|
@ -96,8 +107,22 @@ def monitor_info(args):
|
||||||
elif args['disk']:
|
elif args['disk']:
|
||||||
check_disk()
|
check_disk()
|
||||||
elif args['ifconfig']:
|
elif args['ifconfig']:
|
||||||
ifconfig()
|
ifconfig()
|
||||||
elif args['uptime']:
|
elif args['uptime']:
|
||||||
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()
|
processcount()
|
||||||
|
return { _("Total") : str(processcount['total']), _("Running") : str(processcount['running']), _("Sleeping") : str(processcount['sleeping']) }
|
||||||
|
|
Loading…
Add table
Reference in a new issue