From 85f2706ea675620ad0710a701aa7f494ea108ae3 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 1 Oct 2013 10:02:22 +0200 Subject: [PATCH 01/11] change yunohost check process --- action_map.yml | 2 +- yunohost_monitor.py | 35 +++++++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/action_map.yml b/action_map.yml index 46fdaa0b..cf19ae7b 100644 --- a/action_map.yml +++ b/action_map.yml @@ -436,7 +436,7 @@ monitor: -c: full: --check help: Check process - metavar: PORT + action: store_true -i: full: --info help: Process info diff --git a/yunohost_monitor.py b/yunohost_monitor.py index c46c48d4..ff079fa7 100644 --- a/yunohost_monitor.py +++ b/yunohost_monitor.py @@ -29,6 +29,17 @@ import psutil from urllib import urlopen from datetime import datetime, timedelta from yunohost import YunoHostError, win_msg, colorize, validate, get_required_args +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 +if not __debug__: + import traceback s = xmlrpclib.ServerProxy('http://127.0.0.1:61209') @@ -73,17 +84,21 @@ def process_stop(args): else: raise YunoHostError(1, 'Stop : ' + args.title() + " " + _("failure")) + 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: - return { 'Port' : args + " " + _("is open") } + with open('process.yml', 'r') as f: + process = yaml.load(f) + + for i in process: + cmd = process[i]['command'] + if cmd == 'service': + status = os.system("service " + i + " status") else: - return { 'Warning' : args + " " + _("is closed in your box") } - else: - raise YunoHostError(1, args + " " + _("is closed") ) + status = os.system(cmd) + if status == 0: + return { i + " " + _("is up") } + else: + raise YunoHostError(1, i + " " + _("is down") ) def monitor_info(memory=False, cpu=False, disk=False, ifconfig=False, uptime=False, public=False): @@ -137,7 +152,7 @@ def monitor_info(memory=False, cpu=False, disk=False, ifconfig=False, uptime=Fal else: raise YunoHostError(1, _('No arguments provided')) -def monitor_process(enable=None, disable=None, start=None, stop=None, check=None, info=False): +def monitor_process(enable=None, disable=None, start=None, stop=None, check=False, info=False): """ Check Process From 17ca215e3bd81ba95346478f5f5eaf77338b78b8 Mon Sep 17 00:00:00 2001 From: Adrien Beudin Date: Tue, 1 Oct 2013 11:35:11 +0200 Subject: [PATCH 02/11] add process yml --- process.yml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 process.yml diff --git a/process.yml b/process.yml new file mode 100644 index 00000000..54f15ce8 --- /dev/null +++ b/process.yml @@ -0,0 +1,4 @@ +glances: + command: service +tahoe-lafs: + command: ps aux | grep tahoe |grep -v grep From 21877f6e7b234931a4fb1bf60d0a4149d215428e Mon Sep 17 00:00:00 2001 From: Adrien Beudin Date: Tue, 1 Oct 2013 12:44:39 +0200 Subject: [PATCH 03/11] check process ok --- process.yml | 4 ++-- yunohost_monitor.py | 23 ++++++++++++----------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/process.yml b/process.yml index 54f15ce8..c1150d00 100644 --- a/process.yml +++ b/process.yml @@ -1,4 +1,4 @@ glances: - command: service + status: service tahoe-lafs: - command: ps aux | grep tahoe |grep -v grep + status: ps aux | grep tahoe |grep -v grep diff --git a/yunohost_monitor.py b/yunohost_monitor.py index ff079fa7..c683f8af 100644 --- a/yunohost_monitor.py +++ b/yunohost_monitor.py @@ -84,22 +84,23 @@ def process_stop(args): else: raise YunoHostError(1, 'Stop : ' + args.title() + " " + _("failure")) - def process_check(args): with open('process.yml', 'r') as f: - process = yaml.load(f) + processes = yaml.load(f) - for i in process: - cmd = process[i]['command'] - if cmd == 'service': - status = os.system("service " + i + " status") + result = {} + for process, commands in processes.items(): + if commands['status'] == 'service': + cmd = "service " + process + " status" else: - status = os.system(cmd) - if status == 0: - return { i + " " + _("is up") } - else: - raise YunoHostError(1, i + " " + _("is down") ) + cmd = commands['status'] + if os.system(cmd + " > /dev/null 2>&1") == 0: + result.update({ process : _('Running') }) + else: + result.update({ process : _('Down') }) + + return { 'Status' : result } def monitor_info(memory=False, cpu=False, disk=False, ifconfig=False, uptime=False, public=False): """ From 164e571a978d3cd07e9abb70c3ef7fad861b4df5 Mon Sep 17 00:00:00 2001 From: Adrien Beudin Date: Wed, 2 Oct 2013 08:33:26 +0200 Subject: [PATCH 04/11] update process list --- process.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/process.yml b/process.yml index c1150d00..85882d58 100644 --- a/process.yml +++ b/process.yml @@ -1,4 +1,22 @@ +apache2: + status: service +bind9: + status: service +dovecot: + status: service +postfix: + status: service +mysql: + status: service glances: status: service tahoe-lafs: status: ps aux | grep tahoe |grep -v grep +ssh: + status: service +metronome: + status: metronomectl status +samba: + status: service +slapd: + status: service From f10f6dcbb0a7eeb00443261495f15c0cf361495b Mon Sep 17 00:00:00 2001 From: Adrien Beudin Date: Fri, 4 Oct 2013 12:23:25 +0200 Subject: [PATCH 05/11] ifconfig ok --- yunohost_monitor.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/yunohost_monitor.py b/yunohost_monitor.py index c683f8af..e3ac18e6 100644 --- a/yunohost_monitor.py +++ b/yunohost_monitor.py @@ -38,11 +38,22 @@ except ImportError: sys.stderr.write('apt-get install python-yaml\n') sys.exit(1) import json +import socket +import fcntl +import struct if not __debug__: import traceback s = xmlrpclib.ServerProxy('http://127.0.0.1:61209') +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]) + def bytes2human(n): symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y') prefix = {} @@ -126,8 +137,13 @@ def monitor_info(memory=False, cpu=False, disk=False, ifconfig=False, uptime=Fal result = {} for k, fs in enumerate(json.loads(s.getNetwork())): interface = fs['interface_name'] - del fs['interface_name'] - result[interface] = fs + if interface != "lo": + ip = get_ip_address(str(interface)) + del fs['interface_name'] + result[ip] = fs + else: + del fs['interface_name'] + result[interface] = fs return result elif disk: From 21de15e4a3d2f0cb873d32ba588deff27602fea1 Mon Sep 17 00:00:00 2001 From: abeudin Date: Fri, 4 Oct 2013 12:31:02 +0200 Subject: [PATCH 06/11] Update yunohost_monitor.py --- yunohost_monitor.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/yunohost_monitor.py b/yunohost_monitor.py index e3ac18e6..ab3484f6 100644 --- a/yunohost_monitor.py +++ b/yunohost_monitor.py @@ -136,14 +136,14 @@ def monitor_info(memory=False, cpu=False, disk=False, ifconfig=False, uptime=Fal # TODO: c'est pas ifconfig ça ;) result = {} for k, fs in enumerate(json.loads(s.getNetwork())): - interface = fs['interface_name'] + 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 + ip = get_ip_address(str(interface)) + del fs['interface_name'] + result[ip] = fs + else: + del fs['interface_name'] + result[interface] = fs return result elif disk: From 09a267a167d5bdb19a8e1177bed0edd123b35ba7 Mon Sep 17 00:00:00 2001 From: abeudin Date: Fri, 4 Oct 2013 12:32:58 +0200 Subject: [PATCH 07/11] Update yunohost_monitor.py --- yunohost_monitor.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/yunohost_monitor.py b/yunohost_monitor.py index ab3484f6..05add6d3 100644 --- a/yunohost_monitor.py +++ b/yunohost_monitor.py @@ -133,18 +133,17 @@ def monitor_info(memory=False, cpu=False, disk=False, ifconfig=False, uptime=Fal return json.loads(s.getLoad()) elif ifconfig: - # TODO: c'est pas ifconfig ça ;) - 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 - return result + 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 + return result elif disk: result = {} From 8fbfd12432d569587c9248f2236c0b7bdde5091a Mon Sep 17 00:00:00 2001 From: Adrien Beudin Date: Fri, 4 Oct 2013 12:48:08 +0200 Subject: [PATCH 08/11] fix indent --- yunohost_monitor.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/yunohost_monitor.py b/yunohost_monitor.py index 05add6d3..6b18294f 100644 --- a/yunohost_monitor.py +++ b/yunohost_monitor.py @@ -133,17 +133,17 @@ def monitor_info(memory=False, cpu=False, disk=False, ifconfig=False, uptime=Fal return json.loads(s.getLoad()) elif ifconfig: - 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 - return result + 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 + return result elif disk: result = {} From 720f0a5a0dae3f265cd82e6af8526eb64fc291fb Mon Sep 17 00:00:00 2001 From: Adrien Beudin Date: Fri, 4 Oct 2013 12:50:04 +0200 Subject: [PATCH 09/11] fix indent2 --- yunohost_monitor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/yunohost_monitor.py b/yunohost_monitor.py index 6b18294f..2087d81a 100644 --- a/yunohost_monitor.py +++ b/yunohost_monitor.py @@ -42,7 +42,7 @@ import socket import fcntl import struct if not __debug__: - import traceback + import traceback s = xmlrpclib.ServerProxy('http://127.0.0.1:61209') @@ -143,7 +143,7 @@ def monitor_info(memory=False, cpu=False, disk=False, ifconfig=False, uptime=Fal else: del fs['interface_name'] result[interface] = fs - return result + return result elif disk: result = {} From f357060ed91c9fcdca5c6496afa447b9bcd4fa99 Mon Sep 17 00:00:00 2001 From: Adrien Beudin Date: Fri, 4 Oct 2013 21:51:04 +0200 Subject: [PATCH 10/11] add swap check --- action_map.yml | 4 ++++ yunohost_monitor.py | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/action_map.yml b/action_map.yml index cf19ae7b..457ed8f6 100644 --- a/action_map.yml +++ b/action_map.yml @@ -396,6 +396,10 @@ monitor: full: --memory help: Check Memory action: store_true + -s: + full: --swap + help: Check Swap + action: store_true -c: full: --cpu help: Check CPU diff --git a/yunohost_monitor.py b/yunohost_monitor.py index 2087d81a..8d15128f 100644 --- a/yunohost_monitor.py +++ b/yunohost_monitor.py @@ -113,7 +113,7 @@ def process_check(args): return { 'Status' : result } -def monitor_info(memory=False, cpu=False, disk=False, ifconfig=False, uptime=False, public=False): +def monitor_info(memory=False, swap=False, cpu=False, disk=False, ifconfig=False, uptime=False, public=False): """ Check System @@ -123,12 +123,16 @@ def monitor_info(memory=False, cpu=False, disk=False, ifconfig=False, uptime=Fal public -- Show IP public cpu -- Check CPU memory -- Check Memory + swap -- Check Swap ifconfig -- Show Ip and MAC Adress """ if memory: return json.loads(s.getMem()) + if swap: + return json.loads(s.getMemSwap()) + elif cpu: return json.loads(s.getLoad()) From eaedc52f6a3570c08d1c4bdedb0f7a7785b23491 Mon Sep 17 00:00:00 2001 From: Beudbeud Date: Sat, 5 Oct 2013 12:29:48 +0200 Subject: [PATCH 11/11] fix indent --- yunohost_monitor.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yunohost_monitor.py b/yunohost_monitor.py index 8d15128f..0358e195 100644 --- a/yunohost_monitor.py +++ b/yunohost_monitor.py @@ -98,7 +98,7 @@ def process_stop(args): def process_check(args): with open('process.yml', 'r') as f: processes = yaml.load(f) - + result = {} for process, commands in processes.items(): if commands['status'] == 'service': @@ -123,7 +123,7 @@ def monitor_info(memory=False, swap=False, cpu=False, disk=False, ifconfig=False public -- Show IP public cpu -- Check CPU memory -- Check Memory - swap -- Check Swap + swap -- Check Swap ifconfig -- Show Ip and MAC Adress """ @@ -196,4 +196,4 @@ def monitor_process(enable=None, disable=None, start=None, stop=None, check=Fals elif check: return process_check(check) elif info: - return json.loads(s.getProcessCount()) + return json.loads(s.getProcessCount())