From 02189f20e5c66f96f1aae499ab2745a6c36e8cca Mon Sep 17 00:00:00 2001 From: Albert Cervera i Areny Date: Mon, 2 Feb 2015 01:46:17 +0100 Subject: [PATCH] Add Tryton and NMAP checks. --- monitoring.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++++- monitoring.xml | 20 ++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/monitoring.py b/monitoring.py index 6adfa9f..68a55ca 100644 --- a/monitoring.py +++ b/monitoring.py @@ -1,8 +1,11 @@ # The COPYRIGHT file at the top level of this repository contains the full # copyright notices and license terms. -from trytond.pool import PoolMeta import subprocess import json +import nmap +from jsonrpclib import Server as ServerProxy +from trytond.pool import PoolMeta + import pingparser __all__ = ['CheckPlan'] @@ -46,3 +49,73 @@ class CheckPlan: 'float_value': result['avgping'] }) return res + + def check_tryton(self): + urls = self.get_attribute('tryton_urls') + urls = urls.split() + res = [] + for url in urls: + server = ServerProxy(url, verbose=0) + try: + databases = server.common.db.list(None, None) + except Exception, e: + res.append({ + 'result': 'tryton_status', + 'char_value': 'Error', + 'label': url, + 'payload': json.dumps({ + 'exception': str(e), + }) + }) + continue + res.append({ + 'result': 'tryton_status', + 'char_value': 'OK', + 'label': url, + 'payload': json.dumps({ + 'list': databases, + }) + }) + return res + + def check_open_ports(self): + ''' + Expected structure in ports attribute: + + protocol:port + + Example: + + TCP:22 + TCP:8000 + ''' + ip = self.get_attribute('ip') + valid_entries = set() + entries = [x.strip() for x in self.get_attribute('open_ports').split()] + for entry in entries: + if len(entry.split(':')) != 2: + continue + protocol, port = entry.split(':') + valid_entries.add((protocol.upper(), int(port))) + + scanner = nmap.PortScanner() + #scanner.scan(ip, '1-65535') + scanner.scan(ip, '25-25') + # Get ip from scanner as user may have given a URL + # but scanner results are indexed with ip + ip = scanner.all_hosts()[0] + entries = set() + for protocol in scanner[ip].all_protocols(): + for port in scanner[ip][protocol].keys(): + if protocol.upper() in ('TCP', 'UDP'): + entries.add((protocol.upper(), port)) + value = 'OK' + if entries - valid_entries: + value = 'Error' + return [{ + 'result': 'open_ports_status', + 'char_value': value, + 'payload': json.dumps({ + 'invalid_ports': list(entries - valid_entries), + }) + }] diff --git a/monitoring.xml b/monitoring.xml index 9558c59..1dab634 100644 --- a/monitoring.xml +++ b/monitoring.xml @@ -23,5 +23,25 @@ float + + + Tryton + check_tryton + + + Tryton Status + tryton_status + char + + + + NMAP Open Ports + check_open_ports + + + NMAP Open Ports Status + open_ports_status + char +