def post(self): data = json.loads(self.request.body) node_config = data["data"] node_name = node_config.get("node_name", None) node_type = node_config.get("node_type", None) if node_name and node_type: c = mongo_api.MongoExecuter(mongo_driver.db_handler) c.update("nagios_host_status", {"host": node_name}, {"node_type": node_type}) self.set_header("Content-Type", "application/json") self.write(json.dumps({"return_code": 0})) else: self.send_error(500)
def post(self): ''' 保存所有节点在拓扑图中的坐标位置 并在settings集合中记录一个'top_has_saved'的标志 作为判断所有节点是否保存坐标的依据 ''' c = mongo_api.MongoExecuter(mongo_driver.db_handler) data = json.loads(self.request.body) node_position = data["data"] for host_name, position in node_position.items(): c.update("nagios_host_status", {"host": host_name}, { "X": position["X"], "Y": position["Y"] }) if not c.query_one("settings", {"topo_has_saved": 1}): c.insert("settings", {"topo_has_saved": 1}) self.set_header("Content-Type", "application/json") self.write(json.dumps({"return_code": 0}))
def get_state(host): ''' 取得每个主机的状态和保存的拓扑图的坐标 如果无坐标,则主机出现在拓扑图的0,0处 ''' state = {} c = mongo_api.MongoExecuter(mongo_driver.db_handler) data = c.query_one("nagios_host_status", {"host": host}) if data: state["status"] = data["return_code"] state["X"] = data.get("X", 0) state["Y"] = data.get("Y", 0) state["node_type"] = data.get("node_type", "host") return state else: state["status"] = 0 state["X"] = 0 state["Y"] = 0 state["node_type"] = "host" return state
def get(self): ''' 返回前序遍历拓扑图生成的各子树的有序列表 并判断拓扑图中所有节点的坐标是否保存过 如果保存过前端使用保存的坐标显示节点 否则节点的位置自动显示 ''' c = mongo_api.MongoExecuter(mongo_driver.db_handler) data = c.query("nagios_hosts", {}) result = process_data(data) data = c.query_one("settings", {}) top_has_saved = 0 if data: top_has_saved = 1 self.set_header("Content-Type", "application/json") self.write( json.dumps({ "data": result, "all_host_data": get_allhost_state(), "topo_has_saved": top_has_saved }))
def get_allhost_state(): ''' 取得所有主机和主机服务的状态 ''' hosts = {} c = mongo_api.MongoExecuter(mongo_driver.db_handler) data = c.query("nagios_host_status", {}) for host in data: host_name = host["host"] hosts[host_name] = {} hosts[host_name]["last_update"] = str(host["last_update"]) hosts[host_name]["output"] = host["output"] host_service_status = c.query("nagios_service_status", {"host": host_name}) hosts[host_name]["service_ok"] = 0 hosts[host_name]["service_warn"] = 0 hosts[host_name]["service_critical"] = 0 hosts[host_name]["service_unknow"] = 0 if host_service_status: for host_service in host_service_status: if host_service["return_code"] == 0: hosts[host_name]["service_ok"] += 1 if host_service["return_code"] == 1: hosts[host_name]["service_warn"] += 1 if host_service["return_code"] == 2: hosts[host_name]["service_critical"] += 1 if host_service["return_code"] == 3: hosts[host_name]["service_unknow"] += 1 host_info = c.query_one("nagios_hosts", {"host_name": host_name}) if host_info: hosts[host_name]["host_name"] = host_info["host_name"] hosts[host_name]["host_alias"] = host_info["host_alias"] hosts[host_name]["host_address"] = host_info["host_address"] return hosts