diff --git a/clients/client-linux.py b/clients/client-linux.py index 0c858aa..301231e 100755 --- a/clients/client-linux.py +++ b/clients/client-linux.py @@ -222,56 +222,57 @@ def _disk_io(): 磁盘读写有误差:4k,8k ,https://stackoverflow.com/questions/34413926/psutil-vs-dd-monitoring-disk-i-o :return: ''' - # pre pid snapshot - snapshot_first = {} - # next pid snapshot - snapshot_second = {} - # read count snapshot - snapshot_read = 0 - # write count snapshot - snapshot_write = 0 - # process snapshot - pid_snapshot = [str(i) for i in os.listdir("/proc") if i.isdigit() is True] - for pid in pid_snapshot: - try: - with open("/proc/{}/io".format(pid)) as f: - pid_io = {} - for line in f.readlines(): - if "read_bytes" in line: - pid_io["read"] = int(line.split("read_bytes:")[-1].strip()) - elif "write_bytes" in line and "cancelled_write_bytes" not in line: - pid_io["write"] = int(line.split("write_bytes:")[-1].strip()) - pid_io["name"] = open("/proc/{}/comm".format(pid), "r").read().strip() - snapshot_first[pid] = pid_io - except: - if pid in snapshot_first: - snapshot_first.pop(pid) + while True: + # pre pid snapshot + snapshot_first = {} + # next pid snapshot + snapshot_second = {} + # read count snapshot + snapshot_read = 0 + # write count snapshot + snapshot_write = 0 + # process snapshot + pid_snapshot = [str(i) for i in os.listdir("/proc") if i.isdigit() is True] + for pid in pid_snapshot: + try: + with open("/proc/{}/io".format(pid)) as f: + pid_io = {} + for line in f.readlines(): + if "read_bytes" in line: + pid_io["read"] = int(line.split("read_bytes:")[-1].strip()) + elif "write_bytes" in line and "cancelled_write_bytes" not in line: + pid_io["write"] = int(line.split("write_bytes:")[-1].strip()) + pid_io["name"] = open("/proc/{}/comm".format(pid), "r").read().strip() + snapshot_first[pid] = pid_io + except: + if pid in snapshot_first: + snapshot_first.pop(pid) - time.sleep(INTERVAL) + time.sleep(INTERVAL) - for pid in pid_snapshot: - try: - with open("/proc/{}/io".format(pid)) as f: - pid_io = {} - for line in f.readlines(): - if "read_bytes" in line: - pid_io["read"] = int(line.split("read_bytes:")[-1].strip()) - elif "write_bytes" in line and "cancelled_write_bytes" not in line: - pid_io["write"] = int(line.split("write_bytes:")[-1].strip()) - pid_io["name"] = open("/proc/{}/comm".format(pid), "r").read().strip() - snapshot_second[pid] = pid_io - except: - if pid in snapshot_first: - snapshot_first.pop(pid) - if pid in snapshot_second: - snapshot_second.pop(pid) + for pid in pid_snapshot: + try: + with open("/proc/{}/io".format(pid)) as f: + pid_io = {} + for line in f.readlines(): + if "read_bytes" in line: + pid_io["read"] = int(line.split("read_bytes:")[-1].strip()) + elif "write_bytes" in line and "cancelled_write_bytes" not in line: + pid_io["write"] = int(line.split("write_bytes:")[-1].strip()) + pid_io["name"] = open("/proc/{}/comm".format(pid), "r").read().strip() + snapshot_second[pid] = pid_io + except: + if pid in snapshot_first: + snapshot_first.pop(pid) + if pid in snapshot_second: + snapshot_second.pop(pid) - for k, v in snapshot_first.items(): - if snapshot_first[k]["name"] == snapshot_second[k]["name"] and snapshot_first[k]["name"] != "bash": - snapshot_read += (snapshot_second[k]["read"] - snapshot_first[k]["read"]) - snapshot_write += (snapshot_second[k]["write"] - snapshot_first[k]["write"]) - diskIO["read"] = snapshot_read - diskIO["write"] = snapshot_write + for k, v in snapshot_first.items(): + if snapshot_first[k]["name"] == snapshot_second[k]["name"] and snapshot_first[k]["name"] != "bash": + snapshot_read += (snapshot_second[k]["read"] - snapshot_first[k]["read"]) + snapshot_write += (snapshot_second[k]["write"] - snapshot_first[k]["write"]) + diskIO["read"] = snapshot_read + diskIO["write"] = snapshot_write def get_realtime_data(): '''