moulinette/yunohost_monitor.py

119 lines
3.9 KiB
Python
Raw Normal View History

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
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):
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)
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):
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)
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):
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() }
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):
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() }
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):
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") }
else:
2012-11-01 19:05:14 +01:00
return { 'Warning' : args + " " + _("is closed in your box") }
else:
2012-11-01 19:05:14 +01:00
raise YunoHostError(1, args + " " + _("is closed") )
2012-10-16 17:52:39 +02:00
def monitor_info(memory=False, cpu=False, disk=False, ifconfig=False, uptime=False, public=False):
2013-02-20 21:32:08 +01:00
if memory:
2013-02-20 21:32:08 +01:00
return json.loads(s.getMem())
elif cpu:
2013-02-20 21:32:08 +01:00
return json.loads(s.getLoad())
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
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 }
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
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)
elif disable:
2013-02-20 21:32:08 +01:00
return process_disable(disable)
elif start:
2013-02-20 21:32:08 +01:00
return process_start(start)
elif stop:
2013-02-20 21:32:08 +01:00
return process_stop(stop)
elif check:
2013-02-20 21:32:08 +01:00
return process_check(check)
elif info:
2013-02-20 21:32:08 +01:00
return json.loads(s.getProcessCount())