def platform_token(action=None, id=None, args=None): produce.Async_log(g.user, request.url) db_token = db_op.platform_token tm = time.strftime('%Y-%m-%d', time.localtime()) form = MyForm.Form_platform_token() tables = ['第三方平台', '连接方式', 'Token', '颁发日期', '失效日期', '管理'] if action == 'add': expire_date = "2999-12-30" if id > 0: expire_date = datetime.datetime.now() + datetime.timedelta(days=id) expire_date = expire_date.strftime('%Y-%m-%d') try: c = db_token(platform=args, channel='api', token=Md5.Md5_make(tools.Produce(8, string.digits)), award=tm, expire=expire_date) db_op.DB.session.add(c) db_op.DB.session.commit() return render_template_string('success') except Exception as e: logging.error(e) return render_template_string('fail') if action == 'modify': try: db_token.query.filter(db_token.id == id).update( {db_token.expire: args}) db_op.DB.session.commit() return render_template_string('success') except Exception as e: logging.error(e) return render_template_string('fail') if action == 'drop': try: v = db_token.query.filter(db_token.id == id).all() for c in v: db_op.DB.session.delete(c) db_op.DB.session.commit() return render_template_string('success') except Exception as e: logging.error(e) return render_template_string('fail') vals = db_token.query.with_entities( db_token.id, db_token.platform, db_token.channel, db_token.token, db_token.award, db_token.expire).order_by(desc(db_token.id)).all() return render_template('platform_token.html', form=form, vals=vals, tables=tables, tm=tm)
def publish(): produce.Async_log(g.user, request.url) importlib.reload(MyForm) secret_key= tools.Produce(length=8, chars=string.digits) session['secret_key'] = secret_key K = '%s_%s' %(g.user,secret_key) Msg_Key = '%s_publish_msg' %K form = MyForm.MyForm_publish() if form.submit.data: try: if Redis.exists(Msg_Key): raise flash('上线操作过于频繁,请稍等%s秒......' %Redis.ttl(Msg_Key)) package_url = form.package_url.data describe = form.describe.data package_md5 = form.package_md5.data package_type = form.package_type.data publish_type = form.publish_type.data check_url = form.check_url.data restart = form.restart.data execute = form.execute.data rb_project = form.project.data rb_version = form.version.data gray = form.gray.data if execute == 'rollback': if not rb_project and not rb_version: raise logging.error("choice can not be empty!") if execute == 'publish': if not package_url or not package_md5 or not check_url or not describe: raise logging.error("input can not be empty!") Package_name = package_url.split('/')[-1] package_name = Package_name.replace('.zip', '') rb_project = '-'.join(package_name.split('-')[:-1]) INFOS = {'package_url': package_url, 'package_md5': package_md5, 'package_type': package_type, 'publish_type': publish_type,'user':g.user,'describe':describe.replace('"','').replace("'",''),'gray':int(gray), 'restart': restart, 'execute': execute, 'check_url': check_url.replace('https','http'), 'project': rb_project, 'version': rb_version,'channel':'web','callback_url':'None','token':'None','timestamp' :int(time.time())} #启动代码分发控制中心 Scheduler = produce.Scheduler_publish() Scheduler = Scheduler.Scheduler_mem(task_publish.Publish_center,[INFOS,Msg_Key,K]) Scheduler.start() return render_template('publish_show.html') except Exception as e: if 'old-style' not in str(e): logging.error(e) return render_template('publish.html',form=form)
def index(): try: db_zabbix = db_idc.zabbix_info td = time.strftime("%Y-%m-%d", time.localtime()) td_1 = datetime.datetime.now() - datetime.timedelta(days=1) td_1 = td_1.strftime("%Y-%m-%d") td_7 = datetime.datetime.now() - datetime.timedelta(days=7) td_7 = td_7.strftime("%Y-%m-%d") TM = time.strftime('%M', time.localtime()) BUSIS = [] #仪表盘展示 try: gauge_busi = Gauge(width='100%',height='100%') gauge_busi.add("线上业务健康率", "", 100,scale_range=[0, 100], is_toolbox_show=False) gauge_server = Gauge(width='100%',height='100%') gauge_server.add("线上服务器连通率", "", 100, scale_range=[0, 100],is_toolbox_show=False) gauge_network = Gauge(width='100%', height='100%') gauge_network.add("机房网络连通率", "", 100, scale_range=[0, 100], is_toolbox_show=False) gauge = {'network':gauge_network,'server':gauge_server, 'busi':gauge_busi} except Exception as e: logging.error(e) #业务信息展示 try: total_key = 'op_totals_alarms_tmp' for i in range(7): data_now = datetime.datetime.now() - datetime.timedelta(days=i) dd = data_now.strftime('%Y-%m-%d') alarm_count_key = '%s_%s' % ('op_business_alarm_count', dd) if RC_CLUSTER.exists(alarm_count_key): vals = RC_CLUSTER.hgetall(alarm_count_key) vals = sorted(vals.items(), key=lambda item: int(item[1])) for val in vals: RC_CLUSTER.hincrby(total_key, dd, val[1]) line = Line("业务监控报警近期统计", width='105%', height=250, title_pos='center', title_text_size=12) if RC_CLUSTER.exists(total_key): vals = RC_CLUSTER.hgetall(total_key) vals = sorted(vals.items(), key=lambda item: item[0],reverse=True) RC_CLUSTER.delete(total_key) attrs = [val[0] for val in vals] vals = [int(val[1]) for val in vals] line.add("", attrs, vals, is_label_show=True, is_toolbox_show=False,is_legend_show = False, xaxis_interval=0,is_fill=True,area_opacity=0.3,is_smooth=True) bar = Bar("线上业务PV实时统计", width='105%', height=250, title_pos='center', title_text_size=12) busi_vals = RC_CLUSTER.hgetall('op_business_pv_%s' %td) if busi_vals: busi_vals = sorted(busi_vals.items(), key=lambda item: int(float(item[1])), reverse=True) bar_vals = [val[0] for val in busi_vals[:8]] bar_counts = [float('%.4f' %(float(val[1])/100000000)) for val in busi_vals[:8]] bar.add("", bar_vals, bar_counts, is_label_show=True, is_toolbox_show=False, legend_orient='vertical', legend_pos='right', xaxis_interval=0,yaxis_formatter='亿') BUSIS.extend((bar,line)) except Exception as e: logging.error(e) #网站并发访问展示 try: NEW_DATA = [eval(v) for v in RC.lrange('internet_access_%s' %td, 0, -1)] attr = [DATA[0] for DATA in NEW_DATA] vals =[int(int(DATA[1])/60) for DATA in NEW_DATA] line = Line('墨迹线上实时并发访问',title_pos='center',title_text_size=12,width='109%',height='250px') line.add("今天", attr, vals,is_toolbox_show=False,is_smooth=True,mark_point=["max", "min"], mark_point_symbolsize=80,is_datazoom_show=True,datazoom_range=[v for v in range(100,10)], datazoom_type= 'both',legend_pos='70%') if RC.exists('internet_access_%s' % td_1): OLD_DATA = [eval(v) for v in RC.lrange('internet_access_%s' % td_1, 0, -1)] OLD_DATA = [val for val in OLD_DATA if val[0] in attr] old_attr = [DATA[0] for DATA in OLD_DATA] old_vals = [int(int(DATA[1]) / 60) for DATA in OLD_DATA] if attr and vals: line.add("昨天", old_attr,old_vals, is_toolbox_show=False, is_smooth=True, mark_point=["max", "min"], mark_point_symbolsize=80,is_datazoom_show=True,datazoom_range=[v for v in range(100,10)], datazoom_type= 'both',legend_pos='70%') if RC.exists('internet_access_%s' % td_7): OLD_DATA = [eval(v) for v in RC.lrange('internet_access_%s' % td_7, 0, -1)] OLD_DATA = [val for val in OLD_DATA if val[0] in attr] old_attr = [DATA[0] for DATA in OLD_DATA] old_vals = [int(int(DATA[1]) / 60) for DATA in OLD_DATA] if attr and vals: line.add("上周", old_attr,old_vals, is_toolbox_show=False, is_smooth=True, mark_point=["max", "min"], mark_point_symbolsize=80, is_datazoom_show=True, datazoom_range=[v for v in range(100, 10)], datazoom_type='both', legend_pos='70%') except Exception as e: logging.error(e) #监控数据展示 try: tm = datetime.datetime.now() - datetime.timedelta(minutes=1) tm = tm.strftime('%H:%M') z_triggers = RC.hgetall('zabbix_triggers_%s' %tm) if z_triggers: z_triggers = [[t,z_triggers[t]]for t in z_triggers] except Exception as e: logging.error(e) #服务器预警信息 try: z_infos = defaultdict() cpu_load = db_zabbix.query.with_entities(db_zabbix.ip,db_zabbix.ssh_port,db_zabbix.cpu_load).filter(and_(db_zabbix.cpu_load >100,db_zabbix.icmpping ==1)).all() disk_io = db_zabbix.query.with_entities(db_zabbix.ip,db_zabbix.ssh_port,db_zabbix.disk_io).filter(and_(db_zabbix.disk_io>30,db_zabbix.icmpping ==1)).all() openfile = db_zabbix.query.with_entities(db_zabbix.ip, db_zabbix.ssh_port, db_zabbix.openfile).filter(and_(db_zabbix.openfile >500000,db_zabbix.icmpping ==1)).all() if cpu_load: z_infos['cpu_load']=cpu_load if disk_io: z_infos['disk_io'] = disk_io if openfile: z_infos['openfile'] = openfile except Exception as e: logging.error(e) # 获取问题服务器列表 fault_servers = defaultdict() try: for key in ('ssh_login_fault_%s'%td, 'ssh_port_fault_%s'%td): if RC.exists(key): fault_vals = RC.hgetall(key) if fault_vals: fault_servers[key] = zip([fault_vals[val] for val in fault_vals],[val.split(':')[0] for val in fault_vals],[val.split(':')[1] for val in fault_vals]) except Exception as e: logging.error(e) app_resp = make_response(render_template('index.html',gauge=gauge,line=line,tm=TM,z_triggers=z_triggers,z_infos=z_infos,fault_servers=fault_servers,BUSIS=BUSIS)) app_resp.set_cookie('secret_key',tools.Produce(length=8,chars=string.digits),path='/') return app_resp except Exception as e: logging.error(e) flash('获取数据错误!',"error") return render_template('Message.html')
def login(): try: try: token = tools.Produce(length=24, chars=string.hexdigits) except Exception as e: logging.error(e) ym = time.strftime('%Y', time.localtime()) session['Menu'] = {} #钉钉验证授权 if tools.http_args(request, 'code') and tools.http_args( request, 'state') == 'STATE': db_auth = db_op.user_auth code = tools.http_args(request, 'code') #获取token try: url = "https://oapi.dingtalk.com/sns/gettoken?appid=dingoadq3qon8zb34vzdff&appsecret=Tu6IlXjTn1m4vqrOA580xLOt2VbOK26bVu3sBOtvp0MnqIp2zpcwkL3qVpqAT7rG" if ENV == 'dev': url = "https://oapi.dingtalk.com/sns/gettoken?appid=dingoa7wymhx6dbeffjels&appsecret=I-v3OXL1hFKYZlJ3b6pqABmoNGYREXePpdzQ5JaSK8DqJdQyn_1J3wEUYBTpdiE_" r = requests.get(url) access_token = r.json()['access_token'] r = requests.post( "https://oapi.dingtalk.com/sns/get_persistent_code?access_token=%s" % access_token, data=json.dumps({"tmp_auth_code": code})) openid = r.json()['openid'] persistent_code = r.json()['persistent_code'] r = requests.post( "https://oapi.dingtalk.com/sns/get_sns_token?access_token=%s" % access_token, data=json.dumps({ "openid": openid, "persistent_code": persistent_code })) sns_token = r.json()['sns_token'] #获取用户信息 r = requests.get( 'https://oapi.dingtalk.com/sns/getuserinfo?sns_token=%s' % sns_token) user_info = r.json()['user_info'] nick = user_info['nick'] dingId = user_info['dingId'] except Exception as e: logging.error(e) #授权用户登陆 if nick and dingId: try: val = db_auth.query.filter( and_(db_auth.dingId == dingId, db_auth.openid == openid)).all() if val: db_auth.query.filter( and_(db_auth.dingId == dingId, db_auth.openid == openid)).update({ db_auth.token: token, db_auth.update_time: time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) }) db_op.DB.session.commit() URL = url_for('main') timestamp = check.timestamp(7) else: #跳转至权限申请页 URL = url_for('approval.apply') timestamp = check.timestamp(1) except Exception as e: logging.error(e) app_resp = make_response(redirect(URL)) try: app_resp.set_cookie('user', Md5.Md5_make(nick), expires=timestamp, path='/') app_resp.set_cookie('openid', Md5.Md5_make(openid), expires=timestamp, path='/') app_resp.set_cookie('dingId', Md5.Md5_make(dingId), expires=timestamp, path='/') app_resp.set_cookie('token', Md5.Md5_make(token), expires=timestamp, path='/') except Exception as e: logging.error(e) else: Redis.set('OP_verify_%s' % dingId, token) Redis.set('OP_token_%s' % Md5.Md5_make(token), token) Redis.set('OP_dingId_%s' % Md5.Md5_make(dingId), dingId) Redis.set('OP_user_%s' % Md5.Md5_make(nick), nick) Redis.set('OP_openid_%s' % Md5.Md5_make(openid), openid) return app_resp except Exception as e: flash('登录失败!') logging.error(e) finally: db_op.DB.session.remove() return render_template('login.html', ym=ym, ENV=ENV)
def publish_code(): try: timestamp = None db_token = db_op.platform_token params = request.json GRAY = {'Flase': 0, 'True': 1} # 检查时间戳是否存在 if 'timestamp' in params['data']: timestamp = params['data']['timestamp'] else: return jsonify({'status': 'timestamp is null', 'timestamp': None}) #md5对比验证数据 new_md5 = Md5.Md5_make(params['data']) if new_md5 == params['data_md5']: params = params['data'] token = params['access_token'] #验证token是否有效 vals = db_token.query.filter( and_( db_token.token == token, db_token.expire > time.strftime( '%Y-%m-%d', time.localtime()))).all() if vals: user = params['proposer'] package_url = params['package_url'] #检查压缩包下载地址格式 if not package_url.endswith( '.zip') and not package_url.endswith('.war'): return jsonify({ 'status': 'the package must be zip or war', 'timestamp': timestamp }) #获取详细参数 describe = params['describe'] package_md5 = params['package_md5'] package_type = params['package_type'] publish_type = params['publish_type'] restart = params['restart'] execute = params['execute'] check_url = params['check_url'] rb_project = params['project_name'] rb_version = params['project_version'] callback_url = params['callback_url'] gray = GRAY[params['gray']] #生成随机key种子 K = '%s_%s' % (token, tools.Produce(length=8, chars=string.digits)) Msg_Key = '%s_publish_msg' % K INFOS = { 'package_url': package_url, 'package_md5': package_md5, 'package_type': package_type, 'publish_type': publish_type, 'user': user, 'describe': describe.replace('"', '').replace("'", ''), 'gray': gray, 'restart': restart, 'execute': execute, 'check_url': check_url.replace('https', 'http'), 'project': rb_project, 'version': rb_version, 'channel': 'api', 'callback_url': callback_url, 'token': token, 'timestamp': timestamp } #启动代码分发控制中心 Scheduler = produce.Scheduler_publish() Scheduler = Scheduler.Scheduler_mem( task_publish.Publish_center, [INFOS, Msg_Key, K]) Scheduler.start() return jsonify({'status': 'ok', 'timestamp': timestamp}) else: return jsonify({ 'status': 'token deny', 'timestamp': timestamp }) else: return jsonify({ 'status': 'data_md5 error', 'timestamp': timestamp }) except Exception as e: logging.error(e) return jsonify({'status': str(e), 'timestamp': timestamp}) finally: db_op.DB.session.remove()
def index(): try: whitelist = [] Key = "op_alarm_load_whitelist" if RC_CLUSTER.exists(Key): whitelist = RC_CLUSTER.smembers(Key) td = time.strftime("%Y-%m-%d", time.localtime()) td_1 = datetime.datetime.now() - datetime.timedelta(days=1) td_1 = td_1.strftime("%Y-%m-%d") td_7 = datetime.datetime.now() - datetime.timedelta(days=7) td_7 = td_7.strftime("%Y-%m-%d") TM = time.strftime('%M', time.localtime()) BUSIS = [] #业务信息展示 try: Key = 'op_k8s_ingress_log' k8s_domains_key = 'op_k8s_domains_%s' % td total_key = 'op_totals_alarms_tmp' if RC.exists(k8s_domains_key): try: line = Line("容器平台业务RPS统计", width='105%', height=250, title_pos='center', title_text_size=12) for domain in RC.smembers(k8s_domains_key): if RC.exists('%s_%s_%s' % (Key, domain, td)): vals = RC.hgetall('%s_%s_%s' % (Key, domain, td)) vals = sorted(vals.items(), key=lambda item: item[0]) attrs = [val[0] for val in vals[-10:]] vals = [ int(int(val[1]) / 60) for val in vals[-10:] ] line.add(domain, attrs, vals, is_label_show=True, is_toolbox_show=False, legend_pos='65%', xaxis_interval=0, is_fill=True, area_opacity=0.3, is_smooth=True) except Exception as e: logging.error(e) else: for i in range(7): data_now = datetime.datetime.now() - datetime.timedelta( days=i) dd = data_now.strftime('%Y-%m-%d') alarm_count_key = '%s_%s' % ('op_business_alarm_count', dd) if RC_CLUSTER.exists(alarm_count_key): vals = RC_CLUSTER.hgetall(alarm_count_key) vals = sorted(vals.items(), key=lambda item: int(item[1])) for val in vals: RC_CLUSTER.hincrby(total_key, dd, val[1]) line = Line("业务监控报警近期统计", width='105%', height=250, title_pos='center', title_text_size=12) if RC_CLUSTER.exists(total_key): vals = RC_CLUSTER.hgetall(total_key) vals = sorted(vals.items(), key=lambda item: item[0], reverse=True) RC_CLUSTER.delete(total_key) attrs = [val[0] for val in vals] vals = [int(val[1]) for val in vals] line.add("", attrs, vals, is_label_show=True, is_toolbox_show=False, is_legend_show=False, xaxis_interval=0, is_fill=True, area_opacity=0.3, is_smooth=True) bar = Bar("线上业务实时PV统计", width='105%', height=250, title_pos='center', title_text_size=12) busi_vals = RC_CLUSTER.hgetall('op_business_pv_%s' % td) if busi_vals: busi_vals = sorted(busi_vals.items(), key=lambda item: int(float(item[1])), reverse=True) bar_vals = [val[0] for val in busi_vals[:8]] bar_counts = [ float('%.4f' % (float(val[1]) / 100000000)) for val in busi_vals[:8] ] bar.add("", bar_vals, bar_counts, is_label_show=True, is_toolbox_show=False, legend_orient='vertical', legend_pos='right', xaxis_interval=0, yaxis_formatter='亿') BUSIS.extend((bar, line)) except Exception as e: logging.error(e) #网站并发访问展示 try: NEW_DATA = [ eval(v) for v in RC.lrange('internet_access_%s' % td, 0, -1) ] attr = [DATA[0] for DATA in NEW_DATA] vals = [int(int(DATA[1]) / 60) for DATA in NEW_DATA] line = Line('墨迹线上业务RPS统计', title_pos='center', title_text_size=12, width='109%', height='250px') line.add("今天", attr, vals, is_toolbox_show=False, is_smooth=True, mark_point=["max", "min"], mark_point_symbolsize=80, is_datazoom_show=True, datazoom_range=[v for v in range(100, 10)], datazoom_type='both', legend_pos='70%') if RC.exists('internet_access_%s' % td_1): OLD_DATA = [ eval(v) for v in RC.lrange('internet_access_%s' % td_1, 0, -1) ] OLD_DATA = [val for val in OLD_DATA if val[0] in attr] old_attr = [DATA[0] for DATA in OLD_DATA] old_vals = [int(int(DATA[1]) / 60) for DATA in OLD_DATA] if attr and vals: line.add("昨天", old_attr, old_vals, is_toolbox_show=False, is_smooth=True, mark_point=["max", "min"], mark_point_symbolsize=80, is_datazoom_show=True, datazoom_range=[v for v in range(100, 10)], datazoom_type='both', legend_pos='70%') if RC.exists('internet_access_%s' % td_7): OLD_DATA = [ eval(v) for v in RC.lrange('internet_access_%s' % td_7, 0, -1) ] OLD_DATA = [val for val in OLD_DATA if val[0] in attr] old_attr = [DATA[0] for DATA in OLD_DATA] old_vals = [int(int(DATA[1]) / 60) for DATA in OLD_DATA] if attr and vals: line.add("上周", old_attr, old_vals, is_toolbox_show=False, is_smooth=True, mark_point=["max", "min"], mark_point_symbolsize=80, is_datazoom_show=True, datazoom_range=[v for v in range(100, 10)], datazoom_type='both', legend_pos='70%') except Exception as e: logging.error(e) #监控数据展示 try: tm = datetime.datetime.now() - datetime.timedelta(minutes=1) tm = tm.strftime('%H:%M') z_triggers = RC.hgetall('zabbix_triggers_%s' % tm) if z_triggers: z_triggers = [[t, z_triggers[t]] for t in z_triggers] except Exception as e: logging.error(e) #服务器预警信息 try: z_infos = defaultdict() dict_load = None dict_openfile = None if RC_CLUSTER.exists('op_zabbix_server_load_top'): dict_load = eval(RC_CLUSTER.get('op_zabbix_server_load_top')) if RC_CLUSTER.exists('op_zabbix_server_openfile_top'): dict_openfile = eval( RC_CLUSTER.get('op_zabbix_server_openfile_top')) if dict_load: z_infos['cpu_load'] = dict_load if dict_openfile: z_infos['openfile'] = dict_openfile except Exception as e: logging.error(e) # 获取问题服务器列表 fault_servers = defaultdict() try: for key in ('ssh_login_fault_%s' % td, 'ssh_port_fault_%s' % td): if RC.exists(key): fault_vals = RC.hgetall(key) if fault_vals: fault_servers[key] = zip( [fault_vals[val] for val in fault_vals], [val.split(':')[0] for val in fault_vals], [val.split(':')[1] for val in fault_vals]) except Exception as e: logging.error(e) app_resp = make_response( render_template('index.html', line=line, tm=TM, z_triggers=z_triggers, z_infos=z_infos, fault_servers=fault_servers, BUSIS=BUSIS, whitelist=whitelist)) app_resp.set_cookie('secret_key', tools.Produce(length=8, chars=string.digits), path='/') return app_resp except Exception as e: logging.error(e) flash('获取数据错误!', "error") return render_template('Message.html')