Merge pull request #4 from YunoHost/dev_beudbeud

add start, stop function and update ifconfig
This commit is contained in:
abeudin 2012-10-30 13:17:37 -07:00
commit 72db806b1a

View file

@ -9,6 +9,12 @@ except ImportError:
sys.stderr.write('Error: Yunohost CLI Require psutil\n') sys.stderr.write('Error: Yunohost CLI Require psutil\n')
sys.stderr.write('apt-get install python-psutil\n') sys.stderr.write('apt-get install python-psutil\n')
sys.exit(1) sys.exit(1)
try:
import netifaces
except ImportError:
sys.stderr.write('Error: Yunohost CLI Require netifaces\n')
sys.stderr.write('apt-get install python-netifaces\n')
sys.exit(1)
from datetime import datetime, timedelta from datetime import datetime, timedelta
from yunohost import YunoHostError, win_msg, colorize, validate, get_required_args from yunohost import YunoHostError, win_msg, colorize, validate, get_required_args
@ -27,32 +33,32 @@ 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)
result = (templ % (part.mountpoint, return { _("Partition") : (part.mountpoint, bytes2human(usage.used), bytes2human(usage.total), bytes2human(usage.free), int(usage.percent)) }
bytes2human(usage.used),
bytes2human(usage.total),
bytes2human(usage.free),
int(usage.percent)))
print result
def check_cpu(): def check_cpu():
print psutil.cpu_percent(interval=1) return { _("CPU") : psutil.cpu_percent(interval=3) }
def check_memory(): def check_memory():
print getattr(psutil.phymem_usage(), "percent") mem = getattr(psutil.phymem_usage(), "percent")
print getattr(psutil.virtmem_usage(), "percent") swap = getattr(psutil.virtmem_usage(), "percent")
return { _("Memory") : mem, _("Swap") : swap }
def ifconfig(): def ifconfig():
output = subprocess.Popen(['ifconfig'], stdout=subprocess.PIPE).communicate()[0] listinterfaces = netifaces.interfaces()[1:]
if 'HWaddr' in output: for interface in listinterfaces:
mac = output[(output.find('HWaddr')+7):(output.find('HWaddr')+24)] try:
ip = output[(output.find('Bcast')-15):(output.find('inet')+22)] for link in netifaces.ifaddresses(interface)[netifaces.AF_INET]:
print 'MAC: ' + mac + ' IP: ' +ip ip = link['addr']
else: for link in netifaces.ifaddresses(interface)[netifaces.AF_LINK]:
print 'MAC NOT FOUND!' mac = link['addr']
except:
pass
return { _('IP') : ip, _('MAC') : mac }
def uptime(): def uptime():
uptimeres = (str(datetime.now() - datetime.fromtimestamp(psutil.BOOT_TIME)).split('.')[0]) uptime = (str(datetime.now() - datetime.fromtimestamp(psutil.BOOT_TIME)).split('.')[0])
return uptimeres return { _("Uptime") : uptime }
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()]
@ -81,19 +87,27 @@ def processcount():
process.append(self.__get_process_stats__(proc)) process.append(self.__get_process_stats__(proc))
except Exception: except Exception:
pass pass
return { _("Total") : str(processcount['total']), _("Running") : str(processcount['running']), _("Sleeping") : str(processcount['sleeping']) }
def process_enable(args): def process_enable(args):
print 'process_enable' print 'process_enable'
def process_disable(args): def process_disable(args):
uptime = datetime.now() - datetime.fromtimestamp(psutil.BOOT_TIME) print 'process disable'
print "Uptime: %s" % (str(uptime).split('.')[0])
def process_start(args): def process_start(args):
print 'process_start' output = subprocess.Popen(['service', args, 'start'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
if output.wait() == 0:
return { _('Start') + " " + args : "OK" }
else:
raise YunoHostError(1, _('Start failure'))
def process_stop(args): def process_stop(args):
print 'process_stop' output = subprocess.Popen(['service', args, 'stop'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
if output.wait() == 0:
return { _('Stop') + " " + args : "OK" }
else:
raise YunoHostError(1, _('Start failure'))
def process_check(args): def process_check(args):
print 'process_check' print 'process_check'
@ -101,28 +115,33 @@ def process_check(args):
def monitor_info(args): def monitor_info(args):
if args['memory']: if args['memory']:
check_memory() resultat = check_memory()
return resultat
elif args['cpu']: elif args['cpu']:
check_cpu() resultat = check_cpu()
return resultat
elif args['disk']: elif args['disk']:
check_disk() resultat = check_disk()
return resultat
elif args['ifconfig']: elif args['ifconfig']:
ifconfig() resultat = ifconfig()
return resultat
elif args['uptime']: elif args['uptime']:
uptime() resultat = uptime()
return { 'Uptime' : uptimeres } return resultat
def monitor_process(args): def monitor_process(args):
if args['enable']: if args['enable']:
process_enable() process_enable(args['enable'])
elif args['disable']: elif args['disable']:
process_disable() process_disable(args['disable'])
elif args['start']: elif args['start']:
process_start() resultat = process_start(args['start'])
return resultat
elif args['stop']: elif args['stop']:
process_stop() process_stop(args['stop'])
elif args['check']: elif args['check']:
process_check() process_check(args['check'])
elif args['info']: elif args['info']:
processcount() resultat = processcount()
return { _("Total") : str(processcount['total']), _("Running") : str(processcount['running']), _("Sleeping") : str(processcount['sleeping']) } return resultat