mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
Add human readable option for monitor actions
This commit is contained in:
parent
0f2f59daf3
commit
c745298404
3 changed files with 62 additions and 19 deletions
|
@ -57,9 +57,9 @@ Specifications
|
||||||
|
|
||||||
### Monitoring
|
### Monitoring
|
||||||
|
|
||||||
yunohost monitor disk [-h] [-m MOUNTPOINT] [-t] [-f]
|
yunohost monitor disk [-h] [-m MOUNTPOINT] [-t] [-f] [-H]
|
||||||
yunohost monitor network [-h] [-u] [-i]
|
yunohost monitor network [-h] [-u] [-i] [-H]
|
||||||
yunohost monitor system [-h] [-m] [-u] [-i] [-p] [-c]
|
yunohost monitor system [-h] [-m] [-u] [-i] [-p] [-c] [-H]
|
||||||
yunohost monitor process [-h] [-e PROCESS] [-d PROCESS]
|
yunohost monitor process [-h] [-e PROCESS] [-d PROCESS]
|
||||||
[--stop PROCESS] [-c PORT] [-i]
|
[--stop PROCESS] [-c PORT] [-i]
|
||||||
[--start PROCESS]
|
[--start PROCESS]
|
||||||
|
|
|
@ -444,6 +444,10 @@ monitor:
|
||||||
full: --mountpoint
|
full: --mountpoint
|
||||||
help: Monitor only the device mounted on MOUNTPOINT
|
help: Monitor only the device mounted on MOUNTPOINT
|
||||||
action: store
|
action: store
|
||||||
|
-H:
|
||||||
|
full: --human-readable
|
||||||
|
help: Print sizes in human readable format
|
||||||
|
action: store_true
|
||||||
|
|
||||||
### monitor_network()
|
### monitor_network()
|
||||||
network:
|
network:
|
||||||
|
@ -461,6 +465,10 @@ monitor:
|
||||||
action: append_const
|
action: append_const
|
||||||
const: infos
|
const: infos
|
||||||
dest: units
|
dest: units
|
||||||
|
-H:
|
||||||
|
full: --human-readable
|
||||||
|
help: Print sizes in human readable format
|
||||||
|
action: store_true
|
||||||
|
|
||||||
### monitor_system()
|
### monitor_system()
|
||||||
system:
|
system:
|
||||||
|
@ -496,6 +504,10 @@ monitor:
|
||||||
action: append_const
|
action: append_const
|
||||||
const: infos
|
const: infos
|
||||||
dest: units
|
dest: units
|
||||||
|
-H:
|
||||||
|
full: --human-readable
|
||||||
|
help: Print sizes in human readable format
|
||||||
|
action: store_true
|
||||||
|
|
||||||
process:
|
process:
|
||||||
action_help: Check Process
|
action_help: Check Process
|
||||||
|
|
|
@ -36,17 +36,6 @@ from yunohost import YunoHostError, win_msg, colorize, validate, get_required_ar
|
||||||
|
|
||||||
glances_uri = 'http://127.0.0.1:61209'
|
glances_uri = 'http://127.0.0.1:61209'
|
||||||
|
|
||||||
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 process_enable(args):
|
def process_enable(args):
|
||||||
output = subprocess.Popen(['update-rc.d', args, 'defaults'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
output = subprocess.Popen(['update-rc.d', args, 'defaults'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||||
if output.wait() == 0:
|
if output.wait() == 0:
|
||||||
|
@ -96,13 +85,14 @@ def process_check(args):
|
||||||
return { 'Status' : result }
|
return { 'Status' : result }
|
||||||
|
|
||||||
|
|
||||||
def monitor_disk(units=None, mountpoint=None):
|
def monitor_disk(units=None, mountpoint=None, human_readable=False):
|
||||||
"""
|
"""
|
||||||
Monitor disk space and usage
|
Monitor disk space and usage
|
||||||
|
|
||||||
Keyword argument:
|
Keyword argument:
|
||||||
units -- Unit(s) to monitor
|
units -- Unit(s) to monitor
|
||||||
mountpoint -- Device mountpoint
|
mountpoint -- Device mountpoint
|
||||||
|
human_readable -- Print sizes in human readable format
|
||||||
|
|
||||||
"""
|
"""
|
||||||
glances = _get_glances_api()
|
glances = _get_glances_api()
|
||||||
|
@ -145,6 +135,9 @@ def monitor_disk(units=None, mountpoint=None):
|
||||||
if dm != dmount:
|
if dm != dmount:
|
||||||
continue
|
continue
|
||||||
del d['device_name']
|
del d['device_name']
|
||||||
|
if human_readable:
|
||||||
|
for i in ['used', 'avail', 'size']:
|
||||||
|
d[i] = _bytes_to_human(d[i])
|
||||||
if len(units) > 1:
|
if len(units) > 1:
|
||||||
result[dn][u] = d
|
result[dn][u] = d
|
||||||
else:
|
else:
|
||||||
|
@ -157,12 +150,13 @@ def monitor_disk(units=None, mountpoint=None):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def monitor_network(units=None):
|
def monitor_network(units=None, human_readable=False):
|
||||||
"""
|
"""
|
||||||
Monitor network interfaces
|
Monitor network interfaces
|
||||||
|
|
||||||
Keyword argument:
|
Keyword argument:
|
||||||
units -- Unit(s) to monitor
|
units -- Unit(s) to monitor
|
||||||
|
human_readable -- Print sizes in human readable format
|
||||||
|
|
||||||
"""
|
"""
|
||||||
glances = _get_glances_api()
|
glances = _get_glances_api()
|
||||||
|
@ -188,6 +182,10 @@ def monitor_network(units=None):
|
||||||
iname = i['interface_name']
|
iname = i['interface_name']
|
||||||
if iname in devices.keys():
|
if iname in devices.keys():
|
||||||
del i['interface_name']
|
del i['interface_name']
|
||||||
|
if human_readable:
|
||||||
|
for k in i.keys():
|
||||||
|
if k != 'time_since_update':
|
||||||
|
i[k] = _bytes_to_human(i[k], True)
|
||||||
result[u][iname] = i
|
result[u][iname] = i
|
||||||
elif u == 'infos':
|
elif u == 'infos':
|
||||||
try:
|
try:
|
||||||
|
@ -219,12 +217,13 @@ def monitor_network(units=None):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def monitor_system(units=None):
|
def monitor_system(units=None, human_readable=False):
|
||||||
"""
|
"""
|
||||||
Monitor system informations and usage
|
Monitor system informations and usage
|
||||||
|
|
||||||
Keyword argument:
|
Keyword argument:
|
||||||
units -- Unit(s) to monitor
|
units -- Unit(s) to monitor
|
||||||
|
human_readable -- Print sizes in human readable format
|
||||||
|
|
||||||
"""
|
"""
|
||||||
glances = _get_glances_api()
|
glances = _get_glances_api()
|
||||||
|
@ -236,9 +235,18 @@ def monitor_system(units=None):
|
||||||
# Retrieve monitoring for unit(s)
|
# Retrieve monitoring for unit(s)
|
||||||
for u in units:
|
for u in units:
|
||||||
if u == 'memory':
|
if u == 'memory':
|
||||||
|
ram = json.loads(glances.getMem())
|
||||||
|
swap = json.loads(glances.getMemSwap())
|
||||||
|
if human_readable:
|
||||||
|
for i in ram.keys():
|
||||||
|
if i != 'percent':
|
||||||
|
ram[i] = _bytes_to_human(ram[i])
|
||||||
|
for i in swap.keys():
|
||||||
|
if i != 'percent':
|
||||||
|
swap[i] = _bytes_to_human(swap[i])
|
||||||
result[u] = {
|
result[u] = {
|
||||||
'ram': json.loads(glances.getMem()),
|
'ram': ram,
|
||||||
'swap': json.loads(glances.getMemSwap())
|
'swap': swap
|
||||||
}
|
}
|
||||||
elif u == 'cpu':
|
elif u == 'cpu':
|
||||||
result[u] = {
|
result[u] = {
|
||||||
|
@ -307,6 +315,7 @@ def _extract_inet(string):
|
||||||
|
|
||||||
Keyword argument:
|
Keyword argument:
|
||||||
string -- String to search in
|
string -- String to search in
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# TODO: Return IPv4 and IPv6?
|
# TODO: Return IPv4 and IPv6?
|
||||||
ip4_prog = re.compile('((25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}/[0-9]{1,2})')
|
ip4_prog = re.compile('((25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}/[0-9]{1,2})')
|
||||||
|
@ -321,3 +330,25 @@ def _extract_inet(string):
|
||||||
return m.group(1)
|
return m.group(1)
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _bytes_to_human(n, bits=False):
|
||||||
|
"""
|
||||||
|
Convert bytes (or bits) into human readable format
|
||||||
|
|
||||||
|
Keyword argument:
|
||||||
|
n -- Number to convert
|
||||||
|
bits -- Process n as bits
|
||||||
|
|
||||||
|
"""
|
||||||
|
symbols = ('B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
|
||||||
|
if bits:
|
||||||
|
symbols = ('b', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb')
|
||||||
|
prefix = {}
|
||||||
|
for i, s in enumerate(symbols):
|
||||||
|
prefix[s] = 1 << i*10
|
||||||
|
for s in reversed(symbols):
|
||||||
|
if n >= prefix[s]:
|
||||||
|
value = float(n) / prefix[s]
|
||||||
|
return '%.1f%s' % (value, s)
|
||||||
|
return "%s%s" % (n, symbols[0])
|
||||||
|
|
Loading…
Add table
Reference in a new issue