In [1]:
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   |          |          |       |
+--------------------+----------+----------+----------+-----------+----------+----------+-------+
"""
In [2]:
line = [l for l in raw.split("\n") if l.strip() != ""]
In [3]:
line
Out[3]:
['+--------------------+----------+----------+----------+-----------+----------+----------+-------+',
 '| 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   |          |          |       |',
 '+--------------------+----------+----------+----------+-----------+----------+----------+-------+']
In [4]:
cols = [i.strip().replace(":", "") for i in line[1].split("|")]
In [5]:
cols
Out[5]:
['',
 'FreeIPA servers',
 'ipa01',
 'ipa02',
 'ipa03',
 'ipa04',
 'ipa05',
 'ipa06',
 'STATE',
 '']
In [10]:
rows = []
for l in line[2:]:
    l2 = [i.strip() for i in l.split("|")]
    print(l2)
    rows.append(l2)
['+--------------------+----------+----------+----------+-----------+----------+----------+-------+']
['', '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', '', '', '', '']
['+--------------------+----------+----------+----------+-----------+----------+----------+-------+']
In [ ]:
 
In [8]:
cols2 = cols[1:-1]
In [9]:
cols2
Out[9]:
['FreeIPA servers',
 'ipa01',
 'ipa02',
 'ipa03',
 'ipa04',
 'ipa05',
 'ipa06',
 'STATE']
In [11]:
rows2 = rows[1:-1]
rows2 = [i[1:-1] for i in rows2]
In [12]:
rows2
Out[12]:
[['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', '', '', '']]
In [ ]:
 
In [13]:
#svcs
In [16]:
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}")
freeipa_svc_status{"description"="Active Users"} 0
freeipa_svc_status{"description"="Stage Users"} 0
freeipa_svc_status{"description"="Preserved Users"} 0
freeipa_svc_status{"description"="Hosts"} 0
freeipa_svc_status{"description"="Services"} 0
freeipa_svc_status{"description"="User Groups"} 0
freeipa_svc_status{"description"="Host Groups"} 0
freeipa_svc_status{"description"="Netgroups"} 0
freeipa_svc_status{"description"="HBAC Rules"} 0
freeipa_svc_status{"description"="SUDO Rules"} 0
freeipa_svc_status{"description"="DNS Zones"} 0
freeipa_svc_status{"description"="Certificates"} 0
freeipa_svc_status{"description"="LDAP Conflicts"} 0
freeipa_svc_status{"description"="Ghost Replicas"} 0
freeipa_svc_status{"description"="Anonymous BIND"} 0
freeipa_svc_status{"description"="Microsoft ADTrust"} 0
freeipa_svc_status{"description"="Replication Status"} 0
In [ ]:
 
In [33]:
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}")
0 ['Active Users', '1199', '1199', '1199', '1199', '1199', '1199', 'OK']
active_users{description="ipa01"} 1199
active_users{description="ipa02"} 1199
active_users{description="ipa03"} 1199
active_users{description="ipa04"} 1199
active_users{description="ipa05"} 1199
active_users{description="ipa06"} 1199
active_users{description="total"} 7194
1 ['Stage Users', '0', '0', '0', '0', '0', '0', 'OK']
stage_users{description="ipa01"} 0
stage_users{description="ipa02"} 0
stage_users{description="ipa03"} 0
stage_users{description="ipa04"} 0
stage_users{description="ipa05"} 0
stage_users{description="ipa06"} 0
stage_users{description="total"} 0
2 ['Preserved Users', '0', '0', '0', '0', '0', '0', 'OK']
preserved_users{description="ipa01"} 0
preserved_users{description="ipa02"} 0
preserved_users{description="ipa03"} 0
preserved_users{description="ipa04"} 0
preserved_users{description="ipa05"} 0
preserved_users{description="ipa06"} 0
preserved_users{description="total"} 0
3 ['Hosts', '357', '357', '357', '357', '357', '357', 'OK']
hosts{description="ipa01"} 357
hosts{description="ipa02"} 357
hosts{description="ipa03"} 357
hosts{description="ipa04"} 357
hosts{description="ipa05"} 357
hosts{description="ipa06"} 357
hosts{description="total"} 2142
4 ['Services', '49', '49', '49', '49', '49', '49', 'OK']
services{description="ipa01"} 49
services{description="ipa02"} 49
services{description="ipa03"} 49
services{description="ipa04"} 49
services{description="ipa05"} 49
services{description="ipa06"} 49
services{description="total"} 294
5 ['User Groups', '55', '55', '55', '55', '55', '55', 'OK']
user_groups{description="ipa01"} 55
user_groups{description="ipa02"} 55
user_groups{description="ipa03"} 55
user_groups{description="ipa04"} 55
user_groups{description="ipa05"} 55
user_groups{description="ipa06"} 55
user_groups{description="total"} 330
6 ['Host Groups', '29', '29', '29', '29', '29', '29', 'OK']
host_groups{description="ipa01"} 29
host_groups{description="ipa02"} 29
host_groups{description="ipa03"} 29
host_groups{description="ipa04"} 29
host_groups{description="ipa05"} 29
host_groups{description="ipa06"} 29
host_groups{description="total"} 174
7 ['Netgroups', '11', '11', '11', '11', '11', '11', 'OK']
netgroups{description="ipa01"} 11
netgroups{description="ipa02"} 11
netgroups{description="ipa03"} 11
netgroups{description="ipa04"} 11
netgroups{description="ipa05"} 11
netgroups{description="ipa06"} 11
netgroups{description="total"} 66
8 ['HBAC Rules', '3', '3', '3', '3', '3', '3', 'OK']
hbac_rules{description="ipa01"} 3
hbac_rules{description="ipa02"} 3
hbac_rules{description="ipa03"} 3
hbac_rules{description="ipa04"} 3
hbac_rules{description="ipa05"} 3
hbac_rules{description="ipa06"} 3
hbac_rules{description="total"} 18
9 ['SUDO Rules', '2', '2', '2', '2', '2', '2', 'OK']
sudo_rules{description="ipa01"} 2
sudo_rules{description="ipa02"} 2
sudo_rules{description="ipa03"} 2
sudo_rules{description="ipa04"} 2
sudo_rules{description="ipa05"} 2
sudo_rules{description="ipa06"} 2
sudo_rules{description="total"} 12
10 ['DNS Zones', '114', '114', '114', '114', '114', '114', 'OK']
dns_zones{description="ipa01"} 114
dns_zones{description="ipa02"} 114
dns_zones{description="ipa03"} 114
dns_zones{description="ipa04"} 114
dns_zones{description="ipa05"} 114
dns_zones{description="ipa06"} 114
dns_zones{description="total"} 684
11 ['Certificates', '0', '0', '0', '0', '0', '0', 'OK']
certificates{description="ipa01"} 0
certificates{description="ipa02"} 0
certificates{description="ipa03"} 0
certificates{description="ipa04"} 0
certificates{description="ipa05"} 0
certificates{description="ipa06"} 0
certificates{description="total"} 0
12 ['LDAP Conflicts', '0', '0', '0', '0', '0', '0', 'OK']
ldap_conflicts{description="ipa01"} 0
ldap_conflicts{description="ipa02"} 0
ldap_conflicts{description="ipa03"} 0
ldap_conflicts{description="ipa04"} 0
ldap_conflicts{description="ipa05"} 0
ldap_conflicts{description="ipa06"} 0
ldap_conflicts{description="total"} 0
13 ['Ghost Replicas', '0', '0', '0', '0', '0', '0', 'OK']
ghost_replicas{description="ipa01"} 0
ghost_replicas{description="ipa02"} 0
ghost_replicas{description="ipa03"} 0
ghost_replicas{description="ipa04"} 0
ghost_replicas{description="ipa05"} 0
ghost_replicas{description="ipa06"} 0
ghost_replicas{description="total"} 0
14 ['Anonymous BIND', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'OK']
anonymous_bind{description="ipa01"} 0
anonymous_bind{description="ipa02"} 0
anonymous_bind{description="ipa03"} 0
anonymous_bind{description="ipa04"} 0
anonymous_bind{description="ipa05"} 0
anonymous_bind{description="ipa06"} 0
anonymous_bind{description="total"} 0
15 ['Microsoft ADTrust', 'False', 'False', 'False', 'False', 'False', 'False', 'OK']
microsoft_adtrust{description="ipa01"} 1
microsoft_adtrust{description="ipa02"} 1
microsoft_adtrust{description="ipa03"} 1
microsoft_adtrust{description="ipa04"} 1
microsoft_adtrust{description="ipa05"} 1
microsoft_adtrust{description="ipa06"} 1
microsoft_adtrust{description="total"} 6
In [ ]:
 
In [ ]: