add option to log time series to a csv file

This commit is contained in:
xenofem 2022-02-08 02:34:15 -05:00
parent f1ac7516b0
commit a8a2009923

View file

@ -16,6 +16,7 @@ parser.add_argument('-w', '--window', type=int, default=100, help='Number of sam
parser.add_argument('-d', '--decay', type=float, default=1, help='Decay coefficient for older samples (default 1). Must be between 0 and 1 inclusive. The lower this is, the more responsive/swingy the estimate will be.')
parser.add_argument('-V', '--termination-value-threshold', type=float, default=0.95, metavar='FRACTION', help='Fraction of the expected final value that must be reached in order to terminate (default 0.95). Reaching this threshold is necessary but not sufficient, see also -I')
parser.add_argument('-I', '--termination-inactivity-threshold', type=int, default=10, metavar='COUNT', help='Number of consecutive unchanged samples that must be observed in order to terminate (default 10). Reaching this threshold is necessary but not sufficient, see also -V')
parser.add_argument('-l', '--log-file', type=str, metavar='PATH', help='File to log the time series to. Will be saved as a csv, with columns for timestamp and for value. Will append data if the file already exists.')
tracker_types = parser.add_mutually_exclusive_group(required=True)
tracker_types.add_argument('-p', '--path', type=str, help='Track total disk usage of a given path')
tracker_types.add_argument('-c', '--command', type=str, help='Track value returned by a shell command; this should return a single number, optionally with a K/M/G/T suffix')
@ -63,14 +64,25 @@ def display_timedelta(d):
return result
if args.path:
def current_val():
def current_val_helper():
du = subprocess.run(['du', '--bytes', '--summarize', args.path], capture_output=True, text=True).stdout
return parse_value(du.split()[0])
else:
def current_val():
def current_val_helper():
result = subprocess.run(args.command, shell=True, capture_output=True, text=True).stdout
return parse_value(result.strip())
if args.log_file:
log_file = open(args.log_file, mode='a', buffering=1)
else:
log_file = None
def current_val():
result = current_val_helper()
if log_file:
print('{},{}'.format(datetime.datetime.now(datetime.timezone.utc), result), file=log_file)
return result
if args.final:
final = parse_value(args.final)
if final <= 0: