Esempio n. 1
0
    def process_action(self, message):
        ""
        self.logger.debug("received 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.debug(action + ":" + msg)
        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.bs.processMq(action, json_msg)
        except Exception, e:
            log = str(e)
            self.logger.error(log)
Esempio n. 2
0
    def vmProxy(self, taskinfo):
        """
        @summary: 任务执行结果
        @param:json_msg 消息内容
        """

        self.logger.debug('vmProxy: begin dealwith task')
        jsonParser = JsonParser()

        taskId = taskinfo['taskId']
        content = taskinfo['content']
        businessId = taskinfo['businessId']

        content = jsonParser.decode(content)

        #获取数据操作对象
        db = self.__getDb()

        #读取应用实例信息表
        dealwithInfoModule = DealwithInfoModule(db)

        # 类型
        proxyType = content['type']
        proxyPort = content['port']

        # 查询vm信息
        vmId = content['vmId']
        vm = VminfoModule(db)
        vmInfo = vm.getById(vmId)

        if not vmInfo or not vmInfo['engineId']:
            #写任务信息表
            self.logger.debug(
                'vmProxy: update task status taskStatus=40 and dealwith=close')
            tim_param = {}
            tim_param['taskId'] = taskId
            tim_param['taskStatus'] = '31'
            tim_param['dealwith'] = 'close'
            taskinfoModule = TaskinfoModule(db)
            res = taskinfoModule.update(tim_param)

            #写任务处理表(DealwithInfoModule) 启动申请
            self.logger.debug('vmProxy: write message to dealwithinfo table')
            message = {}
            dealwithId = UUIDUtils.getId()
            message['action'] = 'network.portproxy.error'
            message['taskId'] = dealwithId
            message['content'] = {}
            message['instanceId'] = businessId
            message['errorCode'] = '2011'
            message['errorMessage'] = 'vm not exist.'
            message = jsonParser.encode(message)

            dim_param = {}
            dim_param['dealwithId'] = dealwithId
            dim_param['taskId'] = taskId
            dim_param['dealwithType'] = '31'
            dim_param['message'] = message
            dim_param['status'] = 'failure'
            dealwithInfoModule.insert(dim_param)
            return

        self.logger.debug('vmProxy: start send mq msg.')

        engineId = vmInfo['engineId']
        vmName = vmInfo['vmName']
        ip = vmInfo['ip']

        router_key = engineId + ".network.portproxy.apply"
        action = ".network.portproxy.apply"

        dealwithId = UUIDUtils.getId()
        message = {}
        message['action'] = action
        message['taskId'] = dealwithId
        message['content'] = {}
        message['content']['vmName'] = vmName
        message['content']['type'] = proxyType
        message['content']['ip'] = ip
        message['content']['port'] = proxyPort

        message = jsonParser.encode(message)

        #写任务处理表(DealwithInfoModule) 启动申请消息
        dim_param = {}
        dim_param['dealwithId'] = dealwithId
        dim_param['taskId'] = taskId
        dim_param['dealwithType'] = '20'
        dim_param['message'] = message
        dim_param['status'] = 'success'
        dealwithInfoModule.insert(dim_param)

        #开启事物
        self.logger.debug('vmProxy: begin change the task status')
        db.begin()
        try:
            #更新任务信息表 状态
            self.logger.debug(
                'vmProxy: update task status taskStatus=20 and dealwith=close')

            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('vmProxy: send vm proxy request to task of mq')
            self.mq.send_task_message(router_key, message)
            db.end()
            self.logger.debug('vmProxy: send vm proxy request was over')
        except Exception, e:
            db.end(option='rollback')
            self.logger.debug(
                'vmProxy: process status error, error message: ' + str(e))
Esempio n. 3
0
    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))
Esempio n. 4
0
    def vmCreate_can(self, json_msg):
        """
        @summary: 资源应答处理,向第一个应答的引擎下发创建任务
        @param:taskinfo 任务内容
        """

        #发送创建任务 action=task_rst.vm.deploy.can
        #更改taskStatus=30等待创建结果,dealwith=close
        #写应用实例信息
        self.logger.debug('vmCreate_can: received vmcreate ask reply from mq')
        jsonParser = JsonParser()

        dealwithId = json_msg['taskId']
        engineId = json_msg['content']['engineId']
        engineIp = json_msg['content']['engineIp']

        self.logger.debug('vmCreate_can: reply engineId: ' + engineId +
                          ",taskId: " + dealwithId)

        #获取数据操作对象
        db = self.__getDb()

        #读取任务处理表(DealwithInfoModule)
        self.logger.debug(
            'vmCreate_can: read task dealwithinfo by dealwithid: ' +
            dealwithId)
        dealwithInfoModule = DealwithInfoModule(db)
        dealwithInfo = dealwithInfoModule.getById(dealwithId)
        taskId = dealwithInfo['taskId']

        #读取任务信息表(TaskinfoModule)
        self.logger.debug('vmCreate_can: read task info by taskId: ' + taskId)
        taskinfoModule = TaskinfoModule(db)
        taskinfo = taskinfoModule.getById(taskId)
        businessId = taskinfo['businessId']
        content = taskinfo['content']
        taskStatus = taskinfo['taskStatus']

        if taskStatus != '20':
            '已经接收到资源应答,并且执行了创建'
            self.logger.debug(
                'vmCreate_can: the task has  access to resources: taskStatus:'
                + taskStatus)
            return

        #写任务处理表(DealwithInfoModule) 接收应答
        self.logger.debug(
            'vmCreate_can: write received ask reply message to dealwithinfo table'
        )
        'dealwithId,taskId,dealwithType,message,status'
        dim_param = {}
        dim_param['dealwithId'] = UUIDUtils.getId()
        dim_param['taskId'] = taskId
        dim_param['dealwithType'] = '30'
        dim_param['message'] = jsonParser.encode(json_msg)
        dim_param['status'] = 'success'

        dealwithInfoModule = DealwithInfoModule(db)
        dealwithInfoModule.insert(dim_param)

        #创建申请开始-----------------------------
        self.logger.debug('vmCreate_can: begin deploy the task to engine')
        contentObj = jsonParser.decode(content)
        #资源参数
        vmName = contentObj['vmName']
        cpu = contentObj['cpu']
        mem = contentObj['mem']
        disk = contentObj['size']
        imageId = contentObj['imageId']

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

        action = 'vm.create.apply'
        router_key = engineId + ".vm.create.apply"
        dealwithId = UUIDUtils.getId()

        #创建创建申请消息
        message = {}
        message['action'] = action
        message['taskId'] = dealwithId
        message['content'] = {}
        message['content']['vmName'] = vmName
        message['content']['cpu'] = cpu
        message['content']['mem'] = mem
        message['content']['disk'] = disk
        message['content']['vmType'] = vmType
        # 磁盘
        if 'attachDisks' in contentObj:
            message['content']['attachDisks'] = contentObj['attachDisks']
        # 主机名
        if 'hostName' in contentObj:
            message['content']['hostName'] = contentObj['hostName']

        message['content']['imageId'] = imageId
        message = jsonParser.encode(message)

        # 暂时没有
        # host
        # attachDisks

        #写任务处理表(DealwithInfoModule) 创建申请
        self.logger.debug(
            'vmCreate_can: write deploy message to dealwithinfo table')
        dim_param = {}
        dim_param['dealwithId'] = dealwithId
        dim_param['taskId'] = taskId
        dim_param['dealwithType'] = '40'
        dim_param['message'] = message
        dim_param['status'] = 'success'
        dealwithInfoModule.insert(dim_param)

        #开启事物
        self.logger.debug('vmCreate_can: begin change the task status')
        db.begin()
        try:
            #修改任务信息表(TaskinfoModule)
            self.logger.debug(
                'vmCreate_can: update task status taskStatus=40 and dealwith=close'
            )

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

            #修改应用实例表(InstanceInfoModule)
            self.logger.debug('vmCreate_can: update vminfo engineId=%s' %
                              engineId)

            iim_param = {}
            iim_param['vmId'] = businessId
            iim_param['engineId'] = engineId
            iim_param['engineIp'] = engineIp
            #engineIp
            vminfo = VminfoModule(db)
            vminfo.update(iim_param)

            #发送广播消息
            self.logger.debug(
                'vmCreate_can: send deploy request to task of mq')
            self.mq.send_task_message(router_key, message)
            db.end()
            self.logger.debug('vmCreate_can: send deploy request was over')
        except Exception, e:
            db.end(option='rollback')
            self.logger.debug(
                'vmCreate_can: process status error, error message: ' + str(e))
Esempio n. 5
0
    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))
Esempio n. 6
0
    def appDeploy_can(self, json_msg):
        """
        @summary: 资源应答处理,向第一个应答的引擎下发部署任务
        @param:taskinfo 任务内容
        """
        #发送部署任务 action=task_rst.app.deploy.can
        #更改taskStatus=30等待部署结果,dealwith=close
        #写应用实例信息
        self.logger.debug('appDeploy_can: received deploy ask reply from mq')
        jsonParser = JsonParser()

        dealwithId = json_msg['taskId']
        engineId = json_msg['content']['engineId']
        self.logger.debug('appDeploy_can: reply engineId: ' + engineId +
                          ",taskId: " + dealwithId)
        #获取数据操作对象
        db = self.__getDb()

        #读取任务处理表(DealwithInfoModule)
        self.logger.debug(
            'appDeploy_can: read task dealwithinfo by dealwithid: ' +
            dealwithId)
        dealwithInfoModule = DealwithInfoModule(db)
        self.logger.debug('appDeploy_can: ssss1111 ')
        dealwithInfo = dealwithInfoModule.getById(dealwithId)
        self.logger.debug('appDeploy_can: ssss22222 ')
        taskId = dealwithInfo['taskId']
        self.logger.debug('appDeploy_can: ssss33333 ')

        #读取任务信息表(TaskinfoModule)
        self.logger.debug('appDeploy_can: read task info by taskId: ' + taskId)
        taskinfoModule = TaskinfoModule(db)
        taskinfo = taskinfoModule.getById(taskId)
        businessId = taskinfo['businessId']
        content = taskinfo['content']
        taskStatus = taskinfo['taskStatus']

        if taskStatus != '20':
            '已经接收到资源应答,并且执行了部署'
            self.logger.debug(
                'appDeploy_can: the task has  access to resources: taskStatus:'
                + taskStatus)
            return

        #写任务处理表(DealwithInfoModule) 接收应答
        self.logger.debug(
            'appDeploy_can: write received ask reply message to dealwithinfo table'
        )
        'dealwithId,taskId,dealwithType,message,status'
        dim_param = {}
        dim_param['dealwithId'] = UUIDUtils.getId()
        dim_param['taskId'] = taskId
        dim_param['dealwithType'] = '30'
        dim_param['message'] = jsonParser.encode(json_msg)
        dim_param['status'] = 'success'

        dealwithInfoModule = DealwithInfoModule(db)
        dealwithInfoModule.insert(dim_param)

        #部署申请开始-----------------------------
        self.logger.debug('appDeploy_can: begin deploy the task to engine')
        contentObj = jsonParser.decode(content)
        #资源参数
        cpu = contentObj['cpu']
        mem = contentObj['mem']
        disk = contentObj['disk']
        serviceId = contentObj['serviceId']

        #软件信息
        domain = contentObj['domain']
        appName = contentObj['appName']
        appFileId = contentObj['appFileId']
        #环境变量
        contentKeys = contentObj.keys()
        appEnv = None
        userEnv = None
        if 'appEnv' in contentKeys:
            appEnv = contentObj['appEnv']
        if 'userEnv' in contentKeys:
            userEnv = contentObj['userEnv']

        #”listenPort”

        action = 'app.deploy.apply'
        router_key = engineId + ".app.deploy.apply"
        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['content']['instanceId'] = businessId

        message['content']['domain'] = domain
        message['content']['appName'] = appName
        message['content']['filename'] = appFileId

        message['content']['param'] = {}
        if appEnv is not None:
            message['content']['env'] = appEnv
        if userEnv is not None:
            message['content']['userEnv'] = userEnv
        message = jsonParser.encode(message)

        #写任务处理表(DealwithInfoModule) 部署申请
        self.logger.debug(
            'appDeploy_can: write deploy message to dealwithinfo table')
        dim_param = {}
        dim_param['dealwithId'] = dealwithId
        dim_param['taskId'] = taskId
        dim_param['dealwithType'] = '40'
        dim_param['message'] = message
        dim_param['status'] = 'success'
        dealwithInfoModule.insert(dim_param)

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

            #修改应用实例表(InstanceInfoModule)
            self.logger.debug(
                'appDeploy_can: update instanceinfo status engineId=%s' %
                engineId)
            'engineId=$engineId'
            iim_param = {}
            iim_param['instanceId'] = businessId
            iim_param['engineId'] = engineId
            instanceInfoModule = InstanceInfoModule(db)
            instanceInfoModule.update(iim_param)

            #发送广播消息
            self.logger.debug(
                'appDeploy_can: send deploy request to task of mq')
            self.mq.send_task_message(router_key, message)
            db.end()
            self.logger.debug('appDeploy_can: send deploy request was over')
        except Exception, e:
            db.end(option='rollback')
            self.logger.debug(
                'appDeploy_can: process status error, error message: ' +
                str(e))
Esempio n. 7
0
    def processVmRunStat(self) :
        """
        @summary: 虚拟机和应用资源使用监控信息入库
        """
        _time = time.time()
        nowTime = time.localtime(_time)
        now = time.strftime('%Y-%m-%d %H:%M:%S',nowTime)
        cur_tableTime = time.strftime('%Y%m',nowTime)
        #获取数据操作对象
        db = self.__getDb()
        self.logger.debug("this is running 1......")
        #重新载入
        if WarnAction.resourceLoadTime is None or _time - WarnAction.resourceLoadTime > self.warn_resource_keep_max_time :
            self.logger.debug("this is running 2......")
            self.resourceStat.reloadVmInfo(db)
            self.logger.debug("this is running 3......")
            self.resourceStat.reloadAppInstanceInfo(db)
            self.logger.debug("this is running 4......")
            WarnAction.resourceLoadTime = _time
            
            
        vmRunInfos = self.vmUseStat.getAllVmRunInfo()
        
        rule = '''{
                   "highWarn":{"cpu":90,"mem":90,"flow":8388608,"duration":3},
                   "lowWarn":{"cpu":10,"mem":40,"flow":8000,"duration":240},
                   "overtime":300
                }'''
        jp = JsonParser()
        rule = jp.decode(rule)
        self.logger.debug("warn info:"+str(self.vmWarnStat.getAllVmWarnInfo()))
        #appInstanceKeys = WarnAction.appInstance.keys()
        #vmInstanceKeys = WarnAction.vmInstance.keys()
        instanceIds = []
        vmNames = []
        
        load_effective_time = 1800
        for info in vmRunInfos :
            keys = info.keys()
            self.logger.debug("this is running 5......")
            vmName = info['vmName']
            status = info['vmState']
            self.logger.debug("this is running 6......")
            flag = 'normal'
            cpuRate = float(0)
            memRate = float(0)
            flow = long(0)
            
            instanceId = ''
            
            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'])
            
            vmInfo = None
            instanceInfo = None

            if 'instanceId' in keys and info['instanceId'] :
                instanceInfo = self.resourceStat.getInstanceInfo(info['instanceId'])
            else:
                vmInfo = self.resourceStat.getVmInfo(vmName)
                
            if vmInfo is None and instanceInfo is None:
                continue
                        
            #规则信息
            ruleKeys = rule.keys()
            hightFlag = False
            lowFlag = False
            hightDuration = 0
            lowDuration = 0
            
            if 'overtime' in ruleKeys:
                maxDuration = float(rule['overtime'])                
                overtime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(_time - maxDuration))
                #过期事件不处理
                if receiveTime < overtime :
                    continue
                    
            if 'lowWarn' in ruleKeys:
                lowWarn = rule['lowWarn']
                lowWarnKeys = lowWarn.keys()
                
                lowFlag = True
                if 'cpu' in lowWarnKeys:
                    if cpuRate >= float(lowWarn['cpu']):
                        lowFlag = False
                if 'mem' in lowWarnKeys:
                    if memRate >= float(lowWarn['mem']):
                        lowFlag = False
                if 'flow' in lowWarnKeys:
                    if flow >= long(lowWarn['flow']):
                        lowFlag = False
                if 'duration' in lowWarnKeys:
                    lowDuration = int(lowWarn['duration'])

            if 'highWarn' in ruleKeys:
                highWarn = rule['highWarn']
                highWarnKeys = highWarn.keys()
                hightFlag = True
                if 'cpu' in highWarnKeys:
                    if cpuRate < float(highWarn['cpu']):
                        hightFlag = False
                if 'mem' in highWarnKeys:
                    if memRate < float(highWarn['mem']):
                        hightFlag = False
                if 'flow' in highWarnKeys:
                    if flow < long(highWarn['flow']):
                        hightFlag = False
                if 'duration' in highWarnKeys:
                    hightDuration = int(highWarn['duration'])
                
            #根据运行状态,判断是否告警
            maxDuration = 0
            if "running" == status:
                if hightFlag:
                    flag = 'highWarn'
                    maxDuration = hightDuration
                if lowFlag:
                    flag = 'lowWarn'
                    maxDuration = lowDuration              
                
            elif "paused" == status:
                flag = 'paused'
                if instanceInfo is not None and instanceInfo['status'] != 'running':
                     flag = 'normal'
                elif vmInfo is not None and vmInfo['status'] != 'running':
                     flag = 'normal'
            elif "shutdown" == status:
                flag = 'shutdown'
                if instanceInfo is not None and instanceInfo['status'] != 'running':
                     flag = 'normal'
                elif vmInfo is not None and vmInfo['status'] != 'running':
                     flag = 'normal'
            elif "crashed" == status:
                flag = 'crashed'
            
            #处理过的应用实例和虚拟机
            if instanceInfo:
                instanceIds.append(instanceInfo['instanceId'])
            elif vmInfo:
                vmNames.append(vmInfo['vmName'])
                
                
            #告警处理
            if flag == 'normal':
                self.vmWarnStat.removeVmRunInfo('vmName')
                if instanceInfo:
                    #主要考虑warnType=overtime的应用,vmName设置的值为instanceId
                    self.vmWarnStat.removeVmRunInfo(instanceInfo['instanceId'])
            else :
                self.vmWarnStat.addVmRunInfo(info, flag, maxDuration) 
        self.logger.debug("this is running 7......")                         
        #没有监控信息的虚拟机告警
        vmInfoList = self.resourceStat.getAllVmInfoList()
        for info in vmInfoList:
            if info['vmName'] not in vmNames:
                ruleKeys = rule.keys()
                tmp = {}
                tmp['vmName'] = info['vmName']
                tmp['vmState'] = 'shutdown'
                tmp['eventTime'] = now
                tmp['receiveTime'] = now
                flag = 'overtime'
                maxDuration = -1
                if 'overtime' in ruleKeys:
                    maxDuration = float(rule['overtime'])
                
                self.vmWarnStat.addVmRunInfo(tmp, flag, maxDuration)
        self.logger.debug("this is running 8......")
        
        #没有监控信息的应用实例告警
        appInstanceInfoList = self.resourceStat.getAllAppInstanceInfoList()
        for info in appInstanceInfoList:
            if info['instanceId'] not in instanceIds:
                ruleKeys = rule.keys()
                tmp = {}
                tmp['instanceId'] = info['instanceId']
                tmp['vmName'] = info['instanceId']
                tmp['vmState'] = 'shutdown'
                tmp['eventTime'] = now
                tmp['receiveTime'] = now
                flag = 'overtime'
                maxDuration = -1
                if 'overtime' in ruleKeys:
                    maxDuration = float(rule['overtime'])
                self.vmWarnStat.addVmRunInfo(tmp, flag, maxDuration)
        self.logger.debug("this is running 9......")
Esempio n. 8
0
                
                self.vmWarnStat.addVmRunInfo(tmp, flag, maxDuration)
        self.logger.debug("this is running 8......")
        
        #没有监控信息的应用实例告警
        appInstanceInfoList = self.resourceStat.getAllAppInstanceInfoList()
        for info in appInstanceInfoList:
            if info['instanceId'] not in instanceIds:
                ruleKeys = rule.keys()
                tmp = {}
                tmp['instanceId'] = info['instanceId']
                tmp['vmName'] = info['instanceId']
                tmp['vmState'] = 'shutdown'
                tmp['eventTime'] = now
                tmp['receiveTime'] = now
                flag = 'overtime'
                maxDuration = -1
                if 'overtime' in ruleKeys:
                    maxDuration = float(rule['overtime'])
                self.vmWarnStat.addVmRunInfo(tmp, flag, maxDuration)
        self.logger.debug("this is running 9......")
                
if __name__ == "__main__" :   
    rule = '''{
                   "highWarn":{"cpu":90,"mem":90,"flow":8388608,"duration":3},
                   "lowWarn":{"cpu":10,"mem":40,"flow":8000,"duration":240}
                }'''
    jp = JsonParser()
    rule = jp.decode(rule)
    print rule['highWarn']['cpu']