Skip to content

Instantly share code, notes, and snippets.

@mhf-ir
Created July 6, 2022 10:03
Show Gist options
  • Save mhf-ir/c431e2246bf34443cbd699c4ed80d131 to your computer and use it in GitHub Desktop.
Save mhf-ir/c431e2246bf34443cbd699c4ed80d131 to your computer and use it in GitHub Desktop.
IPMI Log to json
#!/usr/bin/env python3
import csv
import subprocess
import re
import json
from datetime import datetime
def map_entry(s: str) -> str:
return re.sub(' +', ' ', s.strip()).strip()
date_regex = re.compile(r'(?P<MM>\d{1,2})/(?P<dd>\d{1,2})/(?P<yyyy>\d{4})')
time_regex = re.compile(r'(?P<HH>\d{1,2}):(?P<mm>\d{1,2}):(?P<ss>\d{1,2})')
ipmi_tool = subprocess.Popen(
['ipmitool', 'sel', 'elist', '-c'], stdout=subprocess.PIPE)
output = ipmi_tool.communicate()[0].decode('utf8')
csv_entries = csv.reader(output.splitlines(), delimiter=",")
default_time = datetime.now()
for row_raw in csv_entries:
row = list(map(map_entry, row_raw))
time = default_time
# init
item = {
# record id
"id": row[0],
"item": row[3],
"prop": row[4],
"stat": row[5],
}
if len(row) > 6:
item["extra"] = row[6]
# datetime checker
date_matched = list(date_regex.finditer(row[1]))
time_matched = list(time_regex.finditer(row[2]))
if (len(date_matched) > 0 and len(time_matched) > 0):
date = {
"yyyy": date_matched[0].group(3),
"MM": date_matched[0].group(1),
"dd": date_matched[0].group(2),
"HH": time_matched[0].group(1),
"mm": time_matched[0].group(2),
"ss": time_matched[0].group(3),
}
formatted_datetime = "{yyyy}/{MM}/{dd} {HH}:{mm}:{ss}".format(**date)
time = datetime.strptime(formatted_datetime, '%Y/%m/%d %H:%M:%S')
default_time = time
item["time"] = time.timestamp()
print(json.dumps(item))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment