From 5a6949271977775b3dad67c9d1ad2891de1f52e3 Mon Sep 17 00:00:00 2001 From: Aleksander Kiryk Date: Mon, 21 Feb 2022 12:28:48 +0100 Subject: [PATCH] Add backwards compatible sargraph.py script --- graph.py | 84 ++++++++++++++++++++++++++--------------------------- sargraph.py | 10 +++++++ watch.py | 11 ++++--- 3 files changed, 59 insertions(+), 46 deletions(-) create mode 100755 sargraph.py diff --git a/graph.py b/graph.py index 938f0a1..03bbf35 100755 --- a/graph.py +++ b/graph.py @@ -19,7 +19,6 @@ global gnuplot GNUPLOT_VERSION_EXPECTED = 5.0 - # Run a command in a running gnuplot process def g(command): global gnuplot @@ -62,60 +61,61 @@ cpu_name="unknown" labels = [] -for line in sys.stdin: - value = None +with open("data.txt", "r") as f: + for line in f: + value = None - if len(line) <= 0: - continue + if len(line) <= 0: + continue - if line[0] != '#': - if not START_DATE: - START_DATE = scan("^(\S+)", str, line) - END_DATE = scan("^(\S+)", str, line) + if line[0] != '#': + if not START_DATE: + START_DATE = scan("^(\S+)", str, line) + END_DATE = scan("^(\S+)", str, line) - value = scan("label: (.+)", str, line) - if value is not None: - key = scan("(\S+) label:", str, line) - labels.append([key, value]) + value = scan("label: (.+)", str, line) + if value is not None: + key = scan("(\S+) label:", str, line) + labels.append([key, value]) - # Comments are not mixed with anything else, so skip - continue + # Comments are not mixed with anything else, so skip + continue - value = scan("machine: ([^,]+)", str, line) - if value is not None: - uname = value + value = scan("machine: ([^,]+)", str, line) + if value is not None: + uname = value - value = scan("cpu count: ([^,]+)", int, line) - if value is not None: - cpus = value + value = scan("cpu count: ([^,]+)", int, line) + if value is not None: + cpus = value - value = scan("cpu: ([^,\n]+)", str, line) - if value is not None: - cpu_name = value + value = scan("cpu: ([^,\n]+)", str, line) + if value is not None: + cpu_name = value - value = scan("observed disk: ([^,]+)", str, line) - if value is not None: - NAME_FS = value + value = scan("observed disk: ([^,]+)", str, line) + if value is not None: + NAME_FS = value - value = scan("total ram: (\S+)", stof, line) - if value is not None: - TOTAL_RAM = value + value = scan("total ram: (\S+)", stof, line) + if value is not None: + TOTAL_RAM = value - value = scan("max ram used: (\S+)", stof, line) - if value is not None: - MAX_USED_RAM = value + value = scan("max ram used: (\S+)", stof, line) + if value is not None: + MAX_USED_RAM = value - value = scan("total disk space: (\S+)", stof, line) - if value is not None: - TOTAL_FS = value + value = scan("total disk space: (\S+)", stof, line) + if value is not None: + TOTAL_FS = value - value = scan("max disk used: (\S+)", stof, line) - if value is not None: - MAX_USED_FS = value + value = scan("max disk used: (\S+)", stof, line) + if value is not None: + MAX_USED_FS = value - value = scan("average load: (\S+)", stof, line) - if value is not None: - AVERAGE_LOAD = value + value = scan("average load: (\S+)", stof, line) + if value is not None: + AVERAGE_LOAD = value # Initialize the plot diff --git a/sargraph.py b/sargraph.py new file mode 100755 index 0000000..e9c3fdc --- /dev/null +++ b/sargraph.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python3 + +# +# (c) 2019-2022 Antmicro +# License: Apache +# + + +import watch +import graph diff --git a/watch.py b/watch.py index 58b3dd8..8d5df20 100755 --- a/watch.py +++ b/watch.py @@ -17,6 +17,9 @@ import subprocess import sys import time +# Get access to main module information +import __main__ + from common import * global die @@ -35,8 +38,8 @@ args = parser.parse_args() # Handle SIGTERM def kill_handler(a, b): - global die - die = 1 + global die + die = 1 # Check if a process is running @@ -102,7 +105,7 @@ if args.session: if cmd[0] == "start": print(f"Starting sargraph session '{sid}'") - p = subprocess.Popen(["screen", "-dmSL", sid, os.path.realpath(__file__), *sys.argv[3:]]) + p = subprocess.Popen(["screen", "-dmSL", sid, os.path.realpath(__main__.__file__), *sys.argv[3:]]) while p.poll() is None: time.sleep(0.1) gpid = 0 @@ -286,7 +289,7 @@ with open("data.txt", "a") as f: 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"average load: {AVERAGE_LOAD:.2f} %", f"observed disk: {FS_NAME}", f"duration: {delta_t:.2f} minutes", sep=", ", file=f)