2012-10-14 11:56:57 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2013-07-06 09:42:26 +02:00
|
|
|
""" License
|
|
|
|
|
|
|
|
Copyright (C) 2013 YunoHost
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program; if not, see http://www.gnu.org/licenses
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
""" yunohost_monitor.py
|
2013-07-06 10:17:16 +02:00
|
|
|
|
|
|
|
Monitoring functions
|
2013-07-06 09:42:26 +02:00
|
|
|
"""
|
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
|
2013-10-01 10:02:22 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
try:
|
|
|
|
import yaml
|
|
|
|
except ImportError:
|
|
|
|
sys.stderr.write('Error: Yunohost CLI Require yaml lib\n')
|
|
|
|
sys.stderr.write('apt-get install python-yaml\n')
|
|
|
|
sys.exit(1)
|
|
|
|
import json
|
2013-10-04 12:23:25 +02:00
|
|
|
import socket
|
|
|
|
import fcntl
|
|
|
|
import struct
|
2013-10-01 10:02:22 +02:00
|
|
|
if not __debug__:
|
2013-10-04 12:50:04 +02:00
|
|
|
import traceback
|
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')
|
|
|
|
|
2013-10-04 12:23:25 +02:00
|
|
|
def get_ip_address(ifname):
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
return socket.inet_ntoa(fcntl.ioctl(
|
|
|
|
s.fileno(),
|
|
|
|
0x8915, # SIOCGIFADDR
|
|
|
|
struct.pack('256s', ifname[:15])
|
|
|
|
)[20:24])
|
|
|
|
|
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):
|
2013-10-01 10:02:22 +02:00
|
|
|
with open('process.yml', 'r') as f:
|
2013-10-01 12:44:39 +02:00
|
|
|
processes = yaml.load(f)
|
2013-10-05 12:29:48 +02:00
|
|
|
|
2013-10-01 12:44:39 +02:00
|
|
|
result = {}
|
|
|
|
for process, commands in processes.items():
|
|
|
|
if commands['status'] == 'service':
|
|
|
|
cmd = "service " + process + " status"
|
|
|
|
else:
|
|
|
|
cmd = commands['status']
|
|
|
|
|
|
|
|
if os.system(cmd + " > /dev/null 2>&1") == 0:
|
|
|
|
result.update({ process : _('Running') })
|
2012-11-01 10:42:17 +01:00
|
|
|
else:
|
2013-10-01 12:44:39 +02:00
|
|
|
result.update({ process : _('Down') })
|
2012-10-16 17:52:39 +02:00
|
|
|
|
2013-10-01 12:44:39 +02:00
|
|
|
return { 'Status' : result }
|
2012-10-16 17:52:39 +02:00
|
|
|
|
2013-10-04 21:51:04 +02:00
|
|
|
def monitor_info(memory=False, swap=False, cpu=False, disk=False, ifconfig=False, uptime=False, public=False):
|
2013-07-06 09:53:43 +02:00
|
|
|
"""
|
2013-07-06 10:17:16 +02:00
|
|
|
Check System
|
|
|
|
|
|
|
|
Keyword argument:
|
|
|
|
uptime -- Show Uptime
|
|
|
|
disk -- Check Disk
|
2013-07-06 12:59:06 +02:00
|
|
|
public -- Show IP public
|
|
|
|
cpu -- Check CPU
|
2013-07-06 10:17:16 +02:00
|
|
|
memory -- Check Memory
|
2013-10-05 12:29:48 +02:00
|
|
|
swap -- Check Swap
|
2013-07-06 12:59:06 +02:00
|
|
|
ifconfig -- Show Ip and MAC Adress
|
2013-07-06 10:17:16 +02:00
|
|
|
|
2013-07-06 09:53:43 +02:00
|
|
|
"""
|
2012-11-29 15:05:57 +01:00
|
|
|
if memory:
|
2013-02-20 21:32:08 +01:00
|
|
|
return json.loads(s.getMem())
|
|
|
|
|
2013-10-04 21:51:04 +02:00
|
|
|
if swap:
|
|
|
|
return json.loads(s.getMemSwap())
|
|
|
|
|
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-10-04 12:48:08 +02:00
|
|
|
result = {}
|
|
|
|
for k, fs in enumerate(json.loads(s.getNetwork())):
|
|
|
|
interface = fs['interface_name']
|
|
|
|
if interface != "lo":
|
|
|
|
ip = get_ip_address(str(interface))
|
|
|
|
del fs['interface_name']
|
|
|
|
result[ip] = fs
|
|
|
|
else:
|
|
|
|
del fs['interface_name']
|
|
|
|
result[interface] = fs
|
2013-10-04 12:50:04 +02:00
|
|
|
return result
|
2013-02-20 21:32:08 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2013-10-01 10:02:22 +02:00
|
|
|
def monitor_process(enable=None, disable=None, start=None, stop=None, check=False, info=False):
|
2013-07-06 12:59:06 +02:00
|
|
|
"""
|
|
|
|
Check Process
|
|
|
|
|
|
|
|
Keyword argument:
|
|
|
|
enable -- Enable process
|
|
|
|
disable -- Disable process
|
|
|
|
stop -- Stop process
|
|
|
|
check -- Check process
|
|
|
|
info -- Process info
|
|
|
|
start -- Start process
|
|
|
|
|
|
|
|
"""
|
|
|
|
if enable:
|
|
|
|
return process_enable(enable)
|
|
|
|
elif disable:
|
|
|
|
return process_disable(disable)
|
|
|
|
elif start:
|
|
|
|
return process_start(start)
|
|
|
|
elif stop:
|
|
|
|
return process_stop(stop)
|
|
|
|
elif check:
|
|
|
|
return process_check(check)
|
|
|
|
elif info:
|
2013-10-05 12:29:48 +02:00
|
|
|
return json.loads(s.getProcessCount())
|