moulinette/yunohost_monitor.py

63 lines
1.7 KiB
Python
Raw Normal View History

2012-10-14 11:56:57 +02:00
# -*- coding: utf-8 -*-
import os
import sys
import subprocess
import psutil
2012-10-14 13:42:22 +02:00
from datetime import datetime, timedelta
2012-10-15 22:48:05 +02:00
#from psutil._compat import print_
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)
2012-10-15 22:48:05 +02:00
print(templ % (part.mountpoint,
2012-10-14 11:56:57 +02:00
bytes2human(usage.used),
bytes2human(usage.total),
bytes2human(usage.free),
int(usage.percent)))
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:
2012-10-14 13:42:22 +02:00
print 'MAC NOT FOUND!'
2012-10-14 11:56:57 +02:00
2012-10-14 13:42:22 +02:00
def uptime():
uptime = datetime.now() - datetime.fromtimestamp(psutil.BOOT_TIME)
print "Uptime: %s" % (str(uptime).split('.')[0])
2012-10-14 11:56:57 +02:00
2012-10-15 22:48:05 +02:00
def monitor_info(args):
if args['memory']:
2012-10-14 11:56:57 +02:00
check_memory()
2012-10-15 22:48:05 +02:00
elif args['cpu']:
check_cpu()
elif args['disk']:
2012-10-14 11:56:57 +02:00
check_disk()
2012-10-15 22:48:05 +02:00
elif args['ifconfig']:
2012-10-14 11:56:57 +02:00
ifconfig()
2012-10-15 22:48:05 +02:00
elif args['uptime']:
2012-10-14 13:42:22 +02:00
uptime()