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 6437239edb06617bc685cd5d16d2d05de64ee20c Author: Alexis Guilbaud <guilbaud@codelutin.com> Date: Fri Feb 20 17:47:39 2015 +0100 create_global_conf OK --- app/app.py | 31 +++++++++++++++++++----- app/module_loader.py | 32 ++++++++++++++++++++++--- app/modules/connection_modules/ssh.py | 4 ++-- app/modules/monitoring_modules/unix/__init__.py | 5 +++- app/modules/storage_modules/shelve_db.py | 19 ++++++++++++++- 5 files changed, 78 insertions(+), 13 deletions(-) diff --git a/app/app.py b/app/app.py index a1336f5..522f56d 100755 --- a/app/app.py +++ b/app/app.py @@ -9,6 +9,21 @@ import json import threading import module_loader +NMAP_SCAN_DEMAND = "10" +DETECTION_DEMAND = "11" +MONITORING_DEMAND = "12" +HOST_INFO_DEMAND = "13" +GET_HOSTS_DEMAND = "14" +CONF_CHANGE_DEMAND = "15" + +SUCCESS_MODULE = "20" +INFO_HOST = "21" +GET_HOSTS_RESPONSE = "22" + +CURRENT_STATE_INFO = "30" +BROWSER_NOTIFICATION = "31" + +ERROR = "40" # Pour lancer la detection nmap avec un nouveau thread class ThreadDetect(threading.Thread): @@ -20,7 +35,11 @@ class ThreadDetect(threading.Thread): def run(self): db = module_loader.load_db() scanned_ip = module_loader.run_nmap_detection(self.ip_range, db, self.ws) - self.ws.send(json.dumps({"20" : scanned_ip})) + self.ws.send(json.dumps({SUCCESS_MODULE: scanned_ip})) + # now launching full detection + for ip in json.loads(scanned_ip): + conn = module_loader.load_conn("ssh", ip, "aguilbaud", "/home/aguilbaud/.ssh/id_rsa") + module_loader.run_all_detection_modules(db.get_host_os(ip), conn, db) @route('/') @@ -78,7 +97,7 @@ def start_first_detection(ip_range, ws): t.start() else: # Si non, on envoie un message d'erreur - ws.send(json.dumps({"40": "Ip range incorrectly formatted"})) + ws.send(json.dumps({ERROR: "Ip range incorrectly formatted"})) @error(404) def error404(error): @@ -105,11 +124,11 @@ def receive(ws): if response is not None: msg = json.loads(response) for code in msg: - if code == "10": - start_first_detection(msg["10"], ws) - elif code == "14": + if code == NMAP_SCAN_DEMAND: + start_first_detection(msg[NMAP_SCAN_DEMAND], ws) + elif code == GET_HOSTS_DEMAND: db = module_loader.load_db() - ws.send(json.dumps({"22": db.get_hosts()})) + ws.send(json.dumps({GET_HOSTS_RESPONSE: db.get_hosts()})) del db else: break diff --git a/app/module_loader.py b/app/module_loader.py index cea3d72..d084005 100644 --- a/app/module_loader.py +++ b/app/module_loader.py @@ -33,7 +33,7 @@ def run_nmap_detection(ip_range, 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 +def load_conn(conn_name, addr_host, username, 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 @@ -42,7 +42,7 @@ def load_conn(conn_name, addr_host, key_location): # /home/aguilbaud/.ssh/id_ :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) + conn_instance = getattr(conn, conn_name)(addr_host, username, key_location) return conn_instance @@ -90,4 +90,30 @@ 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() \ No newline at end of file + mod_instance.check() + + +def get_info_mod_monitoring(os): + """ + Get information about the output and block of the monitoring modules. These informations must be written by the + module developper on the __init__.py file (add on info_mod dictionnary). + :param os: the os of the modules + :return: a dictionary containing these informations on the form : + { + mod_name: {'block': val, 'unit': 'bool' or '%' or unit_name} + } + """ + __import__("modules.monitoring_modules." + os) + pack_mod_os = __import__("modules.monitoring_modules." + os, fromlist=modules.monitoring_modules.__all__) + return pack_mod_os.info_mod + + +def create_global_conf(db): + """ + Asks the database to create a global configuration in function of the monitoring modules descibed on the __init__.py + :param db: the database instance + """ + dict_mod = {} + for os in modules.monitoring_modules.__all__: + dict_mod[os] = get_info_mod_monitoring(os) + db.create_global_conf(dict_mod) \ No newline at end of file diff --git a/app/modules/connection_modules/ssh.py b/app/modules/connection_modules/ssh.py index b4473b4..9befed0 100644 --- a/app/modules/connection_modules/ssh.py +++ b/app/modules/connection_modules/ssh.py @@ -3,11 +3,11 @@ import paramiko class ssh: - def __init__(self, addr_host, key_location): + def __init__(self, addr_host, usrname, key_location): 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='aguilbaud', pkey=key) + self.ssh.connect(addr_host, username=usrname, pkey=key) self.addr_host = addr_host def get_addr_host(self): diff --git a/app/modules/monitoring_modules/unix/__init__.py b/app/modules/monitoring_modules/unix/__init__.py index e7ab721..ac9e130 100644 --- a/app/modules/monitoring_modules/unix/__init__.py +++ b/app/modules/monitoring_modules/unix/__init__.py @@ -1,2 +1,5 @@ __author__ = 'aguilbaud' -__all__=['updated_packages'] \ No newline at end of file +__all__=['updated_packages'] +info_mod = { + 'updated_packages': {'block': 'software', 'unit': 'bool'} +} \ 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 418f363..d04297c 100644 --- a/app/modules/storage_modules/shelve_db.py +++ b/app/modules/storage_modules/shelve_db.py @@ -288,7 +288,7 @@ class shelve_db: self.db['global_conf'][os][mod]['major_limit'] = 100 elif unit == 'bool': self.db['global_conf'][os][mod]['minor_limit'] = False - self.db['global_conf'][os][mod]['major_limit'] = False + self.db['global_conf'][os][mod]['major_limit'] = True else: self.db['global_conf'][os][mod]['minor_limit'] = 8 self.db['global_conf'][os][mod]['major_limit'] = 10 @@ -368,3 +368,20 @@ class shelve_db: finally: self.close_db() + def get_host_os(self, addr_host): + """ + Get the operating system corresponding to a host + :param addr_host: the IP adress of host to retreive os + :return: the os type corresponding to what have been detected + """ + self.open_db() + try: + detected = json.loads(self.db["hosts"][addr_host]["detected"]["nmap"]) + os = detected['os'].lower() + if os == "Unknown": + raise Exception('Os not detected') + if os == "linux": + os = "unix" + return os + finally: + self.close_db() \ No newline at end of file -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.