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 9af4a08881702be613fe21aae23e0b22504e5fd5 Author: Alexis Guilbaud <guilbaud@codelutin.com> Date: Wed Apr 1 11:54:05 2015 +0200 surcharge des modules de monitoring --- app/module_loader.py | 65 +++++++++---------- app/modules/monitoring_modules/disk.py | 74 ++++++++-------------- app/modules/monitoring_modules/memory.py | 72 +++++++-------------- app/modules/monitoring_modules/ping.py | 59 ++++++----------- app/modules/monitoring_modules/updated_packages.py | 38 +++-------- 5 files changed, 108 insertions(+), 200 deletions(-) diff --git a/app/module_loader.py b/app/module_loader.py index 33ff089..1ed2863 100644 --- a/app/module_loader.py +++ b/app/module_loader.py @@ -148,20 +148,14 @@ class ModuleLoader: if mod_name not in sys.modules: try: loaded_mod = __import__("modules.monitoring_modules." + mod_name, fromlist=[mod_name]) - class_name = getattr(loaded_mod, "get_class_name")() - if mod_name == 'ping': - mod_inst = getattr(loaded_mod, class_name)(None, None) - else: - mod_inst = getattr(loaded_mod, class_name)(None, None, None) infos_mod = {} infos_mod['imported'] = loaded_mod - infos_mod['class_name'] = getattr(mod_inst, 'get_name')() - infos_mod['compatible_os'] = getattr(mod_inst, 'get_compatible_os')() + infos_mod['compatible_os'] = getattr(loaded_mod, 'compatible_os') for os in infos_mod['compatible_os']: if os not in self.compatible_os_list: self.compatible_os_list.append(os) - infos_mod['block'] = getattr(mod_inst, 'get_block')() - infos_mod['unit'] = getattr(mod_inst, 'get_unit')() + infos_mod['block'] = getattr(loaded_mod, 'block') + infos_mod['unit'] = getattr(loaded_mod, 'unit') infos_mod['external'] = False self.loaded_mod_moni[mod_name] = infos_mod except AttributeError: @@ -172,24 +166,29 @@ class ModuleLoader: sys.path.insert(0, self.conf['external_modules_location']) # Adding the external diretory into pythonpath for importer, mod_name, ispkg in pkgutil.iter_modules([self.conf['external_modules_location']]): if mod_name not in sys.modules: - try: - loaded_mod = __import__(mod_name, fromlist=[mod_name]) - class_name = getattr(loaded_mod, "get_class_name")() - mod_inst = getattr(loaded_mod, class_name)(None, None, None) - infos_mod = {} - infos_mod['imported'] = loaded_mod - infos_mod['class_name'] = getattr(mod_inst, 'get_name')() - infos_mod['compatible_os'] = getattr(mod_inst, 'get_compatible_os')() - for os in infos_mod['compatible_os']: - if os not in self.compatible_os_list: - self.compatible_os_list.append(os) - infos_mod['block'] = getattr(mod_inst, 'get_block')() - infos_mod['unit'] = getattr(mod_inst, 'get_unit')() - infos_mod['external'] = True - self.loaded_mod_moni[mod_name] = infos_mod - except AttributeError: - print "Error : external monitoring module " + mod_name + " could not have been loaded. " - print "Please verify that every necessary methods have been implemented." + loaded_mod = __import__(mod_name, fromlist=[mod_name]) + if mod_name in self.loaded_mod_moni: # if the module overrides an internal one + for attr in dir(loaded_mod): # for each attribute on the external module + if not re.search('^_{2}\S*_{2}$', attr): # we override each declarated attribute + # (we don't override those named __*__) + setattr(self.loaded_mod_moni[mod_name]['imported'], + attr, + getattr(loaded_mod, attr)) + else: # otherwise, we save the external module normally + try: + infos_mod = {} + infos_mod['imported'] = loaded_mod + infos_mod['compatible_os'] = getattr(loaded_mod, 'compatible_os') + for os in infos_mod['compatible_os']: + if os not in self.compatible_os_list: + self.compatible_os_list.append(os) + infos_mod['block'] = getattr(loaded_mod, 'block') + infos_mod['unit'] = getattr(loaded_mod, 'unit') + infos_mod['external'] = True + self.loaded_mod_moni[mod_name] = infos_mod + except AttributeError: + print "Error : external monitoring module " + mod_name + " could not have been loaded. " + print "Please verify that every necessary methods have been implemented." def run_one_monitoring_module(self, mod_name, addr_host, conn, db): """ @@ -202,17 +201,15 @@ class ModuleLoader: if db is None: db = self.get_db() if mod_name == 'ping': - mod_inst = getattr(self.loaded_mod_moni[mod_name]['imported'], - self.loaded_mod_moni[mod_name]['class_name'])(db, addr_host) - mod_inst.check() + dict_notif = getattr(self.loaded_mod_moni[mod_name]['imported'], + 'check')(db, addr_host) + self.run_notification_modules(dict_notif) else: conn = self.create_connection(addr_host) if conn is not None: - mod_inst = getattr(self.loaded_mod_moni[mod_name]['imported'], - self.loaded_mod_moni[mod_name]['class_name'])(conn, db, - modules.ModuleNotCompatibleException) try: - dict_notif = mod_inst.check() + dict_notif = getattr(self.loaded_mod_moni[mod_name]['imported'], + 'check')(conn, db, modules.ModuleNotCompatibleException) self.run_notification_modules(dict_notif) except modules.ModuleNotCompatibleException.ModuleNotCompatibleException as mnce: print mnce.__str__() diff --git a/app/modules/monitoring_modules/disk.py b/app/modules/monitoring_modules/disk.py index 93a1e32..41cfd3d 100644 --- a/app/modules/monitoring_modules/disk.py +++ b/app/modules/monitoring_modules/disk.py @@ -3,52 +3,28 @@ __author__ = 'aguilbaud' import re -def get_class_name(): - return "Disk" - - -class Disk: - """ - Check and returns the percentage of disk used ( = percentage of use of the partition mounted on /) - """ - def __init__(self, conn, db, mnce): - self.conn = conn - self.db = db - self.name = get_class_name() - self.compatible_os = ['linux', 'unix'] - self.block = "hardware" - self.unit = '%' - self.ModuleNotCompatibleException = mnce - - def get_name(self): - return self.name - - def get_compatible_os(self): - return self.compatible_os - - def get_block(self): - return self.block - - def get_unit(self): - return self.unit - - def check(self): - cmd = "df -h" - stdout = self.conn.exec_command(cmd) - disk_used = None - ignore = True - for line in stdout.splitlines(): - # we ignore the first line which contains no value - if ignore: - ignore = False - else: - 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 = int(disk_used) - return self.db.add_check(self.conn.get_addr_host(), 'disk', res_check) \ No newline at end of file +compatible_os = ['linux', 'unix'] +block = "hardware" +unit = "%" + + +def check(conn, db, mnce): + cmd = "df -h" + stdout = conn.exec_command(cmd) + disk_used = None + ignore = True + for line in stdout.splitlines(): + # we ignore the first line which contains no value + if ignore: + ignore = False + else: + 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(mnce, "ModuleNotCompatibleException")( + "disk", conn.get_addr_host() + ) + raise exception_inst + res_check = int(disk_used) + return db.add_check(conn.get_addr_host(), 'disk', res_check) \ No newline at end of file diff --git a/app/modules/monitoring_modules/memory.py b/app/modules/monitoring_modules/memory.py index 87c69f6..7cafb09 100644 --- a/app/modules/monitoring_modules/memory.py +++ b/app/modules/monitoring_modules/memory.py @@ -3,51 +3,27 @@ __author__ = 'aguilbaud' import re -def get_class_name(): - return "Memory" - - -class Memory: - """ - Check and returns the percentage of total memory used of the machine - """ - def __init__(self, conn, db, mnce): - self.conn = conn - self.db = db - self.name = get_class_name() - self.compatible_os = ['linux', 'unix'] - self.block = "hardware" - self.unit = '%' - self.ModuleNotCompatibleException = mnce - - def get_name(self): - return self.name - - def get_compatible_os(self): - return self.compatible_os - - def get_block(self): - return self.block - - def get_unit(self): - return self.unit - - def check(self): - cmd = "cat /proc/meminfo" - stdout = self.conn.exec_command(cmd) - memfree = 0 - memtotal = 0 - for line in stdout.splitlines(): - tab_res = line.split(':') - if(tab_res[0]) == 'MemTotal': - memtotal = re.sub("[^0-9]", "", tab_res[1]) - elif(tab_res[0]) == 'MemFree': - memfree = re.sub("[^0-9]", "", tab_res[1]) - 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 = memused * 100 / int(memtotal) - return self.db.add_check(self.conn.get_addr_host(), "memory", res_check) \ No newline at end of file +compatible_os = ['linux', 'unix'] +block = "hardware" +unit = "%" + + +def check(conn, db, mnce): + cmd = "cat /proc/meminfo" + stdout = conn.exec_command(cmd) + memfree = 0 + memtotal = 0 + for line in stdout.splitlines(): + tab_res = line.split(':') + if(tab_res[0]) == 'MemTotal': + memtotal = re.sub("[^0-9]", "", tab_res[1]) + elif(tab_res[0]) == 'MemFree': + memfree = re.sub("[^0-9]", "", tab_res[1]) + memused = int(memtotal) - int(memfree) + if memused == 0: + exception_inst = getattr(mnce, "ModuleNotCompatibleException")( + "memory", conn.get_addr_host() + ) + raise exception_inst + res_check = memused * 100 / int(memtotal) + return db.add_check(conn.get_addr_host(), "memory", res_check) \ No newline at end of file diff --git a/app/modules/monitoring_modules/ping.py b/app/modules/monitoring_modules/ping.py index b9ad35a..6fbea01 100644 --- a/app/modules/monitoring_modules/ping.py +++ b/app/modules/monitoring_modules/ping.py @@ -3,44 +3,23 @@ __author__ = 'aguilbaud' import pexpect -def get_class_name(): - return "Ping" - - -class Ping: - - def __init__(self, db, addr_host): - self.db = db - self.name = get_class_name() - self.compatible_os = [] - self.block = "" - self.unit = 'bool' - self.addr_host = addr_host - - def get_name(self): - return self.name - - def get_compatible_os(self): - return self.compatible_os - - def get_block(self): - return self.block - - def get_unit(self): - return self.unit - - def check(self): +compatible_os = [] +block = "" +unit = "bool" + + +def check(db, addr_host): + res_check = False + try: + child = pexpect.spawn('ping '+ addr_host + ' -c 1') + while child.isalive(): + child.expect('packets transmitted, ') + child.expect(' received') + res = child.before + res_check = res == "1" + except pexpect.EOF: + res_check = False + except pexpect.TIMEOUT: res_check = False - try: - child = pexpect.spawn('ping '+ self.addr_host + ' -c 1') - while child.isalive(): - child.expect('packets transmitted, ') - child.expect(' received') - res = child.before - res_check = res == "1" - except pexpect.EOF: - res_check = False - except pexpect.TIMEOUT: - res_check = False - finally: - return self.db.add_check(self.addr_host, "ping", res_check) \ No newline at end of file + finally: + return db.add_check(addr_host, "ping", res_check) \ No newline at end of file diff --git a/app/modules/monitoring_modules/updated_packages.py b/app/modules/monitoring_modules/updated_packages.py index d511039..817d275 100644 --- a/app/modules/monitoring_modules/updated_packages.py +++ b/app/modules/monitoring_modules/updated_packages.py @@ -1,34 +1,14 @@ __author__ = 'aguilbaud' -def get_class_name(): - return "UpdatedPackages" +compatible_os = ['linux', 'unix'] +block = "software" +unit = "bool" -class UpdatedPackages: - def __init__(self, conn, db, mnce): - self.conn = conn - self.db = db - self.name = get_class_name() - self.compatible_os = ['linux', 'unix'] - self.block = "software" - self.unit = "bool" - - def get_name(self): - return self.name - - def get_compatible_os(self): - return self.compatible_os - - def get_block(self): - return self.block - - def get_unit(self): - return self.unit - - def check(self): - cmd = "apt-get upgrade -s" - stdout = self.conn.exec_command(cmd) - tab_res = stdout.split(':') - res_check = len(tab_res) <= 2 - return self.db.add_check(self.conn.get_addr_host(), "updated_packages", res_check) \ No newline at end of file +def check(conn, db, mnce): + cmd = "apt-get upgrade -s" + stdout = conn.exec_command(cmd) + tab_res = stdout.split(':') + res_check = len(tab_res) <= 2 + return db.add_check(conn.get_addr_host(), "updated_packages", 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>.