-
Notifications
You must be signed in to change notification settings - Fork 5
/
summarizedMetricsTransformer.py
66 lines (55 loc) · 2.01 KB
/
summarizedMetricsTransformer.py
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
import argparse
import json
def convertInputToJSON(inputFile: str):
# Expected format is:
# Timestamp
# Header
# 1-N pods
state = "Timestamp"
with open(inputFile, "r") as f:
data = {}
instantData = None
samples = None
sample = None
headers = None
podCount = 0
for line in f:
if state == "Timestamp":
timestamp = line.strip()
headers = []
samples = []
instantData = {"timestamp": timestamp, "samples": samples}
data[timestamp] = instantData
state = "Header"
elif state == "Header":
headers = line.strip().split()
state = "Pods"
elif state == "Pods":
if line == "---":
state = "Timestamp"
else:
sample = {}
podCount += 1
elements = line.strip().split()
for i in range(0, len(elements)):
filteredHeader = headers[i].replace("(", "_").replace(")", "")
sample[filteredHeader] = elements[i]
samples.append(sample)
# Hack for count ... need a delimiter between each sample.
if podCount >= 19:
state = "Timestamp"
podCount = 0
return data
def writeJSON(content: dict, outputFile: str):
with open(outputFile, "w") as f:
json.dump(content, f, indent=4)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Converts gathered Kubernetes metrics from tabular form to JSON."
)
parser.add_argument("-i", "--input", help="The input file.", required=True)
parser.add_argument("-o", "--output", help="The output file.", required=True)
args = parser.parse_args()
result = convertInputToJSON(args.input)
writeJSON(result, args.output)
print("Done")