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 7d977bac276a337cf4cb14876f24ef8428ff2cd1 Author: Alexis Guilbaud <guilbaud@codelutin.com> Date: Tue Feb 17 15:22:17 2015 +0100 function get_hosts() implemented --- app/modules/storage.py | 76 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 63 insertions(+), 13 deletions(-) diff --git a/app/modules/storage.py b/app/modules/storage.py index c898104..d4d7d06 100644 --- a/app/modules/storage.py +++ b/app/modules/storage.py @@ -1,43 +1,92 @@ -# -*- coding: utf8 -*- -from __future__ import unicode_literals __author__ = 'aguilbaud' import shelve from datetime import datetime from math import sqrt import os.path +import json +# Returns an instance of the python database +# If the base doesn't exists, it initialize the first elements def init_db(): if not os.path.isfile("mum.db"): db = shelve.open("mum.db", writeback=True) try: - db[str("hosts")] = {} - db[str("users")] = {} - db[str("groups")] = {} - db[str("global_conf")] = {} + db["hosts"] = {} + db["users"] = {} + db["groups"] = {} + db["global_conf"] = {} finally: - db.close() - return shelve.open("mum.db" , writeback=True) + return db + return shelve.open("mum.db", writeback=True) +# Closes the database def close_db(db): db.close() +# 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(addr_host, nmap_res): + addr_host = str(addr_host) # Shelve doesn't support Unicode db = init_db() try: - db[str("hosts")][str(addr_host)] = {} - db[str("hosts")][str(addr_host)][str("detected")] = {} - db[str("hosts")][str(addr_host)][str("detected")][str("nmap")] = nmap_res + # Add the nmap detection + db["hosts"][addr_host] = {} + db["hosts"][addr_host]["detected"] = {} + db["hosts"][addr_host]["detected"]["nmap"] = nmap_res + # Preconfiguration + db["hosts"][addr_host]["conf"] = {} + db["hosts"][addr_host]["conf"]["monitoring"] = db["global_conf"] + db["hosts"][addr_host]["conf"]["groups"] = {"name": "all"} + db["hosts"][addr_host]["conf"]["subscribers"] = {} # Add current user automatically ? + db["hosts"][addr_host]["conf"]["custom_info"] = "" + db["hosts"][addr_host]["conf"]["interventions"] = {} + # Create structure for monitoring data + db["hosts"][addr_host]["monitoring"] = {} + # Create structure for archiving data + db["hosts"][addr_host]["archive"] = {} finally: close_db(db) +# Returns the essential data about all hosts under monitoring +# These are used by the front-end +# If no hosts have been added, the function will return an empty list +def get_hosts(): + res = [] + db = init_db() + try: + if db["hosts"] != {}: + for host in db["hosts"]: + detected = json.loads(db["hosts"][host]["detected"]["nmap"]) + info_host = {} + info_host["addr"] = detected["addr"] + info_host["name"] = detected["hostname"] + if "status" in db["hosts"][host]["monitoring"]: + info_host["status"] = db["hosts"][host]["monitoring"]["status"] + else: + info_host["status"] = "" + info_host["group"] = [] + for group in db["hosts"][host]["conf"]["groups"]: + info_host["group"].append({"name": db["hosts"][host]["conf"]["groups"][group]}) + if "date" in db["hosts"][host]["monitoring"]: + info_host["last_check"] = db["hosts"][host]["monitoring"]["date"] + else: + info_host["last_check"] = 0 + res.append(info_host) + finally: + close_db(db) + return json.dumps(res) + + +# Add a new check of a host from a specific module def add_check(addr_host, name_part, val): new_val = {"date": datetime.now()} - db = shelve.open('mum.db', writeback=True) + db = init_db() try: if val >= db['hosts']['conf']['monitorig'][name_part]['minor_limit']: new_val['state'] = 'warning' @@ -51,7 +100,7 @@ def add_check(addr_host, name_part, val): if db['hosts'][addr_host]['archive'].has_key(name_part): db['hosts'][addr_host]['archive'][name_part] = update_stats(db['hosts'][addr_host]['archive'][name_part], val) finally: - db.close() + close_db(db) # Updates calulated statistics once a new value is received @@ -98,6 +147,7 @@ def get_slope_of_linear_regression(stats): print "Division by 0 on get_slope_of_linear_regression(stats)" return res + def save_detection(name_part, json_res): db = shelve.open('mum.db', writeback=True) try: -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.