def doWork(self): content = {} for name, monitor in self.montorInstances.iteritems(): data = monitor.getData() if data == None: AgentLog.warning( "MonitorSenderThread.doWork can not get the %s Info,size is 0, now skip this time" % name) return if len(data) == 0: AgentLog.warning( "MonitorSenderThread.doWork can not get the %s Info,size is 0" % name) continue content = dict(content, **data) request = AgentRequest(content=content) request.setId(self.instanceId, self.groupId, self.clusterId) for filter in self.filters: request.setContent(filter.filter(request.getContent())) if not self.isFirst: self.sender.send(request.getRequest()) else: self.isFirst = False
def check(self): """ 检测进程是否存在,检测方法: 1.检测pid文件是否存在,如果不存在,则说明进程没有运行,新进程可以运行 2.如果pid文件存在,则读取pid文件中的pid,在所有进程中查找文件中的pid的进程是否存在, 如果进程存在,则新的进程不能运行,直接退出,如果进程不存在,说明可能是由于非正常退出(如:CTRL + C) 造成pid文件未被清理,这种情况下,agent可以启动,先删除失效的pid文件,然后启动新的进程即可。 """ if not os.path.isfile(self.__pidFile__): return pid = 0 try: file = open(self.__pidFile__, 'rt') data = file.read() file.close() # 获取文件中的pid进程号 pid = int(data) if not self.isAlive(pid): try: os.remove(self.__pidFile__) return except: AgentLog.warning("remove pid file {0} failed.".format( self.__pidFile__)) except: pass AgentLog.info("agent exist, only one process allow running") sys.exit(1)
def getDataDir(self): dataDir = self.getValue(self.configFile, "datadir") if dataDir == "": AgentLog.warning( 'Mysql can not get datadir value from config, start get innodb_data_home_dir' ) dataDir = self.getValue(self.configFile, "innodb_data_home_dir") return dataDir
def getSocket(self): socket = self.getValue(self.configFile, "socket") if socket == "": AgentLog.warning( 'Mysql can not get socket value from config, use /tmp/mysql.sock' ) socket = "/tmp/mysql.sock" return socket
def reconnect(self): AgentLog.warning("receiver %s disconnectted, re-connect..." % self.name) RabbitMQWrapper.reconnect(self) queue_ = self.queue if self.exclusive: queue_ = self.channel.queue_declare(exclusive=True).method.queue self.queue = queue_ else: self.channel.queue_declare(queue=queue_) self.channel.queue_bind(exchange=self.exchange, queue=queue_, routing_key=self.routing_key)
def stop(self): """ 程序退出时调用,在进程退出时,删除pid文件 """ AgentLog.info("RDS Agent start to exit") try: os.remove(self.__pidFile__) except: AgentLog.warning("remove pid file {0} error".format( self.__pidFile__)) raise AgentException("remove pid file {0} error".format( self.__pidFile__))
def doWork(self): try: temp = {} items = [ 'COUNT_STAR', 'SUM_TIMER_WAIT', 'MIN_TIMER_WAIT', 'AVG_TIMER_WAIT', 'MAX_TIMER_WAIT', 'SUM_LOCK_TIME', 'SUM_ERRORS', 'SUM_WARNINGS', 'SUM_ROWS_AFFECTED', 'SUM_ROWS_SENT', 'SUM_ROWS_EXAMINED', 'SUM_CREATED_TMP_DISK_TABLES', 'SUM_CREATED_TMP_TABLES', 'SUM_SELECT_FULL_JOIN', 'SUM_SELECT_FULL_RANGE_JOIN', 'SUM_SELECT_RANGE', 'SUM_SELECT_RANGE_CHECK', 'SUM_SELECT_SCAN', 'SUM_SORT_MERGE_PASSES', 'SUM_SORT_RANGE', 'SUM_SORT_ROWS', 'SUM_SORT_SCAN', 'SUM_NO_INDEX_USED', 'SUM_NO_GOOD_INDEX_USED' ] result = {} #查询每个field的topN数据,然后合并 for item in items: cmd = 'select SCHEMA_NAME,DIGEST,'+','.join(self.fields)+\ ' from performance_schema.events_statements_summary_by_digest order by '+item +' desc limit '+str(self.topNum) rows = self.dataBaseInstance.execSql(cmd) for row in rows: if str(row[0]) + row[1] not in result.keys(): result[str(row[0]) + row[1]] = row if result == {}: AgentLog.warning( 'TopSQLMonitor.doWork can not get topsql info') return for key, line in result.iteritems(): if line[0] is None: # 有schema为None的,这里取出来就是空了,为了下面的加法正确,将这个特殊值处理 statusBase = 'null' + "|" + line[1] else: statusBase = line[0] + "|" + line[1] for index, val in enumerate(line[2:]): status = statusBase + '|' + self.fields[index] temp[status] = val self.lock.acquire() self.dataDict = temp self.lock.release() except Exception, e: raise e
def receive(self, doWork, ack=False): self.doWork = doWork while 1: try: self.channel.basic_consume(self.callback, queue=self.queue, no_ack=ack) self.channel.start_consuming() except KeyboardInterrupt: AgentLog.warning("receive KeyboardInterrupt error") break except ConnectionClosed, e: AgentLog.warning("receive ConnectionClosed error:%s" % e) if not self.normalExit: self.reconnect() else: break except Exception, e: self.reconnect() AgentLog.warning("receive error: %s" % e)
if not Util.isExists(configFile): raise AgentFileException( 'Mysql can not find config File from path :%s' % configFile) try: with open(configFile, 'r') as f: for line in f: if line.find(key) != -1: key = (line.split('=')[0]).strip() if key[0] != '#': value = (line.split('=')[1]).strip() print value return value except IOError, e: raise AgentFileException( 'Mysql can not find config File from path :%s' % configFile) except Exception, e: AgentLog.warning( 'Mysql can not get Value from config, key: %s,configFile:%s' % (key, configFile)) return "" class SlowLogFile(DBFile): def getData(self): pass class ErrorLogFile(DBFile): def getData(self): pass
ch.basic_ack(delivery_tag=method.delivery_tag) except Exception,e: AgentLog.error('RabbitMQReceiver.callback error %s'%e) def receive(self, doWork, ack=False): self.doWork = doWork while 1: try: self.channel.basic_consume(self.callback, queue=self.queue, no_ack=ack) self.channel.start_consuming() except KeyboardInterrupt: AgentLog.warning("receive KeyboardInterrupt error") break except ConnectionClosed, e: AgentLog.warning("receive ConnectionClosed error:%s" % e) if not self.normalExit: self.reconnect() else: break except Exception, e: self.reconnect() AgentLog.warning("receive error: %s" % e) except: self.reconnect() AgentLog.warning("receive failed, unknown errors") def clear(self): RabbitMQWrapper.clear(self) self.channel.stop_consuming()