def get_all_processes_mem_cpu_usage(self): process_mem_cpu_usage = {} for key in self.process_state_db: pstat = self.process_state_db[key] if (pstat.process_state == 'PROCESS_STATE_RUNNING'): try: mem_cpu_usage_data = MemCpuUsageData(pstat.pid) process_mem_cpu = mem_cpu_usage_data.get_process_mem_cpu_info() except psutil.NoSuchProcess: sys.stderr.write("NoSuchProcess: process name:%s pid:%d\n" % (pstat.pname, pstat.pid)) else: process_mem_cpu.__key = pstat.pname process_mem_cpu_usage[process_mem_cpu.__key] = process_mem_cpu # walk through all processes being monitored by nodemgr, # not spawned by supervisord third_party_process_list = self.get_node_third_party_process_list() for pname in third_party_process_list: cmd = "ps -aux | grep -v grep | grep " + str(pname) + " | awk '{print $2}' | head -n1" proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = proc.communicate() if (stdout != ''): pid = int(stdout.strip('\n')) try: mem_cpu_usage_data = MemCpuUsageData(pid) process_mem_cpu = mem_cpu_usage_data.get_process_mem_cpu_info() except psutil.NoSuchProcess: sys.stderr.write("NoSuchProcess: process name:%s pid:%d\n" % (pname, pid)) else: process_mem_cpu.__key = pname process_mem_cpu_usage[process_mem_cpu.__key] = process_mem_cpu return process_mem_cpu_usage
def get_group_processes_mem_cpu_usage(self, group_name): process_mem_cpu_usage = {} for key in self.process_state_db: pstat = self.process_state_db[key] if pstat.group != group_name: continue if (pstat.process_state == 'PROCESS_STATE_RUNNING'): try: mem_cpu_usage_data = MemCpuUsageData( pstat.pid, pstat.last_cpu, pstat.last_time) process_mem_cpu = mem_cpu_usage_data.get_process_mem_cpu_info( ) except psutil.NoSuchProcess: sys.stderr.write( "NoSuchProcess: process name:%s pid:%d\n" % (pstat.pname, pstat.pid)) else: process_mem_cpu.__key = pstat.pname process_mem_cpu_usage[ process_mem_cpu.__key] = process_mem_cpu pstat.last_cpu = mem_cpu_usage_data.last_cpu pstat.last_time = mem_cpu_usage_data.last_time # walk through all processes being monitored by nodemgr, # not spawned by supervisord for pname, pattern in self.third_party_process_dict.items(): cmd = "ps -aux | grep " + pattern + " | awk '{print $2}' | head -n1" proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) stdout, stderr = proc.communicate() if (stdout != ''): pid = int(stdout.strip('\n')) if pname in self.third_party_process_state_db: pstat = self.third_party_process_state_db[pname] else: pstat = self.get_process_stat_object(pname) pstat.pid = pid self.third_party_process_state_db[pname] = pstat try: mem_cpu_usage_data = MemCpuUsageData( pstat.pid, pstat.last_cpu, pstat.last_time) process_mem_cpu = mem_cpu_usage_data.get_process_mem_cpu_info( ) except psutil.NoSuchProcess: sys.stderr.write( "NoSuchProcess: process name:%s pid:%d\n" % (pstat.pname, pstat.pid)) self.third_party_process_state_db.pop(pstat.pname) else: process_mem_cpu.__key = pname process_mem_cpu_usage[ process_mem_cpu.__key] = process_mem_cpu pstat.last_cpu = mem_cpu_usage_data.last_cpu pstat.last_time = mem_cpu_usage_data.last_time return process_mem_cpu_usage
def get_group_processes_mem_cpu_usage(self, group_name): process_mem_cpu_usage = {} for key in self.process_state_db[group_name]: pstat = self.process_state_db[group_name][key] if (pstat.process_state == 'PROCESS_STATE_RUNNING'): try: mem_cpu_usage_data = MemCpuUsageData(pstat.pid, pstat.last_cpu, pstat.last_time) process_mem_cpu = mem_cpu_usage_data.get_process_mem_cpu_info() except psutil.NoSuchProcess: self.msg_log('NoSuchProcess: process name: %s pid:%d' % (pstat.pname, pstat.pid), SandeshLevel.SYS_ERR) else: process_mem_cpu.__key = pstat.pname process_mem_cpu_usage[process_mem_cpu.__key] = process_mem_cpu pstat.last_cpu = mem_cpu_usage_data.last_cpu pstat.last_time = mem_cpu_usage_data.last_time # walk through all processes being monitored by nodemgr, # not spawned by supervisord for pname, pattern in self.third_party_process_dict.items(): cmd = "ps -aux | grep " + pattern + " | awk '{print $2}' | head -n1" proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) stdout, stderr = proc.communicate() if (stdout != ''): pid = int(stdout.strip('\n')) if pname in self.third_party_process_state_db: pstat = self.third_party_process_state_db[pname] else: pstat = self.get_process_stat_object(pname) pstat.pid = pid self.third_party_process_state_db[pname] = pstat try: mem_cpu_usage_data = MemCpuUsageData(pstat.pid, pstat.last_cpu, pstat.last_time) process_mem_cpu = mem_cpu_usage_data.get_process_mem_cpu_info() except psutil.NoSuchProcess: self.msg_log('NoSuchProcess: process name:%s pid:%d' % (pstat.pname, pstat.pid), SandeshLevel.SYS_ERR) self.third_party_process_state_db.pop(pstat.pname) else: process_mem_cpu.__key = pname process_mem_cpu_usage[process_mem_cpu.__key] = process_mem_cpu pstat.last_cpu = mem_cpu_usage_data.last_cpu pstat.last_time = mem_cpu_usage_data.last_time return process_mem_cpu_usage
def get_all_processes_mem_cpu_usage(self): process_mem_cpu_usage = {} for key in self.process_state_db: pstat = self.process_state_db[key] if pstat.process_state == "PROCESS_STATE_RUNNING": try: mem_cpu_usage_data = MemCpuUsageData(pstat.pid, pstat.last_cpu, pstat.last_time) process_mem_cpu = mem_cpu_usage_data.get_process_mem_cpu_info() except psutil.NoSuchProcess: sys.stderr.write("NoSuchProcess: process name:%s pid:%d\n" % (pstat.pname, pstat.pid)) else: process_mem_cpu.__key = pstat.pname process_mem_cpu_usage[process_mem_cpu.__key] = process_mem_cpu pstat.last_cpu = mem_cpu_usage_data.last_cpu pstat.last_time = mem_cpu_usage_data.last_time # walk through all processes being monitored by nodemgr, # not spawned by supervisord third_party_process_dict = self.get_node_third_party_process_dict() for pname in third_party_process_dict: pattern = third_party_process_dict[pname] cmd = "ps -aux | grep " + pattern + " | awk '{print $2}' | head -n1" proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = proc.communicate() if stdout != "": pid = int(stdout.strip("\n")) if pname in self.third_party_process_state_db: pstat = self.third_party_process_state_db[pname] else: pstat = self.get_process_stat_object(pname) pstat.pid = pid self.third_party_process_state_db[pname] = pstat try: mem_cpu_usage_data = MemCpuUsageData(pstat.pid, pstat.last_cpu, pstat.last_time) process_mem_cpu = mem_cpu_usage_data.get_process_mem_cpu_info() except psutil.NoSuchProcess: sys.stderr.write("NoSuchProcess: process name:%s pid:%d\n" % (pstat.pname, pstat.pid)) self.third_party_process_state_db.pop(pstat.pname) else: process_mem_cpu.__key = pname process_mem_cpu_usage[process_mem_cpu.__key] = process_mem_cpu pstat.last_cpu = mem_cpu_usage_data.last_cpu pstat.last_time = mem_cpu_usage_data.last_time return process_mem_cpu_usage
def get_all_processes_mem_cpu_usage(self): process_mem_cpu_usage = [] for key in self.process_state_db: pstat = self.process_state_db[key] if (pstat.process_state == 'PROCESS_STATE_RUNNING'): try: mem_cpu_usage_data = MemCpuUsageData(pstat.pid) except psutil.NoSuchProcess: sys.stderr.write( "NoSuchProcess: process name:%s pid:%d\n" % (pstat.pname, pstat.pid)) else: process_mem_cpu = mem_cpu_usage_data.get_process_mem_cpu_info( ) process_mem_cpu.module_id = pstat.pname process_mem_cpu.inst_id = "0" # ?? process_mem_cpu_usage.append(process_mem_cpu) # walk through all processes being monitored by nodemgr, # not spawned by supervisord third_party_process_list = self.get_node_third_party_process_list() for pname in third_party_process_list: cmd = "ps -aux | grep -v grep | grep " + str( pname) + " | awk '{print $2}' | head -n1" proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = proc.communicate() if (stdout != ''): pid = int(stdout.strip('\n')) try: mem_cpu_usage_data = MemCpuUsageData(pid) except psutil.NoSuchProcess: sys.stderr.write( "NoSuchProcess: process name:%s pid:%d\n" % (pname, pid)) else: process_mem_cpu = mem_cpu_usage_data.get_process_mem_cpu_info( ) process_mem_cpu.module_id = pname process_mem_cpu.inst_id = "0" process_mem_cpu_usage.append(process_mem_cpu) return process_mem_cpu_usage