This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository mum. See http://git.chorem.org/mum.git commit 782a408b6b0ad52e965f9b53a792982f45ebf1cf Author: Alexis Guilbaud <guilbaud@codelutin.com> Date: Mon Feb 23 12:38:54 2015 +0100 ModuleNotCompatibleException and CommandNotFoundException created --- app/module_loader.py | 27 +++++++++++++++++++++------ app/modules/CommandNotFoundException.py | 14 ++++++++++++++ app/modules/ModuleNotCompatibleException.py | 14 ++++++++++++++ app/modules/connection_modules/ssh.py | 9 ++++++++- app/modules/monitoring_modules/unix/disk.py | 10 ++++++++-- app/modules/monitoring_modules/unix/memory.py | 11 +++++++++-- 6 files changed, 74 insertions(+), 11 deletions(-) diff --git a/app/module_loader.py b/app/module_loader.py index 8ea04d2..a782900 100644 --- a/app/module_loader.py +++ b/app/module_loader.py @@ -4,6 +4,8 @@ import modules.connection_modules import modules.detection_modules import modules.monitoring_modules import modules.storage_modules +import modules.ModuleNotCompatibleException as moduleNotCompatibleException +import modules.CommandNotFoundException as commandNotFoundException """ Loads dynamically modules from packages connection_modules, detection_modules, monitoring_modules, storage_modules. @@ -42,7 +44,7 @@ def load_conn(conn_name, addr_host, username, key_location): # /home/aguilbau :return: the instance of connection module created """ conn = __import__("modules.connection_modules." + conn_name, fromlist=modules.connection_modules) - conn_instance = getattr(conn, conn_name)(addr_host, username, key_location) + conn_instance = getattr(conn, conn_name)(addr_host, username, key_location, commandNotFoundException) return conn_instance @@ -59,7 +61,10 @@ def run_all_detection_modules(os, conn, db): for mod_name in pack_mod_os.__all__: mod = __import__ ("modules.detection_modules." + os + "." + mod_name, fromlist=modules.detection_modules.unix.__all__) # on charge le module mod_instance = getattr(mod, mod_name)(conn, db) # on appelle le constructeur - mod_instance.run_detection() + try: + mod_instance.run_detection() + except commandNotFoundException as cnfe: + print cnfe.__str__ def run_all_monitoring_modules(os, conn, db): @@ -74,8 +79,13 @@ def run_all_monitoring_modules(os, conn, db): pack_mod_os = __import__("modules.monitoring_modules." + os, fromlist=modules.monitoring_modules.__all__) for mod_name in pack_mod_os.__all__: mod = __import__ ("modules.monitoring_modules." + os + "." + mod_name, fromlist=modules.monitoring_modules.unix.__all__) # on charge le module - mod_instance = getattr(mod, mod_name)(conn, db) # on appelle le constructeur - mod_instance.check() + mod_instance = getattr(mod, mod_name)(conn, db, moduleNotCompatibleException) # on appelle le constructeur + try: + mod_instance.check() + except moduleNotCompatibleException as mnce: + print mnce.__str__ + except commandNotFoundException as cnfe: + print cnfe.__str__ def run_one_monitoring_module(mod_name, os, conn, db): @@ -89,8 +99,13 @@ def run_one_monitoring_module(mod_name, os, conn, db): """ __import__("modules.monitoring_modules." + os) mod = __import__("modules.monitoring_modules." + os + "." + mod_name, fromlist=modules.monitoring_modules.unix.__all__) - mod_instance = getattr(mod, mod_name)(conn, db) # on appelle le constructeur - mod_instance.check() + mod_instance = getattr(mod, mod_name)(conn, db, moduleNotCompatibleException) # on appelle le constructeur + try: + mod_instance.check() + except moduleNotCompatibleException as mnce: + print mnce.__str__ + except commandNotFoundException as cnfe: + print cnfe.__str__ def get_info_mod_monitoring(os): diff --git a/app/modules/CommandNotFoundException.py b/app/modules/CommandNotFoundException.py new file mode 100644 index 0000000..3492ec6 --- /dev/null +++ b/app/modules/CommandNotFoundException.py @@ -0,0 +1,14 @@ +__author__ = 'aguilbaud' + +""" +Raised if a command called with a connection does not exists on the host +""" + + +class CommandNotFoundException(Exception): + def __init__(self, command, addr_host): + self.command = command + self.addr_host = addr_host + + def __str__(self): + return "Command '" + self.command + "' not found on host " + self.addr_host \ No newline at end of file diff --git a/app/modules/ModuleNotCompatibleException.py b/app/modules/ModuleNotCompatibleException.py new file mode 100644 index 0000000..86db67b --- /dev/null +++ b/app/modules/ModuleNotCompatibleException.py @@ -0,0 +1,14 @@ +__author__ = 'aguilbaud' + +""" +Raised if the module cannot have perform his treatment on a host (command not found or result unexpected) +""" + + +class ModuleNotCompatibleException(Exception): + def __init__(self, mod_name, addr_host): + self.mod_name = mod_name + self.addr_host = addr_host + + def __str__(self): + return "Module '" + self.mod_name + "' not compatible on host " + self.addr_host \ No newline at end of file diff --git a/app/modules/connection_modules/ssh.py b/app/modules/connection_modules/ssh.py index 0d799b9..7eaee28 100644 --- a/app/modules/connection_modules/ssh.py +++ b/app/modules/connection_modules/ssh.py @@ -3,12 +3,13 @@ import paramiko class ssh: - def __init__(self, addr_host, usrname, key_location): + def __init__(self, addr_host, usrname, key_location, cnfe): key = paramiko.RSAKey.from_private_key_file(key_location) # "/home/aguilbaud/.ssh/id_rsa" self.ssh = paramiko.SSHClient() self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.ssh.connect(addr_host, username=usrname, pkey=key) self.addr_host = addr_host + self.CommandNotFoundException = cnfe def get_addr_host(self): return self.addr_host @@ -16,6 +17,12 @@ class ssh: def exec_command(self, cmd): stdin, stdout, stderr = self.ssh.exec_command(cmd) res = stdout.read() + err = stderr.read() + if not err == "" and res == "": + exception_inst = getattr(self.CommandNotFoundException, "CommandNotFoundException")( + cmd, self.addr_host + ) + raise exception_inst return res def disconnect(self): diff --git a/app/modules/monitoring_modules/unix/disk.py b/app/modules/monitoring_modules/unix/disk.py index 48586cc..4e7ac7d 100644 --- a/app/modules/monitoring_modules/unix/disk.py +++ b/app/modules/monitoring_modules/unix/disk.py @@ -9,14 +9,15 @@ Check and returns the percentage of disk used ( = percentage of use of the parti class disk: - def __init__(self, conn, db): + def __init__(self, conn, db, mnce): self.conn = conn self.db = db + self.ModuleNotCompatibleException = mnce def check(self): cmd = "df -h" stdout = self.conn.exec_command(cmd) - disk_used = 0 + disk_used = None ignore = True for line in stdout.splitlines(): # we ignore the first line which contains no value @@ -26,5 +27,10 @@ class disk: values = line.split() if values[len(values)-1] == "/": disk_used = re.sub("[^0-9]", "", values[len(values)-2]) + if disk_used is None: + exception_inst = getattr(self.ModuleNotCompatibleException, "ModuleNotCompatibleException")( + "disk", self.conn.get_addr_host() + ) + raise exception_inst res_check = json.dumps({"disk": int(disk_used)}) self.db.add_check(self.conn.get_addr_host(), "disk", res_check) \ No newline at end of file diff --git a/app/modules/monitoring_modules/unix/memory.py b/app/modules/monitoring_modules/unix/memory.py index c58cdcb..adc30d4 100644 --- a/app/modules/monitoring_modules/unix/memory.py +++ b/app/modules/monitoring_modules/unix/memory.py @@ -2,14 +2,17 @@ __author__ = 'aguilbaud' import json import re + """ Check and returns the percentage of total memory used of the machine """ + class memory: - def __init__(self, conn, db): + def __init__(self, conn, db, mnce): self.conn = conn self.db = db + self.ModuleNotCompatibleException = mnce def check(self): cmd = "cat /proc/meminfo" @@ -22,7 +25,11 @@ class memory: memtotal = re.sub("[^0-9]", "", tab_res[1]) elif(tab_res[0]) == 'MemFree': memfree = re.sub("[^0-9]", "", tab_res[1]) - #TODO si memfree ou memtotal = 0, retourner une exception comme quoi le module n'est pas compatible avec l'hote memused = int(memtotal) - int(memfree) + if memused == 0: + exception_inst = getattr(self.ModuleNotCompatibleException, "ModuleNotCompatibleException")( + "memory", self.conn.get_addr_host() + ) + raise exception_inst res_check = json.dumps({"memory": memused * 100 / int(memtotal)}) self.db.add_check(self.conn.get_addr_host(), "memory", res_check) \ No newline at end of file -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.