raw = """
+--------------------+----------+----------+----------+-----------+----------+----------+-------+
| FreeIPA servers: | ipa01 | ipa02 | ipa03 | ipa04 | ipa05 | ipa06 | STATE |
+--------------------+----------+----------+----------+-----------+----------+----------+-------+
| Active Users | 1199 | 1199 | 1199 | 1199 | 1199 | 1199 | OK |
| Stage Users | 0 | 0 | 0 | 0 | 0 | 0 | OK |
| Preserved Users | 0 | 0 | 0 | 0 | 0 | 0 | OK |
| Hosts | 357 | 357 | 357 | 357 | 357 | 357 | OK |
| Services | 49 | 49 | 49 | 49 | 49 | 49 | OK |
| User Groups | 55 | 55 | 55 | 55 | 55 | 55 | OK |
| Host Groups | 29 | 29 | 29 | 29 | 29 | 29 | OK |
| Netgroups | 11 | 11 | 11 | 11 | 11 | 11 | OK |
| HBAC Rules | 3 | 3 | 3 | 3 | 3 | 3 | OK |
| SUDO Rules | 2 | 2 | 2 | 2 | 2 | 2 | OK |
| DNS Zones | 114 | 114 | 114 | 114 | 114 | 114 | OK |
| Certificates | 0 | 0 | 0 | 0 | 0 | 0 | OK |
| LDAP Conflicts | 0 | 0 | 0 | 0 | 0 | 0 | OK |
| Ghost Replicas | 0 | 0 | 0 | 0 | 0 | 0 | OK |
| Anonymous BIND | ON | ON | ON | ON | ON | ON | OK |
| Microsoft ADTrust | False | False | False | False | False | False | OK |
| Replication Status | ipa03 0 | ipa03 0 | ipa04 0 | ipa03 0 | ipa03 0 | ipa04 0 | OK |
| | ipa04 0 | ipa04 0 | ipa05 0 | ipa01 0 | ipa01 0 | | |
| | ipa05 0 | ipa05 0 | ipa01 0 | ipa02 0 | ipa02 0 | | |
| | ipa02 0 | ipa01 0 | ipa02 0 | ipa06 0 | | | |
+--------------------+----------+----------+----------+-----------+----------+----------+-------+
"""
line = [l for l in raw.split("\n") if l.strip() != ""]
line
cols = [i.strip().replace(":", "") for i in line[1].split("|")]
cols
rows = []
for l in line[2:]:
l2 = [i.strip() for i in l.split("|")]
print(l2)
rows.append(l2)
cols2 = cols[1:-1]
cols2
rows2 = rows[1:-1]
rows2 = [i[1:-1] for i in rows2]
rows2
#svcs
for r in rows2:
if r[0] == "": continue
svc = r[0]
status = r[-1]
status = 0 if status == 'OK' else 1
print(f"freeipa_svc_status{{\"description\"=\"{svc}\"}} {status}")
for idy,r in enumerate(rows2):
if r[0] == 'Replication Status': break
print(idy, r)
total = 0
metric = r[0].replace(" ", "_").lower()
for idx,c in enumerate(cols2):
if idx == 0: continue # Free IPA servers:
if idx == 7: continue # STATE
val = r[idx]
if val.isdigit():
val = int(val)
elif val == 'ON':
val = 0
elif val == 'False':
val = 1
else: # FAIL?
val = 1
total += val
print(f"{metric}{{description=\"{c}\"}} {val}")
print(f"{metric}{{description=\"total\"}} {total}")