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 7a94ab211e15049613909a7210f64050b4d826d1 Author: Alexis Guilbaud <guilbaud@codelutin.com> Date: Tue Jun 16 11:34:43 2015 +0200 logging support + pretty name for modules --- app/module_loader.py | 75 +++++++-------- app/modules/connection_modules/snmp.py | 6 +- app/modules/connection_modules/snmp_walk.py | 6 +- app/modules/monitoring_modules/cpu_glances.py | 3 +- app/modules/monitoring_modules/cpu_snmp_linux.py | 3 +- app/modules/monitoring_modules/cpu_ssh_linux.py | 3 +- app/modules/monitoring_modules/disk_snmp_linux.py | 3 +- app/modules/monitoring_modules/disk_ssh_linux.py | 3 +- app/modules/monitoring_modules/http.py | 5 +- .../monitoring_modules/load_10_min_snmp_linux.py | 3 +- .../monitoring_modules/load_15_min_snmp_linux.py | 3 +- .../monitoring_modules/load_15_min_ssh_linux.py | 3 +- .../monitoring_modules/load_5_min_snmp_linux.py | 3 +- .../monitoring_modules/memory_snmp_linux.py | 3 +- app/modules/monitoring_modules/memory_ssh_linux.py | 3 +- app/modules/monitoring_modules/ping.py | 3 +- app/modules/monitoring_modules/smtp.py | 5 +- app/modules/monitoring_modules/swap_snmp_linux.py | 3 +- app/modules/monitoring_modules/swap_ssh_linux.py | 3 +- .../updated_packages_ssh_linux.py | 11 ++- app/modules/notification_modules/sms_notif.py | 10 +- app/modules/storage_modules/shelve_db.py | 103 +++++++++++---------- app/mum.py | 22 ++++- app/process_monitoring.py | 3 +- app/websocket_func.py | 4 +- mum.conf | 1 + 26 files changed, 173 insertions(+), 120 deletions(-) diff --git a/app/module_loader.py b/app/module_loader.py index e99df41..a77ee7a 100644 --- a/app/module_loader.py +++ b/app/module_loader.py @@ -17,6 +17,7 @@ import pkgutil import sys import os import traceback +import logging class ModuleLoader: @@ -28,6 +29,7 @@ class ModuleLoader: self.conf = conf self.db = self.load_db() self.db.reset_tasks() + self.logger = logging.getLogger("mum_log") self.loaded_mod_moni = {} # See load_all_monitoring_modules self.loaded_mod_detect = {} # See load_all_detection_modules self.loaded_mod_conn = {} # See load_all_connection_modules @@ -112,7 +114,7 @@ class ModuleLoader: process_monitoring.remove_to_waiting_list(ip, None) return ip_range except modules.HostNotFoundException.HostNotFoundException as hnfe: - print hnfe.__str__() + self.logger.warning(hnfe.__str__()) ws.send(json.dumps({"ERROR": hnfe.__str__()})) return None @@ -187,8 +189,8 @@ class ModuleLoader: self.compatible_os_list.append(os) self.loaded_mod_detect[mod_name] = infos_mod except AttributeError: - print "Error : internal detection module " + mod_name + " could not have been loaded. Traceback:" - print traceback.format_exc() + self.logger.warning("Error : internal detection module " + mod_name + " could not have been loaded.") + self.logger.debug(traceback.format_exc()) def run_all_detection_modules(self, addr_host): """ @@ -210,12 +212,12 @@ class ModuleLoader: ) success_detection = True except modules.ModuleNotCompatibleException.ModuleNotCompatibleException as mnce: - print mnce.__str__() + self.logger.warning(mnce.__str__()) except modules.CommandNotFoundException.CommandNotFoundException as cnfe: - print cnfe.__str__() + self.logger.warning(cnfe.__str__()) except Exception: - print "An unexpected error occured during the full detection of " + addr_host + ". Traceback:" - print traceback.format_exc() + self.logger.error("An unexpected error occured during the full detection of " + addr_host) + self.logger.debug(traceback.format_exc()) return success_detection def load_all_monitoring_modules(self): @@ -223,30 +225,29 @@ class ModuleLoader: Instanciates and stores the informations about each monitoring modules avaliable (internal and loaded externally) on the loaded_mod_moni attribute """ - all_internal_mod = {} # for searching easily the existance of a module without specify the part for importer, mod_name, ispkg in pkgutil.iter_modules(["app/modules/monitoring_modules/"]): if mod_name not in sys.modules: try: loaded_mod = __import__("modules.monitoring_modules." + mod_name, fromlist=[mod_name]) part = getattr(loaded_mod, 'part') + mod_pretty_name = getattr(loaded_mod, 'name') if part not in self.loaded_mod_moni: self.loaded_mod_moni[part] = {} self.loaded_mod_moni[part]['compatible_os'] = [] self.loaded_mod_moni[part]['compatible_conn'] = [] self.loaded_mod_moni[part]['modules'] = {} - if mod_name not in self.loaded_mod_moni[part]['modules']: - self.loaded_mod_moni[part]['modules'][mod_name] = {} - self.loaded_mod_moni[part]['modules'][mod_name]['imported'] = loaded_mod - self.loaded_mod_moni[part]['modules'][mod_name]['external'] = False + if mod_pretty_name not in self.loaded_mod_moni[part]['modules']: + self.loaded_mod_moni[part]['modules'][mod_pretty_name] = {} + self.loaded_mod_moni[part]['modules'][mod_pretty_name]['imported'] = loaded_mod + self.loaded_mod_moni[part]['modules'][mod_pretty_name]['external'] = False self.loaded_mod_moni[part]['compatible_os'] = \ list(set(self.loaded_mod_moni[part]['compatible_os'] + getattr(loaded_mod, 'compatible_os'))) self.loaded_mod_moni[part]['compatible_conn'].append(getattr(loaded_mod, 'connection')) self.loaded_mod_moni[part]['compatible_conn'] = \ list(set(self.loaded_mod_moni[part]['compatible_conn'])) - all_internal_mod[mod_name] = part except AttributeError: - print "Error : internal monitoring module " + mod_name + " could not have been loaded. Traceback:" - print traceback.format_exc() + self.logger.warning("Error : internal monitoring module " + mod_name + " could not have been loaded.") + self.logger.debug(traceback.format_exc()) # Now for external modules: if self.conf['external_modules_location'] is not None: @@ -254,34 +255,34 @@ class ModuleLoader: for importer, mod_name, ispkg in pkgutil.iter_modules([self.conf['external_modules_location']]): if mod_name not in sys.modules: loaded_mod = __import__(mod_name, fromlist=[mod_name]) - if mod_name in all_internal_mod: # if the module overrides an internal one - part = all_internal_mod[mod_name] + part = getattr(loaded_mod, 'part') + mod_pretty_name = getattr(loaded_mod, 'name') + if part in self.loaded_mod_moni and mod_pretty_name in self.loaded_mod_moni[part]['modules']: # 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[part]['modules'][mod_name]['imported'], + setattr(self.loaded_mod_moni[part]['modules'][mod_pretty_name]['imported'], attr, getattr(loaded_mod, attr)) else: # otherwise, load the external module normally try: - part = getattr(loaded_mod, 'part') if part not in self.loaded_mod_moni: self.loaded_mod_moni[part] = {} self.loaded_mod_moni[part]['compatible_os'] = [] self.loaded_mod_moni[part]['compatible_conn'] = [] self.loaded_mod_moni[part]['modules'] = {} - if mod_name not in self.loaded_mod_moni[part]['modules']: - self.loaded_mod_moni[part]['modules'][mod_name] = {} - self.loaded_mod_moni[part]['modules'][mod_name]['imported'] = loaded_mod - self.loaded_mod_moni[part]['modules'][mod_name]['external'] = True + if mod_pretty_name not in self.loaded_mod_moni[part]['modules']: + self.loaded_mod_moni[part]['modules'][mod_pretty_name] = {} + self.loaded_mod_moni[part]['modules'][mod_pretty_name]['imported'] = loaded_mod + self.loaded_mod_moni[part]['modules'][mod_pretty_name]['external'] = True self.loaded_mod_moni[part]['compatible_os'] = \ list(set(self.loaded_mod_moni[part]['compatible_os'] + getattr(loaded_mod, 'compatible_os'))) self.loaded_mod_moni[part]['compatible_conn'].append(getattr(loaded_mod, 'connection')) self.loaded_mod_moni[part]['compatible_conn'] = \ list(set(self.loaded_mod_moni[part]['compatible_conn'])) except AttributeError: - print "Error : external monitoring module " + mod_name + " could not have been loaded. Traceback:" - print traceback.format_exc() + self.logger.warning("Error : external monitoring module " + mod_name + " could not have been loaded.") + self.logger.debug(traceback.format_exc()) def run_one_monitoring_module(self, part_name, addr_host): """ @@ -304,7 +305,7 @@ class ModuleLoader: self.run_notification_modules(dict_notif) except modules.CommandNotFoundException.CommandNotFoundException: msg = "No port " + part_name + " on " + addr_host + ". This part checking have been deactivated." - print msg + self.logger.warning(msg) self.wsc.notify_module_deactivation(msg) process_monitoring.remove_to_waiting_list(addr_host, part_name) dict_deactivation_request = {} @@ -335,15 +336,15 @@ class ModuleLoader: self.run_notification_modules(dict_notif) check_done = True except Exception: - print "The connection could not have been established with " + conf_conn[i]['conn_mod_name']\ - + " on " + addr_host + ". Traceback:" - print traceback.format_exc() - print "Now trying on next avaliable connection..." + self.logger.warning("The connection could not have been established with " + conf_conn[i]['conn_mod_name']\ + + " on " + addr_host) + self.logger.debug(traceback.format_exc()) + self.logger.warning("Now trying on next avaliable connection...") i += 1 if not check_done: msg = "No necessary connection have been properly configured for " + part_name + " on " + addr_host + \ ". Therefore it has been deactivated." - print msg + self.logger.warning(msg) self.wsc.notify_module_deactivation(msg) process_monitoring.remove_to_waiting_list(addr_host, part_name) dict_deactivation_request = {} @@ -414,8 +415,8 @@ class ModuleLoader: infos_mod['known_port'] = getattr(mod_inst, 'get_known_port')() self.loaded_mod_conn[mod_name] = infos_mod except AttributeError: - print "Error : internal connection module " + mod_name + " could not have been loaded. Traceback:" - print traceback.format_exc() + self.logger.warning("Error : internal connection module " + mod_name + " could not have been loaded.") + self.logger.debug(traceback.format_exc()) def get_conection_modules_list(self): """ @@ -470,8 +471,8 @@ class ModuleLoader: infos_mod['class_name'] = getattr(mod_inst, 'get_name')() self.loaded_mod_notif[mod_name] = infos_mod except AttributeError: - print "Error : internal notification module " + mod_name + " could not have been loaded. Traceback:" - print traceback.format_exc() + self.logger.warning("Error : internal notification module " + mod_name + " could not have been loaded.") + self.logger.debug(traceback.format_exc()) def get_info_mod_notification(self): """ @@ -512,8 +513,8 @@ class ModuleLoader: try: mod_inst.notify() except KeyError: - print 'Missing setting for notification module ' + notif_mod - print traceback.format_exc() + self.logger.warning('Missing setting for notification module ' + notif_mod) + self.logger.debug(traceback.format_exc()) def update_global_conf(self): """ diff --git a/app/modules/connection_modules/snmp.py b/app/modules/connection_modules/snmp.py index 79acf7b..1deff5f 100644 --- a/app/modules/connection_modules/snmp.py +++ b/app/modules/connection_modules/snmp.py @@ -1,6 +1,7 @@ __author__ = 'aguilbaud' from pysnmp.entity.rfc3413.oneliner import cmdgen +import logging def get_class_name(): @@ -16,6 +17,7 @@ class SNMP: self.known_port = 161 self.CommandNotFoundException = cnfe self.cmdGen = cmdgen.CommandGenerator() + self.logger = logging.getLogger("mum_log") def get_name(self): return self.name @@ -40,10 +42,10 @@ class SNMP: # Check for errors and print out results if errorIndication: - print(errorIndication) + self.logger.warning(errorIndication) else: if errorStatus: - print('%s at %s' % ( + self.logger.warning('%s at %s' % ( errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex)-1] or '?' )) diff --git a/app/modules/connection_modules/snmp_walk.py b/app/modules/connection_modules/snmp_walk.py index d9d8722..a346de6 100644 --- a/app/modules/connection_modules/snmp_walk.py +++ b/app/modules/connection_modules/snmp_walk.py @@ -1,6 +1,7 @@ __author__ = 'aguilbaud' from pysnmp.entity.rfc3413.oneliner import cmdgen +import logging def get_class_name(): @@ -16,6 +17,7 @@ class SNMPWalk: self.known_port = 161 self.CommandNotFoundException = cnfe self.cmdGen = cmdgen.CommandGenerator() + self.logger = logging.getLogger("mum_log") def get_name(self): return self.name @@ -40,10 +42,10 @@ class SNMPWalk: # Check for errors and print out results if errorIndication: - print(errorIndication) + self.logger.warning(errorIndication) else: if errorStatus: - print('%s at %s' % ( + self.logger.warning('%s at %s' % ( errorStatus.prettyPrint(), errorIndex and varBindTable[-1][int(errorIndex)-1] or '?' )) diff --git a/app/modules/monitoring_modules/cpu_glances.py b/app/modules/monitoring_modules/cpu_glances.py index f38584f..df3de74 100644 --- a/app/modules/monitoring_modules/cpu_glances.py +++ b/app/modules/monitoring_modules/cpu_glances.py @@ -5,7 +5,8 @@ import json compatible_os = ['linux', 'freebsd', 'osx', 'windows'] block = "hardware" -part = "cpu" +part = "CPU" +name = "CPU Glances" unit = "%" connection = "glances" diff --git a/app/modules/monitoring_modules/cpu_snmp_linux.py b/app/modules/monitoring_modules/cpu_snmp_linux.py index 4e7e9f7..24e2ce4 100644 --- a/app/modules/monitoring_modules/cpu_snmp_linux.py +++ b/app/modules/monitoring_modules/cpu_snmp_linux.py @@ -3,7 +3,8 @@ __author__ = 'aguilbaud' compatible_os = ['linux'] block = "hardware" -part = "cpu" +part = "CPU" +name = "CPU SNMP" unit = "%" connection = "snmp" diff --git a/app/modules/monitoring_modules/cpu_ssh_linux.py b/app/modules/monitoring_modules/cpu_ssh_linux.py index e4e7be5..b34a0a3 100644 --- a/app/modules/monitoring_modules/cpu_ssh_linux.py +++ b/app/modules/monitoring_modules/cpu_ssh_linux.py @@ -3,7 +3,8 @@ __author__ = 'aguilbaud' compatible_os = ['linux'] block = "hardware" -part = "cpu" +part = "CPU" +name = "CPU SSH" unit = "%" connection = "ssh" diff --git a/app/modules/monitoring_modules/disk_snmp_linux.py b/app/modules/monitoring_modules/disk_snmp_linux.py index 18ff25c..f474b05 100644 --- a/app/modules/monitoring_modules/disk_snmp_linux.py +++ b/app/modules/monitoring_modules/disk_snmp_linux.py @@ -2,7 +2,8 @@ __author__ = 'aguilbaud' compatible_os = ['linux'] block = "hardware" -part = "disk" +part = "Disk" +name = "Disk SNMP" unit = "%" connection = "snmp_walk" diff --git a/app/modules/monitoring_modules/disk_ssh_linux.py b/app/modules/monitoring_modules/disk_ssh_linux.py index 0511100..41af338 100644 --- a/app/modules/monitoring_modules/disk_ssh_linux.py +++ b/app/modules/monitoring_modules/disk_ssh_linux.py @@ -5,7 +5,8 @@ import re compatible_os = ['linux'] block = "hardware" -part = "disk" +part = "Disk" +name = "Disk SSH" unit = "%" connection = "ssh" diff --git a/app/modules/monitoring_modules/http.py b/app/modules/monitoring_modules/http.py index 659b605..87e09cd 100644 --- a/app/modules/monitoring_modules/http.py +++ b/app/modules/monitoring_modules/http.py @@ -4,7 +4,8 @@ import urllib2 compatible_os = ["all"] block = "network" -part = "http" +part = "HTTP" +name = part unit = "bool" connection = "" @@ -15,7 +16,7 @@ def check(addr_host, port_list, cnfe): for i in range(len(port_list)): # for each http port detected for this host - if port_list[i]['portname'] == part: + if port_list[i]['portname'] == "http": http_port_found = True try: # the result of the check is true if urlopen returns the http code 200 diff --git a/app/modules/monitoring_modules/load_10_min_snmp_linux.py b/app/modules/monitoring_modules/load_10_min_snmp_linux.py index 69308fa..3f55732 100644 --- a/app/modules/monitoring_modules/load_10_min_snmp_linux.py +++ b/app/modules/monitoring_modules/load_10_min_snmp_linux.py @@ -3,7 +3,8 @@ __author__ = 'aguilbaud' compatible_os = ['linux'] block = "hardware" -part = "load_10_min" +part = "Load 10 min" +name = "Load 10 min SNMP" unit = "" connection = "snmp" diff --git a/app/modules/monitoring_modules/load_15_min_snmp_linux.py b/app/modules/monitoring_modules/load_15_min_snmp_linux.py index c24b33b..d19d123 100644 --- a/app/modules/monitoring_modules/load_15_min_snmp_linux.py +++ b/app/modules/monitoring_modules/load_15_min_snmp_linux.py @@ -3,7 +3,8 @@ __author__ = 'aguilbaud' compatible_os = ['linux'] block = "hardware" -part = "load_15_min" +part = "Load 15 min" +name = "Load 15 min SNMP" unit = "" connection = "snmp" diff --git a/app/modules/monitoring_modules/load_15_min_ssh_linux.py b/app/modules/monitoring_modules/load_15_min_ssh_linux.py index aadfc63..a3f3174 100644 --- a/app/modules/monitoring_modules/load_15_min_ssh_linux.py +++ b/app/modules/monitoring_modules/load_15_min_ssh_linux.py @@ -3,7 +3,8 @@ __author__ = 'aguilbaud' compatible_os = ['linux'] block = "hardware" -part = "load_15_min" +part = "Load 15 min" +name = "Load 15 min SSH" unit = "" connection = "ssh" diff --git a/app/modules/monitoring_modules/load_5_min_snmp_linux.py b/app/modules/monitoring_modules/load_5_min_snmp_linux.py index 565b516..5e21882 100644 --- a/app/modules/monitoring_modules/load_5_min_snmp_linux.py +++ b/app/modules/monitoring_modules/load_5_min_snmp_linux.py @@ -3,7 +3,8 @@ __author__ = 'aguilbaud' compatible_os = ['linux'] block = "hardware" -part = "load_5_min" +part = "Load 5 min" +name = "Load 5 min SNMP" unit = "" connection = "snmp" diff --git a/app/modules/monitoring_modules/memory_snmp_linux.py b/app/modules/monitoring_modules/memory_snmp_linux.py index 2ea19ba..323b791 100644 --- a/app/modules/monitoring_modules/memory_snmp_linux.py +++ b/app/modules/monitoring_modules/memory_snmp_linux.py @@ -2,7 +2,8 @@ __author__ = 'aguilbaud' compatible_os = ['linux'] block = "hardware" -part = "memory" +part = "Memory" +name = "Memory SNMP" unit = "%" connection = "snmp" diff --git a/app/modules/monitoring_modules/memory_ssh_linux.py b/app/modules/monitoring_modules/memory_ssh_linux.py index 2f44649..6d466bd 100644 --- a/app/modules/monitoring_modules/memory_ssh_linux.py +++ b/app/modules/monitoring_modules/memory_ssh_linux.py @@ -5,7 +5,8 @@ import re compatible_os = ['linux'] block = "hardware" -part = "memory" +part = "Memory" +name = "Memory SSH" unit = "%" connection = "ssh" diff --git a/app/modules/monitoring_modules/ping.py b/app/modules/monitoring_modules/ping.py index c433507..4a64fd5 100644 --- a/app/modules/monitoring_modules/ping.py +++ b/app/modules/monitoring_modules/ping.py @@ -5,7 +5,8 @@ import pexpect compatible_os = ["all"] block = "network" -part = "ping" +part = "Ping" +name = part unit = "bool" connection = "" diff --git a/app/modules/monitoring_modules/smtp.py b/app/modules/monitoring_modules/smtp.py index fefb8c4..39e9c61 100644 --- a/app/modules/monitoring_modules/smtp.py +++ b/app/modules/monitoring_modules/smtp.py @@ -4,7 +4,8 @@ import smtplib compatible_os = ["all"] block = "network" -part = "smtp" +part = "SMTP" +name = part unit = "bool" connection = "" @@ -15,7 +16,7 @@ def check(addr_host, port_list, cnfe): for i in range(len(port_list)): # for each smtp port detected for this host - if port_list[i]['portname'] == part: + if port_list[i]['portname'] == "smtp": smtp_port_found = True # try to connect to this port, with a timeout of 10 seconds try: diff --git a/app/modules/monitoring_modules/swap_snmp_linux.py b/app/modules/monitoring_modules/swap_snmp_linux.py index 12f61a0..47136e3 100644 --- a/app/modules/monitoring_modules/swap_snmp_linux.py +++ b/app/modules/monitoring_modules/swap_snmp_linux.py @@ -2,7 +2,8 @@ __author__ = 'aguilbaud' compatible_os = ['linux'] block = "hardware" -part = "swap" +part = "Swap" +name = "Swap SNMP" unit = "%" connection = "snmp" diff --git a/app/modules/monitoring_modules/swap_ssh_linux.py b/app/modules/monitoring_modules/swap_ssh_linux.py index 0a6f155..daf1348 100644 --- a/app/modules/monitoring_modules/swap_ssh_linux.py +++ b/app/modules/monitoring_modules/swap_ssh_linux.py @@ -3,7 +3,8 @@ __author__ = 'aguilbaud' compatible_os = ['linux'] block = "hardware" -part = "swap" +part = "Swap" +name = "Swap SSH" unit = "%" connection = "ssh" diff --git a/app/modules/monitoring_modules/updated_packages_ssh_linux.py b/app/modules/monitoring_modules/updated_packages_ssh_linux.py index 13329d5..8436752 100644 --- a/app/modules/monitoring_modules/updated_packages_ssh_linux.py +++ b/app/modules/monitoring_modules/updated_packages_ssh_linux.py @@ -4,7 +4,8 @@ __author__ = 'aguilbaud' compatible_os = ['linux'] block = "software" -part = "updated_packages" +part = "Updated packages with APT" +name = "Updated packages with APT (SSH)" unit = "bool" connection = "ssh" @@ -21,10 +22,10 @@ def check(conn, mnce, subparts): Veuillez aussi noter que le verrouillage est désactivé, et la situation n'est donc pas forcément représentative de la réalité ! -Lecture des listes de paquets... Fait -Construction de l'arbre des dépendances -Lecture des informations d'état... Fait -0 mis à jour, 0 nouvellement installés, 0 à enlever et 0 non mis à jour. + Lecture des listes de paquets... Fait + Construction de l'arbre des dépendances + Lecture des informations d'état... Fait + 0 mis à jour, 0 nouvellement installés, 0 à enlever et 0 non mis à jour. """ tab_res = stdout.split(':') diff --git a/app/modules/notification_modules/sms_notif.py b/app/modules/notification_modules/sms_notif.py index 1ddad7c..b9f33ee 100644 --- a/app/modules/notification_modules/sms_notif.py +++ b/app/modules/notification_modules/sms_notif.py @@ -2,6 +2,7 @@ __author__ = 'aguilbaud' import urllib2 import re +import logging def get_class_name(): return "SMS" @@ -14,6 +15,7 @@ class SMS: self.title = title self.msg = msg self.settings = settings # = conf attribute on ModuleLoader class + self.logger = logging.getLogger("mum_log") def get_name(self): return self.name @@ -27,10 +29,10 @@ class SMS: except IOError, e: if hasattr(e,'code'): if e.code == 400: - print 'One of the url parameters is missing.' + self.logger.warning('sms_notif code 400: One of the url parameters is missing.') if e.code == 402: - print 'Too many SMS have been sent in short time.' + self.logger.warning('sms_notif code 402: Too many SMS have been sent in short time.') if e.code == 403: - print 'The service is not activated or wrong login/key.' + self.logger.warning('sms_notif code 403: The service is not activated or wrong login/key.') if e.code == 500: - print 'Server error. Please try again later.' \ No newline at end of file + self.logger.warning('sms_notif code 500: Server error. Please try again later.') \ No newline at end of file diff --git a/app/modules/storage_modules/shelve_db.py b/app/modules/storage_modules/shelve_db.py index 91cd226..a5a9a0d 100644 --- a/app/modules/storage_modules/shelve_db.py +++ b/app/modules/storage_modules/shelve_db.py @@ -9,6 +9,7 @@ import json import shelve import traceback import threading +import logging import os.path @@ -22,6 +23,7 @@ class shelve_db: self.db = None self.lock = threading.Lock() self.db_loc = db_loc + self.logger = logging.getLogger("mum_log") def open_db(self): """ @@ -40,7 +42,7 @@ class shelve_db: self.db["tasks"] = [] self.db["version"] = "0.1" except: - print "Database initilalization error" + self.logger.error("Database initilalization error") self.db.close() self.lock.release() else: @@ -59,7 +61,7 @@ class shelve_db: try: self.db['tasks'] = [] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -110,6 +112,8 @@ class shelve_db: mod_conf['minor_limit'] = 8 mod_conf['major_limit'] = 10 self.db['global_conf'][mod] = mod_conf + if 'param' in loaded_mod_moni: + mod_conf['param'] = loaded_mod_moni['param'] # removing entries of modules that are no loaded anymore mods_to_del = [] for mod in self.db['global_conf']: @@ -118,7 +122,7 @@ class shelve_db: for mod in mods_to_del: del self.db['global_conf'][mod] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -133,7 +137,7 @@ class shelve_db: try: res = self.db['global_conf'] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() return res @@ -160,7 +164,7 @@ class shelve_db: else: self.db['global_conf'][args['mod_name']]['nb_' + period] = args['nb_' + period] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -176,7 +180,7 @@ class shelve_db: try: res = self.db['hosts'][addr_host]['conf']['monitoring'][modname]['subparts'] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() return res @@ -196,7 +200,7 @@ class shelve_db: self.db['hosts'][args['addr_host']]['conf']['monitoring'][args['modname']]['subparts'] = \ args['subpart_list'] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -258,11 +262,12 @@ class shelve_db: # Create structure for archiving data self.db["hosts"][addr_host]["archive"] = {} self.db["hosts"][addr_host]["stats"] = {} + #self.db["hosts"][addr_host]['param']['port'] = nmap_res_data['openports'] # set the initial number of failures to 0 self.db['hosts'][addr_host]['status']['nb_warning'] = 0 self.db['hosts'][addr_host]['status']['nb_danger'] = 0 except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -314,7 +319,7 @@ class shelve_db: try: res = self.db['hosts'][args['addr_host']]['conf']['connections'] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() return res @@ -351,7 +356,7 @@ class shelve_db: dict_moni['freq'] = self.db['hosts'][addr_host]['conf']['monitoring'][mod]['check_frequency'] res.append(dict_moni) except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() return res @@ -367,7 +372,7 @@ class shelve_db: for addr_host in self.db['hosts']: res.append(addr_host) except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() return res @@ -431,7 +436,7 @@ class shelve_db: info_host['display_name'] = self.db['hosts'][host]['conf']['display_name'] res.append(info_host) except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() return res @@ -496,7 +501,7 @@ class shelve_db: self.db['hosts'][addr_host]['status']['nb_warning'] = 0 self.db['hosts'][addr_host]['status']['nb_danger'] = 0 except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() return res @@ -512,7 +517,7 @@ class shelve_db: try: res = json.loads(self.db['hosts'][addr_host]['detected']['nmap']) except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() return res @@ -537,7 +542,7 @@ class shelve_db: if addr_host in self.db['groups'][group_id]['hosts']: self.db['groups'][group_id]['hosts'].remove(host) except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -552,7 +557,7 @@ class shelve_db: try: self.db["hosts"][addr_host]["detected"][name_part] = json_res_str except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -573,7 +578,7 @@ class shelve_db: nmap_detection[args['attribute']] = args['new_value'] self.db['hosts'][args['addr_host']]['detected']['nmap'] = json.dumps(nmap_detection) except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -591,7 +596,7 @@ class shelve_db: try: self.db['hosts'][args['addr_host']]['conf']['display_name'] = args['display_name'] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -611,7 +616,7 @@ class shelve_db: try: self.db["hosts"][addr_host]["conf"]["custom_info"] = txt except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -636,7 +641,7 @@ class shelve_db: try: self.db["hosts"][addr_host]["conf"]["interventions"].append(intervention) except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -671,7 +676,7 @@ class shelve_db: self.db['hosts'][addr_host]['conf']['idling']['modules'] = [] self.db['hosts'][addr_host]['status']['state'] = self.db['hosts'][addr_host]['conf']['idling']['status'] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() return res_instr @@ -743,7 +748,7 @@ class shelve_db: self.db['hosts'][addr_host]['status']['state'] = state except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() return dict_instr @@ -774,7 +779,7 @@ class shelve_db: for period in ['min', 'hour', 'day', 'week', 'month', 'year']: res['nb_' + period] = self.db['hosts'][addr_host]['conf']['monitoring'][mod_name]['nb_' + period] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() return res @@ -804,7 +809,7 @@ class shelve_db: else: self.db['hosts'][addr_host]['conf']['monitoring'][mod_name]['nb_' + period] = args['nb_' + period] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -827,7 +832,7 @@ class shelve_db: self.db['hosts'][addr_host]['conf']['connections'][mod] = {} self.db['hosts'][addr_host]['conf']['connections'][mod]['priority'] = args['priorities'][mod] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -850,7 +855,7 @@ class shelve_db: self.db['hosts'][addr_host]['conf']['connections'][modname][param] = \ args['current_config'][modname][param] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -878,7 +883,7 @@ class shelve_db: pos += 1 res.insert(pos, dict_conn) except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() return res @@ -948,7 +953,7 @@ class shelve_db: dict_new_val, self.db['hosts'][addr_host]['conf']['monitoring'][mod_name]) except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() return dict_notif @@ -1138,7 +1143,7 @@ class shelve_db: res['stats'] = self.db['hosts'][args['addr_host']]['stats'] res['archive'] = self.db['hosts'][args['addr_host']]['archive'] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() return res @@ -1167,7 +1172,7 @@ class shelve_db: self.db['groups'][group]['hosts'].append(host) self.db['hosts'][host]['conf']['groups'].append(group) except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -1192,7 +1197,7 @@ class shelve_db: if len(self.db['groups'][group]['hosts']) == 0: del self.db['groups'][group] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -1211,7 +1216,7 @@ class shelve_db: self.db['groups'][group]['subscribers'][user]['major'] = {} self.db['groups'][group]['subscribers'][user]['minor'] = {} except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -1239,7 +1244,7 @@ class shelve_db: try: self.db['groups'][group]['subscribers'] = args['subscription'] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -1255,7 +1260,7 @@ class shelve_db: try: del self.db['groups'][group]['subscribers'][username] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -1273,7 +1278,7 @@ class shelve_db: try: res = self.db['groups'] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() return res @@ -1300,7 +1305,7 @@ class shelve_db: try: res = self.db['groups'][group]['subscribers'] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() return res @@ -1322,7 +1327,7 @@ class shelve_db: self.db['users'][username]['account'] = {} self.db['users'][username]['settings'] = {} except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -1338,7 +1343,7 @@ class shelve_db: for user in self.db['users']: res.append(user) except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() return res @@ -1361,7 +1366,7 @@ class shelve_db: try: res['settings'] = self.db['users'][args['username']]['settings'] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() return res @@ -1383,7 +1388,7 @@ class shelve_db: try: self.db['users'][args['username']]['settings'] = args['settings'] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -1421,7 +1426,7 @@ class shelve_db: if username in self.db['groups'][group]['subscribers']: res['groups'][group] = self.db['groups'][group]['subscribers'][username] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() return res @@ -1447,7 +1452,7 @@ class shelve_db: if username in self.db['groups'][group]['subscribers']: del self.db['groups'][group]['subscribers'][username] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -1466,7 +1471,7 @@ class shelve_db: self.db['hosts'][addr_host]['conf']['subscribers'][user]['minor'] = {} self.db['hosts'][addr_host]['conf']['subscribers'][user]['major'] = {} except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -1494,7 +1499,7 @@ class shelve_db: try: self.db['hosts'][addr_host]['conf']['subscribers'] = args['subscription'] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -1510,7 +1515,7 @@ class shelve_db: try: del self.db['hosts'][addr_host]['conf']['subscribers'][username] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -1536,7 +1541,7 @@ class shelve_db: try: res = self.db['hosts'][addr_host]['conf']['subscribers'] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() return res @@ -1550,7 +1555,7 @@ class shelve_db: try: self.db['tasks'].append(task_id) except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -1563,7 +1568,7 @@ class shelve_db: try: self.db['tasks'].remove(task_id) except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() @@ -1576,7 +1581,7 @@ class shelve_db: try: res = self.db['tasks'] except Exception: - print traceback.format_exc() + self.logger.error(traceback.format_exc()) finally: self.close_db() return res \ No newline at end of file diff --git a/app/mum.py b/app/mum.py index ce802af..a426c0c 100755 --- a/app/mum.py +++ b/app/mum.py @@ -11,6 +11,8 @@ from module_loader import ModuleLoader import websocket_func import argparse import os +import logging +from logging.handlers import RotatingFileHandler @route('/') def index(section='home'): @@ -74,6 +76,7 @@ def bower_files(filepath): def receive(ws): global ml ml.get_websocket_container().add_websocket(ws) + logger = logging.getLogger("mum_log") while True: try: response = ws.receive() @@ -83,7 +86,7 @@ def receive(ws): try: getattr(websocket_func, code)(msg, ws, ml) except AttributeError: - print "Unknown websocket code: " + code + logger.error("Unknown websocket code: " + code) except WebSocketError: # Should be WebSocketError when closing the connection ml.get_websocket_container().remove_websocket(ws) break @@ -97,6 +100,21 @@ def do_upload(): except IOError: return 'This file already exists. <a href="/">Back to dashboard.</a>' +def create_logger(dict_conf): + logger = logging.getLogger("mum_log") + logger.setLevel(logging.DEBUG) + formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: %(message)s') + file_handler = RotatingFileHandler(dict_conf['log_location'], 'a', 1000000, 1) + file_handler.setLevel(logging.DEBUG) + file_handler.setFormatter(formatter) + logger.addHandler(file_handler) + + stream_handler = logging.StreamHandler() + stream_handler.setLevel(logging.DEBUG) + logger.addHandler(stream_handler) + + return logger + # Launching the server if __name__ == '__main__': global ml @@ -163,6 +181,8 @@ if __name__ == '__main__': if dict_args[arg] is not None: conf[arg] = dict_args[arg] + logger = create_logger(conf) + ml = ModuleLoader(conf) ml.load_all_monitoring_modules() ml.load_all_connection_modules() diff --git a/app/process_monitoring.py b/app/process_monitoring.py index 9d50eba..48796e4 100644 --- a/app/process_monitoring.py +++ b/app/process_monitoring.py @@ -61,7 +61,8 @@ class ProcessMonitoring(threading.Thread): rm.start() ready_to_launch = False time.sleep(1) - rm.join(5) + if rm is not None: + rm.join(5) def add_to_waiting_list(dict_mod): diff --git a/app/websocket_func.py b/app/websocket_func.py index f8a1d12..83bd05b 100644 --- a/app/websocket_func.py +++ b/app/websocket_func.py @@ -4,6 +4,7 @@ __author__ = 'aguilbaud' import os import json import threading +import logging from geventwebsocket import WebSocketError # ---- Definitions for the nmap detection procedure ---- @@ -44,12 +45,13 @@ def start_first_detection(args, ml, ws): # asked from scan page def NMAP_SCAN_DEMAND(msg, ws, ml): + logger = logging.getLogger("mum_log") if msg["NMAP_SCAN_DEMAND"]["nmap_options"] == '': ml.create_empty_host(msg["NMAP_SCAN_DEMAND"]["ip_range"], ws) ws.send(json.dumps({"SUCCESS_MODULE": "New host successfully created"})) else: if os.getegid() != 0: - print "Error: Cannot run nmap without root privileges." + logger.error("Cannot run nmap without root privileges.") ws.send(json.dumps({"ERROR": "Cannot run nmap without root privileges."})) else: start_first_detection(msg["NMAP_SCAN_DEMAND"], ml, ws) diff --git a/mum.conf b/mum.conf index ca53a58..cdff5fb 100644 --- a/mum.conf +++ b/mum.conf @@ -1,6 +1,7 @@ server_port=1337 server_addr=0.0.0.0 db_location=/var/lib/mum/data/mum.db +log_location=/var/lib/mum/logs external_modules_location=/etc/mum/modules/ keys_location=/var/lib/mum/keys/ smtp_server= -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.