def getData(self): status_content = {} #alarm = [] try: if 'cpu' in self.taskConf['type']: status_content['cpu'] = self.get_cpu_info() if 'mem' in self.taskConf['type']: status_content['mem'] = self.get_mem_info() if 'diskio' in self.taskConf['type']: status_content['diskio'] = self.get_disk_io_info() if 'diskstore' in self.taskConf['type']: status_content['diskstore'] = self.get_disk_rate_info() if 'netio' in self.taskConf['type']: status_content['netio'] = self.get_net_info() if 'procsum' in self.taskConf['type']: status_content['procsum'] = self.get_process_info() if 'tcpstatus' in self.taskConf['type']: status_content['tcpstatus'] = self.get_tcp_info() if 'loadavg' in self.taskConf['type']: status_content['loadavg'] = self.get_linux_loadavg_info() ''' def _check(): self.alarm = [] reslist = ['cpu', 'mem', 'diskstore', 'diskio', 'procsum', 'loadavg', 'tcpstatus', 'netio'] # 检查告警 tmpConf = self.taskConf['snmp_list'] for res in reslist: if res in tmpConf.keys(): for i in tmpConf[res].items(): if len(i[1]) > 1: tmpValue = i[1]['threshold'] * 1.00 alarmid = i[0] action = self.option[i[1]['cond']] metric = i[1]['metric'] result = status_content[res][metric] if type(result) is not dict: checkedvalue = result if checkedvalue == action(checkedvalue, tmpValue): self.alarm.append(alarmid) elif type(result) is dict: checkedvaluelist = status_content[ res][metric].values() for checkedvalue in checkedvaluelist: if alarmid not in self.alarm: checkedvalue = float(checkedvalue) if checkedvalue == action(checkedvalue, tmpValue): self.alarm.append(alarmid) _check() alarm = list(set(self.alarm)) ''' except Exception: pybixlib.error(self.logHead + traceback.format_exc()) self.errorInfoDone(traceback.format_exc()) finally: self.setData({'agentType': self.agentType, 'uuid': self.uuid, 'code': self.code, 'time': self.getCurTime(), 'data': status_content, 'error_info': self.error_info}) self.intStatus()
def strtotime(self, strtime): try: time_tuple = time.strptime(strtime, "%d-%b-%Y %H:%M:%S") timestamp = time.mktime(time_tuple) return timestamp except Exception: pybixlib.error(self.logHead + traceback.format_exc()) return
def get_cpu_info(self): returnData = {} try: cpu = psutil.cpu_percent(interval=5, percpu=False) returnData['cpu_percent'] = cpu except Exception: pybixlib.error(self.logHead + traceback.format_exc()) self.errorInfoDone(traceback.format_exc()) return returnData
def get_process_info(self): returnData = {} try: p = psutil.pids() returnData['process'] = len(p) except Exception: pybixlib.error(self.logHead + traceback.format_exc()) self.errorInfoDone(traceback.format_exc()) return returnData
def get_mem_info(self): returnData = {} try: mem = psutil.virtual_memory() returnData['phymem_used'] = self.data_format_KB(mem.used) returnData['phymem_free'] = self.data_format_KB(mem.free) returnData['phymem_percent'] = mem.percent except Exception: pybixlib.error(self.logHead + traceback.format_exc()) self.errorInfoDone(traceback.format_exc()) return returnData
def getData(self): status_content = {} try: host = self.taskConf.get("host") port = self.taskConf.get("port") password = self.taskConf.get("password") self.server = redis.StrictRedis(host=host, port=port, password=password, socket_connect_timeout=30) self.info = self.server.info() status_content['redis_version'] = self.info['redis_version'] status_content['used_memory'] = self.info['used_memory'] status_content['connected_clients'] = self.info[ 'connected_clients'] status_content['connected_slaves'] = self.info['connected_slaves'] status_content[ 'uptime_in_minutes'] = self.info['uptime_in_seconds'] / 60 #status_content['connected_per_min'] = self.data_format_connected_per_min(status_content['connected_clients'], status_content['uptime_in_minutes']) status_content['rejected_connections'] = self.info[ 'rejected_connections'] status_content['pubsub_patterns'] = self.info['pubsub_patterns'] status_content['pubsub_channels'] = self.info['pubsub_channels'] status_content['keyspace_hits'] = self.info['keyspace_hits'] status_content['keyspace_misses'] = self.info['keyspace_misses'] #status_content['keyspace_hits'] = self.data_format_Ratio(self.info['keyspace_hits'], self.info['keyspace_misses']) status_content['commands_total'] = self.info[ 'total_commands_processed'] #status_content['command_per_min'] = self.data_format_command_per_min(self.info['total_commands_processed'], status_content['uptime_in_minutes']) status_content['usedMemoryRss'] = self.info['used_memory_rss'] status_content['memFragmentationRatio'] = self.info[ 'mem_fragmentation_ratio'] status_content['blockedClients'] = self.info['blocked_clients'] totalKey = 0 for key in self.info: if key.startswith('db'): totalKey = totalKey + self.info[key]['keys'] status_content['totalKeys'] = totalKey except Exception: pybixlib.error(self.logHead + traceback.format_exc()) self.errorInfoDone(traceback.format_exc()) status_content = {} finally: self.setData({ 'agentType': self.agentType, 'uuid': self.uuid, 'code': self.code, 'time': self.getCurTime(), 'data': status_content, 'error_info': self.error_info }) self.intStatus()
def check_host(self): try: hostUrl = config.hostUrl + "/" + self.mac req = urllib2.Request(hostUrl) req.add_header("Content-Type", "application/json") res = urllib2.urlopen(req) res.close() return True except Exception: pybixlib.error(traceback.format_exc()) pybixlib.error( "Please use method 'POST /hosts' to add the host record.") return False
def get_linux_loadavg_info(self): returnData = {} try: f = open('/proc/loadavg', 'r') tmp = f.readline().split() f.close() returnData['lavg'] = {} returnData['lavg']['1min'] = float(tmp[0]) returnData['lavg']['5min'] = float(tmp[1]) returnData['lavg']['15min'] = float(tmp[2]) except Exception: pybixlib.error(self.logHead + traceback.format_exc()) self.errorInfoDone(traceback.format_exc()) return returnData
def run(self): time.sleep(random.randint(1, 20)) while self.running: if 'pluginTime' in self.taskConf: self.interval = int(self.taskConf['pluginTime']) cur_time = time.time() self.getData() self.postData() cur_time = time.time() - cur_time if(cur_time < self.interval): time.sleep(self.interval - cur_time) else: pybixlib.error('get data out time!!') time.sleep(60)
def run(self): time.sleep(random.randint(1, 20)) while self.running: if 'pluginTime' in self.taskConf: self.interval = int(self.taskConf['pluginTime']) cur_time = time.time() self.getData() self.postData() cur_time = time.time() - cur_time if (cur_time < self.interval): time.sleep(self.interval - cur_time) else: pybixlib.error('get data out time!!') time.sleep(60)
def startOne(self, plug): try: module_meta = __import__( 'plugin', globals(), locals(), [str(plug['pluginFileName'])]) class_meta = getattr(module_meta, plug['pluginFileName']) c = getattr(class_meta, plug['pluginClassName']) obj = c(plug['uuid'], plug['taskConf'], plug['agentType']) self.objList[plug['uuid']] = obj obj.setName(plug['pluginClassName']+plug['uuid']) obj.setDaemon(True) obj.start() except Exception: pybixlib.error(traceback.format_exc())
def get_net_info(self): returnData = {} try: net = psutil.net_io_counters(pernic=True) iplist = {} for name in self.ip: iplist[name.decode('cp936').encode('UTF8')] = self.ip[name] for (name, counters) in net.items(): if 'net_packets_recv' not in returnData: returnData['net_packets_recv'] = {} returnData['net_packets_sent'] = {} returnData['net_recv_kb'] = {} returnData['net_sent_kb'] = {} returnData['net_sent_rate_kbs'] = {} returnData['net_recv_rate_kbs'] = {} name = name.decode('cp936').encode('UTF8') if name not in iplist: continue returnData['net_sent_kb'][ name+';'+iplist[name]] = self.data_format_KB(counters.bytes_sent) returnData['net_recv_kb'][ name+';'+iplist[name]] = self.data_format_KB(counters.bytes_recv) returnData['net_packets_recv'][ name+';'+iplist[name]] = counters.packets_recv returnData['net_packets_sent'][ name+';'+iplist[name]] = counters.packets_sent if name not in self.oldNetSentValue: self.oldNetSentValue[name] = returnData[ 'net_sent_kb'][name+';'+iplist[name]] self.oldNetRecvValue[name] = returnData[ 'net_recv_kb'][name+';'+iplist[name]] tmp2 = ((returnData['net_sent_kb'][ name+';'+iplist[name]] - self.oldNetSentValue[name]) * 1.0 / self.interval) * 8 returnData['net_sent_rate_kbs'][ name+';'+iplist[name]] = float("%.2f" % tmp2) tmp2 = ((returnData['net_recv_kb'][ name+';'+iplist[name]] - self.oldNetRecvValue[name]) * 1.0 / self.interval) * 8 returnData['net_recv_rate_kbs'][ name+';'+iplist[name]] = float("%.2f" % tmp2) self.oldNetSentValue[name] = returnData[ 'net_sent_kb'][name+';'+iplist[name]] self.oldNetRecvValue[name] = returnData[ 'net_recv_kb'][name+';'+iplist[name]] except Exception: pybixlib.error(self.logHead + traceback.format_exc()) self.errorInfoDone(traceback.format_exc()) return returnData
def getConf(self): if self.check_host(): try: mac_adress = self.mac url = config.configUrl + "/" + str(mac_adress) req = urllib2.Request( url, headers={"Content-Type": "application/json"}) res = urllib2.urlopen(req) redata = res.read().decode('UTF8') res.close() return json.loads(redata) except Exception: pybixlib.error(traceback.format_exc()) else: return {}
def getData(self): status_content = {} try: host = self.taskConf.get("host") port = self.taskConf.get("port") password = self.taskConf.get("password") self.server = redis.StrictRedis(host=host, port=port, password=password, socket_connect_timeout=30) self.info = self.server.info() status_content['redis_version'] = self.info['redis_version'] status_content['used_memory'] = self.info['used_memory'] status_content['connected_clients'] = self.info[ 'connected_clients'] status_content['connected_slaves'] = self.info['connected_slaves'] status_content['uptime_in_minutes'] = self.info[ 'uptime_in_seconds'] / 60 #status_content['connected_per_min'] = self.data_format_connected_per_min(status_content['connected_clients'], status_content['uptime_in_minutes']) status_content['rejected_connections'] = self.info[ 'rejected_connections'] status_content['pubsub_patterns'] = self.info['pubsub_patterns'] status_content['pubsub_channels'] = self.info['pubsub_channels'] status_content['keyspace_hits'] = self.info['keyspace_hits'] status_content['keyspace_misses'] = self.info['keyspace_misses'] #status_content['keyspace_hits'] = self.data_format_Ratio(self.info['keyspace_hits'], self.info['keyspace_misses']) status_content['commands_total'] = self.info[ 'total_commands_processed'] #status_content['command_per_min'] = self.data_format_command_per_min(self.info['total_commands_processed'], status_content['uptime_in_minutes']) status_content['usedMemoryRss'] = self.info['used_memory_rss'] status_content['memFragmentationRatio'] = self.info[ 'mem_fragmentation_ratio'] status_content['blockedClients'] = self.info['blocked_clients'] totalKey = 0 for key in self.info: if key.startswith('db'): totalKey = totalKey + self.info[key]['keys'] status_content['totalKeys'] = totalKey except Exception: pybixlib.error(self.logHead + traceback.format_exc()) self.errorInfoDone(traceback.format_exc()) status_content = {} finally: self.setData({'agentType': self.agentType, 'uuid': self.uuid, 'code': self.code, 'time': self.getCurTime(), 'data': status_content, 'error_info': self.error_info}) self.intStatus()
def getData(self): try: redata = {"foo": "bar"} except Exception: pybixlib.error(self.logHead + traceback.format_exc()) self.errorInfoDone(traceback.format_exc()) redata = {} finally: self.setData({ 'agentType': self.agentType, 'uuid': self.uuid, 'code': self.code, 'time': self.getCurTime(), 'data': redata, 'error_info': self.error_info }) self.intStatus()
def get_disk_io_info(self): returnData = {'readiokps': {}, 'writeiokps': {}} try: old_info = psutil.disk_io_counters(perdisk=True) time.sleep(1) new_info = psutil.disk_io_counters(perdisk=True) for (diskname, rwinfo) in old_info.items(): oldr, oldw = rwinfo.read_bytes, rwinfo.write_bytes newr, neww = new_info[diskname].read_bytes, new_info[ diskname].write_bytes riok = (newr - oldr) / 1024.0 wiok = (neww - oldw) / 1024.0 returnData['readiokps'][diskname] = riok returnData['writeiokps'][diskname] = wiok except Exception: pybixlib.error(self.logHead + traceback.format_exc()) self.errorInfoDone(traceback.format_exc()) return returnData
def run(self): self.objList = {} try: if 'tasks' in self.conf: for uuid in self.conf['tasks']: plug = self.conf['tasks'][uuid+''] if plug['status'] == 1: self.startOne(plug) else: pybixlib.info('none tasks info!!') while self.running: time.sleep(self.conf_interval) conf = self.getConf() self.initPlugin(conf) except Exception: self.stop() print traceback.format_exc() pybixlib.error(traceback.format_exc())
def getData(self): redata = {} try: host = self.taskConf.get("host") port = self.taskConf.get("port") #user = self.taskConf.get("user") #password = self.taskConf.get("password") engine = pymongo.MongoClient( host=host, port=port, document_class=dict, tz_aware=False, connect=True) db = engine.test data = db.command('serverStatus') redata['version'] = data['version'] redata['host'] = data['host'] redata['uptime'] = data['uptime'] if 'ratio' not in data['globalLock']: data['globalLock']['ratio'] = 0 redata['globalLock_ratio'] = data['globalLock']['ratio'] redata['connections_current'] = data['connections']['current'] redata['connections_available'] = data['connections']['available'] redata['page_faults'] = data['extra_info']['page_faults'] redata['globalLock_currentQueue_total'] = data[ 'globalLock']['currentQueue']['total'] redata['globalLock_currentQueue_readers'] = data[ 'globalLock']['currentQueue']['readers'] redata['globalLock_currentQueue_writers'] = data[ 'globalLock']['currentQueue']['writers'] redata['opcounters_insert'] = data['opcounters']['insert'] redata['opcounters_query'] = data['opcounters']['query'] redata['opcounters_update'] = data['opcounters']['update'] redata['opcounters_delete'] = data['opcounters']['delete'] redata['opcounters_getmore'] = data['opcounters']['getmore'] redata['opcounters_command'] = data['opcounters']['command'] redata['mem_resident'] = data['mem']['resident'] redata['mem_maped'] = data['mem']['mapped'] except Exception: pybixlib.error(self.logHead + traceback.format_exc()) self.errorInfoDone(traceback.format_exc()) redata = {} finally: self.setData({'agentType': self.agentType, 'uuid': self.uuid, 'code': self.code, 'time': self.getCurTime(), 'data': redata, 'error_info': self.error_info}) self.intStatus()
def get_tcp_info(self): returnData = {} try: conns = psutil.net_connections() sumConn = {} for con in conns: if con.status == 'NONE': continue if con.status not in sumConn: sumConn[con.status] = 0 sumConn[con.status] = sumConn[con.status] + 1 for element in sumConn: if 'tcpsum' not in returnData: returnData['tcpsum'] = {} returnData['tcpsum'][element] = sumConn[element] except Exception: pybixlib.error(self.logHead + traceback.format_exc()) self.errorInfoDone(traceback.format_exc()) return returnData
def get_disk_rate_info(self): returnData = {} returnData['disk_total'] = {} returnData['disk_used'] = {} returnData['disk_percent'] = {} try: disk = psutil.disk_partitions() for val in disk: if val.fstype != "": mountpoint = val.mountpoint one = psutil.disk_usage(mountpoint) tmp = one.total/1024/1024/1024.0 returnData['disk_total'][mountpoint] = "%.2f" % tmp tmp = one.used/1024/1024/1024.0 returnData['disk_used'][mountpoint] = "%.2f" % tmp returnData['disk_percent'][mountpoint] = one.percent except Exception: pybixlib.error(self.logHead + traceback.format_exc()) self.errorInfoDone(traceback.format_exc()) return returnData
def initPlugin(self, conf): try: if 'tasks' in conf: for plugOne in self.objList: if plugOne in conf['tasks']: if 'taskConf' in conf['tasks'][plugOne]: self.objList[plugOne].setConf( conf['tasks'][plugOne]['taskConf']) for uuid in conf['tasks']: plug = conf['tasks'][uuid+''] if plug['status'] == '1' and plug['uuid'] not in self.objList: self.startOne(plug) if plug['status'] == '0' and plug['uuid'] in self.objList: self.stopOne(plug) if plug['status'] == '2' and plug['uuid'] in self.objList: self.stopOne(plug) self.startOne(plug) except Exception: pybixlib.error(traceback.format_exc())
def postData(self): try: reData = {} reData[self.uuid+''] = self.returnData() self.clearData() parms = {'post_data': reData, 'post_time': int( self.getCurTime()), 'plug_post': 'true'} PostUrl = config.postUrl + "/" + self.uuid try: req = urllib2.Request(PostUrl) req.add_header("Content-Type", "application/json") res = urllib2.urlopen( req, json.dumps(parms)) res.close() except Exception: pybixlib.error(traceback.format_exc()) res = urllib2.urlopen(urllib2.Request(PostUrl, parms)) res.close() except Exception: pybixlib.error(traceback.format_exc())
def getData(self): status_content = {} try: host = self.taskConf.get("host") port = self.taskConf.get("port") mc = Client((host, port)) status_content = mc.stats() except Exception: pybixlib.error(self.logHead + traceback.format_exc()) self.errorInfoDone(traceback.format_exc()) status_content = {} finally: self.setData({ 'agentType': self.agentType, 'uuid': self.uuid, 'code': self.code, 'time': self.getCurTime(), 'data': status_content, 'error_info': self.error_info }) self.intStatus()
def postData(self): try: reData = {} reData[self.uuid + ''] = self.returnData() self.clearData() parms = { 'post_data': reData, 'post_time': int(self.getCurTime()), 'plug_post': 'true' } PostUrl = config.postUrl + "/" + self.uuid try: req = urllib2.Request(PostUrl) req.add_header("Content-Type", "application/json") res = urllib2.urlopen(req, json.dumps(parms)) res.close() except Exception: pybixlib.error(traceback.format_exc()) res = urllib2.urlopen(urllib2.Request(PostUrl, parms)) res.close() except Exception: pybixlib.error(traceback.format_exc())
def stopOne(self, plug): try: self.objList[plug['uuid']].plugStop() del self.objList[plug['uuid']] except Exception: pybixlib.error(traceback.format_exc())
def getData(self): status_content = {} try: req = self.getWebReq() res = urllib2.urlopen(req) data = res.read() res.close() appname = self.taskConf.get("appname") try: data = data.decode("UTF8") except Exception: data = data.decode("GBK") rem = re.compile( r'<td class="row-center"><small>(.*)</small></td>', re.M) matches = rem.findall(data) if matches: status_content['tomcat_ver'] = matches[0] status_content['jvm_ver'] = matches[1] status_content['os_ver'] = matches[3]+' '+matches[4] else: pybixlib.error( self.logHead + "can't match the data tomcat_ver") self.errorInfoDone("can't match the data tomcat_ver") rem = re.compile( r'Free memory: ([0-9.]*) MB Total memory: ([0-9.]*) MB Max memory: ([0-9.]*) MB</p>', re.M) matches = rem.findall(data) if matches: status_content['free_memory'] = matches[0][0] status_content['total_memory'] = matches[0][1] status_content['max_memory'] = matches[0][2] else: pybixlib.error( self.logHead + "can't match the data free_memory") self.errorInfoDone("can't match the data free_memory") rem = re.compile(r'<h1>([^JVM][^</.]*)</h1>', re.M) matches = rem.findall(data) flag = -1 if matches: for key, val in enumerate(matches): val = val.strip('"') if val == appname: flag = key break if flag == -1: pybixlib.error( self.logHead + "can't find the data appname") self.errorInfoDone("can't find the data appname") else: pybixlib.error(self.logHead + "can't match the data appname") self.errorInfoDone("can't match the data appname") status_content['appname'] = appname rem = re.compile(r'Max threads: ([0-9]*)[^\d^.]', re.M) matches = rem.findall(data) if matches: status_content['max_threads'] = matches[flag] else: pybixlib.error( self.logHead + "can't match the data max_threads") self.errorInfoDone("can't match the data max_threads") rem = re.compile(r'Current thread count: ([0-9]*)[^\d^.]', re.M) matches = rem.findall(data) if matches: status_content['cur_thread'] = matches[flag] else: pybixlib.error( self.logHead + "can't match the data cur_thread") self.errorInfoDone("can't match the data cur_thread") rem = re.compile(r'Current thread busy: ([0-9]*)[^\d^.]', re.M) matches = rem.findall(data) if matches: status_content['cur_thread_b'] = matches[flag] else: pybixlib.error( self.logHead + "can't match the data cur_thread_b") self.errorInfoDone("can't match the data cur_thread_b") rem = re.compile(r'Max processing time: ([0-9.]*)[^\d^.]', re.M) matches = rem.findall(data) if matches: status_content['max_processing_time'] = matches[flag] else: pybixlib.error( self.logHead + "can't match the data max_processing_time") self.errorInfoDone("can't match the data max_processing_time") rem = re.compile(r'Processing time: ([0-9.]*)[^\d^.]', re.M) matches = rem.findall(data) if matches: status_content['processing_time'] = matches[flag] else: pybixlib.error( self.logHead + "can't match the data processing_time") self.errorInfoDone("can't match the data processing_time") rem = re.compile(r'Request count: ([0-9.]*)[^\d^.]', re.M) matches = rem.findall(data) if matches: status_content['request_count'] = matches[flag] else: pybixlib.error( self.logHead + "can't match the data request_count") self.errorInfoDone("can't match the data request_count") rem = re.compile(r'Error count: ([0-9.]*)[^\d^.]', re.M) matches = rem.findall(data) if matches: status_content['error_count'] = matches[flag] else: pybixlib.error( self.logHead + "can't match the data error_count") self.errorInfoDone("can't match the data error_count") rem = re.compile(r'Bytes received: ([0-9.]*)[^\d^.]', re.M) matches = rem.findall(data) if matches: status_content['bytes_received'] = matches[flag] else: pybixlib.error( self.logHead + "can't match the data bytes_received") self.errorInfoDone("can't match the data bytes_received") rem = re.compile(r'Bytes sent: ([0-9.]*)[^\d^.]', re.M) matches = rem.findall(data) if matches: status_content['bytes_sent'] = matches[flag] else: pybixlib.error( self.logHead + "can't match the data bytes_sent") self.errorInfoDone("can't match the data bytes_sent") except Exception: pybixlib.error(self.logHead + traceback.format_exc()) self.errorInfoDone(traceback.format_exc()) status_content = {} finally: self.setData({'agentType': self.agentType, 'uuid': self.uuid, 'code': self.code, 'time': self.getCurTime(), 'data': status_content, 'error_info': self.error_info}) self.intStatus()
def getData(self): redata = {} try: host = self.taskConf.get("host") port = self.taskConf.get("port") user = self.taskConf.get("user") password = self.taskConf.get("password") key_variables = [ 'log_slow_queries', 'slow_launch_time', 'max_connections', 'key_buffer_size', 'tmp_table_size', 'max_heap_table_size', 'table_open_cache', 'thread_cache_size', 'query_cache_limit', 'query_cache_min_res_unit', 'query_cache_size', 'query_cache_type', 'query_cache_wlock_invalidate', 'open_files_limit' ] key_status = [ 'Slow_launch_threads', 'Slow_queries', 'Max_used_connections', 'Key_read_requests', 'Key_reads', 'Key_blocks_unused', 'Key_blocks_used', 'Created_tmp_disk_tables', 'Created_tmp_files', 'Created_tmp_tables', 'Open_tables', 'Opened_tables', 'Threads_cached', 'Threads_connected', 'Threads_created', 'Threads_running', 'Qcache_free_blocks', 'Qcache_free_memory', 'Qcache_hits', 'Qcache_inserts', 'Qcache_lowmem_prunes', 'Qcache_not_cached', 'Qcache_queries_in_cache', 'Qcache_total_blocks', 'Sort_merge_passes', 'Sort_range', 'Sort_rows', 'Sort_scan', 'Open_files', 'Table_locks_immediate', 'Table_locks_waited', 'Handler_read_first', 'Handler_read_key', 'Handler_read_next', 'Handler_read_prev', 'Handler_read_rnd', 'Handler_read_rnd_next', 'Com_change_db', 'Com_delete', 'Com_insert', 'Com_select', 'Com_update', 'Com_update_multi', 'Connections', 'Bytes_received', 'Bytes_sent', 'Innodb_buffer_pool_read_requests', 'Innodb_buffer_pool_reads', 'Com_commit', 'Com_rollback', 'Queries', 'Uptime' ] key_other = ['QCACHE_USE_RATE'] keys = '' for key in key_variables: keys += "'" + key + "'," keys = keys.strip(',') conn = pymysql.connect(host=host, port=port, user=user, password=password, charset="utf8", cursorclass=pymysql.cursors.DictCursor) try: with conn.cursor() as cur: sql = "show variables where Variable_name in (" + \ keys + ")" cur.execute(sql) results = cur.fetchall() for result in results: key = result['Variable_name'] value = result['Value'] redata[key] = value keys = '' for key in key_status: keys += "'" + key + "'," keys = keys.strip(',') sql = "show global status where Variable_name in (" + \ keys + ")" cur.execute(sql) results = cur.fetchall() for result in results: key = result['Variable_name'] value = result['Value'] redata[key] = value for key in key_other: if key == 'QCACHE_USE_RATE': if redata['query_cache_size'] == float(0): redata['QCACHE_USE_RATE'] = "0" else: t = float(redata['query_cache_size']) f = float(redata['Qcache_free_memory']) redata['QCACHE_USE_RATE'] = float( (t - f) / f) * 100.00 finally: conn.close() except Exception: pybixlib.error(self.logHead + traceback.format_exc()) self.errorInfoDone(traceback.format_exc()) redata = {} finally: self.setData({ 'agentType': self.agentType, 'uuid': self.uuid, 'code': self.code, 'time': self.getCurTime(), 'data': redata, 'error_info': self.error_info }) self.intStatus()
def getData(self): redata = {} try: req = self.getWebReq() res = urllib2.urlopen(req) data = res.read() res.close() try: data = data.decode("UTF8") except Exception: data = data.decode("GBK") data = data.replace(codecs.BOM_UTF8.decode("UTF8"), "") rem = re.compile(r'Active connections: ([0-9]*)', re.M) matches = rem.findall(data) if matches: curr_reqs = matches[0] else: pybixlib.error(self.logHead + ' data format:Active connections') return rem = re.compile(r' ([0-9]*) ([0-9]*) ([0-9]*) ', re.M) matches = rem.findall(data) if matches: accepts = matches[0][0] handled = matches[0][1] requests = matches[0][2] else: pybixlib.error(self.logHead + ' data format:num') return rem = re.compile(r'Reading: ([0-9]*)', re.M) matches = rem.findall(data) if matches: reading = matches[0] else: pybixlib.error(self.logHead + ' data format:Reading') return rem = re.compile(r'Writing: ([0-9]*)', re.M) matches = rem.findall(data) if matches: writing = matches[0] else: pybixlib.error(self.logHead + ' data format:Writing') return rem = re.compile(r'Waiting: ([0-9]*)', re.M) matches = rem.findall(data) if matches: waiting = matches[0] else: pybixlib.error(self.logHead + ' data format:Waiting') return redata = { 'curr_reqs': curr_reqs, 'reading': reading, 'writing': writing, 'waiting': waiting, 'accepts': accepts, 'handled': handled, 'requests': requests } except Exception: pybixlib.error(self.logHead + traceback.format_exc()) self.errorInfoDone(traceback.format_exc()) finally: self.setData({ 'agentType': self.agentType, 'uuid': self.uuid, 'code': self.code, 'time': self.getCurTime(), 'data': redata, 'error_info': self.error_info }) self.intStatus()
def getData(self): redata = {} try: req = self.getWebReq() res = urllib2.urlopen(req) data = res.read() res.close() try: data = data.decode("UTF8") except Exception: data = data.decode("GBK") data = data.replace(codecs.BOM_UTF8.decode("UTF8"), "") rem = re.compile(r'Active connections: ([0-9]*)', re.M) matches = rem.findall(data) if matches: curr_reqs = matches[0] else: pybixlib.error( self.logHead + ' data format:Active connections') return rem = re.compile(r' ([0-9]*) ([0-9]*) ([0-9]*) ', re.M) matches = rem.findall(data) if matches: accepts = matches[0][0] handled = matches[0][1] requests = matches[0][2] else: pybixlib.error(self.logHead + ' data format:num') return rem = re.compile(r'Reading: ([0-9]*)', re.M) matches = rem.findall(data) if matches: reading = matches[0] else: pybixlib.error(self.logHead + ' data format:Reading') return rem = re.compile(r'Writing: ([0-9]*)', re.M) matches = rem.findall(data) if matches: writing = matches[0] else: pybixlib.error(self.logHead + ' data format:Writing') return rem = re.compile(r'Waiting: ([0-9]*)', re.M) matches = rem.findall(data) if matches: waiting = matches[0] else: pybixlib.error(self.logHead + ' data format:Waiting') return redata = {'curr_reqs': curr_reqs, 'reading': reading, 'writing': writing, 'waiting': waiting, 'accepts': accepts, 'handled': handled, 'requests': requests} except Exception: pybixlib.error(self.logHead + traceback.format_exc()) self.errorInfoDone(traceback.format_exc()) finally: self.setData({'agentType': self.agentType, 'uuid': self.uuid, 'code': self.code, 'time': self.getCurTime(), 'data': redata, 'error_info': self.error_info}) self.intStatus()
def getData(self): redata = {} try: req = self.getWebReq() res = urllib2.urlopen(req) data = res.read() res.close() try: data = data.decode("UTF8") except Exception: data = data.decode("GBK") data = data.replace(codecs.BOM_UTF8.decode("UTF8"), "") rem = re.compile(r'<h1>Server-Status(.*)<h2>Connections', re.S) matches = rem.findall(data) if matches: status_content = matches[0] else: pybixlib.error(self.logHead + ' data format:Server-Status') return rem = re.compile( r'Requests<\/td><td class="string">([0-9 ]*)req\/s<\/td>', re.S) matches = rem.findall(status_content) if matches: rps = matches[1] rps = int(rps) else: pybixlib.error(self.logHead + ' data format:Requests') return rem = re.compile(r'<b>([0-9 ]*)connections', re.S) matches = rem.findall(status_content) if matches: curr_reqs = matches[0] curr_reqs = int(curr_reqs) else: pybixlib.error(self.logHead + ' data format:connections') return rem = re.compile(r'connections<\/b>(.*)<\/pre><hr', re.S) matches = rem.findall(status_content) if matches: statusStr = matches[0] else: pybixlib.error(self.logHead + ' data format:connections pre') return lens = len(statusStr) curr_reqs_status = {} for i in range(0, lens): tmp = statusStr[i] if tmp == "\n": continue if tmp in curr_reqs_status: curr_reqs_status[tmp] += 1 else: curr_reqs_status[tmp] = 1 redata = {'rps': rps, 'curr_reqs': curr_reqs, 'curr_reqs_status': curr_reqs_status} for key in curr_reqs_status: redata['s_'+key] = curr_reqs_status[key] except Exception: pybixlib.error(self.logHead + traceback.format_exc()) self.errorInfoDone(traceback.format_exc()) redata = {} finally: self.setData({'agentType': self.agentType, 'uuid': self.uuid, 'code': self.code, 'time': self.getCurTime(), 'data': redata, 'error_info': self.error_info}) self.intStatus()
del self.objList[plug['uuid']] except Exception: pybixlib.error(traceback.format_exc()) # 获取配置 def getConf(self): if self.check_host(): try: mac_adress = self.mac url = config.configUrl + "/" + str(mac_adress) req = urllib2.Request( url, headers={"Content-Type": "application/json"}) res = urllib2.urlopen(req) redata = res.read().decode('UTF8') res.close() return json.loads(redata) except Exception: pybixlib.error(traceback.format_exc()) else: return {} if __name__ == "__main__": pybixlib.info('Starting agent process') pybixlib.printout('Starting agent process') try: agentProcess = AgentProcess() agentProcess.setName('AgentProcess') agentProcess.start() except Exception: pybixlib.error(traceback.format_exc())
def getData(self): redata = {} try: req = self.getWebReq() res = urllib2.urlopen(req) data = res.read() res.close() try: data = data.decode("UTF8") except Exception: data = data.decode("GBK") data = data.replace(codecs.BOM_UTF8.decode("UTF8"), "") rem = re.compile(r'<dt>Current Time: [a-zA-Z]*, (.*) [^0-9]*</dt>', re.M) matches = rem.findall(data) if matches: curr_time = self.strtotime(matches[0]) else: pybixlib.error(self.logHead + ' data format:Current Time') return rem = re.compile(r'<dt>Restart Time: [a-zA-Z]*, (.*) [^0-9]*</dt>', re.M) matches = rem.findall(data) if matches: restart_time = self.strtotime(matches[0]) else: pybixlib.error(self.logHead + ' data format:Restart Time') return uptime = curr_time - restart_time rem = re.compile(r'([0-9]*) requests currently being processed', re.M) matches = rem.findall(data) if matches: curr_reqs = matches[0] else: pybixlib.error(self.logHead + ' data format:requests currently') return rem = re.compile(r'<dt>Total accesses: ([0-9]*) -', re.M) matches = rem.findall(data) if matches: total_reqs = matches[0] else: pybixlib.error(self.logHead + ' data format:Total accesses') return rem = re.compile(r'<pre>(.*)</pre>', re.S) matches = rem.findall(data) if matches: statusStr = matches[0] else: pybixlib.error(self.logHead + ' data format:pre') return lens = len(statusStr) curr_reqs_status = {} for i in range(0, lens): tmp = statusStr[i] if tmp == "\n": continue if tmp in curr_reqs_status: curr_reqs_status[tmp] += 1 else: curr_reqs_status[tmp] = 1 redata = { 'uptime': uptime, 'total_reqs': total_reqs, 'curr_reqs': curr_reqs, 'curr_reqs_status': curr_reqs_status } except Exception: pybixlib.error(self.logHead + traceback.format_exc()) self.errorInfoDone(traceback.format_exc()) redata = {} finally: self.setData({ 'agentType': self.agentType, 'uuid': self.uuid, 'code': self.code, 'time': self.getCurTime(), 'data': redata, 'error_info': self.error_info }) self.intStatus()
def getData(self): redata = {} try: req = self.getWebReq() res = urllib2.urlopen(req) data = res.read() res.close() try: data = data.decode("UTF8") except Exception: data = data.decode("GBK") data = data.replace(codecs.BOM_UTF8.decode("UTF8"), "") rem = re.compile( r'<dt>Current Time: [a-zA-Z]*, (.*) [^0-9]*</dt>', re.M) matches = rem.findall(data) if matches: curr_time = self.strtotime(matches[0]) else: pybixlib.error(self.logHead + ' data format:Current Time') return rem = re.compile( r'<dt>Restart Time: [a-zA-Z]*, (.*) [^0-9]*</dt>', re.M) matches = rem.findall(data) if matches: restart_time = self.strtotime(matches[0]) else: pybixlib.error(self.logHead + ' data format:Restart Time') return uptime = curr_time - restart_time rem = re.compile( r'([0-9]*) requests currently being processed', re.M) matches = rem.findall(data) if matches: curr_reqs = matches[0] else: pybixlib.error( self.logHead + ' data format:requests currently') return rem = re.compile(r'<dt>Total accesses: ([0-9]*) -', re.M) matches = rem.findall(data) if matches: total_reqs = matches[0] else: pybixlib.error(self.logHead + ' data format:Total accesses') return rem = re.compile(r'<pre>(.*)</pre>', re.S) matches = rem.findall(data) if matches: statusStr = matches[0] else: pybixlib.error(self.logHead + ' data format:pre') return lens = len(statusStr) curr_reqs_status = {} for i in range(0, lens): tmp = statusStr[i] if tmp == "\n": continue if tmp in curr_reqs_status: curr_reqs_status[tmp] += 1 else: curr_reqs_status[tmp] = 1 redata = {'uptime': uptime, 'total_reqs': total_reqs, 'curr_reqs': curr_reqs, 'curr_reqs_status': curr_reqs_status} except Exception: pybixlib.error(self.logHead + traceback.format_exc()) self.errorInfoDone(traceback.format_exc()) redata = {} finally: self.setData({'agentType': self.agentType, 'uuid': self.uuid, 'code': self.code, 'time': self.getCurTime(), 'data': redata, 'error_info': self.error_info}) self.intStatus()