def __task(self): exec_tasks = get_data_by_key("exec_tasks") if exec_tasks["exec_status"] == True and exec_tasks[ "exec_type"] == "once": exec_tasks["exec_status"] = False exec_tasks["exec_type"] = None exec_tasks["exec_time"] = None exec_tasks["exec_datetime"] = None exec_tasks["exec_patrol"] = None set_data_by_key("exec_tasks", exec_tasks) ##### #### with get_data_by_key("_app").app_context(): active_rules = Patrollist.query.filter_by(warning=False).all() for rule in active_rules: result = self.task_proxy.do_task(rule) if result == True: try: rule.warning = True rule.wctime = datetime.utcnow() if rule.auto == True: rule.active = True cmd_exec = CommandExecAcl( rule.rule, rule.category, rule.type, Command("_pub_rule_server")) cmd_exec.add_acl() db.session.add(rule) db.session.commit() except Exception as e: print(e) return False
def send_cmd(self, cmd): result = [] plugin_tmp = [] plugins = get_data_by_key("plugins") poller = zmq.Poller() poller.register(self.route, zmq.POLLIN) err_count = 0 for plugin in plugins: self.route.send_string(plugin, zmq.SNDMORE) #zmq.SNDMORE表明消息是多帧的,就是一个长消息 self.route.send_json(cmd) try: socks = dict(poller.poll(3000)) if self.route in socks: who = self.route.recv() message = self.route.recv_json() if message["op"] == "real-stats-rep" and who == plugin: result.append(message["data"]) plugin_tmp.append(plugin) else: err_count += 1 except KeyboardInterrupt: continue if err_count > 0: set_data_by_key("plugins", plugin_tmp) return result
def sub_msg_main(): with get_data_by_key("_app").app_context(): config = current_app.config addr = config.get('MQ_ADDR') port = config.get('MQ_REP_MISC_PORT') server = RepMiscServer(addr, port, req_msg_proc) server.loop_main()
def index(): count = {} count["plugins_num"] = len(get_data_by_key("plugins")) count["whitelist_rules"] = len(Whitelist.query.all()) count["blacklist_rules"] = len(Blacklist.query.all()) count["patrollist_rules"] = len(Patrollist.query.all()) count["warning_rules"] = len(Patrollist.query.filter_by(warning=True).all()) return render_template('main/dashboard.html',count=count)
def delete(self): try: exec_tasks = get_data_by_key("exec_tasks") if exec_tasks["exec_status"] == True: exec_tasks["exec_patrol"].stop_task() exec_tasks["exec_status"] = False exec_tasks["exec_type"] = None exec_tasks["exec_time"] = None exec_tasks["exec_datetime"] = None exec_tasks["exec_patrol"] = None set_data_by_key("exec_tasks",exec_tasks) return {"result":"successful"} except Exception,e : print(e) return {"result":"failed"}
def post(self): try: exec_tasks = get_data_by_key("exec_tasks") if exec_tasks["exec_status"] == False: req_json = request.json exec_tasks["exec_status"] = True exec_tasks["exec_type"] = req_json["type"] exec_tasks["exec_time"] = datetime.strptime(req_json["time"], '%H:%M') if req_json["time"] else None exec_tasks["exec_datetime"] = datetime.strptime(req_json["datetime"], '%Y-%m-%d %H:%M') if req_json["datetime"] else None if exec_tasks["exec_type"] == "once" and exec_tasks["exec_datetime"] != None: exec_tasks["exec_patrol"] = TaskScheduler(task_type=exec_tasks["exec_type"],task_datetime=exec_tasks["exec_datetime"]) elif exec_tasks["exec_type"] == "periodicity" and exec_tasks["exec_time"] != None: exec_tasks["exec_patrol"] = TaskScheduler(task_type=exec_tasks["exec_type"],task_datetime=exec_tasks["exec_time"]) else: return {"result":"failed"} exec_tasks["exec_patrol"].exec_task() set_data_by_key("exec_tasks",exec_tasks) return {"result":"successful"} except Exception,e : print(e) return {"result":"failed"}
def moscow(): """Moscow-specific page.""" # Check pre-rendered HTML city = "Москва" rendered = data.get_rendered_page(city) if rendered: return rendered main_links = [{ "name": "Больницы", "link": "#hospitals" }, { "name": "Транспорт", "link": "#transport" }] secondary_links = filter_secondary_links(city) # Getting data full_data = data.get_newest_data() moscow_data_full, moscow_data = data.get_region_data(full_data, city) # Cases statistics block stats = [] for category in cts.CATEGORIES[:-1]: style = cts.CATEGORIES_STYLES[category].copy() style["value"] = moscow_data[category] style["diff"] = moscow_data[f"{category}_diff"] stats.append(style) # Cases plots cases_plot = plotting.plot_region(moscow_data_full, city) cases_log_plot = plotting.plot_region(moscow_data_full, city, log_y=True) # Transport plots transport_data = data.get_data_by_key([cts.MSK_DIR, "transport"], sort_by="date", set_index="date") public_tr_plot = plotting.plot_region( transport_data / 100., city, plot_cols=[tr["key"] for tr in cts.PUBLIC_TR_COLS], legend_map=[tr["name"] for tr in cts.PUBLIC_TR_COLS], legend_loc="top_right", dt_fmt="%d-%m-%Y %H:%S", fmt="{0 %}", set_yticks=True, width_policy="max", height=int(0.75 * cfg.MAX_MAIN_HEIGHT), xaxis_ticks=5, yrange=(-1.1, 0)) private_tr_plot = plotting.plot_region( transport_data / 100., city, plot_cols=[tr["key"] for tr in cts.PRIVATE_TR_COLS], legend_map=[tr["name"] for tr in cts.PRIVATE_TR_COLS], legend_loc="top_right", dt_fmt="%d-%m-%Y %H:%S", fmt="{0 %}", set_yticks=True, width_policy="max", height=int(0.75 * cfg.MAX_MAIN_HEIGHT), xaxis_ticks=5, yrange=(-1.1, 0)) plots = { "cases": cases_plot, "cases_log": cases_log_plot, "public_transport": public_tr_plot, "private_transport": private_tr_plot } # Transport block transport_data_latest = transport_data.iloc[-1].to_dict() tr_stats = [] for category in cts.PUBLIC_TR_COLS + cts.PRIVATE_TR_COLS: tr_stats.append({ "name": category["name"].capitalize(), "value": transport_data_latest[category["key"]] }) # Hospitals hospitals = [] for hospital in cts.MSK_HOSPITALS: hospital_data = data.get_data_by_key( [cts.MSK_DIR, cts.MSK_HOSPITALS_DIR, hospital["key"]]) hospital_plot = plotting.plot_region( hospital_data, hospital["name"], plot_cols=[tr["key"] for tr in hospital["fields"]], legend_map=[tr["name"] for tr in hospital["fields"]], glyphs={ tr["key"]: tr["glyph"] for tr in hospital["fields"] if "glyph" in tr }, alphas={ tr["key"]: tr["alpha"] for tr in hospital["fields"] if "alpha" in tr }, bar_bottom=0, alpha=0.9, legend_loc="top_left", width_policy="max", width=cfg.MAX_MAIN_WIDTH * 2, height_policy="fit", additional_tools=["ywheel_zoom", "ypan", "reset"]) plots[hospital["key"]] = hospital_plot hospitals.append(hospital) # Getting Bokeh components script, div = components(plots) for hospital in hospitals: hospital_plot = div.pop(hospital["key"]) hospital["plot"] = hospital_plot # Rendering HTML rendered = render_template("moscow.html", city=city, main_links=main_links, secondary_links=secondary_links, stats=stats, tr_stats=tr_stats, bokeh_script=script, hospitals=hospitals, **div) data.save_rendered_page("moscow", rendered) return rendered
def req_msg_proc(client, who, message): if "register-req" == message["op"]: plugins = get_data_by_key("plugins") if message["data"]["id"] not in plugins: plugins.append(message["data"]["id"]) set_data_by_key("plugins", plugins) client.route.send_string(who, zmq.SNDMORE) client.route.send_json({"op": "register-rep", "data": {}}) elif "batch-rule-req" == message["op"]: client.route.send_string(who, zmq.SNDMORE) # whitelist_rules = Whitelist.query.all() for rule in whitelist_rules: client.route.send_json( { "op": "batch-rule-rep", "data": { "rule": rule.rule, "category": rule.category, "type": rule.type, "op": "add" } }, zmq.SNDMORE) blacklist_rules = Blacklist.query.all() for rule in blacklist_rules: client.route.send_json( { "op": "batch-rule-rep", "data": { "rule": rule.rule, "category": rule.category, "type": rule.type, "op": "add" } }, zmq.SNDMORE) warnings_rules_auto = Patrollist.query.filter_by(warning=True, auto=True).all() for rule in warnings_rules_auto: client.route.send_json( { "op": "batch-rule-rep", "data": { "rule": rule.rule, "category": rule.category, "type": rule.type, "op": "add" } }, zmq.SNDMORE) warnings_rules = Patrollist.query.filter_by(warning=True, auto=False, active=True).all() for rule in warnings_rules: client.route.send_json( { "op": "batch-rule-rep", "data": { "rule": rule.rule, "category": rule.category, "type": rule.type, "op": "add" } }, zmq.SNDMORE) #end client.route.send_json({"op": "batch-rule-end", "data": {}})
def patrol(): user_agent_rules = Patrollist.query.filter_by(category=u'user_agent_rule').all() refferer_rules = Patrollist.query.filter_by(category=u'referrer_rule').all() url_rules = Patrollist.query.filter_by(category=u'url_rule').all() exec_tasks = get_data_by_key("exec_tasks") return render_template('main/patrollist.html',user_agent_rules=user_agent_rules,refferer_rules=refferer_rules,url_rules=url_rules, type=RULE_TYPE,auto=RULE_AUTO,description=RULE_DESCRIPTION,exec_status=exec_tasks["exec_status"])
def __init__(self, server_type): self.cmd = get_data_by_key(server_type)