Merge pull request #6 from YunoHost/dev_beudbeud

update memory check and disk check
This commit is contained in:
abeudin 2012-11-01 11:14:13 -07:00
commit 0b41a48596

View file

@ -31,18 +31,25 @@ def bytes2human(n):
return "%sB" % n return "%sB" % n
def check_disk(): def check_disk():
templ = "%s,%s/%s,%s,%s" result = {}
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)
return { _("Partition") : (part.mountpoint, bytes2human(usage.used), bytes2human(usage.total), bytes2human(usage.free), int(usage.percent)) } result[part.mountpoint] = { 'Total' : str(bytes2human(usage.total)), 'Used' : str(bytes2human(usage.used)),
'Free' : str(bytes2human(usage.free)), 'Percent' : str(usage.percent) + '%' }
return { 'Partition' : result }
def check_cpu(): def check_cpu():
return { _("CPU") : psutil.cpu_percent(interval=3) } return { 'CPU' : psutil.cpu_percent(interval=3) }
def check_memory(): def check_memory():
mem = getattr(psutil.phymem_usage(), "percent") mem = getattr(psutil.phymem_usage(), "total")
swap = getattr(psutil.virtmem_usage(), "percent") memused = getattr(psutil.phymem_usage(), "used")
return { _("Memory") : mem, _("Swap") : swap } mempercent = getattr(psutil.phymem_usage(), "percent")
swap = getattr(psutil.virtmem_usage(), "total")
swapused = getattr(psutil.virtmem_usage(), "used")
swappercent = getattr(psutil.virtmem_usage(), "percent")
return { 'Memory' : { 'Total' : str(bytes2human(mem)), 'Used' : str(bytes2human(memused)), 'Percent' : str(mempercent) + '%' },
'Swap' : { 'Total' : str(bytes2human(swap)), 'Used' : str(bytes2human(swapused)), 'Percent' : str(swappercent) + '%' } }
def ifconfig(): def ifconfig():
listinterfaces = netifaces.interfaces()[1:] listinterfaces = netifaces.interfaces()[1:]
@ -54,11 +61,18 @@ def ifconfig():
mac = link['addr'] mac = link['addr']
except: except:
pass pass
return { _('IP') : ip, _('MAC') : mac } return { 'IP' : ip, 'MAC' : mac }
def uptime(): def uptime():
uptime = (str(datetime.now() - datetime.fromtimestamp(psutil.BOOT_TIME)).split('.')[0]) uptime = (str(datetime.now() - datetime.fromtimestamp(psutil.BOOT_TIME)).split('.')[0])
return { _("Uptime") : uptime } return { 'Uptime' : uptime }
def public():
try:
ip = str(urlopen('http://ip.yunohost.org').read())
except:
raise YunoHostError(1, _("No connection") )
return { 'Public IP' : ip }
def public(): def public():
try: try:
@ -95,7 +109,7 @@ 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']) } return { 'Total' : str(processcount['total']), 'Running' : str(processcount['running']), 'Sleeping' : str(processcount['sleeping']) }
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)
@ -103,7 +117,7 @@ def process_enable(args):
resultat = process_start(args) resultat = process_start(args)
return resultat return resultat
else: else:
raise YunoHostError(1, _('Enable ' + agrs + ' failure')) raise YunoHostError(1, 'Enable : ' + args.title() + " " + _("failure"))
def process_disable(args): def process_disable(args):
output = subprocess.Popen(['update-rc.d', args, 'remove'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) output = subprocess.Popen(['update-rc.d', args, 'remove'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
@ -111,21 +125,21 @@ def process_disable(args):
resultat = process_stop(args) resultat = process_stop(args)
return resultat return resultat
else: else:
raise YunoHostError(1, _('Disable ' + agrs + ' failure')) raise YunoHostError(1, 'Disable : ' + args.title() + " " + _("failure"))
def process_start(args): def process_start(args):
output = subprocess.Popen(['service', args, 'start'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) output = subprocess.Popen(['service', args, 'start'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
if output.wait() == 0: if output.wait() == 0:
return { args.title() + " " + _('Start') : "OK" } return { 'Start' : args.title() }
else: else:
raise YunoHostError(1, _('Start ' + args + ' failure')) raise YunoHostError(1, 'Start : ' + args.title() + " " + _("failure"))
def process_stop(args): def process_stop(args):
output = subprocess.Popen(['service', args, 'stop'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) output = subprocess.Popen(['service', args, 'stop'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
if output.wait() == 0: if output.wait() == 0:
return { args.title() + " " + _('Stop') : "OK" } return { 'Stop' : args.title() }
else: else:
raise YunoHostError(1, _('Stop ' + args + ' failure')) raise YunoHostError(1, 'Stop : ' + args.title() + " " + _("failure"))
def process_check(args): def process_check(args):
ip = public()['Public IP'] ip = public()['Public IP']
@ -133,11 +147,11 @@ def process_check(args):
if output == 0: if output == 0:
output = os.system('/usr/lib/nagios/plugins/check_tcp -H ' + ip + ' -p' + args + ' > /dev/null') output = os.system('/usr/lib/nagios/plugins/check_tcp -H ' + ip + ' -p' + args + ' > /dev/null')
if output == 0: if output == 0:
return { _("Port") + " " + args : _("is open") } return { 'Port' : args + " " + _("is open") }
else: else:
raise YunoHostError(1, "Port " + args + " is closed in your box" ) return { 'Warning' : args + " " + _("is closed in your box") }
else: else:
raise YunoHostError(1, args + " is closed" ) raise YunoHostError(1, args + " " + _("is closed") )
def monitor_info(args): def monitor_info(args):