Beispiel #1
0
class MonitorBss(threading.Thread):
    ""

    def __init__(self):
        ""
        threading.Thread.__init__(self)
        self.logger = LoggerUtil().getLogger()
        self.conf = ConfigManage()
        self.monitorAction = MonitorAction()

    # 收到请求处理函数
    def process_action(self, message):
        ""
        self.logger.debug("received monitor mq message")
        # 1 返回信息
        routing_key = message.delivery_info['routing_key']
        exchange = message.delivery_info['exchange']
        msg = message.body
        action = routing_key.split('.', 1)[1]
        #self.logger.info('action: %s ;message: %s'%(action,str(msg)))

        js = JsonParser()
        json_msg = js.decode(msg)

        # 2 json格式报错,日志记录
        if json_msg == -1:
            self.logger.error('josn format error!')
            return

        # 3业务处理
        try:
            self.monitorAction.processMq(action, json_msg)
        except Exception, e:
            log = str(e)
            self.logger.error(log)
Beispiel #2
0
class WarnDbBss(threading.Thread):
    ""

    def __init__(self):
        ""
        threading.Thread.__init__(self)
        self.logger = LoggerUtil().getLogger()
        self.conf = ConfigManage()
        self.warn2db_frequency = self.conf.get_monitor_conf(
        )['warn2db_frequency']

        self.warnDbAction = WarnDbAction()

    def process(self):
        self.warnDbAction.processVmRunStat()

    def run(self):
        while True:
            try:
                time.sleep(self.warn2db_frequency)
                self.logger.debug("warndb bss start is runing")
                self.process()
            except Exception, e:
                log = str(e)
                self.logger.error(log)
Beispiel #3
0
class MonitorDbBss(threading.Thread):
    ""

    def __init__(self):
        ""
        threading.Thread.__init__(self)
        self.logger = LoggerUtil().getLogger()
        self.conf = ConfigManage()
        self.monitor_frequency = self.conf.get_monitor_conf(
        )['monitor_frequency']
        self.monitorDbAction = MonitorDbAction()

    def process(self):
        try:
            self.logger.debug("start is runing")
            self.monitorDbAction.processVmRunStat()
        except Exception, e:
            log = str(e)
            self.logger.error(log)
            time.sleep(300)
Beispiel #4
0
class TaskBss(threading.Thread) :
    ""
    
    def __init__(self) :
        ""
        threading.Thread.__init__(self)
        
        self.logger = LoggerUtil().getLogger()
        self.conf = ConfigManage()
        self.bs = BusinessAction()
    
    # 收到请求处理函数
    def process_action(self, taskinfo):
        ""        
        #业务处理
        try :
            self.bs.processTask(taskinfo)
        except Exception,e:
            print e
            log = str(e)
            self.logger.error(log)
Beispiel #5
0
class MonitorDbAction:
    ""
    tableTime = None

    def __init__(self):
        ""
        self.logger = LoggerUtil().getLogger()
        self.vmUseStat = VmUseStat()
        self.conf = ConfigManage()
        self.monitor_keep_max_time = self.conf.get_monitor_conf(
        )['monitor_keep_max_time']

    def __getDb(self):
        db_conf = self.conf.get_db_conf()
        db = MysqlTools(db_conf)
        return db

    def processVmRunStat(self):
        """
        @summary: 虚拟机和应用资源使用监控信息入库
        """
        nowTime = time.localtime(time.time())
        now = time.strftime('%Y-%m-%d %H:%M:%S', nowTime)
        cur_tableTime = time.strftime('%Y%m', nowTime)
        #获取数据操作对象
        db = self.__getDb()

        if MonitorDbAction.tableTime != cur_tableTime:
            self.createTable(db, cur_tableTime)
            MonitorDbAction.tableTime = cur_tableTime

        vmRunInfos = self.vmUseStat.cleanAllVmRunInfo(
            keepTime=self.monitor_keep_max_time)
        vm_running_param = []
        appinstance_running_param = []

        for info in vmRunInfos:
            keys = info.keys()
            logId = UUIDUtils.getId()
            vmName = info['vmName']
            status = info['vmState']

            cpuRate = float(0)
            memRate = float(0)
            flow = long(0)

            eventTime = info['eventTime']
            receiveTime = info['receiveTime']

            if 'cpuRate' in keys:
                cpuRate = float(info['cpuRate'])
            if 'memRate' in keys:
                memRate = float(info['memRate'])
            if 'flow' in keys:
                flow = long(info['flow'])

            if 'instanceId' in keys and info['instanceId'] is not None and len(
                    info['instanceId']) > 0:
                #app 应用 监控
                appinstance_running_param.append(
                    (logId, info['instanceId'], status, cpuRate, memRate, flow,
                     eventTime, receiveTime))
            else:
                #vm 监控
                vm_running_param.append(
                    (logId, vmName, status, cpuRate, memRate, flow, eventTime,
                     receiveTime))

        try:
            if len(vm_running_param) != 0:
                sql = "insert into log_vm_running" + MonitorDbAction.tableTime + "(logId,vmName,status,cpuRate,memRate,flow,eventTime,receiveTime,createTime)values(%s,%s,%s,%s,%s,%s,%s,%s,now())"
                db.insertMany(sql, vm_running_param)
        except Exception, e:
            self.logger.error("insert vm running info to db was error,msg:" +
                              str(e))
        try:
            if len(appinstance_running_param) != 0:
                sql = "insert into log_appinstance_running" + MonitorDbAction.tableTime + "(logId,instanceId,status,cpuRate,memRate,flow,eventTime,receiveTime,createTime)values(%s,%s,%s,%s,%s,%s,%s,%s,now())"
                db.insertMany(sql, appinstance_running_param)
        except Exception, e:
            self.logger.error(
                "insert vm app instance running info to db was error,msg:" +
                str(e))
Beispiel #6
0
class AppDrop:
    ""

    def __init__(self) :
        ""
        self.logger = LoggerUtil().getLogger()
        self.conf = ConfigManage()
        self.mq = Mq()
    
    def __getDb(self) :
        db_conf = self.conf.get_db_conf()
        db = MysqlTools(db_conf)
        return db        
    
    
    def appDrop(self, taskinfo):
        """
        taskStatus :
          10 : 等待删除
          20 : 删除MQ消息发送
          
          90 : 删除成功
          91 : 删除失败
        """
        taskId = taskinfo['taskId']        
        taskStatus = taskinfo['taskStatus']        
        if taskStatus == '10':
            self.appDrop_10(taskinfo)
        else :
            self.logger.warning('Not support taskinfo,taskId:'+taskId+',taskStatus:'+taskStatus)
    
    
    
    #------appdrop------#
    #任务一
    def appDrop_10(self, taskinfo):
        ""
        self.logger.debug('appDrop_10: begin drop app ')
        jsonParser = JsonParser()        
        instanceId = taskinfo['businessId']
        taskId = taskinfo['taskId']
        
        #获取数据操作对象
        db = self.__getDb()
        # 查询instance表
        instanceInfoModule = InstanceInfoModule(db)
        instanceInfo = instanceInfoModule.getById(instanceId)
        # 获取engineid
        engineId = instanceInfo['engineId']
        
        
        # 发现消息
        action = 'app.drop.apply'
        router_key = engineId + ".app.drop.apply"
        dealwithId = UUIDUtils.getId()
        
        # 删除应用消息
        message = {}
        message['action'] = action
        message['taskId'] = dealwithId
        message['content'] = {}
        message['content']['instanceId'] = instanceId
        message = jsonParser.encode(message)
        
        
        
        #写任务处理表库(DealwithInfoModule)
        self.logger.debug('appDrop_10: write drop message to dealwithInfo table')
        
        dim_param = {}
        dim_param['dealwithId'] = dealwithId
        dim_param['taskId'] = taskId
        dim_param['dealwithType'] = '20'
        dim_param['message'] = message
        dim_param['status'] = 'success'
        dealwithInfoModule = DealwithInfoModule(db)        
        dealwithInfoModule.insert(dim_param)
        
        
        #开启事物
        db.begin()
        try :
            #修改任务信息表(TaskinfoModule)
            self.logger.debug('appDrop_10: update task status')
            tim_param = {}
            tim_param['taskId'] = taskId
            tim_param['taskStatus'] = '20'
            tim_param['dealwith'] = 'close'
            taskinfoModule = TaskinfoModule(db)
            res = taskinfoModule.update(tim_param)
            
            
            #发送消息
            self.logger.debug('appDrop_10: send drop app request to task of mq')
            self.mq.send_task_message(router_key, message)
            
            db.end()
            self.logger.debug('appDrop_10: process task is over')
        except Exception,e:
            db.end(option='rollback')
            self.logger.error('appDrop_10: process task error, error message: '+str(e))
            
        finally :
Beispiel #7
0
class VmCreate:
    ""

    def __init__(self):
        ""
        self.logger = LoggerUtil().getLogger()
        self.conf = ConfigManage()
        self.mq = Mq()

    def __getDb(self):
        db_conf = self.conf.get_db_conf()
        db = MysqlTools(db_conf)
        return db

    def vmCreate(self, taskinfo):
        """
        @summary: 软件创建任务
        @param:taskinfo 任务内容
        taskStatus :
          10 : 等待创建
          20 : 创建询问广播
          30 : 接收资源应答
          40 : 创建申请
          90 : 创建成功
          91 : 创建失败
        """
        taskId = taskinfo['taskId']
        taskStatus = taskinfo['taskStatus']
        if taskStatus == '10':
            self.vmCreate_10(taskinfo)
        else:
            self.logger.warning('Not support taskinfo,taskId:' + taskId +
                                ',taskStatus:' + taskStatus)

    #------vmdeploy------#
    #任务一
    def vmCreate_10(self, taskinfo):
        """
        @summary: 待创建任务
        @param:taskinfo 任务内容
        """
        #发送ask广播action=bdct.vm.deploy.ask
        #更改taskStatus=20等待资源,dealwith=close
        self.logger.debug('vmCreate_10: begin dealwith task')
        jsonParser = JsonParser()

        taskId = taskinfo['taskId']
        content = taskinfo['content']
        businessId = taskinfo['businessId']
        contentObj = jsonParser.decode(content)
        cpu = contentObj['cpu']
        mem = contentObj['mem']
        disk = contentObj['size']
        imageId = contentObj['imageId']

        #获取数据操作对象
        db = self.__getDb()
        router_key = 'bdct.vm.create.ask'
        action = 'vm.create.ask'
        dealwithId = UUIDUtils.getId()

        # 查询vm类型
        vmImage = VmImageModule(db)
        ImageInfo = vmImage.getByImageId(imageId)
        vmType = ImageInfo['vtype']

        #创建询问广播消息
        message = {}
        message['action'] = action
        message['taskId'] = dealwithId
        message['content'] = {}
        message['content']['cpu'] = cpu
        message['content']['mem'] = mem
        message['content']['disk'] = disk
        message['content']['vmType'] = vmType
        message = jsonParser.encode(message)

        #写任务处理表库(DealwithInfoModule)
        self.logger.debug(
            'vmCreate_10: write send ask message to dealwithInfo table')

        dim_param = {}
        dim_param['dealwithId'] = dealwithId
        dim_param['taskId'] = taskId
        dim_param['dealwithType'] = '20'
        dim_param['message'] = message
        dim_param['status'] = 'success'
        dealwithInfoModule = DealwithInfoModule(db)
        dealwithInfoModule.insert(dim_param)

        self.logger.debug('vmCreate_10: begin change task status')
        #开启事物
        db.begin()
        try:
            #修改任务信息表(TaskinfoModule)
            self.logger.debug('vmCreate_10: update task status')

            tim_param = {}
            tim_param['taskId'] = taskId
            tim_param['taskStatus'] = '20'
            tim_param['dealwith'] = 'close'
            taskinfoModule = TaskinfoModule(db)
            res = taskinfoModule.update(tim_param)

            #发送广播消息
            self.logger.debug(
                'vmCreate_10: send ask message to the bdct of mq')
            self.mq.send_bdct_message(router_key, message)
            db.end()
            self.logger.debug('vmCreate_10: process task is over')
        except Exception, e:
            db.end(option='rollback')
            self.logger.error(
                'vmCreate_10: process task error, error message: ' + str(e))

        finally:
Beispiel #8
0
class AppDeploy:
    ""

    def __init__(self):
        ""
        self.logger = LoggerUtil().getLogger()
        self.conf = ConfigManage()
        self.mq = Mq()

    def __getDb(self):
        db_conf = self.conf.get_db_conf()
        db = MysqlTools(db_conf)
        return db

    def appDeploy(self, taskinfo):
        """
        @summary: 软件部署任务
        @param:taskinfo 任务内容
        taskStatus :
          10 : 等待部署
          20 : 部署询问广播
          30 : 接收资源应答
          40 : 部署申请
          90 : 部署成功
          91 : 部署失败
        """
        taskId = taskinfo['taskId']
        taskStatus = taskinfo['taskStatus']
        if taskStatus == '10':
            self.appDeploy_10(taskinfo)
        else:
            self.logger.warning('Not support taskinfo,taskId:' + taskId +
                                ',taskStatus:' + taskStatus)

    #------appdeploy------#
    #任务一
    def appDeploy_10(self, taskinfo):
        """
        @summary: 待部署任务
        @param:taskinfo 任务内容
        """
        #发送ask广播action=bdct.app.deploy.ask
        #更改taskStatus=20等待资源,dealwith=close
        self.logger.debug('appDeploy_10: begin dealwith task')
        jsonParser = JsonParser()

        taskId = taskinfo['taskId']
        content = taskinfo['content']
        businessId = taskinfo['businessId']
        contentObj = jsonParser.decode(content)
        cpu = contentObj['cpu']
        mem = contentObj['mem']
        disk = contentObj['disk']
        serviceId = contentObj['serviceId']

        #获取数据操作对象
        db = self.__getDb()
        router_key = 'bdct.app.deploy.ask'
        action = 'app.deploy.ask'
        dealwithId = UUIDUtils.getId()

        #部署询问广播消息
        message = {}
        message['action'] = action
        message['taskId'] = dealwithId
        message['content'] = {}
        message['content']['cpu'] = cpu
        message['content']['mem'] = mem
        message['content']['disk'] = disk
        message['content']['serviceId'] = serviceId
        message = jsonParser.encode(message)

        #写任务处理表库(DealwithInfoModule)
        self.logger.debug(
            'appDeploy_10: write send ask message to dealwithInfo table')
        'dealwithId,taskId,dealwithType,message,status'
        dim_param = {}
        dim_param['dealwithId'] = dealwithId
        dim_param['taskId'] = taskId
        dim_param['dealwithType'] = '20'
        dim_param['message'] = message
        dim_param['status'] = 'success'
        print "1999"
        dealwithInfoModule = DealwithInfoModule(db)
        dealwithInfoModule.insert(dim_param)

        self.logger.debug('appDeploy_10: begin change task status')
        #开启事物
        db.begin()
        try:
            #修改任务信息表(TaskinfoModule)
            self.logger.debug('appDeploy_10: update task status')
            'taskStatus=20,dealwith=close'
            tim_param = {}
            tim_param['taskId'] = taskId
            tim_param['taskStatus'] = '20'
            tim_param['dealwith'] = 'close'
            taskinfoModule = TaskinfoModule(db)
            res = taskinfoModule.update(tim_param)

            #修改应用实例表(InstanceInfoModule)
            self.logger.debug('appDeploy_10: update instance status')
            'status=deploy'
            iim_param = {}
            iim_param['instanceId'] = businessId
            iim_param['status'] = 'deploy'
            instanceInfoModule = InstanceInfoModule(db)
            instanceInfoModule.update(iim_param)

            #发送广播消息
            self.logger.debug(
                'appDeploy_10: send ask message to the bdct of mq')
            self.mq.send_bdct_message(router_key, message)
            db.end()
            self.logger.debug('appDeploy_10: process task is over')
        except Exception, e:
            db.end(option='rollback')
            self.logger.error(
                'appDeploy_10: process task error, error message: ' + str(e))

        finally:
Beispiel #9
0
class WarnDbAction:
    ""
    tableTime = None

    def __init__(self):
        ""
        self.logger = LoggerUtil().getLogger()
        self.vmWarnStat = VmWarnStat()
        self.conf = ConfigManage()

    def __getDb(self):
        db_conf = self.conf.get_db_conf()
        db = MysqlTools(db_conf)
        return db

    def processVmRunStat(self):
        """
        @summary: 虚拟机和应用资源使用监控信息入库
        """
        nowTime = time.localtime(time.time())
        now = time.strftime('%Y-%m-%d %H:%M:%S', nowTime)
        cur_tableTime = time.strftime('%Y%m', nowTime)
        #获取数据操作对象
        db = self.__getDb()
        if WarnDbAction.tableTime != cur_tableTime:
            self.createTable(db, cur_tableTime)
            WarnDbAction.tableTime = cur_tableTime

        vmWarnInfos = self.vmWarnStat.getAllVmWarnInfo()
        vm_warn_param = []
        appinstance_warn_param = []

        warnKeys = vmWarnInfos.keys()
        for key in warnKeys:
            info = None
            try:
                info = vmWarnInfos[key]
            except:
                pass
            if info is None or info['in2db'] == False:
                continue

            logId = UUIDUtils.getId()
            keys = info.keys()

            if 'instanceId' in keys and info['instanceId'] is not None and len(
                    info['instanceId']) > 0:
                #app 应用 监控
                appinstance_warn_param.append(
                    (logId, info['instanceId'], info['warnType'],
                     info['vmState'], info['cpuRate'], info['memRate'],
                     info['flow'], info['eventTime'], info['receiveTime'],
                     info['duration'], info['times'], info['maxDuration']))
            else:
                #vm 监控
                vm_warn_param.append(
                    (logId, info['vmName'], info['warnType'], info['vmState'],
                     info['cpuRate'], info['memRate'], info['flow'],
                     info['eventTime'], info['receiveTime'], info['duration'],
                     info['times'], info['maxDuration']))

        try:
            if len(vm_warn_param) != 0:
                sql = "insert into log_vm_warning" + WarnDbAction.tableTime + "(logId,vmName,warnType,status,cpuRate,memRate,flow,eventTime,receiveTime,createTime,duration,times,maxDuration)values(%s,%s,%s,%s,%s,%s,%s,%s,%s,now(),%s,%s,%s)"
                db.insertMany(sql, vm_warn_param)
        except Exception, e:
            self.logger.error("insert vm warn info to db was error,msg:" +
                              str(e))

        try:
            if len(appinstance_warn_param) != 0:
                sql = "insert into log_appinstance_warning" + WarnDbAction.tableTime + "(logId,instanceId,warnType,status,cpuRate,memRate,flow,eventTime,receiveTime,createTime,duration,times,maxDuration)values(%s,%s,%s,%s,%s,%s,%s,%s,%s,now(),%s,%s,%s)"
                db.insertMany(sql, appinstance_warn_param)
        except Exception, e:
            self.logger.error(
                "insert app instance warn info to db was error,msg:" + str(e))
            self.logger.error("param:" + str(appinstance_warn_param))