moulinette/yunohost_monitor.py

148 lines
4.5 KiB
Python
Raw Normal View History

2012-10-14 11:56:57 +02:00
# -*- coding: utf-8 -*-
import os
import sys
import subprocess
2012-10-16 14:56:54 +02:00
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)
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)
2012-10-14 13:42:22 +02:00
from datetime import datetime, timedelta
2012-10-28 15:55:35 +01:00
from yunohost import YunoHostError, win_msg, colorize, validate, get_required_args
2012-10-14 11:56:57 +02:00
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)
return { _("Partition") : (part.mountpoint, bytes2human(usage.used), bytes2human(usage.total), bytes2human(usage.free), int(usage.percent)) }
2012-10-14 11:56:57 +02:00
def check_cpu():
return { _("CPU") : psutil.cpu_percent(interval=3) }
2012-10-14 11:56:57 +02:00
def check_memory():
mem = getattr(psutil.phymem_usage(), "percent")
swap = getattr(psutil.virtmem_usage(), "percent")
return { _("Memory") : mem, _("Swap") : swap }
2012-10-14 11:56:57 +02:00
def ifconfig():
listinterfaces = netifaces.interfaces()[1:]
for interface in listinterfaces:
try:
for link in netifaces.ifaddresses(interface)[netifaces.AF_INET]:
ip = link['addr']
for link in netifaces.ifaddresses(interface)[netifaces.AF_LINK]:
mac = link['addr']
except:
pass
return { _('IP') : ip, _('MAC') : mac }
2012-10-14 11:56:57 +02:00
2012-10-14 13:42:22 +02:00
def uptime():
uptime = (str(datetime.now() - datetime.fromtimestamp(psutil.BOOT_TIME)).split('.')[0])
return { _("Uptime") : uptime }
2012-10-16 17:52:39 +02:00
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
return { _("Total") : str(processcount['total']), _("Running") : str(processcount['running']), _("Sleeping") : str(processcount['sleeping']) }
2012-10-28 15:55:35 +01:00
def process_enable(args):
print 'process_enable'
def process_disable(args):
print 'process disable'
2012-10-28 15:55:35 +01:00
def process_start(args):
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'))
2012-10-28 15:55:35 +01:00
def process_stop(args):
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'))
2012-10-28 15:55:35 +01:00
def process_check(args):
print 'process_check'
2012-10-16 17:52:39 +02:00
2012-10-15 22:48:05 +02:00
def monitor_info(args):
if args['memory']:
resultat = check_memory()
return resultat
2012-10-15 22:48:05 +02:00
elif args['cpu']:
resultat = check_cpu()
return resultat
2012-10-15 22:48:05 +02:00
elif args['disk']:
resultat = check_disk()
return resultat
2012-10-15 22:48:05 +02:00
elif args['ifconfig']:
resultat = ifconfig()
return resultat
2012-10-15 22:48:05 +02:00
elif args['uptime']:
resultat = uptime()
return resultat
2012-10-28 15:55:35 +01:00
def monitor_process(args):
if args['enable']:
process_enable(args['enable'])
2012-10-28 15:55:35 +01:00
elif args['disable']:
process_disable(args['disable'])
2012-10-28 15:55:35 +01:00
elif args['start']:
resultat = process_start(args['start'])
return resultat
2012-10-28 15:55:35 +01:00
elif args['stop']:
process_stop(args['stop'])
2012-10-28 15:55:35 +01:00
elif args['check']:
process_check(args['check'])
2012-10-28 15:55:35 +01:00
elif args['info']:
resultat = processcount()
return resultat