summaryrefslogtreecommitdiff
path: root/.clusterman.py
blob: 506beb7231c06b172cb50dc93ae9cebf57f81272 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/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
POWER_PATH="/home/admin/ina260-zmq-publisher/data"
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(",")
                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)