diff --git a/common.py b/common.py index b39803d..17c4fc7 100644 --- a/common.py +++ b/common.py @@ -17,7 +17,7 @@ def run_process(*argv, **kwargs): try: p = subprocess.Popen(argv, **kwargs) except: - print("Error: '%s' tool not found" % argv[0]) + print(f"Error: '{argv[0]}' tool not found") sys.exit(1) return p diff --git a/graph.py b/graph.py index 62c5f89..938f0a1 100755 --- a/graph.py +++ b/graph.py @@ -43,7 +43,7 @@ def g(command): p = run_process("gnuplot", "--version", stdout=subprocess.PIPE) version = scan(r"gnuplot (\S+)", float, p.stdout.readline().decode()) if version < GNUPLOT_VERSION_EXPECTED: - print("Error: Gnuplot version too low. Need at least %g found %g" % (GNUPLOT_VERSION_EXPECTED, version[0])) + print(f"Error: Gnuplot version too low. Need at least {GNUPLOT_VERSION_EXPECTED} found {version[0]}") sys.exit(1) diff --git a/watch.py b/watch.py index cc0afbd..58b3dd8 100755 --- a/watch.py +++ b/watch.py @@ -41,7 +41,7 @@ def kill_handler(a, b): # Check if a process is running def pid_running(pid): - return os.path.exists("/proc/%d" % pid) + return os.path.exists(f"/proc/{pid}") # Read a single table from sar output @@ -101,19 +101,19 @@ if args.session: cmd = args.command if cmd[0] == "start": - print("Starting sargraph session '%s'" % sid) + print(f"Starting sargraph session '{sid}'") p = subprocess.Popen(["screen", "-dmSL", sid, os.path.realpath(__file__), *sys.argv[3:]]) while p.poll() is None: time.sleep(0.1) gpid = 0 j = 0 time.sleep(1) - print("Session '%s' started" % sid) + print(f"Session '{sid}' started") elif cmd[0] == "stop": - print("Terminating sargraph session '%s'" % sid) + print(f"Terminating sargraph session '{sid}'") try: - gpid = int(os.popen("screen -ls | grep '.%s' | tr -d ' \t' | cut -f 1 -d '.'" % sid).read()) + gpid = int(os.popen(f"screen -ls | grep '.{sid}' | tr -d ' \t' | cut -f 1 -d '.'").read()) except: print("Warning: cannot find pid.") gpid = -1 @@ -134,12 +134,12 @@ if args.session: sys.exit(1) label = cmd[1] - print("Adding label '%s' to sargraph session '%s'." % (label, sid)) - p = subprocess.Popen(["screen", "-S", sid, "-X", "stuff", "%s\n" % label]) + print(f"Adding label '{label}' to sargraph session '{sid}'.") + p = subprocess.Popen(["screen", "-S", sid, "-X", "stuff", f"{label}\n"]) while p.poll() is None: time.sleep(0.1) else: - print("Error: Unknown command '%s'" % cmd[0]) + print(f"Error: Unknown command '{cmd[0]}'") sys.exit(1) sys.exit(0) @@ -154,12 +154,12 @@ with open("/proc/meminfo") as f: p = run_process("sar", "-F", "-u", "-r", "1", stdout=subprocess.PIPE, env=my_env) -print("%d" % os.getpid()) +print(os.getpid()) machine = p.stdout.readline().decode() uname = machine.split(" ")[0:2] -uname = "%s %s" % (uname[0], uname[1]) +uname = f"{uname[0]} {uname[1]}" cpus = int(machine.split(" CPU)")[0].split("(")[-1]) @@ -172,7 +172,11 @@ with open("/proc/cpuinfo") as f: break with open("data.txt", "w") as f: - f.write("# pid: %d, machine: %s, cpu count: %d, cpu: %s\n" % (os.getpid(), uname, cpus, cpu_name)) + print(f"# pid: {os.getpid()}", + f"machine: {uname}", + f"cpu count: {cpus}", + f"cpu: {cpu_name}", + sep=", ", file=f) p.stdout.readline() @@ -180,9 +184,9 @@ if args.fspath: args.fspath = os.path.realpath(args.fspath) with open("/proc/self/mounts", "r") as f: while args.fsdev is None: - args.fsdev = scan("^(/dev/\S+)\s+%s\s+" % re.escape(args.fspath), str, f.readline()) + args.fsdev = scan(f"^(/dev/\S+)\s+{re.escape(args.fspath)}\s+", str, f.readline()) if not args.fsdev: - print("Error: no device is mounted on %s" % args.fspath) + print(f"Error: no device is mounted on {args.fspath}") sys.exit(1) signal.signal(signal.SIGTERM, kill_handler) @@ -199,7 +203,6 @@ FS_SAR_INDEX = None flags = fcntl.fcntl(sys.stdin, fcntl.F_GETFL) fcntl.fcntl(sys.stdin, fcntl.F_SETFL, flags | os.O_NONBLOCK) -labels = [] # Gather data from sar output while 1: @@ -210,14 +213,15 @@ while 1: if label_line == "q": die = 1 break - labels.append(["%04d-%02d-%02d-%02d:%02d:%02d" % (now.year, now.month, now.day, now.hour, now.minute, now.second), label_line]) + with open("data.txt", "a") as f: - f.write("# %04d-%02d-%02d-%02d:%02d:%02d label: %s\n" % (now.year, now.month, now.day, now.hour, now.minute, now.second, label_line)) + timestamp = now.strftime("%Y-%m-%d-%H:%M:%S") + print(f"# {timestamp} label: {label_line}", file=f) if (p.stdout not in rlist): continue - date = "%04d-%02d-%02d" % (now.year, now.month, now.day) - daytime = "%02d:%02d:%02d" % (now.hour, now.minute, now.second) + date = now.strftime("%Y-%m-%d") + daytime = now.strftime("%H:%M:%S") # Read and process CPU data cpu_data = read_table(p.stdout) @@ -254,7 +258,11 @@ while 1: timestamp = date + "-" + daytime with open("data.txt", "a") as f: - f.write("%s %s %s %s\n" % (timestamp, cpu_data["%user"][0], ram_data["%memused"][0], fs_data["%fsused"][FS_SAR_INDEX])) + print(timestamp, + cpu_data['%user'][0], + ram_data['%memused'][0], + fs_data['%fsused'][FS_SAR_INDEX], + file=f) if die: break @@ -274,4 +282,11 @@ edt = datetime.datetime.strptime(END_DATE, '%Y-%m-%d %H:%M:%S') delta_t = ((edt - sdt).total_seconds()) / 60.0 with open("data.txt", "a") as f: - f.write("# total ram: %.2f GB, total disk space: %.2f GB, max ram used: %.2f GB, max disk used: %.2f GB, average load: %.2f %%, observed disk: %s, duration: %.2f minutes\n" % (TOTAL_RAM, TOTAL_FS, MAX_USED_RAM, MAX_USED_FS, AVERAGE_LOAD, FS_NAME, delta_t)) + print(f"# total ram: {TOTAL_RAM:.2f} GB", + f"total disk space: {TOTAL_FS:.2f} GB", + f"max ram used: {MAX_USED_RAM:.2f} GB", + f"max disk used: {MAX_USED_FS:.2f} GB", + f"average load: {AVERAGE_LOAD:.2f} %%", + f"observed disk: {FS_NAME}", + f"duration: {delta_t:.2f} minutes", + sep=", ", file=f)