2012-10-14 11:56:57 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2013-02-20 21:32:08 +01:00
|
|
|
import xmlrpclib
|
|
|
|
import json
|
|
|
|
import psutil
|
2012-11-01 10:42:17 +01:00
|
|
|
from urllib import urlopen
|
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
|
|
|
|
2013-02-20 21:32:08 +01:00
|
|
|
s = xmlrpclib.ServerProxy('http://127.0.0.1:61209')
|
|
|
|
|
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
|
|
|
|
|
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:
|
2013-02-20 21:32:08 +01:00
|
|
|
return process_start(args)
|
2012-11-01 10:42:17 +01:00
|
|
|
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:
|
2013-02-20 21:32:08 +01:00
|
|
|
return process_stop(args)
|
2012-11-01 10:42:17 +01:00
|
|
|
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-11-29 15:05:57 +01:00
|
|
|
def monitor_info(memory=False, cpu=False, disk=False, ifconfig=False, uptime=False, public=False):
|
2013-02-20 21:32:08 +01:00
|
|
|
|
2012-11-29 15:05:57 +01:00
|
|
|
if memory:
|
2013-02-20 21:32:08 +01:00
|
|
|
return json.loads(s.getMem())
|
|
|
|
|
2012-11-29 15:05:57 +01:00
|
|
|
elif cpu:
|
2013-02-20 21:32:08 +01:00
|
|
|
return json.loads(s.getLoad())
|
|
|
|
|
2012-11-29 15:05:57 +01:00
|
|
|
elif ifconfig:
|
2013-02-20 21:32:08 +01:00
|
|
|
# TODO: c'est pas ifconfig ça ;)
|
|
|
|
result = {}
|
|
|
|
for k, fs in enumerate(json.loads(s.getNetwork())):
|
|
|
|
interface = fs['interface_name']
|
|
|
|
del fs['interface_name']
|
|
|
|
result[interface] = fs
|
|
|
|
return result
|
|
|
|
|
|
|
|
elif disk:
|
|
|
|
result = {}
|
|
|
|
for k, fs in enumerate(json.loads(s.getFs())):
|
|
|
|
if fs['fs_type'] != 'tmpfs' and fs['fs_type'] != 'rpc_pipefs':
|
|
|
|
mnt_point = str(fs['mnt_point'])
|
|
|
|
del fs['mnt_point']
|
|
|
|
result[mnt_point] = fs
|
|
|
|
return result
|
|
|
|
|
2012-11-29 15:05:57 +01:00
|
|
|
elif uptime:
|
2013-02-20 21:32:08 +01:00
|
|
|
uptime_value = (str(datetime.now() - datetime.fromtimestamp(psutil.BOOT_TIME)).split('.')[0])
|
|
|
|
return { 'Uptime' : uptime_value }
|
|
|
|
|
2012-11-29 15:05:57 +01:00
|
|
|
elif public:
|
2013-02-20 21:32:08 +01:00
|
|
|
try:
|
|
|
|
ip = str(urlopen('http://ip.yunohost.org').read())
|
|
|
|
except:
|
|
|
|
raise YunoHostError(1, _("No connection") )
|
|
|
|
return { 'Public IP' : ip }
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise YunoHostError(1, _('No arguments provided'))
|
2012-10-28 15:55:35 +01:00
|
|
|
|
2012-11-29 15:05:57 +01:00
|
|
|
def monitor_process(enable=None, disable=None, start=None, stop=None, check=None, info=False):
|
|
|
|
if enable:
|
2013-02-20 21:32:08 +01:00
|
|
|
return process_enable(enable)
|
2012-11-29 15:05:57 +01:00
|
|
|
elif disable:
|
2013-02-20 21:32:08 +01:00
|
|
|
return process_disable(disable)
|
2012-11-29 15:05:57 +01:00
|
|
|
elif start:
|
2013-02-20 21:32:08 +01:00
|
|
|
return process_start(start)
|
2012-11-29 15:05:57 +01:00
|
|
|
elif stop:
|
2013-02-20 21:32:08 +01:00
|
|
|
return process_stop(stop)
|
2012-11-29 15:05:57 +01:00
|
|
|
elif check:
|
2013-02-20 21:32:08 +01:00
|
|
|
return process_check(check)
|
2012-11-29 15:05:57 +01:00
|
|
|
elif info:
|
2013-02-20 21:32:08 +01:00
|
|
|
return json.loads(s.getProcessCount())
|