2012-10-14 11:56:57 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import subprocess
|
2012-11-01 10:42:17 +01:00
|
|
|
from urllib import urlopen
|
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)
|
2012-10-30 21:15:22 +01:00
|
|
|
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():
|
2012-11-01 19:05:14 +01:00
|
|
|
result = {}
|
2012-10-14 11:56:57 +02:00
|
|
|
for part in psutil.disk_partitions(all=False):
|
|
|
|
usage = psutil.disk_usage(part.mountpoint)
|
2012-11-01 19:05:14 +01:00
|
|
|
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 }
|
2012-10-14 11:56:57 +02:00
|
|
|
|
|
|
|
def check_cpu():
|
2012-11-01 19:05:14 +01:00
|
|
|
return { 'CPU' : psutil.cpu_percent(interval=3) }
|
2012-10-14 11:56:57 +02:00
|
|
|
|
|
|
|
def check_memory():
|
2012-11-01 19:05:14 +01:00
|
|
|
mem = getattr(psutil.phymem_usage(), "total")
|
|
|
|
memused = getattr(psutil.phymem_usage(), "used")
|
|
|
|
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) + '%' } }
|
2012-10-14 11:56:57 +02:00
|
|
|
|
|
|
|
def ifconfig():
|
2012-10-30 21:15:22 +01:00
|
|
|
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
|
2012-11-01 19:05:14 +01:00
|
|
|
return { 'IP' : ip, 'MAC' : mac }
|
2012-10-14 11:56:57 +02:00
|
|
|
|
2012-10-14 13:42:22 +02:00
|
|
|
def uptime():
|
2012-10-30 21:15:22 +01:00
|
|
|
uptime = (str(datetime.now() - datetime.fromtimestamp(psutil.BOOT_TIME)).split('.')[0])
|
2012-11-01 19:05:14 +01:00
|
|
|
return { 'Uptime' : uptime }
|
2012-10-30 21:15:22 +01:00
|
|
|
|
2012-11-01 10:42:17 +01:00
|
|
|
def public():
|
|
|
|
try:
|
|
|
|
ip = str(urlopen('http://ip.yunohost.org').read())
|
|
|
|
except:
|
2012-11-01 19:05:14 +01:00
|
|
|
raise YunoHostError(1, _("No connection") )
|
|
|
|
return { 'Public IP' : ip }
|
2012-11-01 10:42:17 +01:00
|
|
|
|
2012-11-01 10:39:09 +01:00
|
|
|
def public():
|
|
|
|
try:
|
|
|
|
ip = str(urlopen('http://ip.yunohost.org').read())
|
|
|
|
except:
|
|
|
|
raise YunoHostError(1, "No connection" )
|
|
|
|
return { _("Public IP"): ip }
|
|
|
|
|
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
|
2012-11-01 19:05:14 +01:00
|
|
|
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):
|
2012-11-01 10:42:17 +01:00
|
|
|
output = subprocess.Popen(['update-rc.d', args, 'defaults'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
|
|
|
if output.wait() == 0:
|
|
|
|
resultat = process_start(args)
|
|
|
|
return resultat
|
|
|
|
else:
|
2012-11-01 19:05:14 +01:00
|
|
|
raise YunoHostError(1, 'Enable : ' + args.title() + " " + _("failure"))
|
2012-10-28 15:55:35 +01:00
|
|
|
|
|
|
|
def process_disable(args):
|
2012-11-01 10:42:17 +01:00
|
|
|
output = subprocess.Popen(['update-rc.d', args, 'remove'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
|
|
|
if output.wait() == 0:
|
|
|
|
resultat = process_stop(args)
|
|
|
|
return resultat
|
|
|
|
else:
|
2012-11-01 19:05:14 +01:00
|
|
|
raise YunoHostError(1, 'Disable : ' + args.title() + " " + _("failure"))
|
2012-10-28 15:55:35 +01:00
|
|
|
|
|
|
|
def process_start(args):
|
2012-10-30 21:15:22 +01:00
|
|
|
output = subprocess.Popen(['service', args, 'start'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
|
|
|
if output.wait() == 0:
|
2012-11-01 19:05:14 +01:00
|
|
|
return { 'Start' : args.title() }
|
2012-10-30 21:15:22 +01:00
|
|
|
else:
|
2012-11-01 19:05:14 +01:00
|
|
|
raise YunoHostError(1, 'Start : ' + args.title() + " " + _("failure"))
|
2012-10-28 15:55:35 +01:00
|
|
|
|
|
|
|
def process_stop(args):
|
2012-10-30 21:15:22 +01:00
|
|
|
output = subprocess.Popen(['service', args, 'stop'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
|
|
|
if output.wait() == 0:
|
2012-11-01 19:05:14 +01:00
|
|
|
return { 'Stop' : args.title() }
|
2012-10-30 21:15:22 +01:00
|
|
|
else:
|
2012-11-01 19:05:14 +01:00
|
|
|
raise YunoHostError(1, 'Stop : ' + args.title() + " " + _("failure"))
|
2012-10-28 15:55:35 +01:00
|
|
|
|
|
|
|
def process_check(args):
|
2012-11-01 10:42:17 +01:00
|
|
|
ip = public()['Public IP']
|
|
|
|
output = os.system('/usr/lib/nagios/plugins/check_tcp -H localhost -p' + args + ' > /dev/null')
|
|
|
|
if output == 0:
|
|
|
|
output = os.system('/usr/lib/nagios/plugins/check_tcp -H ' + ip + ' -p' + args + ' > /dev/null')
|
|
|
|
if output == 0:
|
2012-11-01 19:05:14 +01:00
|
|
|
return { 'Port' : args + " " + _("is open") }
|
2012-11-01 10:42:17 +01:00
|
|
|
else:
|
2012-11-01 19:05:14 +01:00
|
|
|
return { 'Warning' : args + " " + _("is closed in your box") }
|
2012-11-01 10:42:17 +01:00
|
|
|
else:
|
2012-11-01 19:05:14 +01:00
|
|
|
raise YunoHostError(1, args + " " + _("is closed") )
|
2012-10-16 17:52:39 +02:00
|
|
|
|
|
|
|
|
2012-10-15 22:48:05 +02:00
|
|
|
def monitor_info(args):
|
|
|
|
if args['memory']:
|
2012-10-30 21:15:22 +01:00
|
|
|
resultat = check_memory()
|
|
|
|
return resultat
|
2012-10-15 22:48:05 +02:00
|
|
|
elif args['cpu']:
|
2012-10-30 21:15:22 +01:00
|
|
|
resultat = check_cpu()
|
|
|
|
return resultat
|
2012-10-15 22:48:05 +02:00
|
|
|
elif args['disk']:
|
2012-10-30 21:15:22 +01:00
|
|
|
resultat = check_disk()
|
|
|
|
return resultat
|
2012-10-15 22:48:05 +02:00
|
|
|
elif args['ifconfig']:
|
2012-10-30 21:15:22 +01:00
|
|
|
resultat = ifconfig()
|
|
|
|
return resultat
|
2012-10-15 22:48:05 +02:00
|
|
|
elif args['uptime']:
|
2012-10-30 21:15:22 +01:00
|
|
|
resultat = uptime()
|
|
|
|
return resultat
|
2012-11-01 10:42:17 +01:00
|
|
|
elif args['public']:
|
|
|
|
resultat = public()
|
|
|
|
return resultat
|
2012-10-28 15:55:35 +01:00
|
|
|
|
|
|
|
def monitor_process(args):
|
|
|
|
if args['enable']:
|
2012-11-01 10:42:17 +01:00
|
|
|
resultat = process_enable(args['enable'])
|
|
|
|
return resultat
|
2012-10-28 15:55:35 +01:00
|
|
|
elif args['disable']:
|
2012-11-01 10:42:17 +01:00
|
|
|
resultat = process_disable(args['disable'])
|
|
|
|
return resultat
|
2012-10-28 15:55:35 +01:00
|
|
|
elif args['start']:
|
2012-10-30 21:15:22 +01:00
|
|
|
resultat = process_start(args['start'])
|
|
|
|
return resultat
|
2012-10-28 15:55:35 +01:00
|
|
|
elif args['stop']:
|
2012-11-01 10:42:17 +01:00
|
|
|
resultat = process_stop(args['stop'])
|
|
|
|
return resultat
|
2012-10-28 15:55:35 +01:00
|
|
|
elif args['check']:
|
2012-11-01 10:42:17 +01:00
|
|
|
resultat = process_check(args['check'])
|
|
|
|
return resultat
|
2012-10-28 15:55:35 +01:00
|
|
|
elif args['info']:
|
2012-10-30 21:15:22 +01:00
|
|
|
resultat = processcount()
|
|
|
|
return resultat
|