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 1db4361179c52bbbdf65244a04683471e66f8a8c Author: Alexis Guilbaud <guilbaud@codelutin.com> Date: Fri Feb 20 09:41:03 2015 +0100 comments added --- app/module_loader.py | 29 +++++++++ app/modules/detection_modules/__init__.py | 1 + .../detection_modules/unix/cpu_detection.py | 18 ++++++ .../detection_modules/unix/network_detection.py | 18 ++++++ app/modules/storage_modules/shelve_db.py | 71 ++++++++++++++++++++-- 5 files changed, 133 insertions(+), 4 deletions(-) diff --git a/app/module_loader.py b/app/module_loader.py index ccde508..9f6ad2a 100644 --- a/app/module_loader.py +++ b/app/module_loader.py @@ -4,8 +4,15 @@ import modules.detection_modules.unix import modules.connection_modules import modules.storage_modules +""" +Loads dynamically modules from packages connection_modules, detection_modules, monitoring_modules, storage_modules. +""" def load_db(): + """ + Creates an instance of the class shelve_db from storage_modules. + :return: an instance of the shelve_db class + """ db_name = "shelve_db" db = __import__("modules.storage_modules." + db_name, fromlist=modules.storage_modules) db_instance = getattr(db, db_name)() @@ -13,18 +20,40 @@ def load_db(): def run_nmap_detection(ip_range, db, ws): + """ + Instanciates the nmap_detection module from detection_modules, and runs the detection. + :param ip_range: addresses to execute the nmap detection + :param db: an instance of a database module + :param ws: a websocket connection + :return: a list containing the IP adresses checked + """ nmap_mod = __import__("modules.detection_modules.nmap_detection", fromlist=modules.detection_modules) nmap_mod_instance = getattr(nmap_mod, "nmap_detection")(db, ws) return nmap_mod_instance.check_ip_range(ip_range) def load_conn(conn_name, addr_host, key_location): # /home/aguilbaud/.ssh/id_rsa + """ + Instanciates and creates a connection with a connection module. + :param conn_name: the name of the detection module + :param addr_host: the IP adress of the host we want to create a connection + :param key_location: the location of the public key + :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, key_location) return conn_instance def run_all_detection_modules(os, conn, db): + """ + Instanciates and runs every detection_modules listed in the __init__.py file of the package corresponding to + the operating system entered in parameters. + :param os: the oprating system of the host + :param conn: an instance of a connection module + :param db: an instance of a storage module + """ + for mod_name in "modules.detection_modules." + 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 diff --git a/app/modules/detection_modules/__init__.py b/app/modules/detection_modules/__init__.py index fcb43f2..9735bce 100644 --- a/app/modules/detection_modules/__init__.py +++ b/app/modules/detection_modules/__init__.py @@ -1 +1,2 @@ __author__ = 'aguilbaud' +__all__ = ['unix'] \ No newline at end of file diff --git a/app/modules/detection_modules/unix/cpu_detection.py b/app/modules/detection_modules/unix/cpu_detection.py new file mode 100644 index 0000000..d578583 --- /dev/null +++ b/app/modules/detection_modules/unix/cpu_detection.py @@ -0,0 +1,18 @@ +__author__ = 'aguilbaud' +import json + + +class cpu_detection: + def __init__(self, conn, db): + self.conn = conn + self.db = db + + def run_detection(self): + dict_total = {} + cmd = "lshw -class cpu -json" + stdout = self.conn.exec_command(cmd) + all_res = json.loads(stdout) + for key in all_res: + if type(all_res[key]) != "dict": + dict_total[key] = all_res[key] + self.db.save_detection(self.conn.get_addr_host(), "cpu_detection", json.dumps(dict_total)) \ No newline at end of file diff --git a/app/modules/detection_modules/unix/network_detection.py b/app/modules/detection_modules/unix/network_detection.py new file mode 100644 index 0000000..9143464 --- /dev/null +++ b/app/modules/detection_modules/unix/network_detection.py @@ -0,0 +1,18 @@ +__author__ = 'aguilbaud' +import json + + +class cpu_detection: + def __init__(self, conn, db): + self.conn = conn + self.db = db + + def run_detection(self): + dict_total = {} + cmd = "lshw -class network -json" + stdout = self.conn.exec_command(cmd) + all_res = json.loads(stdout) + for key in all_res: + if type(all_res[key]) != "dict": + dict_total[key] = all_res[key] + self.db.save_detection(self.conn.get_addr_host(), "network_detection", json.dumps(dict_total)) \ 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 7cc71dd..74282b7 100644 --- a/app/modules/storage_modules/shelve_db.py +++ b/app/modules/storage_modules/shelve_db.py @@ -9,11 +9,18 @@ import os.path class shelve_db: - + """ + Storage module for the persistant objects in Python : Shelve. + Every function in need to access the database have to be moved in this class. + """ def __init__(self): self.db = None def open_db(self): + """ + Open the shelve database from the file mum.db. + If the file donesn't exists, it will be created and the first structure will also be initialized. + """ if not os.path.isfile("mum.db"): # init of the database at the first opening self.db = shelve.open("mum.db", writeback=True) try: @@ -26,8 +33,10 @@ class shelve_db: else: self.db = shelve.open("mum.db", writeback=True) - # Closes the database def close_db(self): + """ + Closes the database + """ self.db.close() self.db = None @@ -36,6 +45,14 @@ class shelve_db: # It also preconfigure with the default configuration, add the host to the group "all" and # creates empty structures for the monitoring and archive data. def add_host(self,addr_host, nmap_res): + """ + Called by the nmap_detection module. + Add and save a new host after its first nmap detection + It also preconfigure with the default configuration, add the host to the group "all" and + creates empty structures for the monitoring and archive data. + :param addr_host: the IP adress of the host to add + :param nmap_res: a string containing the json reslult of the nmap detection of this host + """ self.open_db() addr_host = str(addr_host) # Shelve doesn't support Unicode try: @@ -62,6 +79,34 @@ class shelve_db: # These are used by the front-end # If no hosts have been added, the function will return an empty list def get_hosts(self): + """ + Returns the essential data about all hosts under monitoring + These are used by the front-end + Called by the app.py, after one client demand. + :return: a list containing the essential data in json, about all hosts under monitoring on the form: + [ + { + "addr":"192.168.74.1", + "name":"www.example.com", + "status":val, //"success" ou "warning" ou "danger" ou "" + "group":[ // au moins 1 groupe "all" + { + "name":"all" + }, + { + "name":"mygroup1" + } + ], + "last_check":val //heure UNIX + "subscribers":{ + "uid":val, + "priority":val + } + }, + ... + ] + If no hosts have been added, the function will return an empty dict. + """ self.open_db() res = [] try: @@ -88,14 +133,27 @@ class shelve_db: return json.dumps(res) def save_detection(self, addr_host, name_part, json_res_str): + """ + Called by a detection module in order to save his detection on the database. + :param addr_host: the IP adress of the host detected + :param name_part: the name of the detection_module which have done the detection + :param json_res_str: a string containing the results of the detection in json + """ self.open_db() try: self.db["hosts"][addr_host]["detected"][name_part] = json_res_str finally: self.close_db() - # Add a new check of a host from a specific module def add_check(self, addr_host, name_part, val): + """ + Called by a monitoring module. + Add a new check of a host from a specific module. + Add the previous entry of monitoring to the archive and call update_stats to update the statistics. + :param addr_host: the IP adress of the host checked + :param name_part: the name of the monitoring_module which have done the check + :param val: the value observed + """ self.open_db() new_val = {"date": datetime.now()} try: @@ -114,8 +172,13 @@ class shelve_db: finally: self.close_db() - # Updates calulated statistics once a new value is received def update_stats(self, stats, val): + """ + Updates calulated statistics once a new value is received. + :param stats: a dictionary taken from the database and corresponding to the statistics stored + :param val: the new value + :return: the statistics dictionary updated + """ stats['nb_check'] += 1 stats['total'] += val if stats['min'] > val: -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.