def isAlive(self): try: self.getStatus() return 'true' except Exception, e: AgentLog.error('Mysql.isAlive failed, %s' % e) return "false"
def doWork(self): isSuccess, errMsg = self.check() if not isSuccess: return self.responseFailed(errMsg) try: ret = self.handle() except Exception, e: AgentLog.error('MysqlShell.doWork error :%s' % e) ret = self.errorHandler("%s" % e) return self.responseFailed(ret)
def getInstanceInfo(self, instanceId): try: ret = {'DBName': None, 'IP': None, 'Port': None} ret['version'] = self.db.execSql('select version()')[0][0] ret['schema'] = len(self.db.execSql('show databases')) ret['Uptime'] = self.db.execSql('show status like "Uptime"')[0][0] ret['Engine'] = 'Innodb' ret['NbCluster'] = 'NO' return ret except Exception, e: AgentLog.error("AWR getInstanceInfo error:%s" % e)
def doWork(self): isSuccess, errMsg = self.check() if not isSuccess: return self.responseFailed(errMsg) timestamp = self.response.getTimeStamp() timestamp = time.mktime(time.strptime(timestamp, '%Y-%m-%d %H:%M:%S')) try: isSuccess, masterFile, masterPos, errMsg, fileSize = self.db.backup( timestamp, self.backupType, self.binlogFile, self.backupTool) except Exception, e: isSuccess = False errMsg = 'Error %s' % e AgentLog.error(errMsg)
def start(self): """ 把进程pid写入到对应的pid文件 """ AgentLog.info("RDS Agent start to run") try: file = open(self.__pidFile__, 'wt') file.write(str(os.getpid())) file.close() except: AgentLog.error("open pid file {0} error, start failed".format( self.__pidFile__)) raise AgentException("open pid file {0} failed".format( self.__pidFile__))
class RabbitMQSender(RabbitMQWrapper): def __init__(self, name_="RabbitMQSender", host_="localhost", port_=5672, exchange_='exchange', routing_key_='key'): RabbitMQWrapper.__init__(self, name_, host_, port_, exchange_, '', routing_key_) AgentLog.info( "create sender %s {host:%s, port:%d, exchange:%s, routing_key:%s}" % (name_, host_, port_, exchange_, routing_key_)) def send(self, msg): try: self.channel.basic_publish(exchange=self.exchange, routing_key=self.routing_key, body=msg) except ConnectionClosed, e: self.reconnect() self.channel.basic_publish(exchange=self.exchange, routing_key=self.routing_key, body=msg) except Exception, e: AgentLog.error("{0} send msg:{1} error".format(self.name, msg))
def parseMsg(self, response): AgentLog.debug("receive msg: {0}".format(response.body)) try: try: command = CommandFactory.getCommand(response.getDBType(), response.getAction())(self.context, response) except AttributeError: request = self.createErrorRequest(response, 'command is not exist') self.sender.send(request.getRequest()) return # request = command.doWork() # self.sender.send(request.getRequest()) data = [((), {'command': command, 'sender': self.sender})] reqs = threadpool.makeRequests(doWork, data) [SyncCMDReceiver.pool.putRequest(req) for req in reqs] except Exception,e: AgentLog.error('syncCMDReceiver.parseMsg error %s'%e)
def run(self): AgentLog.info('thread {0} start running'.format(self.getName())) while self.isRunning: try: self.doWork() if self.lastReportTime == 0: self.lastReportTime = int(time.time()) # 获取当前时间 currentTime = int(time.time()) # 比较是否已经超时,如果超时,则在日志文件中打印线程alive信息 if currentTime >= self.lastReportTime + self.activeReportInterval: AgentLog.info( ' thread:[{0}] is alive '.format(self.getName()).ljust(45, ' ').center(75, '#')) self.lastReportTime = currentTime except Exception, e: AgentLog.error( "thread [{0}] raise Exception: {1}".format(self.getName(), traceback.format_exc())) # 等待下一次运行 self.event.wait(self.loopInterval)
def getCacheProfile(self, instanceId): try: ret = {} ret['PoolSize'] = int(self.db.execSql("show variables like 'innodb_buffer_pool_size'")[0][1]) / 1024 / 1024 ret['RedologSize'] = int(self.db.execSql("show variables like 'innodb_log_file_size'")[0][1]) / 1024 / 1024 # 获取hint answers = self.db.execSql("show status like 'innodb%_reads'") hint = {'Innodb_buffer_pool_reads': 0, 'Innodb_data_reads': 0} for line in answers: if line[0] in ('Innodb_buffer_pool_reads', 'Innodb_data_reads'): hint[line[0]] = line[1] ret['Hint'] = float(hint['Innodb_buffer_pool_reads']) / float(hint['Innodb_data_reads']) return ret except Exception, e: AgentLog.error("AWR getCacheProfile error:%s" % e)
def doWork(self): isSuccess, errMsg = self.check() if not isSuccess: return self.responseFailed(errMsg) endTime = self.endTime startTime = self.startTime instanceID = self.instanceID try: # 获取实例参数 instanceData = self.getInstanceInfo(self.instanceID) # 获取时间参数 elapsed = time.mktime(time.strptime(endTime, "%Y-%m-%d %H:%M")) - time.mktime( time.strptime(startTime, "%Y-%m-%d %H:%M")) if elapsed < 0: return self.responseFailed('TimeError') snapTime = {'BeginSnap': startTime, 'EndSnap': endTime, 'Elapsed': elapsed} # 获取全局参数 showGlobal = self.db.execSql("show global variables") # 获取sql参数 sqlData = self.getSQL(instanceID) # 获取状态参数 # statusData = self.getLoadProfile(elapsed, startTime, endTime, instanceID) cacheProfile = self.getCacheProfile(instanceID) waitEvent = self.getWaitEvent(instanceID) ret = {'awr': {'Instance': instanceData, 'loadProfile': None, 'SnapTime': snapTime, 'CacheProfile': cacheProfile, 'WaitEvent': waitEvent, 'showGlobal': showGlobal, 'sql': sqlData }} return self.responseOK(ret) except Exception, e: AgentLog.error('AWR get, error:%s' % (e))
def callback(self, ch, method, properties, body): try: self.doWork(AgentResponse(body)) ch.basic_ack(delivery_tag=method.delivery_tag) except Exception,e: AgentLog.error('RabbitMQReceiver.callback error %s'%e)