mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
128 lines
3.7 KiB
Python
128 lines
3.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
|
import sys
|
|
import subprocess
|
|
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 yunohost import YunoHostError, win_msg, colorize, validate, get_required_args
|
|
|
|
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)
|
|
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")
|
|
|
|
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:
|
|
print 'MAC NOT FOUND!'
|
|
|
|
def uptime():
|
|
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()]
|
|
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
|
|
|
|
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):
|
|
if args['memory']:
|
|
check_memory()
|
|
elif args['cpu']:
|
|
check_cpu()
|
|
elif args['disk']:
|
|
check_disk()
|
|
elif args['ifconfig']:
|
|
ifconfig()
|
|
elif args['uptime']:
|
|
uptime()
|
|
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']) }
|