ina260-zmq-publisher/.clusterman.py

83 lines
2.5 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
import sys, json, os, argparse
#### Signal must be handled to use pipe the command line (e.g: ./.clusterman.py node-pi-1 1712660604 1712660705|head)
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE,SIG_DFL)
#### Parse arguments
parser = argparse.ArgumentParser(description="Script to collect power measurements")
parser.add_argument('key', help='Key to use e.g: hostname')
parser.add_argument('start', help='Starting timestamp')
parser.add_argument('end', help='Ending timestamp')
parser.add_argument('-u', '--unique', help="Do not report duplication power measurements", action="store_true")
args=parser.parse_args()
#### Loading arguments
key=args.key
startTime=int(args.start)
endTime=int(args.end)
unique=args.unique
#### Init various constancs
LOG_INTERVAL=20
2024-04-09 14:07:52 +02:00
POWER_PATH="/home/admin/ina260-zmq-publisher/data"
2024-04-09 14:09:09 +02:00
MAP_FILE="/home/admin/ina260-zmq-publisher/.map.json"
# Getting measuremnts location according to key
with open(MAP_FILE) as f:
global MAP
MAP = json.load(f)
MEASUREMENTS_LOCATION=os.path.join(POWER_PATH,MAP[key])
START_INTERVAL=startTime-(startTime%LOG_INTERVAL)
END_INTERVAL=endTime-(endTime%LOG_INTERVAL)
#### Init various variables
showHeader=True
interval=START_INTERVAL
lastPower="" # Store last read measurement
finalLinePrinted=False
finalLine=""
# Start reporting
while True:
# Open the correct measurement file
path=os.path.join(MEASUREMENTS_LOCATION,str(interval))
theFile = open(path, 'r')
# Start reading lines in the file
linum=0
while True:
line = theFile.readline()
if not line:
break
linum+=1
line=line.strip() # Remove newline char at the end
if linum <= 1 and showHeader:
print(line)
showHeader=False
elif linum > 1:
if unique:
split=line.split(",")
2024-04-09 14:18:05 +02:00
if lastPower != "":
if split[2] != lastPower:
print(line)
finalLinePrinted=True
else:
finalLinePrinted=False
else:
print(line)
finalLine=line
lastPower=split[2]
else:
print(line)
theFile.close()
# Update interval
if interval >= END_INTERVAL:
break
interval+=LOG_INTERVAL
#### Check if last power measurement need to be explicitely printed
if unique and not finalLinePrinted:
print(finalLine)