def compool_choice(): params = json.loads(request.data) cpool_name = params.get("cpool_name", "") sql_str = "select m_ip,root_passwd from cpool_host where pool_name=%s limit 1;" sql_ret = post_data.sql_query(sql_str, (cpool_name,)) if len(sql_ret) == 0: res = make_result(-1, "满足条件的数据不存在或获取失败") return res # print sql_ret[0][1] node_ip = sql_ret[0][0] root_passwd = sql_ret[0][1] cmd = "/opt/cstech/poolstat.sh" ret = remote_ssh(node_ip, "root", root_passwd, cmd) if ret == -1: res = make_result(-1, "远程获取资源失败") return res data = [] for i in ret: raw = i.strip("\n").split("@") da = { "host_name": raw[0], "instance_cnt": raw[1], "big_page_free": raw[2], "cpu_idle": raw[3] } data.append(da) res = make_result(0, data) return res
def dbpool_add(): """可能会有高级选项,需要执行多条命令""" params = json.loads(request.data) pool_name = params.get("pool_name", "") level = params.get("level", "") min_cnt = params.get("min_cnt", "") max_cnt = params.get("max_cnt", "") sqlstr = "select node_ip,rootpasswd,gi_homepath,gi_name from computepool where pool_name=%s" sqlret = post_data.sql_query(sqlstr, (pool_name, )) if len(sqlret) == 0: res = make_result(-1, "满足要求的数据不存在或获取失败") return res node_ip = sqlret[0][0].split(":")[0] root_passwd = sqlret[0][1] gi_home_path = sqlret[0][2] + "/" + sqlret[0][3] cmd = "%s/bin/srvctl add srvpool -g %s -l %s -u %s -i %s" ret = remote_ssh( node_ip, "root", root_passwd, cmd % (gi_home_path, pool_name, int(min_cnt), int(max_cnt), int(level))) if ret == -1: res = make_result(-1, "远程添加DB资源池失败") return res res = make_result(0, "远程添加DB资源池完成") return res
def computepool_list(): sql_str = """select count(pool_name) from computepool UNION ALL SELECT count(host_id) from cpool_host UNION ALL select count(dbpool_id) from dbpool UNION ALL select count(db_id) from db UNION ALL select count(instance_name) from db""" sql_ret = post_data.sql_query(sql_str) if len(sql_ret) <= 0: res = make_result(0, { "cp_cnt": 0, "dbp_cnt": 0, "db_cnt": 0, "ins_cnt": 0, "host_cnt": 0 }) return res cnt = { "cp_cnt": sql_ret[0][0], "host_cnt": sql_ret[1][0], "dbp_cnt": sql_ret[2][0], "db_cnt": sql_ret[3][0], "ins_cnt": sql_ret[4][0] } sqlstr = "select pool_name,node_ip,rootpasswd, create_time from computepool;" ret = post_data.sql_query(sqlstr) raw = [] for i in ret: data = {} node_ip = i[1] root_passwd = base64.b64decode(i[2]) ret = remote_ssh(node_ip, "root", root_passwd, "/opt/cstech/host_info.sh") if ret == -1: res = make_result(-1, "执行远程脚本获取主机节点资源失败") return res info = get_host_detail(ret) data[i[0]] = info raw.append(data) res = make_result(0, {"cnt": cnt, "dt": raw}) return res
def computepool_srvpool_find(): params = json.loads(request.data) node_ip = params.get("node_ip", "") gi_name = params.get("gi_name", "") root_passwd = params.get("root_passwd", "") root_passwd = base64.b64decode(root_passwd) cmd = "su - %s -c 'srvctl config srvpool'" % gi_name ret = remote_ssh(node_ip, "root", root_passwd, cmd) if ret == -1: res = make_result(-1, "查找srvpool资源池失败") return res if len(ret) < 6: res = make_result(-1, "数据库集群状态异常") return res result = [] for i in range(len(ret) / 3): lines = ret[i * 3:i * 3 + 3] data = {} for idx, j in enumerate(lines): if idx == 0: pool_name = j.split(":")[-1].strip() data["dbpool_name"] = pool_name elif idx == 1: importance, min_v, max_v = [ x.split(":")[-1].strip() for x in j.split(",") ] data["importance"] = importance data["min_v"] = min_v data["max_v"] = max_v else: live_host = j.split(":")[-1].strip() data["live_host"] = live_host result.append(data) res = make_result(0, result) return res
def computepool_host_find(): params = json.loads(request.data) # pool_name = params.get("cpool_name", "") gi_home_path = params.get("gi_home_path", "") ora_home_path = params.get("ora_home_path", "") gi_name = params.get("gi_name", "") ora_name = params.get("ora_name", "") node_ip_port = params.get("node_ip_port", "") # ssh_method = params.get("ssh_method", "") root_passwd = params.get("root_passwd", "") root_passwd = base64.b64decode(root_passwd) node_ip = node_ip_port.split(":")[0] # 检查对应路径是否存在 cmd = "/opt/cstech/sureinfo.sh %s %s %s %s" ret = remote_ssh(node_ip, "root", root_passwd, cmd % (gi_home_path, ora_home_path, gi_name, ora_name)) if ret == -1: res = make_result(-1, "GI_home路径查找失败") return res gz = { "0": "gi_homepath", "1": "ora_homepath", "2": "gi_name", "3": "ora_name" } data = {} for i in ret: state_list = i.strip("\n").split(":") for idx, val in enumerate(state_list): if int(val) == -1: data[gz[idx]] = "not exsit" if len(data) > 0: res = make_result(-1, data) return res cmd = "/" + gi_home_path + "/" + gi_name + "/bin/srvctl config nodeapps -a|grep -v 'Network exists:' | awk '{print $3 $6}'" ret = remote_ssh(node_ip, "root", root_passwd, cmd) if ret == -1: res = make_result(-1, "执行远程脚本获取主机节点资源失败") return res result = [] scan_ip = "" for lines in ret: line = lines.split(",") name = line[-1].strip("\n") vip = line[0].strip("/").split("/")[2] cmd_public = "traceroute -m 1 %s | head -1 | cut -d'(' -f2 | cut -d')' -f1" % name ret = remote_ssh(node_ip, "root", root_passwd, cmd_public) if ret == -1: public_ip = "" else: public_ip = (",".join(ret)).replace("\n", "") m_name = name[:-2] + "adm" + name[len(name) - 2:] cmd_mip = "cat /etc/hosts | grep %s|grep -v 'ilom'|awk '{print $1}'" % m_name ret = remote_ssh(node_ip, "root", root_passwd, cmd_mip) if ret == -1: m_ip = public_ip else: m_ip = ret[0].strip("\n") if scan_ip == "": cmd_scan = "/" + gi_home_path + "/" + gi_name + "/bin/srvctl config scan|grep -v 'SCAN name:'|awk '{print $6}'" ret = remote_ssh(node_ip, "root", root_passwd, cmd_scan) if ret == -1: pass else: for i in ret: scan_ip = scan_ip + i.strip("\n").split("/")[-1] + "," data = {"name": name, "vip": vip, "public_ip": public_ip, "m_ip": m_ip} result.append(data) result.append({"scan_ip": scan_ip.strip(",")}) res = make_result(0, result) return res
def computepool_db_find(): params = json.loads(request.data) node_ip = params.get("node_ip", "") root_passwd = params.get("root_passwd", "") ora_name = params.get("ora_name", "") root_passwd = base64.b64decode(root_passwd) cmd = "su - %s -c '/opt/cstech/db_sga_pga_cpu.sh'" % ora_name ret = remote_ssh(node_ip, "root", root_passwd, cmd) if ret == -1: res = make_result(-1, "查找db资源失败") return res ret = remote_ssh(node_ip, "root", root_passwd, "cat /tmp/db_result.tmp") if ret == -1: res = make_result(-1, "查找db资源失败") return res db_list = ret[0].strip().strip("\n").split() data = [] # ORCL | orcl2 | pga_aggregate_target | 4568645632 # ORCL | orcl2 | sga_target | 13706985472 # ORCL | orcl2 | cpu_count | 0 raw = {"instance_name": ""} for i in ret[1:]: line = i.strip("\n").strip().split("|") if len(line) > 3: if len(raw) >= 5: da = deepcopy(raw) data.append(da) raw.clear() raw["instance_name"] = "" elif raw["instance_name"] != line[1]: raw["instance_name"] = line[1] raw["db_name"] = line[0].lower() if line[2] == "pga_aggregate_target": raw["pga"] = line[3] elif line[2] == "sga_target": raw["sga"] = line[3] else: raw["cpu"] = line[3] if line[0].lower() in db_list: db_list.remove(line[0].lower()) else: line = line[0].split() da = { "db_name": line[0], "instance_name": "N/A", "pga": "N/A", "sga": "N/A", "cpu": "N/A" } data.append(da) if line[0].lower() in db_list: db_list.remove(line[0].lower()) res = make_result(0, [{"data": data, "err_db": db_list}]) return res