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 db30db467b2c6259b7a943531f440c0aa71f4e44 Author: Alexis Guilbaud <guilbaud@codelutin.com> Date: Tue Feb 24 15:03:41 2015 +0100 autoconfiguration of detected hosts by modules : OK --- app/app.py | 4 +- app/module_loader.py | 18 +++--- app/modules/detection_modules/nmap_detection.py | 12 ++-- app/modules/storage_modules/shelve_db.py | 83 +++++++++++++------------ static/js/controllers/mainCtrl.js | 20 ++++-- views/dashboard.html | 2 +- views/hostpage.html | 2 +- 7 files changed, 79 insertions(+), 62 deletions(-) diff --git a/app/app.py b/app/app.py index 0e9f74f..4a07abf 100755 --- a/app/app.py +++ b/app/app.py @@ -36,7 +36,9 @@ class ThreadDetect(threading.Thread): def run(self): db = module_loader.load_db() conn_mod_list = module_loader.get_conection_modules_list() - scanned_ip = module_loader.run_nmap_detection(self.ip_range, db, self.ws, conn_mod_list) + scanned_ip = module_loader.run_nmap_detection(self.ip_range, db, self.ws, + module_loader.get_conection_modules_list(), + module_loader.get_info_mod_monitoring()) self.ws.send(json.dumps({SUCCESS_MODULE: scanned_ip})) # now launching full detection for ip in json.loads(scanned_ip): diff --git a/app/module_loader.py b/app/module_loader.py index e61caed..c8a0951 100644 --- a/app/module_loader.py +++ b/app/module_loader.py @@ -24,7 +24,7 @@ def load_db(): return db_instance -def run_nmap_detection(ip_range, db, ws, conn_mod_list): +def run_nmap_detection(ip_range, db, ws, list_mod_conn, dict_mod_monitoring): """ Instanciates the nmap_detection module from detection_modules, and runs the detection. :param ip_range: addresses to execute the nmap detection @@ -33,7 +33,7 @@ def run_nmap_detection(ip_range, db, ws, conn_mod_list): :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, conn_mod_list) + nmap_mod_instance = getattr(nmap_mod, "nmap_detection")(db, ws, list_mod_conn, dict_mod_monitoring) return nmap_mod_instance.check_ip_range(ip_range) @@ -123,19 +123,21 @@ def run_one_monitoring_module(mod_name, os, conn, db, ws): ws.send(json.dumps({"40": cnfe.__str__})) -def get_info_mod_monitoring(os): +def get_info_mod_monitoring(): """ 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 : - { + {'os:{ 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 + info_mods = {} + for os in modules.monitoring_modules.__all__: + package = __import__("modules.monitoring_modules." + os, fromlist=modules.monitoring_modules.__all__) + info_mods[os] = package.info_mod + return info_mods def get_conection_modules_list(): diff --git a/app/modules/detection_modules/nmap_detection.py b/app/modules/detection_modules/nmap_detection.py index 7d77fb5..3b4b6ec 100644 --- a/app/modules/detection_modules/nmap_detection.py +++ b/app/modules/detection_modules/nmap_detection.py @@ -6,11 +6,12 @@ import json class nmap_detection: - def __init__(self, db, ws, conn_mod_list): + def __init__(self, db, ws, list_mod_conn, dict_mod_monitoring): self.db = db self.ws = ws self.scanned_ip = [] - self.conn_mod_list = conn_mod_list + self.list_mod_conn = list_mod_conn + self.dict_mod_monitoring = dict_mod_monitoring # function for splitting the different ranges of the IP adress # launch the nmap detection of each ip under this range @@ -135,13 +136,16 @@ class nmap_detection: state = port.getElementsByTagName('state')[0] service = port.getElementsByTagName('service')[0] if service.hasAttribute("ostype"): - dict_host['os'] = service.getAttribute("ostype") + if service.getAttribute("ostype").lower() == "linux": + dict_host['os'] = 'unix' + else: + dict_host['os'] = service.getAttribute("ostype").lower() if state.getAttribute('state') == 'open': dict_port['portid'] = port.getAttribute('portid') dict_port['portname'] = service.getAttribute('name') list_dict_port.append(dict_port) dict_host['openports'] = list_dict_port # the host have its IP for ID on the db - self.db.add_host(dict_host['addr'], json.dumps(dict_host), self.conn_mod_list) + self.db.add_host(dict_host['addr'], json.dumps(dict_host), self.list_mod_conn, self.dict_mod_monitoring) pexpect.run("rm -f res.xml") self.scanned_ip.append(ip) \ 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 00b1d88..5467a3e 100644 --- a/app/modules/storage_modules/shelve_db.py +++ b/app/modules/storage_modules/shelve_db.py @@ -27,7 +27,6 @@ class shelve_db: self.db["hosts"] = {} self.db["users"] = {} self.db["groups"] = {} - self.db["global_conf"] = {} except: print "Database initilalization error" else: @@ -43,7 +42,7 @@ class shelve_db: # 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. - def add_host(self, addr_host, nmap_res, conn_mod_list): + def add_host(self, addr_host, nmap_res, list_mod_conn, dict_mod_info): """ Called by the nmap_detection module. Add and save a new host after its first nmap detection @@ -51,6 +50,10 @@ class shelve_db: 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 + :param list_mod_conn: a list containing the names of the different connection modules + (see get_conection_modules_list() on module_loader) + :param dict_mod_info: a dictionnary containing informations about the different monitoring modules + (see get_info_mod_monitoring() on module_loader) """ self.open_db() addr_host = str(addr_host) # Shelve doesn't support Unicode @@ -61,9 +64,10 @@ class shelve_db: self.db["hosts"][addr_host]["detected"]["nmap"] = nmap_res # Preconfiguration self.db["hosts"][addr_host]["conf"] = {} - self.db["hosts"][addr_host]["conf"]["monitoring"] = self.db["global_conf"] + self.db["hosts"][addr_host]["conf"]["monitoring"] = self.generate_global_conf(dict_mod_info, + json.loads(nmap_res)['os']) self.db["hosts"][addr_host]["conf"]["groups"] = ["all"] # Every host is in group "all" - self.db["hosts"][addr_host]["conf"]["connections"] = self.init_conn(json.loads(nmap_res), conn_mod_list) + self.db["hosts"][addr_host]["conf"]["connections"] = self.init_conn(json.loads(nmap_res), list_mod_conn) self.db["hosts"][addr_host]["conf"]["subscribers"] = {} # Add current user automatically ? self.db["hosts"][addr_host]["conf"]["custom_info"] = "" self.db["hosts"][addr_host]["conf"]["interventions"] = [] @@ -74,6 +78,40 @@ class shelve_db: finally: self.close_db() + def generate_global_conf(self, dict_mod_info, os): + """ + Configures automatically the monitoring for a host for each of the monitoring modules avaliable, in + function of the unit of the result of the monitoring module. + :param dict_mod_info: dictionary containing informations about all notification modules, in the form: + [monitoring_module_name][{'block':val, 'unit': val}] + :return a list containing the default parameters for each monitoring module + """ + res = {} + for mod in dict_mod_info[os]: + mod_conf = {} + mod_conf['block'] = dict_mod_info[os][mod]['block'] + mod_conf['activated'] = True + mod_conf['check_frequency'] = 60 + mod_conf['nb_minute'] = 30 + mod_conf['nb_hour'] = 12 + mod_conf['nb_day'] = 15 + mod_conf['nb_week'] = 2 + mod_conf['nb_month'] = 6 + mod_conf['nb_year'] = None + unit = dict_mod_info[os][mod]['unit'] + mod_conf['unit'] = unit + if unit == '%': + mod_conf['minor_limit'] = 95 + mod_conf['major_limit'] = 100 + elif unit == 'bool': + mod_conf['minor_limit'] = True + mod_conf['major_limit'] = False + else: + mod_conf['minor_limit'] = 8 + mod_conf['major_limit'] = 10 + res[mod] = mod_conf + return res + def init_conn(self, dict_nmap_res, conn_list): """ Returns an initialization for the connection configuration on a host. @@ -275,43 +313,6 @@ class shelve_db: stats['M2'] += stats['delta'] * (val - stats['mean']) return stats - def create_global_conf(self, dict_mod_info): - """ - Create an entry on the global_conf for each new monitoring module. - :param dict_mod_info: dictionary containing informations about all notification modules, by os, in the form: - [os_name][monitoring_module_name][{'block':val, 'unit': val}] - """ - self.open_db() - try: - for os in dict_mod_info: - if os not in self.db['global_conf']: - self.db['global_conf'][os] = {} - for mod in dict_mod_info[os]: - if mod not in self.db['global_conf'][os]: - self.db['global_conf'][os][mod] = {} - self.db['global_conf'][os][mod]['block'] = dict_mod_info[os][mod]['block'] - self.db['global_conf'][os][mod]['activated'] = True - self.db['global_conf'][os][mod]['check_frequency'] = 3600 - self.db['global_conf'][os][mod]['nb_minute'] = 30 - self.db['global_conf'][os][mod]['nb_hour'] = 12 - self.db['global_conf'][os][mod]['nb_day'] = 15 - self.db['global_conf'][os][mod]['nb_week'] = 2 - self.db['global_conf'][os][mod]['nb_month'] = 6 - self.db['global_conf'][os][mod]['nb_year'] = None - unit = dict_mod_info[os][mod]['unit'] - self.db['global_conf'][os][mod]['unit'] = unit - if unit == '%': - self.db['global_conf'][os][mod]['minor_limit'] = 95 - self.db['global_conf'][os][mod]['major_limit'] = 100 - elif unit == 'bool': - self.db['global_conf'][os][mod]['minor_limit'] = True - self.db['global_conf'][os][mod]['major_limit'] = False - else: - self.db['global_conf'][os][mod]['minor_limit'] = 8 - self.db['global_conf'][os][mod]['major_limit'] = 10 - finally: - self.close_db() - def add_host_list_to_group(self, host_list, group): """ Add given hosts to a group. If the group doesn't exists on the database, it will be created. diff --git a/static/js/controllers/mainCtrl.js b/static/js/controllers/mainCtrl.js index dc7c4f2..f24a3fe 100644 --- a/static/js/controllers/mainCtrl.js +++ b/static/js/controllers/mainCtrl.js @@ -11,7 +11,7 @@ mumApp.config(function($routeProvider){ .when('/groups',{ templateUrl : 'groups.html', }) - .when('/hostpage',{ + .when('/hostpage/:param',{ templateUrl : 'hostpage.html', }) .when('/notifications',{ @@ -22,6 +22,7 @@ mumApp.config(function($routeProvider){ }) .when('/scan',{ templateUrl : 'scan.html', + controller : 'mainController' }) .when('/settings',{ templateUrl : 'settings.html', @@ -35,11 +36,18 @@ mumApp.config(function($routeProvider){ .when('/users',{ templateUrl : 'users.html', }) + .otherwise({ + redirectTo: '/' + }); }); -mumApp.controller('mainController', ['$scope', 'toastr', '$interval', '$filter', function($scope, toastr, $interval, $filter) { +mumApp.controller('mainController', ['$scope', 'toastr', '$interval', '$filter', '$routeParams', + function($scope, toastr, $interval, $filter, $routeParams) { // init + $scope.master = {}; + $scope.param = $routeParams.param; + $scope.sort = { sortingOrder : 'id', reverse : false @@ -72,10 +80,6 @@ mumApp.controller('mainController', ['$scope', 'toastr', '$interval', '$filter', $scope.scan_is_over = false; // pour afficher ou non certaines parties de la page $scope.ip_scanned = {}; - $scope.post_val = function(){ //lace la detection apres remplissage du champ et validation du formulaire - var request = '{"10" : "' + $scope.ip_range + '"}'; - ws.send(request); - } // Concerning WebSocket var ws = new WebSocket("ws://0.0.0.0:1337/websocket"); @@ -146,6 +150,10 @@ mumApp.controller('mainController', ['$scope', 'toastr', '$interval', '$filter', toastr.error(msg, title); }; + $scope.post_val = function(){ //lace la detection apres remplissage du champ et validation du formulaire + var request = '{"10" : "' + $scope.ip_range + '"}'; + ws.send(request); + } // Concerning the table manipulation var searchMatch = function (haystack, needle) { diff --git a/views/dashboard.html b/views/dashboard.html index d8fafa9..c7e739b 100644 --- a/views/dashboard.html +++ b/views/dashboard.html @@ -72,7 +72,7 @@ orderBy:sort.sortingOrder:sort.reverse | filter:{addr:addr_filter, name:name_filter, status:status_filter, group:{name:group_filter}}" class={{item.status}}> - <td><a href="hostpage.html">{{item.addr}}</a></td> + <td><a href="#/hostpage/{{item.addr}}">{{item.addr}}</a></td> <td>{{item.name}}</td> <td>{{item.status}}</td> <td>{{getGroupsByAddr(item.addr)}}</td> diff --git a/views/hostpage.html b/views/hostpage.html index 060f678..fd9bd28 100644 --- a/views/hostpage.html +++ b/views/hostpage.html @@ -1,6 +1,6 @@ <div class="col-md-offset-2 main"> - <h1 class="page-header">Current state of 192.168.74.1 <small>www.example.com</small></h1> + <h1 class="page-header">Current state of {{param}} <small>www.example.com</small></h1> <button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#modal_block">Activate/Deactivate</button> <button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#modal_conn">Connection settings</button> <button type="button" class="btn btn-info btn-xs">Launch a new detection</button> -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.