diff --git a/views/Makefile b/views/Makefile deleted file mode 100755 index 455d4397c5a5c97b97a6679293e8b89aab986ae8..0000000000000000000000000000000000000000 --- a/views/Makefile +++ /dev/null @@ -1,19 +0,0 @@ -.PHONY:clean -VPATH = math:. - -#CC = gcc -#RM = rm -CFLAGS = -fPIC -CLIBS := -L/usr/lib64/mysql -lmysqlclient -OBJ = check_user_authority.c -OBJS = check_user_authority.o -TARGET = check_user_authority.so - -$(TARGET) :$(OBJS) - g$(CC) -shared -o $@ $^ $(CLIBS) - -$(OBJS) :$(OBJ) - g$(CC) -c $(CFLAGS) $^ -o $@ $(CLIBS) - -clean: - $(RM) $(TARGET) $(OBJS) diff --git a/views/check_user_authority.c b/views/check_user_authority.c deleted file mode 100755 index 630f89f281211a8663e4cde692b0f7febf84d3f9..0000000000000000000000000000000000000000 --- a/views/check_user_authority.c +++ /dev/null @@ -1,200 +0,0 @@ -#include -#include -#include -#include -#include "/usr/include/mysql/mysql.h" -#include "/usr/include/mysql/errmsg.h" -#include "/usr/include/mysql/mysqld_error.h" -#include "sql.h" -#include "r_service.h" -#include "p_check.h" -#include "f_sec.h" -#include "get_databese.h" - -////const char __invoke_dynamic_linker__[] __attribute__ ((section (".interp"))) = "/lib64/ld-linux-x86-64.so.2"; - -#define SSH "ssh" -#define PSW "password" - -int check_user_authority(char *mode); -int perm_check(char *perm_sql); -int restart_ser(char *yum_shell, char *cp_shell, char *s_ser_shell, char *c_ser_shell, char **sys, char **cpu, char **hostname); -int c_Sec(char *check_sql); -int add_knownhost(char *ip); -int free_Sec(char *ip, char *user, char *password); -int get_database(char *name, char **res); -int get_Data(const char* user, const char* password, const char* database, int len, CHECK_DATA *data); -int get_Len(const char* user, const char* password, const char* database, char *sql); - -int check_user_authority(char *mode) -{ - int len = 0,i = 0, num = 0 ;//定义指针存放需要检测的ip - int do_check = 1; - - char *sys; - char *cpu; - char *hostname; - - char *db_user; - char *u_key="DB_USER"; - char *db_database; - char *db_key="DB_NAME"; - char *db_password; - char *pw_key="DB_PASSWORD"; - - int do_sql_res=0;//执行sql语句结果 - int password_ssh = 0;//密码是否正确 - int sec = 0; //查询是否免密 - int free_sec_res = 1; //设置免密结果 - int perm_res = 0; //权限验证结果 - int restart_ser_res; //重启服务结果 - - char *sql; - char *check_sql_1; - char *check_sql_2; - char *yum_shell; - char *c_ssh_shell; - char *perm_shell; - char *cp_shell; - char *s_ser_shell; - char *c_ser_shell; - - //接口输入判断 - if(!(strcmp(mode,SSH)||strcmp(mode,PSW))) - { - return 0; - } - - sql = (char*)malloc(sizeof(char) * 200); - check_sql_1 = (char*)malloc(sizeof(char) * 100); - check_sql_2 = (char*)malloc(sizeof(char) * 100); - c_ssh_shell = (char*)malloc(sizeof(char) * 100); - perm_shell = (char*)malloc(sizeof(char) * 100); - yum_shell = (char*)malloc(sizeof(char) * 100); - cp_shell = (char*)malloc(sizeof(char) * 100); - s_ser_shell = (char*)malloc(sizeof(char) * 100); - c_ser_shell = (char*)malloc(sizeof(char) * 100); - - //获取数据库参数 - get_database(u_key,&db_user); - get_database(pw_key,&db_password); - get_database(db_key,&db_database); - - //查询在线ip数量 - strcpy(sql, "select agent_ip, agent_username from agent_info where agent_online_status = 0"); - len = get_Len(db_user, db_password, db_database, sql); - CHECK_DATA data[len]; - - if(len > 0) - { - //获取在线ip信息 - get_Data(db_user, db_password, db_database, len, &data); - //开始验证 - for( i=0; i -#include - -int free_Sec(char *ip, char *user, char *password); -int add_knownhost(char *ip); -int c_Sec(char *check_sql); - -int c_Sec(char *check_sql) -{ - if(system(check_sql)) - return 1; - return 0; -} - - -int add_knownhost(char *ip) -{ - int add_ip = 0; - char *f_host, *add_host; - - f_host = (char*)malloc(sizeof(char)*100); - add_host = (char*)malloc(sizeof(char)*100); - sprintf(f_host, "cat $HOME/.ssh/known_hosts |grep %s", ip); - sprintf(add_host, "ssh-keyscan %s >> $HOME/.ssh/known_hosts", ip); - - if(system(f_host))//与agent_ip没有连接过 - add_ip = system(add_host);//添加agentip - - free(f_host); - free(add_host); - return add_ip; -} - - -int free_Sec(char *ip, char *user, char *password) -{ - int ssh_key=0; - int add_sec=0; - int free_sec=0; - int ssh_pass=0; - char *f_sec, *f_sec_bak, *t_sec; - - ssh_pass=system("which sshpass"); - if(ssh_pass) - { - return -1; - } - - f_sec_bak = (char*)malloc(sizeof(char)*100); - sprintf(f_sec_bak, "sshpass -p %s ssh-copy-id -f -i $HOME/.ssh/id_rsa.pub %s@%s", password, user, ip); - - if(!system(f_sec_bak)) - { - free(f_sec_bak); - return 0; - } - f_sec = (char*)malloc(sizeof(char)*100); - t_sec = (char*)malloc(sizeof(char)*100); - - sprintf(f_sec, "sshpass -p %s ssh-copy-id -f -i $HOME/.ssh/id_rsa.pub %s@%s", password, user, ip); - sprintf(t_sec, " ssh -tt %s@%s ls ", user, ip); - - ssh_key = system("echo -e 'y\n'|ssh-keygen -t rsa -b 2048 -N '' -f $HOME/.ssh/id_rsa");//强制重新生成密钥 - if(ssh_key) - return ssh_key; - - add_sec = system(f_sec);//设置免密 - free(f_sec); - if(add_sec) - return add_sec; - free_sec=system(t_sec);//验证免密 - free(t_sec); - if(free_sec) - return free_sec; - return 0; -} diff --git a/views/get_databese.h b/views/get_databese.h deleted file mode 100644 index 21a77a1af3f9e2cd42429bb955755df5ca027e1d..0000000000000000000000000000000000000000 --- a/views/get_databese.h +++ /dev/null @@ -1,58 +0,0 @@ -#include -#include -#include - -#define CONF_NAME "/etc/uos-sysmig/uos-sysmig.conf" - -int get_database(char *name, char **res) -{ - FILE *input_file; - unsigned int file_size = 0; //读取文件的字节数 - char line[64]; //接收文件每行 - char *buf; - char *buf0; - int len = 0; - int i = 0, j = 0,flag = 0; - - input_file = fopen(CONF_NAME, "rb"); - if (input_file == NULL) - { - printf("can not find file!\n"); - return -1; - } - while (fgets(line, sizeof(line), input_file)) - { //用fgets函数逐行读取到line - char *pLast = strstr(line, name); //用strrchr查找'='字符最后出现的位置, - if (NULL != pLast) - { - len = strlen(pLast)-strlen(name); - buf = (char *)malloc(sizeof(char) *len); - memcpy(buf,pLast+strlen(name)+1,len); - buf0 = (char *)malloc(sizeof(char) *len); - while(buf[i]!='\0') - { - if(flag==0 && buf[i]=='\"') - { - flag=1; - } - else if(flag==1 && buf[i]=='\"') - { - buf0[j]='\0'; - *res = (char*)malloc(sizeof(char) * j); - memcpy(*res,buf0,j); - free(buf0); - } - else if(flag==1 && buf[i]!='\"') - { - buf0[j++]=buf[i]; - } - i++; - } - } - - } - - free(buf); - fclose(input_file); - return 0; -} diff --git a/views/migration.py b/views/migration.py deleted file mode 100644 index 64471fd2090fc67572c4ac3b7781d15b8622948b..0000000000000000000000000000000000000000 --- a/views/migration.py +++ /dev/null @@ -1,126 +0,0 @@ -# -*- coding: utf-8 -*- -# !/usr/bin/python -from connect_sql import * -from logger import * -from interaction import * -import json - - -os.chdir('/usr/lib/uos-sysmig-server') -migration_log = Logger('/var/tmp/uos-migration/migration.log', logging.DEBUG, logging.DEBUG) - - -def send_task_to_agent(data, url, ip): - """ - 向agent下发任务 - :param data: - :param url: - :param ip: - :return: - """ - log_info = "post " + url + ":" + str(data) - migration_log.info(log_info) - info = splice_url(data, url, ip) - if info.status_code == 200: - migration_log.info(info.text) - elif info is None or info.status_code == 404: - sql = "update agent_info set agent_online_status=1 where agent_ip ='%s';" % ip - DBHelper().execute(sql) - migration_log.error("%s:请求失败,agent端没有启动" % ip) - elif info.status_code != 404 and info.status_code != 200: - sql = "update agent_info set agent_online_status=1 where agent_ip ='%s';" % ip - DBHelper().execute(sql) - migration_log.error("%s:请求失败,错误状态码为%s" % (ip, info.status_code)) - - -def get_agent_ip(data, sql, url): - """ - 获取agent_ip地址 - :return: - """ - agent_info = DBHelper().execute(sql).fetchall() - data = json.loads(data) - for i in agent_info: - task_status_sql = "select task_status from agent_task where agent_ip='%s'" % list(i)[0] - task_status = DBHelper().execute(task_status_sql).fetchall()[0][0] - if task_status == 0 or task_status == 2: - update_sql = "update agent_task set task_progress=0,task_status=1 where agent_ip='%s'" % list(i)[0] - DBHelper().execute(update_sql) - get_task_id_sql = "select task_id from cur_task where agent_ip='%s'" % list(i)[0] - task_id = DBHelper().execute(get_task_id_sql).fetchall() - data['task_id'] = task_id[0][0] - json_data = json.dumps(data) - send_task_to_agent(json_data, url, list(i)[0]) - - -def check_info(data): - """ - 检测系统版本和空间大小 - :param data: - :return: - """ - sql = "select agent_ip from agent_info where agent_online_status = 0;" - get_agent_ip(data, sql, '/check_info') - return 'success' - - -def check_repo(data): - """ - 检测平台软件仓库 - :param data: - :return: - """ - sql = "select agent_ip from agent_info where agent_online_status = 0 and agent_storage >= 10;" - agent_ip_list = DBHelper().execute(sql) - for i in agent_ip_list: - repo_status_sql = "update agent_info set repo_status='2' where agent_ip='%s'" % list(i)[0] - DBHelper().execute(repo_status_sql) - get_agent_ip(data, sql, '/check_repo') - return 'success' - - -def check_kernel(data): - """ - 检测agent内核版本和软件仓库内核版本 - :param data: - :return: - """ - sql = "select agent_ip from agent_info where agent_online_status=0 and agent_storage>=10 and repo_status=0;" - get_agent_ip(data, sql, '/check_kernel') - return 'success' - - -def check_environment(data): - """ - agent迁移前环境检查任务 - :param data: - :return: - """ - agent_ip_list = json.loads(data).get('agent_ip') - data = json.loads(data) - url = '/check_environment' - for i in agent_ip_list: - get_task_id_sql = "select task_id from cur_task where agent_ip='%s'" % i - task_id = DBHelper().execute(get_task_id_sql).fetchall() - data['task_id'] = task_id[0][0] - json_data = json.dumps(data) - send_task_to_agent(json_data, url, i) - return 'success' - - -def system_migration(data): - """ - agent系统迁移 - :param data: - :return: - """ - agent_ip_list = json.loads(data).get('info') - data = json.loads(data) - url = '/system_migration' - for i in agent_ip_list: - get_task_id_sql = "select task_id from cur_task where agent_ip='%s'" % i.get('agent_ip') - task_id = DBHelper().execute(get_task_id_sql).fetchall() - data['task_id'] = task_id[0][0] - json_data = json.dumps(data) - send_task_to_agent(json_data, url, i.get('agent_ip')) - return 'success' diff --git a/views/p_check.h b/views/p_check.h deleted file mode 100644 index 204651223c01ab27534444f18c95fcfce1b8b43e..0000000000000000000000000000000000000000 --- a/views/p_check.h +++ /dev/null @@ -1,13 +0,0 @@ -int perm_check(char *perm_sql); - -int perm_check(char *perm_sql) -{ - int che_res=0; - - che_res=system(perm_sql); - if(che_res) - { - return che_res; - } - return 0; -} diff --git a/views/r_service.h b/views/r_service.h deleted file mode 100644 index 03b3c873ba0648eedc543cdf28a7aeed548648f0..0000000000000000000000000000000000000000 --- a/views/r_service.h +++ /dev/null @@ -1,52 +0,0 @@ -#include -#include -#include -#include -#include - -#define SYS_NAME "/etc/redhat-release" -#define PACK_NAME "uos-sysmig-agent" - -int restart_ser(char *yum_shell, char *cp_shell, char *s_ser_shell, char *c_ser_shell, char **sys, char **cpu, char **hostname); - -int restart_ser(char *yum_shell, char *cp_shell, char *s_ser_shell, char *c_ser_shell, char **sys, char **cpu, char **hostname) -{ - int yum_res=0; - int cp_conf=0; - int s_start=0; - FILE *f; - long len; - struct utsname buf; - - if(uname(&buf)) - { - perror("uname"); - exit(1); - } - *cpu=(char*)malloc((sizeof(char))*strlen(buf.machine)); - strcpy(*cpu, buf.machine); - - *hostname=(char*)malloc((sizeof(char))*100); - gethostname(*hostname, 100); - - f=fopen(SYS_NAME,"rb"); - fseek(f,0,SEEK_END); - len=ftell(f); - fseek(f,0,SEEK_SET); - *sys=(char*)malloc((sizeof(char))*(len)); - fread(*sys,1,len-1,f); - fclose(f); - - yum_res=system(yum_shell); //yum install -y uos-sysmig-agent - if(yum_res) - return 4;//4用于在数据库中标志yum错误 - cp_conf=system(cp_shell); - if(cp_conf) - return cp_conf; - - system(s_ser_shell); - s_start=system(c_ser_shell); - if(s_start) - return 5;//用于在数据库中标志restart服务重启失败 - return 0; -} diff --git a/views/reports.py b/views/reports.py deleted file mode 100644 index c667f7c311a492d63b5a61469b7f187659afb6fe..0000000000000000000000000000000000000000 --- a/views/reports.py +++ /dev/null @@ -1,121 +0,0 @@ -import os -from logger import * -from connect_sql import DBHelper -import pandas as pd -from datetime import datetime - - -os.chdir('/usr/lib/uos-sysmig-server') -migration_log = Logger('/var/tmp/uos-migration/migration.log', logging.DEBUG, logging.DEBUG) - - -def migration_detection(data): - """ - 迁移检测报告 - :return: - """ - agent_ip = data.get('agent_ip') - info_sql = "select AES_DECRYPT(agent_passwd, 'coco'),agent_username from agent_info where agent_ip='%s'" % agent_ip - info = DBHelper().execute(info_sql).fetchall() - scp_log = "sshpass -p %s scp -r %s@%s:/var/tmp/uos-migration/UOS_analysis_report*.tar.gz /var/uos-migration/" % \ - (str(info[0][0], encoding="utf-8"), info[0][1], agent_ip) - try: - os.system(scp_log) - migration_log.info(scp_log) - except: - migration_log.error('export report scp error:%s' % scp_log) - return 'success' - - -def migration_logs(data): - """ - 迁移日志 - :return: - """ - agent_ip = data.get('agent_ip') - info_sql = "select AES_DECRYPT(agent_passwd, 'coco'),agent_username from agent_info where agent_ip='%s'" % agent_ip - info = DBHelper().execute(info_sql).fetchall() - scp_log = "sshpass -p %s scp -r %s@%s:/var/tmp/uos-migration/UOS_migration_log*.tar.gz /var/uos-migration/" % \ - (str(info[0][0], encoding="utf-8"), info[0][1], agent_ip) - try: - os.system(scp_log) - migration_log.info(scp_log) - except: - migration_log.error('export report scp error:%s' % scp_log) - return 'success' - - -def migration_analysis_report(data): - """ - 迁移分析报告 - :return: - """ - agent_ip = data.get('agent_ip') - info_sql = "select AES_DECRYPT(agent_passwd, 'coco'),agent_username from agent_info where agent_ip='%s'" % agent_ip - info = DBHelper().execute(info_sql).fetchall() - scp_log = "sshpass -p %s scp -r %s@%s:/var/tmp/uos-migration/UOS_migration_completed_report*.tar.gz /var/uos-migration/" % \ - (str(info[0][0], encoding="utf-8"), info[0][1], agent_ip) - try: - os.system(scp_log) - migration_log.info(scp_log) - except: - migration_log.error('export report scp error:%s' % scp_log) - return 'success' - - -def export_host_info(data): - """ - 主机列表 - :return: - """ - sql = "select agent_ip,hostname,agent_online_status,agent_os,agent_arch," \ - "agent_history_faild_reason from agent_info;" - data = DBHelper().execute(sql).fetchall() - data = list(data) - for i in range(0, len(data)): - agent_task = "select task_CreateTime,task_status from agent_task where agent_ip='%s';" % data[i][0] - get_agent_task = DBHelper().execute(agent_task) - data[i] = list(data[i]) - get_agent_task = list(get_agent_task) - if get_agent_task == []: - data[i] += ["", ""] - else: - task_CreateTime = get_agent_task[0][0].strftime('%Y-%-m-%d %H:%M:%S') - task_status = get_agent_task[0][1] - data[i].append(task_CreateTime) - data[i].append(task_status) - df = pd.DataFrame(data) - df.columns = ['主机IP', '主机名', '在线状态', '操作系统类型', '架构', '历史失败原因', '迁移时间', '迁移状态'] - time = datetime.now().strftime('%Y-%-m-%d %H:%M:%S') - xls = "/var/uos-migration/主机列表_%s.xls" % time - df.to_excel(xls) - return 'success' - - -def migration_success_list(data): - """ - 迁移成功主机列表 - :param data: - :return: - """ - sql = "select agent_ip,hostname,agent_os,agent_migration_os,agent_arch from agent_info " \ - "where agent_migration_os is not null;" - data = DBHelper().execute(sql).fetchall() - data = list(data) - for i in range(0, len(data)): - data[i] = list(data[i]) - task_update_time_sql = "select task_Updatetime from agent_task where agent_ip='%s'" % data[i][0] - get_task_update_time = DBHelper().execute(task_update_time_sql).fetchall() - get_task_update_time = list(get_task_update_time) - if get_task_update_time == []: - data[i] += [""] - else: - task_Updatetime = get_task_update_time[0][0].strftime('%Y-%-m-%d %H:%M:%S') - data[i].append(task_Updatetime) - df = pd.DataFrame(data) - df.columns = ['主机ip', '主机名', '迁移前OS版本', '迁移后OS版本', '架构', '迁移时间'] - time = datetime.now().strftime('%Y-%-m-%d %H:%M:%S') - xls = "/var/uos-migration/迁移成功主机列表_%s.xls" % time - df.to_excel(xls) - return 'success' - diff --git a/views/server.py b/views/server.py deleted file mode 100644 index a389b38f63c1c1236fca251fc1f987d73d17d8c2..0000000000000000000000000000000000000000 --- a/views/server.py +++ /dev/null @@ -1,619 +0,0 @@ -# -*- coding: utf-8 -*- -# !/usr/bin/python -from connect_sql import * -from ctypes import * -from logger import * -from datetime import datetime -from views.reports import migration_logs, migration_detection, \ - migration_analysis_report, export_host_info, migration_success_list -from sysmig_agent.share import getSysMigConf -from flask import request -import json -import paramiko -import re - - -os.chdir('/usr/lib/uos-sysmig-server') -migration_log = Logger('/var/tmp/uos-migration/migration.log', logging.DEBUG, logging.DEBUG) - - -def import_host_info(data): - """ - 导入agent主机信息 - :param data: - :return: - """ - agent_info = json.loads(data).get("data") - if agent_info == []: - data = {"data": "faild"} - json_data = json.dumps(data) - return json_data - - sql = "insert into agent_info(agent_ip, agent_username, agent_passwd) values (%s, %s, AES_ENCRYPT(%s, 'coco'));" - for i in agent_info: - agent_ip = i.get('agent_ip') - agent_username = i.get('agent_hostname') - agent_passwd = i.get('agent_password') - val = ((agent_ip, agent_username, agent_passwd),) - info = DBHelper().insert(sql, val) - create_task_stream(agent_ip) - - time = datetime.now().strftime('%Y-%-m-%d %H:%M:%S') - uos_sysmig_conf = json.loads(getSysMigConf()) - ip = json.loads(uos_sysmig_conf).get('serverip').strip()[1:-1] - host_report_sql = "insert into report_info(agent_ip,create_time,report_name,report_type) values (%s, %s, %s, %s);" - host_report_sql_val = ((ip, time, '迁移主机列表_%s' % time, '主机列表'),) - DBHelper().insert(host_report_sql, host_report_sql_val) - data = check_user(data) - data_json = json.dumps(data) - return data_json - - -def get_agent_id(agent_ip): - """ - 获取agent_id - :param agent_ip: - :return: - """ - sql = "select agent_id from agent_info where agent_ip='%s'" % agent_ip - get_agent_id = DBHelper().execute(sql).fetchall() - return get_agent_id[0][0] - - -def create_task_stream(agent_ip): - """ - 创建任务流 - :return: - """ - create_task_stream_sql = "insert into task_stream(agent_ip,agent_id,stream_status," \ - "stream_CreateTime,stream_Updatetime) values (%s, %s, %s, %s, %s);" - - stream_status = 'None' - agent_id = get_agent_id(agent_ip) - time = datetime.now().strftime('%Y-%-m-%d %H:%M:%S') - values = ((agent_ip, agent_id, stream_status, time, time),) - DBHelper().insert(create_task_stream_sql, values) - - create_cur_task_sql = "insert into cur_task(task_status,stream_CreateTime,stream_Updatetime," \ - "agent_ip) values (%s, %s, %s, %s);" - values = (('None', time, time, agent_ip),) - DBHelper().insert(create_cur_task_sql, values) - - get_task_id = "select max(task_id) task_id from cur_task" - task_id = DBHelper().execute(get_task_id).fetchone() - task_stream_id_sql = "select task_stream_id from task_stream where agent_ip='%s'" % agent_ip - task_stream_id = DBHelper().execute(task_stream_id_sql).fetchone() - values = ((agent_id, agent_ip, task_id[0], 0, 0, time, time, task_stream_id[0], "00"),) - create_agent_task_sql = "insert into agent_task(agent_id,agent_ip,task_id,task_status,task_progress," \ - "task_CreateTime,task_Updatetime,task_stream_id,task_data) values " \ - "(%s, %s, %s, %s, %s, %s, %s, %s, %s);" - DBHelper().insert(create_agent_task_sql, values) - - -def check_user(data): - """ - 检测账户权限 - :return: - """ - check_type = json.loads.get('type') - check_user_res = CDLL('./check_user_authority.so') - data = check_user_res.check_user_authority(check_type.encode()) - if data == 0: - data = {"data": "faild", "num": 0} - else: - data = {"data": "success", "num": data} - json_data = json.dumps(data) - return jaon_data - - -def update_agent_online_status(data): - """ - 修改agent状态 - :param data: - :return: - """ - sql = "update agent_info set agent_online_status = 1 where agent_ip = '%s';" % data - DBHelper().execute(sql) - - -def host_info_display(data): - """ - 显示主机信息 - agent_ip,hostname,agent_online_status,agent_os,agent_arch, - agent_history_faild_reason,task_CreateTime,task_status - :return: - """ - page = json.loads(data).get('page') - size = json.loads(data).get('size') - sql = "select agent_ip,hostname,agent_online_status,agent_os,agent_arch," \ - "agent_history_faild_reason from agent_info;" - data = DBHelper().execute(sql).fetchall() - data = list(data) - for i in range(0, len(data)): - data[i] = list(data[i]) - agent_task = "select task_CreateTime,task_data from agent_task where agent_ip = '%s';" % data[i][0] - get_agent_task = DBHelper().execute(agent_task).fetchall() - get_agent_task = list(get_agent_task) - - if get_agent_task == []: - data[i] += ["", ""] - else: - task_CreateTime = get_agent_task[0][0].strftime('%Y-%-m-%d %H:%M:%S') - task_status = get_agent_task[0][1] - data[i].append(task_CreateTime) - data[i].append(task_status) - - res = {} - res['num'] = len(data) - info_list = [] - info_dict_keys_list = ['agent_ip', 'hostname', 'agent_online_status', 'agent_os', 'agent_arch', - 'failure_reasons', 'task_CreateTime', 'task_status'] - for i in data: - info_list.append(dict(zip(info_dict_keys_list, i))) - - page_list = pagebreak(info_list, page, size) - res['info'] = page_list - res['page'] = page - res['size'] = size - - json_res = json.dumps(res) - return json_res - - -def delete_host_info(data): - """ - 删除主机信息 - :return: - """ - data = json.loads(data) - for i in data.get('agent_ip'): - sql = "delete from agent_info where agent_ip='%s';" % i - DBHelper().execute(sql) - sql = "delete from agent_task where agent_ip='%s';" % i - DBHelper().execute(sql) - sql = "delete from task_stream where agent_ip='%s';" % i - DBHelper().execute(sql) - sql = "delete from cur_task where agent_ip='%s';" % i - DBHelper().execute(sql) - - res = {'data': 'success'} - json_res = json.dumps(res) - return json_res - - -def get_page_data(data): - """ - 定时获取可用空间页面数据 - agent_ip,hostname,agent_online_status,agent_os,agent_storage,agent_arch,task_CreateTime - :return: - """ - page = json.loads(data).get('page') - size = json.loads(data).get('size') - sql = "select agent_ip,hostname,agent_online_status,agent_os,agent_storage,agent_arch,agent_id from " \ - "agent_info where agent_online_status = 0 and agent_migration_os is null;" - data = DBHelper().execute(sql).fetchall() - data = list(data) - for i in range(0, len(data)): - data[i] = list(data[i]) - data[i][4] = str(data[i][4]) + 'GB' - task_CreateTime = "select task_CreateTime from agent_task where agent_ip = '%s';" % data[i][0] - get_task_CreateTime = DBHelper().execute(task_CreateTime).fetchall() - get_task_CreateTime = list(get_task_CreateTime) - if get_task_CreateTime == []: - data[i] += [""] - else: - task_Createtime = get_task_CreateTime[0][0].strftime('%Y-%-m-%d %H:%M:%S') - data[i].append(task_Createtime) - - res = {} - res['num'] = len(data) - info_list = [] - info_dict_keys_list = ['agent_ip', 'hostname', 'agent_online_status', 'agent_os', 'agent_storage', - 'agent_arch', 'agent_id', 'task_CreateTime'] - for i in data: - info_list.append(dict(zip(info_dict_keys_list, i))) - - res['info'] = info_list - - json_res = json.dumps(res) - return json_res - - -def get_repo_data(data): - """ - 定时检查软件仓库检测结果 - :return: - """ - task_status_sql = "select agent_id from agent_task where task_status=2" - get_task_status = DBHelper().execute(task_status_sql).fetchall() - if len(get_task_status) == 0: - data = {"centos7_x86": "", "centos8_x86": "", "centos7_aarch64": "", "centos8_aarch64": ""} - json_data = json.dumps(data) - return json_data - else: - centos7_x86_sql = "select agent_ip from agent_info where agent_os='centos7' " \ - "and agent_arch='x86_64' and repo_status=1;" - - centos8_x86_sql = "select agent_ip from agent_info where agent_os='centos8' " \ - "and agent_arch='x86_64' and repo_status=1;" - - centos7_aarch64_sql = "select agent_ip from agent_info where agent_os='centos7' " \ - "and agent_arch='aarch64' and repo_status=1;" - - centos8_aarch64_sql = "select agent_ip from agent_info where agent_os='centos8' " \ - "and agent_arch='aarch64' and repo_status=1;" - - data = {} - get_centos7_x86_status = DBHelper().execute(centos7_x86_sql).fetchall() - if len(get_centos7_x86_status) == 0: - data['centos7_x86'] = 'success' - else: - data['centos7_x86'] = 'faild' - - get_centos8_x86_status = DBHelper().execute(centos8_x86_sql).fetchall() - if len(get_centos8_x86_status) == 0: - data['centos8_x86'] = 'success' - else: - data['centos8_x86'] = 'faild' - - get_centos7_aarch64_status = DBHelper().execute(centos7_aarch64_sql).fetchall() - if len(get_centos7_aarch64_status) == 0: - data['centos7_aarch64'] = 'success' - else: - data['centos7_aarch64'] = 'faild' - - get_centos8_aarch64_status = DBHelper().execute(centos8_aarch64_sql).fetchall() - if len(get_centos8_aarch64_status) == 0: - data['centos8_aarch64'] = 'success' - else: - data['centos8_aarch64'] = 'faild' - - json_data = json.dumps(data) - return json_data - - -def modify_task_stream(data): - """ - 修改任务流状态 - :return: - """ - task_id = json.loads(data).get('task_id') - get_task_status_sql = "select task_status,task_stream_id from agent_task where task_id='%s';" % task_id - info = DBHelper().execute(get_task_status_sql).fetchone() - task_status = info[0] - task_stream_id = info[1] - if task_status == 0: - task_status = 'None' - elif task_status == 1: - task_status = 'Doing' - elif task_status == 2: - task_status = 'Done' - else: - task_status = 'Cancel' - time = datetime.now() - modify_task_status_sql = "update cur_task set task_status='%s',stream_Updatetime='%s' " \ - "where task_id ='%s';" % (task_status, time, task_id) - DBHelper().execute(modify_task_status_sql) - - modify_task_stream_sql = "update task_stream set stream_status='%s',stream_Updatetime='%s' " \ - "where task_stream_id='%s';" % (task_status, time, task_stream_id) - DBHelper().execute(modify_task_stream_sql) - return 'success' - - -def get_kernel_data(data): - """ - 获取系统内核和仓库内核版本 - :return: - """ - page = json.loads(data).get('page') - size = json.loads(data).get('size') - get_kernel_version_sql = 'select agent_ip,agent_kernel,agent_repo_kernel from agent_info where ' \ - 'agent_online_status=0 and repo_status=0 and agent_storage>=10 and ' \ - 'agent_migration_os is null;' - data = DBHelper().execute(get_kernel_version_sql).fetchall() - res = {} - info_list = [] - info_dict_keys_list = ['agent_ip', 'agent_kernel', 'agent_repo_kernel'] - if len(data) != 0: - for i in data: - if i[1] and i[2]: - kernel_arr = ('不迁移内核' + ',' + i[2]).split(',') - kernel_list = list(i) - kernel_list[2] = kernel_arr - else: - kernel_list = [list(i)[0], '', ''] - info_list.append(dict(zip(info_dict_keys_list, kernel_list))) - - res['info'] = info_list - res['num'] = len(info_list) - - json_res = json.dumps(res) - return json_res - - -def get_environment_data(data): - """ - 获取环境检查进度 - :return: - """ - page = json.loads(data).get('page') - size = json.loads(data).get('size') - get_environment_pro_sql = "select agent_ip,task_progress,task_status from agent_task;" - progress = DBHelper().execute(get_environment_pro_sql).fetchall() - res = {} - info_list = [] - finall_progress = [] - for i in progress: - sql = "select agent_id from agent_info where agent_ip='%s' and agent_online_status=0 and repo_status=0 " \ - "and agent_storage>=10 and agent_migration_os is null;" % i[0] - get_sql = DBHelper().execute(sql).fetchall() - if get_sql: - finall_progress.append(list(i)) - - info_dict_keys_list = ['agent_ip', 'task_progress', 'task_status'] - for i in finall_progress: - info_list.append(dict(zip(info_dict_keys_list, list(i)))) - - res['info'] = info_list - res['num'] = len(finall_progress) - - json_res = json.dumps(res) - return json_res - - -def get_system_migration_data(data): - """ - 获取agent迁移进度 - :return: - """ - page = json.loads(data).get('page') - size = json.loads(data).get('size') - get_migration_pro_sql = "select agent_ip,task_progress,task_status from agent_task;" - progress = DBHelper().execute(get_migration_pro_sql).fetchall() - res = {} - info_list = [] - finall_progress = [] - for i in progress: - sql = "select agent_id from agent_info where agent_ip='%s' and agent_online_status=0 " \ - "and repo_status=0 and agent_storage>=10;" % i[0] - get_sql = DBHelper().execute(sql).fetchall() - if get_sql: - finall_progress.append(i) - - info_dict_keys_list = ['agent_ip', 'task_progress', 'task_status'] - for i in finall_progress: - info_list.append(dict(zip(info_dict_keys_list, list(i)))) - - res['info'] = info_list - res['num'] = len(finall_progress) - - json_res = json.dumps(res) - return json_res - - -def get_download_center_data(data): - """ - 获取下载中心数据 - :return: - """ - page = json.loads(data).get('page') - size = json.loads(data).get('size') - download_center_data_sql = "select * from report_info;" - info = DBHelper().execute(download_center_data_sql).fetchall() - info = list(info) - res = {} - res['num'] = len(info) - info_list = [] - info_dict_keys_list = ['report_generation_time', 'report_name', 'report_type', 'agent_ip', - 'hostname', 'agent_os', 'agent_arch'] - for i in range(0, len(info)): - info[i] = list(info[i]) - info[i][0] = info[i][0].strftime('%Y-%-m-%d %H:%M:%S') - agent_ip = info[i][3] - agent_info_sql = "select hostname,agent_os,agent_arch from agent_info where agent_ip='%s';" % agent_ip - agent_info = DBHelper().execute(agent_info_sql).fetchall() - if not agent_info: - info[i] += ["", "", ""] - else: - agent_info = list(agent_info[0]) - info[i] += agent_info - for i in info: - info_list.append(dict(zip(info_dict_keys_list, i))) - - page_list = pagebreak(info_list, page, size) - res['info'] = page_list - res['page'] = page - res['size'] = size - - json_res = json.dumps(res) - return json_res - - -reports_type = { - "migration_detection": migration_detection, - "migration_logs": migration_logs, - "migration_analysis_report": migration_analysis_report, - "export_host_info": export_host_info, - "migration_success_list": migration_success_list, -} - - -def export_reports(data): - """ - 导出各种报告 - :param data: - :return: - """ - data = json.loads(data) - report_type = reports_type.get(data.get('reports_type')) - if report_type: - mkdir_log_pwd = "/var/uos-migration/" - isExists = os.path.exists(mkdir_log_pwd) - if not isExists: - try: - os.makedirs(mkdir_log_pwd) - migration_log.info(mkdir_log_pwd) - except: - migration_log.war("export report mkdir war:%s" % mkdir_log_pwd) - - report_type(data) - return 'success' - - -def migration_records(data): - """ - 获取迁移成功记录数据 - :return: - """ - page = json.loads(data).get('page') - size = json.loads(data).get('size') - sql = "select agent_ip,hostname,agent_os,agent_migration_os,agent_arch from agent_info " \ - "where agent_migration_os is not null;" - data = DBHelper().execute(sql).fetchall() - data = list(data) - for i in range(0, len(data)): - data[i] = list(data[i]) - task_update_time_sql = "select task_Updatetime from agent_task where agent_ip='%s'" % data[i][0] - get_task_update_time = DBHelper().execute(task_update_time_sql).fetchall() - get_task_update_time = list(get_task_update_time) - if get_task_update_time == []: - data[i] += [""] - else: - get_task_update_time = get_task_update_time[0][0].strftime('%Y-%-m-%d %H:%M:%S') - data[i].append(get_task_update_time) - - res = {} - res['num'] = len(data) - info_list = [] - info_dict_keys_list = ['agent_ip', 'hostname', 'agent_os', 'agent_migration_os', 'agent_arch', 'create_time'] - for i in data: - info_list.append(dict(zip(info_dict_keys_list, i))) - - page_list = pagebreak(info_list, page, size) - res['info'] = page_list - res['page'] = page - res['size'] = size - - json_res = json.dumps(res) - return json_res - - -def get_migrated_hosts(data): - """ - 获取迁移主机列表数据 - :param data: - :return: - """ - page = json.loads(data).get('page') - size = json.loads(data).get('size') - sql = "select agent_ip,agent_id,hostname,agent_online_status,agent_os,agent_arch," \ - "agent_history_faild_reason from agent_info where agent_online_status='0' and agent_migration_os is null;" - data = DBHelper().execute(sql).fetchall() - data = list(data) - finall_data = [] - for i in range(0, len(data)): - data[i] = list(data[i]) - agent_task = "select task_CreateTime,task_data from agent_task where agent_ip='%s';" % data[i][0] - get_agent_task = DBHelper().execute(agent_task).fetchall() - get_agent_task = list(get_agent_task) - if get_agent_task == []: - pass - else: - task_CreateTime = get_agent_task[0][0].strftime('%Y-%-m-%d %H:%M:%S') - task_status = get_agent_task[0][1] - data[i].append(task_CreateTime) - data[i].append(task_status) - finall_data.append(data[i]) - res = {} - res['num'] = len(finall_data) - info_list = [] - info_dict_keys_list = ['agent_ip', 'agent_id', 'hostname', 'agent_online_status', 'agent_os', 'agent_arch', - 'failure_reasons', 'task_CreateTime', 'task_status'] - for i in finall_data: - info_list.append(dict(zip(info_dict_keys_list, i))) - - page_list = pagebreak(info_list, page, size) - res['info'] = page_list - res['page'] = page - res['size'] = size - - json_res = json.dumps(res) - return json_res - - -def get_storage_num(data): - """ - 获取可用空间足够和不足数量 - :param data: - :return: - """ - success_num_sql = "select agent_ip from agent_info where agent_online_status='0' and agent_storage>='10' " \ - "and agent_migration_os is null;" - get_success_num = DBHelper().execute(success_num_sql).fetchall() - - faild_num_sql = "select agent_ip from agent_info where agent_online_status='0' and agent_storage<'10' " \ - "and agent_migration_os is null;" - get_faild_num = DBHelper().execute(faild_num_sql).fetchall() - - success = len(get_success_num) - faild = len(get_faild_num) - data = {'success': success, 'faild': faild} - json_data = json.dumps(data) - return json_data - - -def pagebreak(data, page, size): - """ - 页面数据分页 - :param data: - :return: - """ - page_start = (page - 1) * size - page_end = page * size - result = data[page_start:page_end] - - return result - - -def get_repo_arch_info(data): - """ - 获取软件仓库架构和系统信息 - :param data: - :return: - """ - sql = "select agent_os,agent_arch from agent_info where agent_online_status='0' and agent_storage>='10' " \ - "and agent_migration_os is null;" - get_info = DBHelper().execute(sql).fetchall() - - info_list = [] - info_dict_keys_list = ['agent_os', 'agent_arch'] - for i in get_info: - info_list.append(dict(zip(info_dict_keys_list, i))) - - res = {} - res['info'] = info_list - json_res = json.dumps(res) - return json_res - - -def close_tool(data): - """ - 关闭迁移软件 - :param data: - :return: - """ - os.system('kill -9 `ps -ef | grep "start_webview.py" | grep -v grep | awk \'{print $2}\'`') - data = {"data": "success"} - data_json = json.dumps(data) - return data_json - - -def modify_task_status(data): - """ - 修改任务状态 - :return: - """ - update_sql = "update agent_task set task_progress=0,task_status=0" - DBHelper().execute(update_sql) - return 'success' diff --git a/views/sql.h b/views/sql.h deleted file mode 100644 index 8d3312d332b280fa157129f22d80ca9567d27652..0000000000000000000000000000000000000000 --- a/views/sql.h +++ /dev/null @@ -1,126 +0,0 @@ -#define SYS “"/etc/redhat-release" -typedef struct CHECK_DATA -{ - char *ip; - char *user; - char *password; -}CHECK_DATA; - -MYSQL conn; -MYSQL_RES *res_ptr; -MYSQL_ROW sqlrow; - -void connection(const char* db_user, const char* db_password, const char* db_database); -int get_Data(const char* db_user, const char* db_password, const char* db_database, int len, CHECK_DATA *data); -int get_Len(const char* db_user, const char* db_password, const char* db_database, char *sql); - -//返回值为负数,执行Sql语句失败 -//当执行的为select之类的会有返回数据的sql语句,返回值为获取数据的行数 -//当中的为无返回数据的sql语句,如alter、updata等,执行成功返回0 -int get_Len(const char* db_user, const char* db_password, const char* db_database, char *sql) -{ - int len = -1, res = 0; - connection(db_user, db_password, db_database); - res=mysql_query(&conn, sql); - if(res) - { - mysql_close(&conn); - return -1; - } - if( mysql_affected_rows( &conn ) == 0 ) //执行sql语句改变的行数,0无任何变动,即未达到期望结果,执行失败;大于0执行成功;为-1时,执行的为查询命令 - { - mysql_close(&conn); - return -1; - } - if( mysql_affected_rows( &conn ) == -1 ) - { - res_ptr = mysql_store_result(&conn); //获取到的数据 - if(!res_ptr) - { - mysql_close(&conn); - return -1; - } - len = (unsigned long)mysql_num_rows(res_ptr); //数据中包括的行数 - } - else - { - mysql_close(&conn); - return 0; - } - mysql_close(&conn); - return len; -} - -int get_Data(const char* db_user, const char* db_password, const char* db_database, int len, CHECK_DATA *data) -{ - int i = 0, k = 0, res = 0, len0 = 0; - - - connection(db_user, db_password, db_database); - res = mysql_query(&conn, "select agent_ip, agent_username, AES_DECRYPT(agent_passwd, 'coco') from agent_info where agent_online_status = 0"); - if(res)//执行sql语句 - { - len=0; - } - else - { - res_ptr = mysql_store_result(&conn); - len0 = (unsigned long)mysql_num_rows(res_ptr);//获取数据库数据行数 - } - - if(len != len0) - len=len0=0; - else - { - - for( i=0; i