def generate_conf(host, user, password, port, data_path, mysql_base, mysql_home, extra_params): """ :param conf_dict, a dict :return: """ mycnf = 'my.cnf_new' conf_tmpl = read_cnf_template() conf_dict = gen_conf_for_instance(host, port, data_path, mysql_base) if not conf_tmpl.has_section('mysqld'): conf_tmpl.add_section('mysqld') for param_name, param_value in conf_dict.items(): conf_tmpl.set('mysqld', param_name, param_value) for param in extra_params: param_name = param['param_name'] param_value = param['param_value'] conf_tmpl.set('mysqld', param_name, param_value) with open(mycnf, 'w') as cnf: conf_tmpl.write(cnf) tools.my_log(log_type, '生成配置文件%s!' % mycnf, '') sftp_upload_file(host, user, password, mycnf, '%s/my.cnf' % mysql_home) tools.my_log(log_type, '上传配置文件至%s/my.cnf!' % mysql_home, '')
def mysql_startup(host, user, password): log_type = '启动Mysql数据库' tools.mysql_exec("delete from many_logs where log_type = '启动Mysql数据库'", '') # 上传脚本 local_file = os.getcwd() + '/frame/mysql_tools/mysql_startup.sh' sftp_upload_file(host, user, password, '/tmp/mysql_startup.sh', local_file) # 执行命令 cmd = 'sh /tmp/mysql_startup.sh > /tmp/mysql_startup.log' exec_command(host, user, password, cmd) tools.my_log(log_type, 'mysql_startup.sh,Mysql数据库启动成功!', '')
def mysql_shutdown(host, user, password, ssh_port): log_type = '关闭Mysql数据库' tools.mysql_exec("delete from many_logs where log_type = '关闭Mysql数据库'", '') # 上传脚本 local_file = os.getcwd() + '/frame/mysql_tools/mysql_shutdown.sh' sftp_upload_file(host, user, password, ssh_port, '/tmp/mysql_shutdown.sh', local_file) # 执行命令 cmd = 'sh /tmp/mysql_shutdown.sh > /tmp/mysql_shutdown.log' exec_command(host, user, password, ssh_port, cmd) tools.my_log(log_type, '执行mysql_shutdown.sh,Mysql数据库关闭成功!', '')
def oracle_startup(host, user, password): log_type = '启动Oracle数据库' tools.mysql_exec("delete from many_logs where log_type = '启动Oracle数据库'", '') # 上传脚本 local_file = os.getcwd() + '/frame/oracle_tools/ora_startup.sh' print local_file sftp_upload_file(host, user, password, '/tmp/ora_startup.sh', local_file) # 执行命令 cmd = 'sh /tmp/ora_startup.sh > /tmp/ora_startup.log' exec_command(host, user, password, cmd) tools.my_log(log_type, 'ora_startup.sh,Oracle数据库启动成功!', '')
def mysql_install(host, user, password, data_path, mysql_base, port): tools.mysql_exec("delete from many_logs where log_type = 'MySQL部署'", '') extra_params = '' mysql_home = '/%s/my%s' % (mysql_base, port) # 1. 创建目录 tools.my_log(log_type, '开始创建目录!', '') create_home_dir(host, user, password, port, data_path, mysql_base) # 2. 生成配置文件 tools.my_log(log_type, '生成配置文件!', '') generate_conf(host, user, password, port, data_path, mysql_base, mysql_home, extra_params) # 3. 初始化数据库 install_default_database(host, user, password, data_path, mysql_base, port, mysql_home)
def go_collect(local_dir): log_type = '日志采集' tools.mysql_exec("delete from many_logs where log_type = '日志采集'", '') # 指定本地保存路径 today = time.strftime('%Y%m%d') now = time.strftime('%H%M%S') # 创建当天目录 local_today = local_dir + '/' + today isExists = os.path.exists(local_today) # cmd_find = "find %s -mtime 0 -name '*'" %log_path if not isExists: os.mkdir(local_today) tools.my_log(log_type, '成功创建本地目录:%s' % local_today, '') logs = tools.mysql_query( "select app_name, host,user,password,log_name,log_path from log_collect_conf order by id" ) print logs for log in logs: app_name = log[0] host = log[1] user = log[2] password = base64.decodestring(log[3]) log_name = log[4] log_path = log[5] # 创建程序目录 local_log_path = local_today + '/' + log_name isExists = os.path.exists(local_log_path) # cmd_find = "find %s -mtime 0 -name '*.trc'" %log_path if not isExists: os.makedirs(local_log_path) tools.my_log(log_type, '成功创建本地日志目录:%s' % local_log_path, '') # 将远端日志路径传入app_path_list,进行遍历抓取 log_list = [] log_list.append(log_path) for log_path in log_list: tools.my_log(log_type, '开始收集日志:%s->%s->%s' % (log_name, host, log_path), '') cmd_find = "find %s -newermt %s -type f -name '*' ! -name '.*'" % ( log_path, today) lt = sftp_exec_command(host, user, password, cmd_find) for each in lt: # 取文件名 filename = each.split('/')[-1] local_app_log = local_log_path + '/' + filename print each print local_app_log sftp_down_file(host, user, password, each, local_app_log) # alert_path_final=alert_path.decode('unicode-escape').encode('utf-8') tools.my_log(log_type, '成功获取日志:%s' % each, '')
def oracle_switchover(p_host, p_user, p_password, s_host, s_user, s_password): log_type = 'Oracle容灾切换' tools.mysql_exec("delete from many_logs where log_type = 'Oracle容灾切换'", '') # 上传脚本 local_file = os.getcwd( ) + '/frame/oracle_tools/switchover/switchOverForPrimary.sh' sftp_upload_file(p_host, p_user, p_password, '/tmp/switchOverForPrimary.sh', local_file) # 执行命令 主转备 cmd = 'sh /tmp/switchOverForPrimary.sh > /tmp/switchOverForPrimary.log' exec_command(p_host, p_user, p_password, cmd) tools.my_log(log_type, 'ssh to %s:执行switchOverForPrimary.sh,主库切换为备库成功!' % p_host, '') # 上传脚本 local_file = os.getcwd( ) + '/frame/oracle_tools/switchover/startupPrimary.sh' sftp_upload_file(p_host, p_user, p_password, '/tmp/startupPrimary.sh', local_file) # 执行命令 开启主库 cmd = 'sh /tmp/startupPrimary.sh > /tmp/startupPrimary.log' exec_command(p_host, p_user, p_password, cmd) tools.my_log(log_type, 'ssh to %s:执行startupPrimary.sh,主库启动成功!' % p_host, '') # 上传脚本 local_file = os.getcwd( ) + '/frame/oracle_tools/switchover/switchOverForStandby.sh' sftp_upload_file(s_host, s_user, s_password, '/tmp/switchOverForStandby.sh', local_file) # 执行命令 备转主 cmd = 'sh /tmp/switchOverForStandby.sh > /tmp/switchOverForStandby.log' print cmd exec_command(s_host, s_user, s_password, cmd) tools.my_log(log_type, 'ssh to %s:执行switchOverForStandby.sh,备库切换为主库成功!' % s_host, '')
def install_default_database(host, user, password, data_path, mysql_base, port, mysql_home): # 上传安装包,解压缩 soft_dir = os.getcwd() + '/frame/mysql_install' soft_name = 'mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz' local_soft = '%s/%s' % (soft_dir, soft_name) remote_loc = '%s/%s' % (mysql_base, soft_name) sftp_upload_file(host, user, password, local_soft, remote_loc) tools.my_log(log_type, '上传安装文件至%s!' % remote_loc, '') cmd = 'tar -xzvf %s -C %s' % (remote_loc, mysql_base) exec_command(host, user, password, cmd) tools.my_log(log_type, '解压缩完成%s!' % remote_loc, '') cmd = 'mv %s/mysql*/* %s' % (mysql_base, mysql_home) exec_command(host, user, password, cmd) # 建用户,授权 cmd = '/usr/bin/groupadd mysql' exec_command(host, user, password, cmd) cmd = '/usr/bin/useradd -d /home/mysql -g mysql -m mysql' exec_command(host, user, password, cmd) tools.my_log(log_type, '创建MySQL用户成功!', '') cmd = 'chown -R mysql:mysql %s' % mysql_base exec_command(host, user, password, cmd) cmd = 'chown -R mysql:mysql %s/my%s' % (data_path, port) exec_command(host, user, password, cmd) # 初始化MySQL cmd = '%s/bin/mysqld --defaults-file=%s/my.cnf --initialize-insecure --user=mysql' % ( mysql_home, mysql_home) exec_command(host, user, password, cmd) tools.my_log(log_type, '初始化MySQL数据库成功!', '') # 启动MySQL数据库 cmd = '%s/bin/mysqld_safe --defaults-file=%s/my.cnf --user=mysql &' % ( mysql_home, mysql_home) # exec_command(host,user,password,cmd) tools.my_log(log_type, "创建MySQL:%s:%s成功,初始密码为空,请运行%s启动MySQL数据库!" % (host, port, cmd), '')
def create_home_dir(host, user, password, port, data_path, mysql_base): # 创建MySQL目录 dirs = get_dir_for_mysql(mysql_base, port) for dir in dirs: cmd = 'mkdir -p %s' % dir exec_command(host, user, password, cmd) tools.my_log(log_type, '创建目录%s!' % dir, '') # 创建数据目录 datadir = '%s/my%s' % (data_path, port) cmd = 'mkdir -p %s' % datadir exec_command(host, user, password, cmd) tools.my_log(log_type, '创建目录%s!' % datadir, '') tools.my_log(log_type, '创建目录完成!', '')
def mysql_install(host, user, password): log_type = 'Mysql部署' tools.mysql_exec("delete from many_logs where log_type = 'Mysql部署'", '') #3. 配置MySQL用户环境变量 cmd = 'sh /tmp/mysql_install/3_mysql_profile.sh > /tmp/mysql_install/3_mysql_profile.log' exec_command(host, user, password, cmd) tools.my_log(log_type, '执行3_mysql_profile.sh,MySQL用户环境变量配置完成!', '') #4. 创建用户组和目录 cmd = 'sh /tmp/mysql_install/4_fd_init.sh > /tmp/mysql_install/4_fd_init.log' exec_command(host, user, password, cmd) tools.my_log(log_type, '执行4_fd_init.sh,目录创建完成!', '') #5.解压、安装 cmd = 'sh /tmp/mysql_install/5_mysql_install.sh > /tmp/mysql_install/5_mysql_install.log' exec_command(host, user, password, cmd) tools.my_log(log_type, '执行5_mysql_install.sh,mysql安装完成!', '')
def mysql_install(host, user, password): log_type = 'Mysql部署' tools.mysql_exec("delete from many_logs where log_type = 'Mysql部署'", '') # 清除目标目录 cmd = 'rm -rf /tmp/mysql_install' exec_command(host, user, password, cmd) # 创建文件目录 cmd = 'mkdir -p /tmp/mysql_install' exec_command(host, user, password, cmd) # 上传安装部署文件 sftp_upload_dir(host, user, password, '/tmp', 'mysql_install') tools.my_log(log_type, '预上传文件完成!', '') # 0. 移除自带MySQL用户及安装包 cmd = 'sh /tmp/mysql_install/0_mysql_delold.sh > /tmp/mysql_install/0_mysql_delold.log' exec_command(host, user, password, cmd) tools.my_log(log_type, '执行0_mysql_delold.sh,移除自带mysql完成!', '') # 1. 安装rpm包 cmd = 'sh /tmp/mysql_install/1_mysql_yum.sh > /tmp/mysql_install/1_mysql_yum.log' exec_command(host, user, password, cmd) tools.my_log(log_type, '执行1_mysql_yum.sh,rpm包安装完成!', '') # 2. 配置资源限制,内核参数 cmd = 'sh /tmp/mysql_install/2_mysql_init.sh > /tmp/mysql_install/2_mysql_init.log' exec_command(host, user, password, cmd) tools.my_log(log_type, '执行2_mysql_init.sh,环境初始化完成!', '') #3. 配置MySQL用户环境变量 cmd = 'sh /tmp/mysql_install/3_fd_init.sh > /tmp/mysql_install/3_fd_init.log' exec_command(host, user, password, cmd) tools.my_log(log_type, '执行3_fd_init.sh.sh,目录创建完成!', '') #4. 创建用户组和目录 cmd = 'sh /tmp/mysql_install/4_mysql_profile.sh > /tmp/mysql_install/4_mysql_profile.log' exec_command(host, user, password, cmd) tools.my_log(log_type, '执行4_mysql_profile.sh,MySQL用户环境变量配置完成!', '')
#! /usr/bin/python
def go_start(): log_type = '程序启停' tools.mysql_exec("delete from many_logs where log_type = '程序启停'",'') starts = tools.mysql_query( "select oper_type,app_name,host,user,password,name,do_cmd,process_check,check_log,id from easy_start_conf order by id") for start in starts: oper_type = start[0] app_name = start[1] host = start[2] user = start[3] password = base64.decodestring(start[4]) name = start[5] do = start[6] process_check = start[7] check_log = start[8] id = start[9] # 关闭程序时如执行命令为空,则选择杀进程方式 if not do and oper_type == 'shutdown': cmd_do = 'ps -ef | grep %s| grep -v "grep" | cut -c 9-15 | xargs kill -s 9' % process_check exec_log = '' # 启动或关闭命令 if oper_type == 'startup': exec_log = '/tmp/%s.log' % name cmd_do = "%s > %s 2>&1 &" % (do, exec_log) # 检查命令 cmd_check = 'ps -ef|grep %s |grep -v "grep"' % process_check # 异常日志 if not check_log: check_log = exec_log error_check = 'tail -100 %s' % check_log tools.my_log(log_type,'跳转至%s,对%s执行%s' %(host,name,oper_type),'') # 执行关闭或启动命令 exec_command(host,user,password,cmd_do) if oper_type == 'shutdown': # 进程数为0,关闭成功 if len(exec_command(host,user,password,cmd_check)) == 0: tools.my_log(log_type, '%s:%s成功!' % (name,oper_type),'') upd_sql = "update easy_start_conf set process_check_result='green' where id = %s" %id tools.mysql_exec(upd_sql,'') else: tools.my_log(log_type, '%s:%s失败!' % (name, oper_type),'') upd_sql = "update easy_start_conf set process_check_result='red' where id = %s" %id tools.mysql_exec(upd_sql,'') break elif oper_type == 'startup': # 进程数为0,启动失败 if len(exec_command(host,user,password,cmd_check)) == 0: # 更新启停状态 upd_sql = "update easy_start_conf set process_check_result='red' where id = %s" % id tools.mysql_exec(upd_sql, '') # 异常日志 err_info = exec_command(host,user,password,error_check) err_info_txt = '' for i in err_info: err_info_txt = err_info_txt + i tools.my_log(log_type, '%s:%s失败!' % (name, oper_type),err_info_txt) break else: upd_sql = "update easy_start_conf set process_check_result='green' where id = %s" % id tools.mysql_exec(upd_sql, '') err_info = exec_command(host,user,password,error_check) err_info_txt = '' for i in err_info: err_info_txt = err_info_txt + i if 'err' in err_info_txt: upd_sql = "update easy_start_conf set check_log_result='red' where id = %s" % id tools.mysql_exec(upd_sql, '') tools.my_log(log_type, '%s:%s失败!' % (name, oper_type), err_info_txt) else: tools.my_log(log_type, '%s:%s成功!' % (name, oper_type),'') upd_sql = "update easy_start_conf set check_log_result='green' where id = %s" % id tools.mysql_exec(upd_sql, '')
def oracle_install(host, user, password): log_type = 'Oracle部署' tools.mysql_exec("delete from many_logs where log_type = 'Oracle部署'", '') # 清除目标目录 cmd = 'rm -rf /tmp/oracle_install' exec_command(host, user, password, cmd) # 创建文件目录 cmd = 'mkdir -p /tmp/oracle_install/oracle_rpms/centos6' exec_command(host, user, password, cmd) # 上传安装部署文件 sftp_upload_dir(host, user, password, '/tmp', 'oracle_install') tools.my_log(log_type, '预上传文件完成!', '') #1. 安装rpm包 cmd = 'sh /tmp/oracle_install/1_ora_yum.sh > /tmp/oracle_install/1_ora_yum.log' exec_command(host, user, password, cmd) tools.my_log(log_type, '执行1_ora_yum.sh,rpm包安装完成!', '') #2.环境初始化,建组、用户,配置资源限制,内核参数 cmd = 'sh /tmp/oracle_install/2_ora_init.sh > /tmp/oracle_install/2_ora_init.log' exec_command(host, user, password, cmd) tools.my_log(log_type, '执行2_ora_init.sh,环境初始化完成!', '') #3. 创建目录 cmd = 'sh /tmp/oracle_install/3_fd_init.sh > /tmp/oracle_install/3_fd_init.log' exec_command(host, user, password, cmd) tools.my_log(log_type, '执行3_fd_init.sh,目录创建完成!', '') #4. 配置Oracle用户环境变量 cmd = 'sh /tmp/oracle_install/4_ora_profile.sh > /tmp/oracle_install/4_ora_profile.log' exec_command(host, user, password, cmd) tools.my_log(log_type, '执行4_ora_profile.sh,Oracle用户环境变量配置完成!', '') #5. 上传安装包和安装文件 cmd = 'sh /tmp/oracle_install/5_init_orahome.sh > /tmp/oracle_install/5_init_orahome.log' exec_command(host, user, password, cmd) tools.my_log(log_type, '执行5_init_orahome.sh,Oracle安装文件传输完成!', '')
def oracle_exec_sql(): log_type = 'Oracle执行sql脚本' local_dir = os.getcwd() cmd = 'ls %s/frame/sqlscripts/*.sql' % local_dir status, result = commands.getstatusoutput(cmd) if status == 0: sql_list = result.split('\n') for sql in sql_list: print '开始执行脚本:%s' % sql sql_name = sql.split('/')[-1] sql_no = sql_name.split('_')[0] user = '******' password = '******' tnsname = sql.split('_')[1] cmd = 'sqlplus -s %s/%s@%s @%s' % (user, password, tnsname, sql) status, result = commands.getstatusoutput(cmd) if status == 0 and not 'ORA-' in result: print result print '%s执行成功!' % sql tools.my_log(log_type, '%s执行成功!' % sql, '') tools.my_log(log_type, '执行信息:%s' % result, '') cmd = 'rm %s' % sql status, result = commands.getstatusoutput(cmd) print '%s脚本已删除!' % sql tools.my_log(log_type, '%s脚本已删除!' % sql, '') sql = "update sql_list set result = '成功',result_color = 'green' where sql_no=%s" % sql_no tools.mysql_exec(sql, '') else: print result print '%s执行失败,已回滚!' % sql tools.my_log(log_type, '%s执行失败,已回滚!' % sql, '') tools.my_log(log_type, '', '异常信息:%s' % result) cmd = 'rm %s' % sql status, result = commands.getstatusoutput(cmd) print '%s脚本已删除!' % sql tools.my_log(log_type, '%s脚本已删除!' % sql, '') sql = "update sql_list set result = '失败',result_color = 'red' where sql_no=%s" % sql_no tools.mysql_exec(sql, '')