def host_unbind_get(): host_id = request.args.get('host_id', '').strip() if not host_id: return jsonify(msg='host_id is blank') group_id = request.args.get('group_id', '').strip() if not group_id: return jsonify(msg='group_id is blank') GroupHost.unbind(int(group_id), host_id) return jsonify(msg='')
def host_remove_post(): group_id = int(request.form['grp_id'].strip()) host_ids = request.form['host_ids'].strip() alarmAdUrl = config.JSONCFG['shortcut']['falconUIC'] + "/api/v1/alarmadjust/whenendpointunbind" GroupHost.unbind(group_id, host_ids) for host_id in host_ids.split(","): data = {'hostgroupId': group_id, 'hostId': host_id} respCode = post2FeUpdateEventCase(alarmAdUrl, data) if respCode != 200: log.error(alarmAdUrl + " got " + str(respCode) + " with " + str(data)) return jsonify(msg='delete host is failed , please try again!') return jsonify(msg='')
def host_remove_post(): group_id = int(request.form['grp_id'].strip()) host_ids = request.form['host_ids'].strip() alarmAdUrl = config.JSONCFG['shortcut'][ 'falconUIC'] + "/api/v1/alarmadjust/whenendpointunbind" GroupHost.unbind(group_id, host_ids) for host_id in host_ids.split(","): data = {'hostgroupId': group_id, 'hostId': host_id} respCode = post2FeUpdateEventCase(alarmAdUrl, data) if respCode != 200: log.error(alarmAdUrl + " got " + str(respCode) + " with " + str(data)) return jsonify(msg='delete host is failed , please try again!') return jsonify(msg='')
def host_add_post(): group_id = request.form['group_id'] if not group_id: return jsonify(msg='no group_id given') group_id = int(group_id) group = HostGroup.read('id = %s', [group_id]) if not group: return jsonify(msg='no such group') hosts = request.form['hosts'].strip() if not hosts: return jsonify(msg='hosts is blank') host_arr = hosts.splitlines() safe_host_arr = [h for h in host_arr if h] if not safe_host_arr: return jsonify(msg='hosts is blank') success = [] failure = [] for h in safe_host_arr: msg = GroupHost.bind(group_id, h) if not msg: success.append('%s<br>' % h) else: failure.append('%s %s<br>' % (h, msg)) data = '<div class="alert alert-danger" role="alert">failure:<hr>' + ''.join( failure ) + '</div><div class="alert alert-success" role="alert">success:<hr>' + ''.join( success) + '</div>' return jsonify(msg='', data=data)
def host_unbind_get(): host_id = request.args.get('host_id', '').strip() data = {'hostgroupId': group_id, 'hostId': host_id} alarmAdUrl = config.JSONCFG['shortcut']['falconUIC'] + "/api/v1/alarmadjust/whenendpointunbind" if not host_id: return jsonify(msg='host_id is blank') group_id = request.args.get('group_id', '').strip() if not group_id: return jsonify(msg='group_id is blank') GroupHost.unbind(int(group_id), host_id) respCode = post2FeUpdateEventCase(alarmAdUrl, data) if respCode != 200: log.error(alarmAdUrl + " got " + str(respCode) + " with " + str(data)) return jsonify(msg='')
def host_add_post(): group_id = request.form['group_id'] if not group_id: return jsonify(msg='no group_id given') group_id = int(group_id) group = HostGroup.read('id = %s', [group_id]) if not group: return jsonify(msg='no such group') hosts = request.form['hosts'].strip() if not hosts: return jsonify(msg='hosts is blank') host_arr = hosts.splitlines() safe_host_arr = [h for h in host_arr if h] if not safe_host_arr: return jsonify(msg='hosts is blank') success = [] failure = [] for h in safe_host_arr: h = h.strip() msg = GroupHost.bind(group_id, h) if not msg: success.append('%s<br>' % h) else: failure.append('%s %s<br>' % (h, msg)) data = '<div class="alert alert-danger" role="alert">failure:<hr>' + ''.join( failure) + '</div><div class="alert alert-success" role="alert">success:<hr>' + ''.join(success) + '</div>' return jsonify(msg='', data=data)
def host_groups_get(host_id,host_name): if not os.path.exists( str(EXTERNAL_NODES) ): host_id = int(host_id) h = Host.read('id = %s', params=[host_id]) if not h: return jsonify(msg='no such host') group_ids = GroupHost.group_ids(h.id) else: (status, output) = commands.getstatusoutput(EXTERNAL_NODES + " GetHostGroupByHost " + host_id ) group_ids = [] h = {} if status == 0: decodejson = json.loads( output ) gname = "1" for i in decodejson: gname = gname + ",'" + decodejson[i] + "'" groupMsg = HostGroup.select(cols="id,grp_name", where="grp_name in (%s)" % gname) for m in groupMsg: group_ids.append( m[0] ) h["id"] = host_id h["hostname"] = host_name groups = [HostGroup.read('id = %s', [group_id]) for group_id in group_ids] return render_template('host/groups.html', groups=groups, host=h)
def host_unbind_get(): host_id = request.args.get('host_id', '').strip() data = {'hostgroupId': group_id, 'hostId': host_id} alarmAdUrl = config.JSONCFG['shortcut'][ 'falconUIC'] + "/api/v1/alarmadjust/whenendpointunbind" if not host_id: return jsonify(msg='host_id is blank') group_id = request.args.get('group_id', '').strip() if not group_id: return jsonify(msg='group_id is blank') GroupHost.unbind(int(group_id), host_id) respCode = post2FeUpdateEventCase(alarmAdUrl, data) if respCode != 200: log.error(alarmAdUrl + " got " + str(respCode) + " with " + str(data)) return jsonify(msg='')
def host_groups_get(host_id): host_id = int(host_id) h = Host.read('id = %s', params=[host_id]) if not h: return jsonify(msg='no such host') group_ids = GroupHost.group_ids(h.id) groups = [HostGroup.read('id = %s', [group_id]) for group_id in group_ids] return render_template('host/groups.html', groups=groups, host=h)
def host_templates_get(host_id): host_id = int(host_id) h = Host.read('id = %s', params=[host_id]) if not h: return jsonify(msg='no such host') group_ids = GroupHost.group_ids(h.id) templates = GrpTpl.tpl_set(group_ids) for v in templates: v.parent = Template.get(v.parent_id) return render_template('host/templates.html', **locals())
def host_templates_get(host_id): host_id = int(host_id) h = Host.read('id = %s', params=[host_id]) if not h: return jsonify(msg='no such host') group_ids = GroupHost.group_ids(h.id) templates = GrpTpl.tpl_set(group_ids) for v in templates: v.parent = Template.get(v.parent_id) return render_template('host/templates.html', config=config, **locals())
def api_marathon_health(): marathon_stats = request.get_json() if marathon_stats["eventType"] == "deployment_success": action_type = marathon_stats["plan"]["steps"][-1]["actions"][-1]["type"] app = marathon_stats["plan"]["steps"][-1]["actions"][-1]["app"].lstrip("/") if action_type == "StopApplication": grp_id = HostGroup.query_grp_id(app) if grp_id > 0: group_service.delete_group(grp_id) else: grp_id = HostGroup.create(app,"root",1) if grp_id < 0: grp_id = HostGroup.query_grp_id(app) if grp_id < 0: return 'grp_name no exist' import requests,json r = requests.get(MARATHON_ADDRESS+"/v2/apps/"+app) app_info = json.loads(r.text) marathon_hosts = [] for i in app_info["app"]["tasks"]: marathon_hosts.append(i["host"]) vs,_ = Host.query(1, 10000000, '', '0', grp_id) of_names = [v.hostname for v in vs] of_names_set = set(of_names) marathon_hosts_set = set(marathon_hosts) delete_hosts = list(of_names_set - marathon_hosts_set) add_hosts = list(marathon_hosts_set - of_names_set) for h in add_hosts: msg = GroupHost.bind(grp_id,h) of_ids = [int(v.id) for v in vs if v.hostname in delete_hosts] if len(delete_hosts)>0: ids = ",".join('%s' % id for id in of_ids) msg = GroupHost.unbind(int(grp_id[0]),ids) return 'ok'
def host_templates_get(host_id, host_name): if not os.path.exists( str(EXTERNAL_NODES) ): host_id = int(host_id) h = Host.read('id = %s', params=[host_id]) if not h: return jsonify(msg='no such host') group_ids = GroupHost.group_ids(h.id) else: (status, output) = commands.getstatusoutput(EXTERNAL_NODES + " GetHostGroupByHost " + host_id ) group_ids = [] if status == 0: decodejson = json.loads( output ) gname = "1" for i in decodejson: gname = gname + ",'" + decodejson[i] + "'" groupMsg = HostGroup.select(cols="id,grp_name", where="grp_name in (%s)" % gname) for m in groupMsg: group_ids.append( m[0] ) templates = GrpTpl.tpl_set(group_ids) for v in templates: v.parent = Template.get(v.parent_id) return render_template('host/templates.html', **locals())
def host_remove_post(): group_id = int(request.form['grp_id'].strip()) host_ids = request.form['host_ids'].strip() GroupHost.unbind(group_id, host_ids) return jsonify(msg='')