def getpercpuinfo(): stats = [] # per_info = {} flag, node_id = getAgentId() # per_info["id"] = node_id percpu_times_percent = psutil.cpu_times_percent(interval=0.0, percpu=True) for cpu_number, cputimes in enumerate(percpu_times_percent): cpu = { 'cpu_number': cpu_number, 'id': node_id, 'total': round(100 - cputimes.idle, 1), 'user': cputimes.user, 'system': cputimes.system, 'idle': cputimes.idle } # The following stats are for API purposes only if hasattr(cputimes, 'nice'): cpu['nice'] = cputimes.nice if hasattr(cputimes, 'iowait'): cpu['iowait'] = cputimes.iowait if hasattr(cputimes, 'irq'): cpu['irq'] = cputimes.irq if hasattr(cputimes, 'softirq'): cpu['softirq'] = cputimes.softirq if hasattr(cputimes, 'steal'): cpu['steal'] = cputimes.steal if hasattr(cputimes, 'guest'): cpu['guest'] = cputimes.guest if hasattr(cputimes, 'guest_nice'): cpu['guest_nice'] = cputimes.guest_nice stats.append(cpu) # per_info["body"] = stats return stats
def get_socket_info(): """ get socket info :return: """ socket_info = {} flag, node_id = getAgentId() socket_info["id"] = node_id try: rtinfo = launchcmd('sar -n SOCK 1 1').readlines() if rtinfo: continue_falg = True for item in rtinfo: if continue_falg: if "totsck" in item: continue_falg = False continue tmplist = item.split() socket_info["time"] = tmplist[0] socket_info["totsck"] = tmplist[1] socket_info["tcpsck"] = tmplist[2] socket_info["udpsck"] = tmplist[3] socket_info["rawsck"] = tmplist[4] socket_info["ip-frag"] = tmplist[5] socket_info["tcp-tw"] = tmplist[6] break except IOError, e: return socket_info
def get_ps_basic_info(): """ return:the process list include basic information """ # pids = get_all_pid() result_list = [] flag, node_id = getAgentId() cmd = 'ps -eo pid,vsz,rss,pcpu,pmem,user,time,pri,stat,psr' res = os.popen(cmd).readlines() for line in res[1:len(res)]: ps_line = line.split() pid = ps_line[0] p = int(pid) try: ps_name = psutil.Process(p).name() ps_dict = { 'nodeId': node_id, 'pid': pid, 'name': ps_name, 'vsz': ps_line[1], 'rss': ps_line[2], 'pcpu': ps_line[3], 'pmem': ps_line[4], 'user': ps_line[5], 'time': ps_line[6], 'pri': ps_line[7], 'stat': ps_line[8], 'psr': ps_line[9] } except psutil.NoSuchProcess: continue result_list.append(ps_dict) return result_list
def getmembasicinfo(): """ get memory info :param :return: """ virtual_memory = {} flag, node_id = getAgentId() virtual_memory["id"] = node_id try: freem = launchcmd('free').readlines() shared = int(strip(freem[1].split()[4])) * 1024 rtinfo = psutil.virtual_memory() if rtinfo: rtinfo_list = str(rtinfo).replace("svmem(", "").replace(")", "").split(",") for item in rtinfo_list: items = item.split("=") virtual_memory[strip(items[0])] = strip(items[1]) virtual_memory["shared"] = shared except IOError: return virtual_memory return virtual_memory
def get_numastat_info(): """ get numastat info :return: """ numastat_list = [] numastat = [] numa_dict = {} flag, node_id = getAgentId() numa_dict["id"] = node_id try: rtninfo = launchcmd('numastat').readlines() for item in rtninfo: items = item.split() numastat.append(items) for i in range(0, len(numastat[0])): numastat_info = {} numastat_info["name"] = numastat[0][i] numastat_info["numa_hit"] = numastat[1][i + 1] numastat_info["numa_miss"] = numastat[2][i + 1] numastat_info["numa_foreign"] = numastat[3][i + 1] numastat_info["interleave_hit"] = numastat[4][i + 1] numastat_info["local_node"] = numastat[5][i + 1] numastat_info["other_node"] = numastat[6][i + 1] numastat_list.append(numastat_info) numa_dict["body"] = numastat_list except IOError: return numa_dict return numa_dict
def get_fans(): (code, resultMSG) = commands.getstatusoutput('ipmitool -I open sensor list') # temp_fans = {"id": "node1", "fans": [{"fanname": "fan1", "fanspeed": "3000"}, # {"fanname": "fan2", "fanspeed": "3000"}]} error = {} error["type"] = "ERROR" if code != 0: error["error"] = resultMSG return error fanlist = {"fans": []} flag, node_id = getAgentId() fanlist["id"] = node_id fanlist["type"] = "FANS" line = resultMSG.split('\n') for i in range(len(line)): tmp = line[i].split('|') tmpdict = {} date = tmp[0] if date.find('FAN') != -1: tmpdict['fanname'] = tmp[0].strip() tmpdict['fanspeed'] = tmp[1].strip() fanlist['fans'].append(tmpdict) return fanlist
def getpaginfo(): """ get mem pag info :return: """ page_memory = {} flag, node_id = getAgentId() page_memory["id"] = node_id try: rtinfo = launchcmd('sar -B 1 1').readlines() if rtinfo[-1]: page_list = rtinfo[-1].split() page_memory["pgpgin"] = page_list[1] page_memory["pgpgout"] = page_list[2] page_memory["fault"] = page_list[3] page_memory["majflt"] = page_list[4] page_memory["pgfree"] = page_list[5] page_memory["pgsteal"] = page_list[8] rtinfo_other = launchcmd('sar -R 1 1').readlines() if rtinfo_other[-1]: page_list_other = rtinfo_other[-1].split() page_memory["bufpg"] = page_list_other[2] page_memory["campg"] = page_list_other[3] except IOError: return page_memory return page_memory
def get_udp_info(): """ get udp info :return: """ udp_info = {} flag, node_id = getAgentId() udp_info["id"] = node_id try: rtinfo = launchcmd('sar -n UDP 1 1').readlines() if rtinfo: continue_falg = True for item in rtinfo: if continue_falg: if "idgm" in item: continue_falg = False continue tmplist = item.split() udp_info["time"] = tmplist[0] udp_info["idgm/s"] = tmplist[1] udp_info["odgm/s"] = tmplist[2] udp_info["noport/s"] = tmplist[3] udp_info["idgmerr/s"] = tmplist[4] break except IOError, e: return udp_info
def get_throughput_info(): """ get throughput info :return: """ throughput_list = {} flag, node_id = getAgentId() throughput_list["id"] = node_id try: rtinfo = launchcmd('sar -n ALL 1 1').readlines() if rtinfo: continue_falg = True falg = True for item in rtinfo: throughput_info = {} if continue_falg: if "IFACE" in item: continue_falg = False continue if "rxerr" in item: falg = False continue if "call" in item: break tmplist = item.split() if tmplist is None or tmplist == []: continue if falg: throughput_info["time"] = tmplist[0] throughput_info["name"] = tmplist[1] throughput_info["rxpck/s"] = tmplist[2] throughput_info["txpck/s"] = tmplist[3] throughput_info["rxkB/s"] = tmplist[4] throughput_info["txkB/s"] = tmplist[5] throughput_info["rxcmp/s"] = tmplist[6] throughput_info["txcmp/s"] = tmplist[7] throughput_info["rxmcst/s"] = tmplist[8] throughput_list[tmplist[1]] = throughput_info else: throughput_info = throughput_list[tmplist[1]] throughput_info["rxerr/s"] = tmplist[2] throughput_info["txerr/s"] = tmplist[3] throughput_info["coll/s"] = tmplist[4] throughput_info["rxdrop/s"] = tmplist[5] throughput_info["txdrop/s"] = tmplist[6] throughput_info["txcarr/s"] = tmplist[7] throughput_info["rxfram/s"] = tmplist[8] throughput_info["rxfifo/s"] = tmplist[9] throughput_info["txfifo/s"] = tmplist[10] throughput_list[tmplist[1]] = throughput_info except IOError, e: return throughput_list
def get_disk_io(): command_b = "sar -b " + "1" + " 1" disk_io = {} flag, node_id = getAgentId() disk_io["id"] = node_id disk_io["type"] = "DISK_IO" ret_flag, dict_list_sar_b = command_exec(command_b, 1, 1, -1, {0: "time"}) if ret_flag: disk_io["body"] = dict_list_sar_b[0:-1] return disk_io
def getcpuinfo(): stats = {} flag, node_id = getAgentId() stats["id"] = node_id stats["total"] = psutil.cpu_percent(interval=0.0) cpu_times_percent = psutil.cpu_times_percent(interval=0.0) for stat in [ 'user', 'system', 'idle', 'nice', 'iowait', 'irq', 'softirq', 'steal', 'guest', 'guest_nice' ]: if hasattr(cpu_times_percent, stat): stats[stat] = getattr(cpu_times_percent, stat) return stats
def getcpuload(): flag, node_id = getAgentId() try: load = os.getloadavg() except (OSError, AttributeError): stats = {} else: stats = { 'min1': load[0], 'min5': load[1], 'min15': load[2], 'id': node_id } return stats
def get_iptrafic_info(): """ get iptrafic info :return: """ iptrafic_info = {} flag, node_id = getAgentId() iptrafic_info["id"] = node_id try: rtinfo = launchcmd('sar -n ALL 1 1').readlines() if rtinfo: continue_falg = True falg = True for item in rtinfo: if continue_falg: if "irec" in item: continue_falg = False continue if "ihdrerr" in item: falg = False continue if "imsg" in item: break tmplist = item.split() if tmplist is None or tmplist == []: continue if falg: iptrafic_info["time"] = tmplist[0] iptrafic_info["irec/s"] = tmplist[1] iptrafic_info["fwddgm/s"] = tmplist[2] iptrafic_info["idel/s"] = tmplist[3] iptrafic_info["orq/s"] = tmplist[4] iptrafic_info["asmrq/s"] = tmplist[5] iptrafic_info["asmok/s"] = tmplist[6] iptrafic_info["fragok/s"] = tmplist[7] iptrafic_info["fragcrt/s"] = tmplist[8] else: iptrafic_info["ihdrerr/s"] = tmplist[1] iptrafic_info["iadrerr/s"] = tmplist[2] iptrafic_info["iukwnpr/s"] = tmplist[3] iptrafic_info["idisc/s"] = tmplist[4] iptrafic_info["odisc/s"] = tmplist[5] iptrafic_info["onort/s"] = tmplist[6] iptrafic_info["asmf/s"] = tmplist[7] iptrafic_info["fragf/s"] = tmplist[8] except IOError, e: return iptrafic_info
def deal_cpu_sensor(): """ get dict that label contains Core or physical id :return: """ re_list = [] cpu = {} flag, node_id = getAgentId() cpu["id"] = node_id # core_index = 0 list_sensor = get_sensor() for r_dict in list_sensor: if r_dict["label"] is not None \ and (r_dict["label"].find("Physical id ") != -1 or r_dict["label"].find("Core ") != -1): re_list.append(r_dict) cpu["body"] = re_list return cpu
def getinterrupts(): intnum = 0 stats = [] interrupts = {} flag, node_id = getAgentId() interrupts["id"] = node_id cpunumbers = get_core_num() with open("/proc/interrupts") as intrf: intnum = 0 for line in intrf: if line.strip(): deal_line = deal_with_line(cpunumbers, line) if (intnum == 0): deal_line.insert(0, " ") intnum += 1 stats.append(deal_line) interrupts["body"] = stats return interrupts
def get_network_io(): netinfo = {} commands_net = "sar -n DEV 1 1" flag, node_id = getAgentId() try: net_info_list = subprocess.Popen(commands_net, stdout=subprocess.PIPE, shell=True).stdout.readlines() rx = 0 tx = 0 for net_info in net_info_list: if -1 != net_info.find(":") or -1 != net_info.find(":"): if -1 == net_info.find("rxkB"): rx = rx + float(net_info.split()[4]) tx = tx + float(net_info.split()[5]) netinfo["rx"] = rx netinfo["tx"] = tx netinfo["id"] = node_id return netinfo except: return None
def getfs(): command = "df -h" flag, node_id = getAgentId() file = {} result = [] file["id"] = node_id try: ret_flag, list_df = command_exec(command, 0) if ret_flag: list_df[0] = "devicename size used free percent mntpoint" dict_list_df = trans_list_to_dict(list_df) for data in dict_list_df: if data['devicename'] != 'tmpfs': result.append(data) file["body"] = result return file else: file["body"] = list_df return file except: return None
def get_tcp_info(): """ get tcp info :return: """ tcp_info = {} flag, node_id = getAgentId() tcp_info["id"] = node_id try: rtinfo = launchcmd('sar -n ALL 1 1').readlines() if rtinfo: continue_falg = True falg = True for item in rtinfo: if continue_falg: if "active" in item: continue_falg = False continue if "atmptf" in item: falg = False continue if "idgm" in item: break tmplist = item.split() if tmplist is None or tmplist == []: continue if falg: tcp_info["time"] = tmplist[0] tcp_info["active/s"] = tmplist[1] tcp_info["passive/s"] = tmplist[2] tcp_info["iseg/s"] = tmplist[3] tcp_info["oseg/s"] = tmplist[4] else: tcp_info["atmptf/s"] = tmplist[1] tcp_info["estres/s"] = tmplist[2] tcp_info["retrans/s"] = tmplist[3] tcp_info["isegerr/s"] = tmplist[4] tcp_info["orsts/s"] = tmplist[5] except IOError, e: return tcp_info
def get_disk_block(): # command: sar -d 1 1 disk_block = {} flag, node_id = getAgentId() disk_block["id"] = node_id command_d = "sar -d " + "1" + " 1" # command: iostat -xd command_iostat = "iostat -xd" ret_flag1, list_d_dict = command_exec(command_d, 1, 1, -1, {0: "time"}) # result of command: iostat -xd ret_flag2, list_d2_dict = command_exec(command_iostat, 1, 1) # get disk name dictionary for join two result list_device_dict = disk_name_mapping() # remove average data list_d_dict_sub = list_d_dict[0:len(list_d_dict)/2] if ret_flag1 and ret_flag2: join_list_twocom = join_iostat_to_sard(list_d_dict_sub, list_d2_dict, list_device_dict) disk_block["body"] = join_list_twocom return disk_block
def get_slab_info(): """ get slab info :return: """ slab_list = [] slab_res = {} flag, node_id = getAgentId() slab_res["id"] = node_id try: rtinfo = launchcmd('slabtop -o').readlines() if rtinfo: continue_falg = True for item in rtinfo: slab_info = {} if continue_falg: if "OBJS" in item: continue_falg = False continue slab_info["name"] = item.split()[7] slab_info["objs"] = item.split()[0] slab_info["slabs"] = item.split()[4] slab_info["obj_slab"] = item.split()[5] slab_info["active"] = item.split()[1] slab_info["obj_size"] = item.split()[3] slab_info["use"] = item.split()[2] slab_list.append(slab_info) slab_res["body"] = slab_list return slab_res except IOError: return slab_res return slab_res
def get_swap_info(): """ get swap info :param :return: """ swap_memory = {} flag, node_id = getAgentId() swap_memory["id"] = node_id try: rtinfo = psutil.swap_memory() if rtinfo: rtinfo_list = str(rtinfo).replace("sswap(", "").replace(")", "").split(",") pg_in_out = launchcmd('sar -W 1 1').readlines() for item in rtinfo_list: items = item.split("=") swap_memory[strip(items[0])] = strip(items[1]) swap_memory["pswpin"] = pg_in_out[-1].split()[1] swap_memory["pswpout"] = pg_in_out[-1].split()[2] except IOError: return swap_memory return swap_memory
task_producer("fun", body, dict) def fans_task_rrd(): body = get_fans() exchange_producer("fanrrd", body, None) def sensor_task(): dict = {"x-message-ttl": 60000} body = deal_cpu_sensor() task_producer("cputemper", body, dict) # 预警信息采集 flag, agentId = getAgentId() if not flag: agentId = '' def alarm_task(): # print "alarm task start" alarm_body = [] alarm_body.extend(cpu_base_alarm()) alarm_body.extend(cpu_load_alarm()) alarm_body.extend(cpu_temperature_alarm()) alarm_body.extend(mem_alarm()) alarm_body.extend(swap_alarm()) alarm_body.extend(fs_alarm()) alarm_body.extend(pscpu_alarm()) alarm_body.extend(psmem_alarm())