def post(self): username = self.get_argument('useranme') password = self.get_argument('password') # valid: 强制密码修改为高强度口令(12位以上,需包括大小写字母和数字混合) def valid_passwd(pw): MIN_LEN = 12 if(len(pw)<12):raise Exception("password too short, min length is %d" % MIN_LEN) has_A = False has_a = False has_0 = False for c in password: if not has_A and c >= 'A' and c <= 'Z': has_A = True continue if not has_a and c >= 'a' and c <= 'z': has_a = True continue if not has_0 and c >= '0' and c <= '9': has_0 = True continue if not has_A: raise Exception("password must at last has one upper char") if not has_a: raise Exception("password must at last has one little char") if not has_0: raise Exception("password must at last has one number char") valid_passwd(password) session = database.getSession() user = session.query(User).filter(and_(User.name == username, User.password == password)).first() session.close() if user: self.set_current_user({'name': user.name, 'password': user.password,"type": user.type }) self.login_jump(user.type) else: self.redirect('/login')
def get_host_list(): host_list = [] session = database.getSession() for instance in session.query(Instance).filter(Instance.role == "gmond"): host_list.append(instance.host) session.close() return host_list
def pre_check(self): ''' 覆盖manager的pre_check 每次检查之前都获取判断规则 ''' #获取instance列表,得到所有机器和角色的对应关系 self.role_host_map = {} self.rule_lists = [] self.group_host_map = {} session = database.getSession() for instance in session.query(Instance).filter( Instance.status == Instance.STATUS_START): host = instance.host role = instance.role if not self.role_host_map.has_key(role): self.role_host_map[role] = [] self.role_host_map[role].append(host) for gh in session.query(GroupHost): group = gh.group host = gh.hostname if not self.group_host_map.has_key(group): self.group_host_map[group] = [] self.group_host_map[group].append(host) for alarm in session.query(Alarm): active_lists, except_lists = alarm.get_host_tuple() self.rule_lists.append( Rule(alarm.name, alarm.expression, alarm.callback, active_lists, except_lists)) session.close()
def run(): session = database.getSession(False) logCount = 0 ; while True: cbs = session.query(CallBack).filter( CallBack.status == CallBack.STATUS_CHECK ) idList = [] cbcache = {} for cb in cbs: idList.append(cb.taskid) if not cbcache.has_key(cb.taskid) : cbcache[cb.taskid]=[] cbcache[cb.taskid].append(cb) logCount = logCount + 1 if logCount == 10: callback_log.info("find " + str( len(idList) ) + " need to callback" ) logCount = 0 if len(idList) != 0 : for task in session.query(Task).filter(Task.id.in_(idList)): callback_log.info(str(task.id)+" "+str(task.status)) if task.status == Task.STATUS_FINISH: remove_callback(session,task.id) callback_log.info("remove "+str(task.id) ) if cbcache.has_key(task.id) : for cb in cbcache[task.id]: callback_log.info("call back "+str(cb.taskid)+" "+cb.func+" "+cb.params ) deal_callback(session, task, cb.func, json.loads(cb.params)) # session. session.commit() #session.flush() time.sleep(3)
def check_instance_state(self): ''' 获取所有的instance状态,报告哪些有问题 ''' service_role_map = {} #get all instance session = database.getSession() for instance in session.query(Instance): result,msg = self.check_instance(instance) self.update_result(result,msg) if not service_role_map.has_key(instance.service) : service_role_map[instance.service] = [] service_role_map[instance.service].append(instance) for (service,roles) in service_role_map.items(): if service == "zookeeper" : zk_leader_port = database.get_service_conf(session,"zookeeper","zookeeper_leader_port") result,msg = self.check_zk_leader(zk_leader_port,roles) self.update_result(result,msg) elif service == "hbase" : #TODO hbase_master_info_port = database.get_service_conf(session,"hbase","hbase_master_info_port") result,msg = self.check_hbase_master(hbase_master_info_port,roles) self.update_result(result,msg) session.close() return (self.state,self.msg)
def get_cluster_name(): cluster_name = "" session = database.getSession() cluster_name = database.get_service_conf(session, "ganglia", "cluster_name") session.close() return cluster_name
def signUp(): session = getSession() method = request.method if(method == 'POST'): name = request.form.get('name') lastname = request.form.get('lastname') email = request.form.get('email') dateBirth=request.form.get('dateBirth') password = toBytes(request.form.get('password')) gender=request.form.get('gender_id') try: user = User() user.email = email user.name = name user.dateBirth=dateBirth user.gender_id=gender user.lastname = lastname hashed = bcrypt.hashpw( password, bcrypt.gensalt() ) user.password = hashed.decode("utf-8") session.add(user) session.commit() login_user(user) return redirect(url_for('main')) except Exception as error: session.rollback() raise error genders=session.query(Gender).all() return render_template('signUp.html',genders=genders)
def add_host(id, hosts, user, port, passwd, sudopasswd): async_set(id, "progressMsg", "begin to run") async_set(id, "progress", "10") time.sleep(2) async_set(id, "progressMsg", "ssh to") async_set(id, "progress", "20") time.sleep(2) async_set(id, "progressMsg", "get info") async_set(id, "progress", "80") time.sleep(2) async_set(id, "progressMsg", "finish") async_set(id, "progress", "100") #保留10秒后清除 session = database.getSession() for hostName in hosts: host = Host(hostName) host.ip = "192.168.1.1" host.cpu = "fade8core" host.mem = "fade100G" host.disk = "fade100T" host.rack = "unknow" session.add(host) session.commit() time.sleep(10) async_remove(id)
def run_update(task): session = database.getSession(False) session.query(Task).filter(Task.id == task.id).update( {Task.status: task.status, Task.result: task.result, Task.msg: task.msg} ) session.commit() session.close()
def create(): method = request.method session = getSession() if (method == 'POST'): date = request.form.get('date') name = request.form.get('name') category_id = request.form.get('category') track = request.form.get('track') competence = Competence() competence.date = date competence.name_competence = name competence.category_id = category_id competence.track_id = track try: session.add(competence) session.commit() userCompetence = UserCompetence() userCompetence.user_id = current_user.id userCompetence.competences_id = competence.id session.add(userCompetence) session.commit() return redirect(url_for('competence.index')) except Exception as error: session.rollback() raise error categories = session.query(Category).all() tracks = session.query(Track).all() return render_template('createCompetences.html', categories=categories, tracks=tracks)
def add_host(id,hosts,user,port,passwd,sudopasswd): async_set(id,"progressMsg","begin to run") async_set(id,"progress","10") time.sleep(2) async_set(id,"progressMsg","ssh to") async_set(id,"progress","20") time.sleep(2) async_set(id,"progressMsg","get info") async_set(id,"progress","80") time.sleep(2) async_set(id,"progressMsg","finish") async_set(id,"progress","100") #保留10秒后清除 session=database.getSession() for hostName in hosts: host = Host(hostName) host.ip="192.168.1.1" host.cpu="fade8core" host.mem="fade100G" host.disk="fade100T" host.rack="unknow" session.add(host) session.commit() time.sleep(10) async_remove(id)
def collectMetrics(self): # 获取当前集群的状态 metrics = self.getMetrics() # recordTime指向上个10分钟的开头时间,所以要往后移动一个10分钟 meRecord = MetricsRecord(self.recordTime + config.collect_interval) recordKey = [ "appsCompleted", "appsPending", "appsRunning", "appsFailed", "appsKilled", "totalMB", "availableMB", "allocatedMB", "containersAllocated", "totalNodes", "activeNodes", ] # totalMB=availableMB+allocatedMB temp = metrics["clusterMetrics"] temp["totalMB"] = int(temp["availableMB"]) + int(temp["allocatedMB"]) for key in recordKey: meRecord.set(key, temp[key]) session = database.getSession() session.merge(meRecord) session.commit()
def createPost(): session = getSession() name = request.form.get('name') location = request.form.get('location') size = request.form.get('size') try: """ connection.execute( "INSERT INTO tracks(name,location,size) VALUES(%s, %s,%s)", name, location, size ) """ track = Track() track.name = name track.location = location track.size = size session.add(track) session.commit() files = request.files.getlist("images") imagesPaths = uploadfiles(files, folderTracks) for imagesPath in imagesPaths: trackImage = TrackImage() trackImage.src = imagesPath trackImage.track_id = track.id session.add(trackImage) session.commit() return redirect(url_for('track.tracks')) except Exception as error: session.rollback() raise error return render_template('createTracks.html')
def run(self): session = getSession() for item in data: track = self.db.session.query(Track).filter( Track.name == item.get("name") ).first() exist = bool(track) if not exist: track = Track() track.name = item.get("name") track.location = item.get("location") track.size = item.get("size") or 5000 if not exist: self.db.session.add(track) self.db.session.commit() images = item.get("images") if images: for image in images: haveImage = self.db.session.query(TrackImage).filter( TrackImage.track_id == track.id ).first() if not haveImage: ext = Path(image).suffix namefile = uuid.uuid4().hex + ext file = open(path + image, 'rb') s3.put_object(Bucket=bucket, Key=to + namefile, Body=file) trackImage = TrackImage() trackImage.src = namefile trackImage.track_id = track.id self.db.session.add(trackImage)
def _get_mail_to(): session = database.getSession() mail_list = session.query(AlarmAssist).filter( AlarmAssist.name == "mail_to").first() log.info("to mail %s" % mail_list.value) session.close() return mail_list.value.split(",")
def trainings(): session = getSession() trainings = session.query(Training, Track, Category).join( Track, Category ).filter(Training.user_id == current_user.id).order_by(Training.date.desc()).limit(10).all() return render_template('trainings.html', trainings=trainings)
def check_instance_state(self): ''' 获取所有的instance状态,报告哪些有问题 ''' service_role_map = {} #get all instance session = database.getSession() for instance in session.query(Instance): #检查实例 key_word,msg = self.check_instance(instance) if key_word != None: self.update_result(key_word, msg) #采集服务 if not service_role_map.has_key(instance.service) : service_role_map[instance.service] = [] service_role_map[instance.service].append(instance) for (service,roles) in service_role_map.items(): if service == "zookeeper" : zk_leader_port = database.get_service_conf(session,"zookeeper","zookeeper_leader_port") result,msg = self.check_zk_leader(zk_leader_port,roles) if not result: key_word = "cluster(zk no leader)" self.update_result(key_word, msg) elif service == "hbase" : #TODO hbase_master_info_port = database.get_service_conf(session,"hbase","hbase_master_info_port") result,msg = self.check_hbase_master(hbase_master_info_port,roles) if not result: key_word = "cluster(hbase no leader)" self.update_result(key_word, msg) session.close() return self.alarm_list
def collectApp(self): #获取所有的过去时段完成的app的列表 apps = self.getAppList() if not apps or not apps["apps"]: log.info("no appid match") return startCollectTime = time.time() #轮询app列表,获取每个app的详细信息 for app in apps["apps"]["app"]: startTime = time.time() appid = app["id"] if app['state'] == 'FINISHED': try: jobid = appidToJobid(appid) jobHistory = self.getJobHistory(jobid) if jobHistory: jobCounter = self.getJobCounter(jobid) jobTasks = self.getJobAllTask(jobid) self.updateWithAppid(app, jobHistory, jobCounter) else: log.info( "find some app run success but no history file:" + appid) except: log.exception("get error while doing app " + appid) endTime = time.time() else: self.updateWithNotSuccAppid(app) log.info("getting appid: %s using %d ms" % (appid, (endTime - startTime) * 1000)) endCollectTime = time.time() log.info("using %d ms to collect the data" % ((endCollectTime - startCollectTime) * 1000)) startFlushTime = time.time() #提交数据 session = database.getSession() for (appid, appRecordValue) in self.appList.items(): session.merge(appRecordValue) session.commit() log.info("push %d appRecord into table" % (len(self.appList))) for (key, NmRecordValue) in self.nmList.items(): session.merge(NmRecordValue) session.commit() log.info("push %d NmRecord into table" % (len(self.nmList))) for (key, RmRecordValue) in self.rmList.items(): session.merge(RmRecordValue) session.commit() log.info("push %d RmRecord into table" % (len(self.rmList))) endFlushTime = time.time() log.info("using %d ms to push to the db" % ((endFlushTime - startFlushTime) * 1000))
def indexC(): session = getSession() connection = session.connection() results = connection.execute( text(''' SELECT competences.id, competences.date, tracks.name as track, categories.NAME AS category, tracks.size AS track_size FROM public.competences INNER JOIN user_competences ON public.competences.id = public.user_competences.competences_id INNER JOIN categories ON competences.category_id = categories.id INNER JOIN tracks ON competences.track_id = tracks.id WHERE user_competences.user_id = :user_id;'''), ({ "user_id": current_user.id })) competences = query_to_dict(results) print(competences) competences = session.query( Competence, Track, Category, UserCompetence).join( UserCompetence, Track, Category).filter(UserCompetence.user_id == current_user.id).all() print(competences) return render_template('competence-table.html', competences=competences)
def pre_check(self): ''' 覆盖manager的pre_check 每次检查之前都获取判断规则 ''' #获取instance列表,得到所有机器和角色的对应关系 self.role_host_map = {} self.rule_lists = [] self.group_host_map = {} session = database.getSession() for instance in session.query(Instance).filter(Instance.status==Instance.STATUS_START) : host = instance.host role = instance.role if not self.role_host_map.has_key(role) : self.role_host_map[role] = [] self.role_host_map[role].append(host) for gh in session.query(GroupHost): group = gh.group host = gh.hostname if not self.group_host_map.has_key(group) : self.group_host_map[group] = [] self.group_host_map[group].append(host) for alarm in session.query(Alarm): active_lists, except_lists = alarm.get_host_tuple() self.rule_lists.append( Rule(alarm.name,alarm.expression,alarm.callback,active_lists,except_lists) ) session.close()
def graphicPreviousCompetitions(id): session = getSession() competence = session.query(Competence).filter(Competence.id == id).first() competences = session.query( Competence, UserCompetence).join(UserCompetence).filter( Competence.date <= competence.date, UserCompetence.user_id == current_user.id, Competence.track_id == competence.track_id).limit(6).all() result = [] for item in competences: c = item.Competence uc = item.UserCompetence result.append({ 'turns': uc.turns, 'duration': uc.duration, 'competence': { 'id': c.id, 'date': c.date.isoformat(), 'name_competence': c.name_competence, 'track_id': c.track_id, 'category_id': c.category_id, }, }) return jsonify(result)
def fade_add_del_service(addRunningId,delRunningId,msg=""): time.sleep(5) session=database.getSession() for id in addRunningId: database.update_task(session,id,Task.STATUS_FINISH,Task.RESULT_SUCCESS,msg) for id in delRunningId: database.update_task(session,id,Task.STATUS_FINISH,Task.RESULT_SUCCESS,msg) session.close()
def namenode_web(self): session = database.getSession() nm_web_port = database.get_service_conf(session,"hdfs","dfs_namenode_http_address_port") nms = [] for inst in session.query(Instance).filter(Instance.role == "namenode"): nms.append(inst.host) session.close() return namenode_web.namenode_web(nms, nm_web_port)
def init_alarm_autofix(): session = database.getSession() session.merge(AlarmAutofix(".*(datanode down)",15,"hdfs","datanode","restart")) session.merge(AlarmAutofix(".*(nodemanager down)",15,"yarn","nodemanager","restart")) session.commit() session.close()
def one(id): session = getSession() competence = session.query( Competence, Track, Category, UserCompetence).join(UserCompetence, Track, Category).filter( Competence.id == id, UserCompetence.user_id == current_user.id).first() return render_template('competence.html', competence=competence)
def run_update(instance): session = database.getSession(False) session.query(Instance).filter(Instance.id==instance.id) \ .update({Instance.health: instance.health, Instance.monitor_time: instance.monitor_time, Instance.msg: instance.msg}) session.commit() session.close()
def update_msg(task, line): session = database.getSession(False) msg = task.msg or "" msg = msg + line task.msg = msg session.query(Task).filter(Task.id == task.id).update({Task.msg: task.msg}) session.commit() session.close()
def run_update(task): session = database.getSession(False) session.query(Task).filter(Task.id==task.id) \ .update({Task.status: task.status, Task.result: task.result, Task.msg: task.msg}) session.commit() session.close()
def init_alarm_assist(): session = database.getSession() #alarm var session.merge(AlarmAssist("mail_to","*****@*****.**")) session.merge(AlarmAssist("ignore_key_word","")) session.commit() session.close()
def init_alarm_assist(): session = database.getSession() #alarm var session.merge(AlarmAssist("mail_to", "*****@*****.**")) session.merge(AlarmAssist("ignore_key_word", "")) session.commit() session.close()
def update_msg(task, line): session = database.getSession(False) msg = task.msg or "" msg = msg + line task.msg = msg session.query(Task).filter(Task.id==task.id) \ .update({Task.msg: task.msg}) session.commit() session.close()
def run_update(instance): session = database.getSession(False) try: session.query(Instance).filter(Instance.id==instance.id) \ .update({Instance.health: instance.health, Instance.monitor_time: instance.monitor_time, Instance.msg: instance.msg}) session.commit() finally: session.close()
def delete(id): session = getSession() training = session.query(Training).filter( Training.id == id, Training.user_id == current_user.id ) if(training.first()): training.delete() session.commit() return redirect(url_for('training.trainings')) return "No tienes acceso"
def delete(id): session = getSession() competence = session.query(UserCompetence).filter( UserCompetence.competences_id == id, UserCompetence.user_id == current_user.id) if competence.first(): competence.delete() session.commit() return redirect(url_for('competence.index')) return "No tienes acceso"
def collectApp(self): #获取所有的过去时段完成的app的列表 apps = self.getAppList() if not apps or not apps["apps"]: log.info("no appid match") return startCollectTime = time.time() #轮询app列表,获取每个app的详细信息 for app in apps["apps"]["app"]: startTime = time.time() appid = app["id"] if app['state'] == 'FINISHED': try: jobid = appidToJobid(appid) jobHistory = self.getJobHistory(jobid) if jobHistory: jobCounter = self.getJobCounter(jobid) jobTasks = self.getJobAllTask(jobid) self.updateWithAppid(app,jobHistory,jobCounter) else: log.info("find some app run success but no history file:"+appid) except: log.exception("get error while doing app "+appid) endTime = time.time() else: self.updateWithNotSuccAppid(app) log.info("getting appid: %s using %d ms" % (appid, (endTime - startTime)*1000)) endCollectTime = time.time() log.info("using %d ms to collect the data" % ((endCollectTime - startCollectTime)*1000) ) startFlushTime = time.time() #提交数据 session = database.getSession() for (appid,appRecordValue) in self.appList.items(): session.merge(appRecordValue) session.commit() log.info("push %d appRecord into table" % (len(self.appList))) for (key,NmRecordValue) in self.nmList.items(): session.merge(NmRecordValue) session.commit() log.info("push %d NmRecord into table" % (len(self.nmList))) for (key,RmRecordValue) in self.rmList.items(): session.merge(RmRecordValue) session.commit() log.info("push %d RmRecord into table" % (len(self.rmList))) endFlushTime = time.time() log.info("using %d ms to push to the db" % ((endFlushTime - startFlushTime)*1000))
def init_alarm_autofix(): session = database.getSession() session.merge( AlarmAutofix(".*(datanode down)", 15, "hdfs", "datanode", "restart")) session.merge( AlarmAutofix(".*(nodemanager down)", 15, "yarn", "nodemanager", "restart")) session.commit() session.close()
def conf_to_db(dir): group_var_dir = os.path.join(dir, "group_vars") host_var_dir = os.path.join(dir, "host_vars") session = database.getSession() #将机器和组的关系写入数据库 #TODO #读取组变量 for group in os.listdir(group_var_dir): if group.startswith("."): continue stream = open(os.path.join(group_var_dir, group), "r") vars = yaml.load(stream) for name in vars: value = vars[name] if name.find("__") > 0: (service, new_name) = name.split("__") else: service = "" new_name = name if isinstance(value, list): gv = GroupVar(group, service, new_name, ",".join(value), 1, "") else: gv = GroupVar(group, service, new_name, value, 0, "") session.merge(gv) #读取机器变量 if os.path.exists(host_var_dir): for host in os.listdir(host_var_dir): if host.startswith("."): continue stream = open(os.path.join(host_var_dir, host), "r") vars = yaml.load(stream) for name in vars: value = vars[name] if name.find("__") > 0: (service, new_name) = name.split("__") else: service = "" new_name = name if isinstance(value, list): hv = HostVar(host, service, new_name, ",".join(value), 1, "") else: hv = HostVar(host, service, new_name, value, 0, "") session.merge(hv) session.commit() session.close()
def loginPost(): email = request.form.get('email') password = toBytes(request.form.get('password')) session = getSession() user = session.query(User).filter_by(email=email).first() hashed = toBytes(user.password) if bcrypt.checkpw(password, hashed): login_user(user) return redirect(url_for('main')) else: return "Constraseña incorrecta"
def getdb(): session = database.getSession() task = Task("1", "2", "3") session.add(task) session.flush() print task.id session.commit() objects = session.query(Task) for object in objects: print object.id, object.task print objects
def getdb(): session=database.getSession() task = Task("1","2","3") session.add(task) session.flush() print task.id session.commit() objects = session.query(Task) for object in objects: print object.id,object.task print objects
def getPerformance(training: Training, track: Track = None, category: Category = None): session = getSession() if not track: track = session.query(Track).get(training.track_id) if not category: category = session.query(Category).get(training.category_id) size = int(track.size) / 1000 turns = (7 / (size * 60)) / (1 / (3 * category.duration_max)) return (training.turns * 100) / turns
def post(self): username = self.get_argument('useranme') password = self.get_argument('password') session = database.getSession() user = session.query(User).filter(and_(User.name == username, User.password == password)).first() session.close() if user: self.set_current_user({'name': user.name, 'password': user.password,"type": user.type }) self.login_jump(user.type) else: self.redirect('/login')
def resourcemanager_web(self): session = database.getSession() rm_port = database.get_service_conf(session,"yarn","yarn_rm_webapp_port") rms = [] for inst in session.query(Instance).filter(Instance.role == "resourcemanager"): rms.append(inst.host) session.close() if len(rms) == 0 : return (contants.ALARM_ERROR, u"%s 检查不到有resourcemanager" % self.rule.name ) if len(rms) != 1 : return (contants.ALARM_ERROR, u"%s 检查到有多个resourcemanager %s" % (self.rule.name, ",".join(rms) ) ) return resourcemanager_web.resourcemanager_web(rms[0],rm_port)
def conf_to_db(dir): group_var_dir=os.path.join(dir,"group_vars") host_var_dir=os.path.join(dir,"host_vars") session=database.getSession() #将机器和组的关系写入数据库 #TODO #读取组变量 for group in os.listdir(group_var_dir): if group.startswith("."): continue stream=open(os.path.join(group_var_dir,group),"r") vars=yaml.load(stream) for name in vars: value = vars[name] if name.find("__") > 0: (service,new_name) = name.split("__") else: service="" new_name=name if isinstance(value,list): gv = GroupVar(group,service,new_name,",".join(value),1,"") else: gv = GroupVar(group,service,new_name,value,0,"") session.merge(gv) #读取机器变量 if os.path.exists(host_var_dir): for host in os.listdir(host_var_dir): if host.startswith("."): continue stream=open(os.path.join(host_var_dir,host),"r") vars=yaml.load(stream) for name in vars: value = vars[name] if name.find("__") > 0: (service,new_name) = name.split("__") else: service="" new_name=name if isinstance(value,list): hv = HostVar(host,service,new_name,",".join(value),1,"") else: hv = HostVar(host,service,new_name,value,0,"") session.merge(hv) session.commit() session.close()
def before_request(): if 'access_key' in session: try: g.sess = database.getSession(session['id']) g.user = g.sess.user except: g.user = None g.sess = None else: g.user = None g.sess = None
def load_db_conf(): conf = {} session=database.getSession() for gv in session.query(GroupVar): if gv.group == "all" : if gv.service !=None and len(gv.service) > 0 : name = "%s__%s" % (gv.service,gv.name) else: name = gv.name conf[name] = { "value": gv.value , "type": gv.type } session.close() return conf
def load_db_conf(): conf = {} session = database.getSession() for gv in session.query(GroupVar): if gv.group == "all": if gv.service != None and len(gv.service) > 0: name = "%s__%s" % (gv.service, gv.name) else: name = gv.name conf[name] = {"value": gv.value, "type": gv.type} session.close() return conf
def fade_add_del_service(addRunningId, delRunningId, msg=""): ''' 已经弃用 ''' time.sleep(5) session = database.getSession() for id in addRunningId: database.update_task(session, id, Task.STATUS_FINISH, Task.RESULT_SUCCESS, msg) for id in delRunningId: database.update_task(session, id, Task.STATUS_FINISH, Task.RESULT_SUCCESS, msg) session.close()
def getTrending(key: str, model: Union[Track, Category], limit=8): session = getSession() sumColumn = ( func.count(getattr(Training, key)) + func.count(getattr(Competence, key)) ).label("count") query = session.query( sumColumn, model ).join(Training, Competence, UserCompetence, isouter=True)\ .filter(or_(Training.user_id == current_user.id, UserCompetence.user_id == current_user.id))\ .group_by(model.id).order_by(sumColumn.desc())\ .limit(limit) return query.all()
def query_all_instance(): """query all instance like sql : "SELECT id, service, role, hostname FROM instance" return: [ instance ] """ session = database.getSession(False) try: rows = session.query(Instance) finally: session.close() return rows
def query_tasks(ids=None): """query special id tasks param: ids: [ taskid ] return: [ task ] """ session = database.getSession(False) tasks = session.query(Task).filter( and_(Task.status.in_([Task.STATUS_INIT]), Task.taskType.in_(["auto_fix", "ansible", "shell"])) ) if ids: tasks = tasks.filter(Task.id.in_(ids)) session.close() return tasks
def init_alarm(): session = database.getSession() for alarm in session.query(Alarm): session.delete(alarm) session.commit() #base machine resource check session.add(Alarm("check load one","max(load_one,cpu_num,cpu_num*2)","send_mail","all")) session.add(Alarm("check disk use","disk_use(90,95)","send_mail","all")) session.add(Alarm("check swap","max((swap_total-swap_free)/mem_total,0.5,0.7)","send_mail","all")) session.add(Alarm("check memory","min(mem_free+mem_cached,2048*1024,1024*1024)","send_mail","all")) session.add(Alarm("check bytesin","max(bytes_in,50*1024*1024,80*1024*1024)","send_mail","all")) session.add(Alarm("check bytesout","max(bytes_out,50*1024*1024,80*1024*1024)","send_mail","all")) session.add(Alarm("check swapadd","max(swap_free-PRE_swap_free,10*1024*1024,100*1024*1024)","send_mail","all")) #instance check state session.add(Alarm("check instance","check_instance_state()","send_mail","cluster")) #zookeeper check session.add(Alarm("check zk heap","max(zookeeper_memory_memHeapUsed/zookeeper_memory_memHeapCommitted,0.8,0.9)","send_mail","zookeeper")) #hdfs check session.add(Alarm("check missing blocks","max(dfs.FSNamesystem.MissingBlocks,0,5)","send_mail","namenode")) session.add(Alarm("check lost heartbeat","max(dfs.FSNamesystem.ExpiredHeartbeats-PRE_dfs.FSNamesystem.ExpiredHeartbeats,0,0)","send_mail","namenode")) session.add(Alarm("check nmweb","namenode_web()","send_mail","cluster")) session.add(Alarm("check nn heap","max(jvm.JvmMetrics.ProcessName=NameNode.MemHeapUsedM/jvm.JvmMetrics.ProcessName=NameNode.MemHeapCommittedM,0.8,0.9)","send_mail","namenode")) session.add(Alarm("check dn heap","max(jvm.JvmMetrics.ProcessName=DataNode.MemHeapUsedM/jvm.JvmMetrics.ProcessName=DataNode.MemHeapCommittedM,0.8,0.9)","send_mail","datanode")) #yarn check session.add(Alarm("check rmweb","resourcemanager_web()","send_mail","cluster")) session.add(Alarm("check rm heap","max(jvm.JvmMetrics.ProcessName=ResourceManager.MemHeapUsedM/jvm.JvmMetrics.ProcessName=ResourceManager.MemHeapCommittedM,0.8,0.9)","send_mail","resourcemanager")) session.add(Alarm("check nm heap","max(jvm.JvmMetrics.ProcessName=NodeManager.MemHeapUsedM/jvm.JvmMetrics.ProcessName=NodeManager.MemHeapCommittedM,0.8,0.9)","send_mail","nodemanager")) #hbase session.add(Alarm("check hm heap","max(hbasemaster_memory_memHeapUsed/hbasemaster_memory_memHeapCommitted,0.8,0.9)","send_mail","hbasemaster")) session.add(Alarm("check rs heap","max(regionserver_memory_memHeapUsed/regionserver_memory_memHeapCommitted,0.8,0.9)","send_mail","regionserver")) #hive session.add(Alarm("check metastore heap","max(hivemetastore_memory_memHeapUsed/hivemetastore_memory_memHeapCommitted,0.8,0.9)","send_mail","hivemetastore")) session.add(Alarm("check hiveserver heap","max(hiveserver_memory_memHeapUsed/hiveserver_memory_memHeapCommitted,0.8,0.9)","send_mail","hiveserver")) session.add(Alarm("check hiveserver2 heap","max(hiveserver2_memory_memHeapUsed/hiveserver2_memory_memHeapCommitted,0.8,0.9)","send_mail","hiveserver2")) session.commit() session.close()
def resourcemanager_web(self): session = database.getSession() rm_port = database.get_service_conf(session,"yarn","yarn_rm_webapp_port") rms = [] for inst in session.query(Instance).filter(Instance.role == "resourcemanager"): rms.append(inst.host) session.close() alarm_list = [] if len(rms) == 0 : key_word = "cluster(no rm)" msg = u"%s 检查不到有resourcemanager" % self.rule.name return [{"key_word":key_word,"msg":msg}] if len(rms) != 1 : key_word = "cluster(too much rm)" msg = u"%s 检查到有多个resourcemanager %s" % (self.rule.name, ",".join(rms) ) return [{"key_word":key_word,"msg":msg}] return resourcemanager_web.resourcemanager_web(rms[0],rm_port)
def _save_to_db(self): session = database.getSession() groups = self.groups # 组变量 for (gname, group) in groups.items(): if gname == self.META: continue if self.VARS not in group: continue gname = gname.lower() for (name, value) in group[self.VARS].items(): if name.find("__") > 0: (service, new_name) = name.split("__") else: service="" new_name=name if isinstance(value, list): gv = GroupVar(gname, service, new_name, ",".join(value), 1, "") else: gv = GroupVar(gname, service, new_name, value, 0, "") session.merge(gv) session.commit() #读取机器变量 host_vars = {} if self.META in groups and self.HVARS in groups[self.META]: host_vars = groups[self.META][self.HVARS] for (host, vars) in host_vars.items(): for (name, value) in vars.items(): if name.find("__") > 0: (service, new_name) = name.split("__") else: service="" new_name=name if isinstance(value,list): hv = HostVar(host,service,new_name,",".join(value),1,"") else: hv = HostVar(host,service,new_name,value,0,"") session.merge(hv) session.commit() session.close()
def init_config(self): ''' 初始化关于获取端口等的内容 默认从数据库中获取,由于可能只使用监控 所以本函数可以直接修改为固定值 ''' session = database.getSession() insts = session.query(Instance).filter(Instance.role=="resourcemanager") for inst in insts: self.rmhost = inst.host; self.rmport=database.get_service_conf(session,"yarn","yarn_rm_webapp_port") insts = session.query(Instance).filter(Instance.role=="historyserver") for inst in insts: self.hshost = inst.host; self.hsport = database.get_service_conf(session,"yarn","mapreduce_jobhistory_webapp_port") session.close()
def update_db(diff_result): session = database.getSession() group = "all" file_only = diff_result["file_only"] for temp in file_only: name = temp["name"] value = temp["value"] type = temp["type"] print "import name:%s value: %s" % (name, value) if name.find("__") > 0: (service, new_name) = name.split("__") else: service = "" new_name = name if type == 1: gv = GroupVar(group, service, new_name, value, 1, "") else: gv = GroupVar(group, service, new_name, value, 0, "") session.add(gv) session.commit() session.close()
def conf_to_db(file): print "import %s into db" % file session=database.getSession() stream=open(file,"r") vars=yaml.load(stream) group="all" for name in vars: value = vars[name] if name.find("__") > 0: (service,new_name) = name.split("__") else: service="" new_name=name if isinstance(value,list): gv = GroupVar(group,service,new_name,",".join(value),1,"") else: gv = GroupVar(group,service,new_name,value,0,"") session.merge(gv) session.close()