def rename(old_str=None, new_str=None, login_user=None): print(old_str, new_str, login_user) gs = HostGroup.select_vs(where='create_user = %s and come_from = %s', params=[login_user, 1]) for g in gs: HostGroup.update_dict( {'grp_name': g.grp_name.replace(old_str, new_str)}, 'id=%s', [g.id]) return ''
def api_group_hosts_json(grp_name): group = HostGroup.read(where='id = %s', params=[grp_name]) if not group: group = HostGroup.read(where='grp_name = %s', params=[grp_name]) if not group: return jsonify(msg='no such group %s' % grp_name) vs, _ = Host.query(1, 10000000, '', '0', group.id) names = [v.hostname for v in vs] return jsonify(msg='', data=names)
def group_update_post(group_id): group_id = int(group_id) new_name = request.form['new_name'].strip() if not new_name: return jsonify(msg='new name is blank') group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group') if not group.writable(g.user): return jsonify(msg='no permission') HostGroup.update_dict({'grp_name': new_name}, 'id=%s', [group_id]) return jsonify(msg='')
def cluster_creator_get(group_id): group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) return render_template('cluster/creator.html', **locals())
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 group_templates_get(grp_id): grp_id = int(grp_id) grp = HostGroup.read(where='id = %s', params=[grp_id]) if not grp: return jsonify(msg='no such group') ts = GrpTpl.tpl_list(grp_id) return render_template('group/templates.html', group=grp, ts=ts)
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 cluster_list_get(group_id): group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) clusters = Cluster.select_vs(where='grp_id = %s', params=[group_id]) return render_template('cluster/list.html', **locals())
def variable_list_get(group_id): group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) variables = Variable.select_vs(where='grp_id = %s', params=[group_id]) return render_template('variable/list.html', group=group, variables=variables)
def plugin_list_get(group_id): group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) plugins = PluginDir.select_vs(where='grp_id = %s', params=[group_id]) return render_template('plugin/list.html', group=group, plugins=plugins)
def group_create_post(): grp_name = request.form['grp_name'].strip() if not grp_name: return jsonify(msg="group name is blank") grp_id = HostGroup.create(grp_name, g.user.name, 1) if grp_id > 0: return jsonify(msg='') else: return jsonify(msg='grp_name has already existent')
def group_delete_get(group_id): group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group') if not group.writable(g.user): return jsonify(msg='no permission') return jsonify(msg=group_service.delete_group(group_id))
def host_add_get(): group_id = request.args.get('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') return render_template('host/add.html', group=group)
def group_hosts_export(group_id): group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) vs, _ = Host.query(1, 10000000, '', '0', group_id) names = [v.hostname for v in vs] response = make_response('\n'.join(names)) response.headers["content-type"] = "text/plain" return response
def plugin_bind_post(): group_id = int(request.form['group_id'].strip()) plugin_dir = request.form['plugin_dir'].strip() group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) PluginDir.insert({ 'grp_id': group_id, 'dir': plugin_dir, 'create_user': g.user.name }) return jsonify(msg='')
def cluster_node_post(group_id): group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) numerator = request.form['numerator'].strip() denominator = request.form['denominator'].strip() endpoint = request.form['endpoint'].strip() metric = request.form['metric'].strip() tags = request.form['tags'].strip() ds_type = 'GAUGE' step = request.form['step'].strip() msg = required_chk({ 'numerator': numerator, 'denominator': denominator, 'endpoint': endpoint, 'metric': metric, 'ds_type': ds_type, 'step': step, }) if msg: return jsonify(msg=msg) if Cluster.exists('endpoint=%s and metric=%s and tags=%s', [endpoint, metric, tags]): return jsonify(msg='%s/%s/%s is already existent' % (endpoint, metric, tags)) last_id = Cluster.insert({ 'grp_id': group_id, 'numerator': numerator, 'denominator': denominator, 'endpoint': endpoint, 'metric': metric, 'tags': tags, 'ds_type': ds_type, 'step': step, 'creator': g.user.name, }) if last_id > 0: return jsonify(msg='') else: return jsonify(msg='occur error')
def home_get(): page = int(request.args.get('p', 1)) limit = int(request.args.get('limit', 10)) query = request.args.get('q', '').strip() mine = request.args.get('mine', '0') me = g.user.name if mine == '1' else None vs, total = HostGroup.query(page, limit, query, me) log.debug(vs) return render_template('group/index.html', data={ 'vs': vs, 'total': total, 'query': query, 'limit': limit, 'page': page, 'mine': mine, 'is_root': g.user.name in config.MAINTAINERS, })
def graph_list_get(group_id): group_id = int(group_id) ret = {"ok": False, "msg": "", "endpoints": [], "counters": []} return render_template("graph/counter.html", data=ret) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: ret['msg'] = 'no such group %s' % group_id return render_template("graph/counter.html", data=ret) vs, _ = Host.query(1, 10000000, '', '0', group_id) names = [v.hostname for v in vs] h = {"Content-type": "application/json"} r = corelib.auth_requests( "GET", config.API_ADDR + "/graph/endpoints?endpoints=%s" % ','.join(names), headers=h) if r.status_code != 200: abort(400, r.text) ret['msg'] = r.text return render_template("graph/counter.html", data=ret) endpoint_ids = json.loads(r.text) # return json.dumps(endpoint_ids) if len(endpoint_ids) == 0: ret['msg'] = 'No any endpoint selected' return render_template("graph/counter.html", data=ret) r = corelib.auth_requests( "GET", config.API_ADDR + "/graph/endpoint_counter?eid=%s" % ",".join('%s' % e['id'] for e in endpoint_ids), headers=h) if r.status_code != 200: ret['msg'] = r.text return render_template("graph/counter.html", data=ret) j = r.json() counters_map = {} for x in j: counters_map[x['counter']] = [x['counter'], x['type'], x['step']] sorted_counters = sorted(counters_map.keys()) sorted_values = [counters_map[x] for x in sorted_counters] ret['ok'] = True ret['endpoints'] = endpoint_ids ret['counters'] = sorted_values return render_template("graph/counter.html", data=ret)
def variable_creator_post(group_id): group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) name = request.form['name'].strip() content = request.form['content'].strip() note = request.form['note'].strip() if Variable.exists('grp_id=%s and name=%s', [group_id, name]): return jsonify(msg='%s/%s is already existent' % (group['name'], name)) last_id = Variable.insert({ 'grp_id': group_id, 'name': name, 'content': content, 'note': note, 'create_user': g.user.name, }) if last_id > 0: return jsonify(msg='') else: return jsonify(msg='occur error')
def group_hosts_list(group_id): g.xbox = request.args.get('xbox', '') group_id = int(group_id) group = HostGroup.read(where='id = %s', params=[group_id]) if not group: return jsonify(msg='no such group %s' % group_id) page = int(request.args.get('p', 1)) limit = int(request.args.get('limit', 10)) query = request.args.get('q', '') maintaining = request.args.get('maintaining', '0') vs, total = Host.query(page, limit, query, maintaining, group_id) return render_template('host/index.html', data={ 'vs': vs, 'total': total, 'query': query, 'limit': limit, 'page': page, 'maintaining': maintaining, 'group': group, })