diff --git a/common.py b/common.py new file mode 100644 index 0000000..b39803d --- /dev/null +++ b/common.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +# +# (c) 2019-2022 Antmicro +# License: Apache +# + + +import os +import subprocess +import sys +import re + + +# Run process, return subprocess object on success, exit script on fail +def run_process(*argv, **kwargs): + try: + p = subprocess.Popen(argv, **kwargs) + except: + print("Error: '%s' tool not found" % argv[0]) + sys.exit(1) + return p + + +# Convert a string to float, also when the separator is a comma +def stof(s): + return float(s.replace(',', '.')) + + +# Get the first group from a given match and convert to required type +def scan(regex, conv, string): + match = re.search(regex, string) + if not match: + return None + try: + value = conv(match.group(1)) + except ValueError: + return None + return value diff --git a/graph.py b/graph.py index 6a30449..07a60cd 100755 --- a/graph.py +++ b/graph.py @@ -6,63 +6,21 @@ # -import argparse import os -import signal import subprocess import sys import time from datetime import datetime, timedelta -from select import select -from fcntl import fcntl, F_GETFL, F_SETFL -from os.path import realpath from socket import gethostname -from re import search, escape -global gnuplot -global die +from common import * -die = 0 +global gnuplot GNUPLOT_VERSION_EXPECTED = 5.0 -parser = argparse.ArgumentParser() -parser.add_argument('session', metavar='SESSION-NAME', type=str, nargs='?', default=None, help='sargraph session name') -parser.add_argument('command', metavar='COMMAND', type=str, nargs='*', help='send command') -parser.add_argument('-f', metavar='DEVICE-NAME', type=str, nargs='?', default=None, dest='fsdev', help='observe a chosen filesystem') -parser.add_argument('-m', metavar='MOUNT-DIR', type=str, nargs='?', default=None, dest='fspath', help='observe a chosen filesystem') -args = parser.parse_args() - - -# Run process, return subprocess object on success, exit script on fail -def run_process(*argv, **kwargs): - try: - p = subprocess.Popen(argv, **kwargs) - except: - print("Error: '%s' tool not found" % argv[0]) - sys.exit(1) - return p - - -# Convert a string to float, also when the separator is a comma -def stof(s): - return float(s.replace(',', '.')) - - -# Get the first group from a given match and convert to required type -def scan(regex, conv, string): - match = search(regex, string) - if not match: - return None - try: - value = conv(match.group(1)) - except ValueError: - return None - return value - - # Run a command in a running gnuplot process def g(command): global gnuplot @@ -82,6 +40,13 @@ def g(command): while gnuplot.poll() is None: time.sleep(0.25) +# Check if the avaliable gnuplot has a required version +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])) + sys.exit(1) + START_DATE = "" END_DATE = "" diff --git a/watch.py b/watch.py index a563c7c..01b3b4d 100755 --- a/watch.py +++ b/watch.py @@ -13,20 +13,19 @@ import subprocess import sys import time -from datetime import datetime, timedelta +from datetime import datetime from select import select from fcntl import fcntl, F_GETFL, F_SETFL from os.path import realpath -from socket import gethostname -from re import search, escape +from re import escape + +from common import * global gnuplot global die die = 0 -GNUPLOT_VERSION_EXPECTED = 5.0 - parser = argparse.ArgumentParser() parser.add_argument('session', metavar='SESSION-NAME', type=str, nargs='?', default=None, help='sargraph session name') @@ -47,28 +46,6 @@ def pid_running(pid): return os.path.exists("/proc/%d" % pid) -# Run process, return subprocess object on success, exit script on fail -def run_process(*argv, **kwargs): - try: - p = subprocess.Popen(argv, **kwargs) - except: - print("Error: '%s' tool not found" % argv[0]) - sys.exit(1) - return p - - -# Get the first group from a given match and convert to required type -def scan(regex, conv, string): - match = search(regex, string) - if not match: - return None - try: - value = conv(match.group(1)) - except ValueError: - return None - return value - - # Read a single table from sar output def read_table(f): # Find the header @@ -96,19 +73,6 @@ def read_table(f): return table -# Convert a string to float, also when the separator is a comma -def stof(s): - return float(s.replace(',', '.')) - - -# Check if the avaliable gnuplot has a required version -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])) - sys.exit(1) - - OUTPUT_TYPE="pngcairo" OUTPUT_EXT="png" try: @@ -140,7 +104,7 @@ if args.session: if cmd[0] == "start": print("Starting sargraph session '%s'" % sid) - p = subprocess.Popen(["screen", "-dmSL", sid, os.path.realpath(__file__), *sys.argv[3:]]) + p = subprocess.Popen(["screen", "-dmSL", sid, realpath(__file__), *sys.argv[3:]]) while p.poll() is None: time.sleep(0.1) gpid = 0 @@ -181,10 +145,6 @@ if args.session: sys.exit(1) sys.exit(0) -# If the script runs in a screen session, initialize the plot and gather data - -gnuplot = run_process("gnuplot", stdin=subprocess.PIPE, stdout=subprocess.PIPE) - my_env = os.environ my_env["S_TIME_FORMAT"] = "ISO"