def process_request(command): pidlist = [] for proc in process_iter(): if re.match(command, proc.name()): pidlist.append(proc.pid) for pid in pidlist: process = Process(pid) try: ios = process.io_counters() for iotype in ios._fields: IO_PROCESS.labels(io_type=iotype, pid=pid, cmd=process.name()).set(getattr(ios, iotype)) except AccessDenied: logging.error("unable to access to PID %s stats" % pid) return IO_PROCESS
def get_io_counters(process: psutil.Process) -> dict: """ Returns the disk usage by a process Args: process (psutil.Process): The process that you want to get the data """ ret = dict() io_counters = process.io_counters() ret['io_read_count'] = io_counters.read_count ret['io_write_count'] = io_counters.write_count ret['io_read_bytes'] = io_counters.read_bytes ret['io_write_bytes'] = io_counters.write_bytes ret['io_read_chars'] = io_counters.read_chars ret['io_write_chars'] = io_counters.write_chars return ret
def collect_sample(pid): """ retrieve current usage sample This will be called every N seconds pid: process id (int) (this should have been validated before calling this function) Return: an instance of the pstat namedtuple """ time_stamp = datetime.now().isoformat() file_name1 = "/proc/{0}/stat".format(pid) with open(file_name1) as f: lines1 = f.readlines() pid_process = Process(pid) io_counters = pid_process.io_counters() with open(FSTAT, 'r') as f: first_line = f.readline() return [lines1[0], first_line, time_stamp, io_counters]
if not exists(log_path): makedirs(log_path) basicConfig( filename='%s/log_%s.log' % ('Log', strftime("%Y-%m-%d-%H-%M-%S", gmtime())), level=TRACE, format= "%(levelname)s:%(filename)s,%(lineno)d:%(name)s.%(funcName)s:%(message)s" ) #, stream=sys.stderr srv = Server() server_thread = Thread(target=srv.run) server_thread.start() print("VERSION: ", __version__) self_usage = Process(getpid()) while True: sleep(1) u_cpu = int(self_usage.cpu_percent()) u_memory = int(self_usage.memory_info()[0] / 2.**20) t_memory = int(virtual_memory()[0] / 2.**20) u_disk = int(self_usage.io_counters()[0] / self_usage.io_counters()[1]) o_slots = len(srv.connections.keys()) t_slots = srv.setting.maximum_users sys.stdout.write( "\rSERVER MONITOR [CPU: %s%% | RAM: %s/%s Mb | DISK: %s%% | SLOTS: %s/%s]" % (u_cpu, u_memory, t_memory, u_disk, o_slots, t_slots)) sys.stdout.flush()
def _get_process_io_bytes(process: psutil.Process): # io_counters, read_bytes, writes_byte return process.io_counters()