0

The question was misleading, that is why I edited it

I have two keywords OK and CRITICAL. I would like to create lists of these two keywords dynamically according to the length of a list A and the state of the hosts in list B. If list A = [a, b], and B = ['master', 'FAULT'] or B = ['master', None]then I want to create C = ['OK', 'CRITICAL']. If B = ['FAULT', 'master'] or B = [None, 'master'] then I want to create C = ['OK', 'CRITICAL'].

If List A = [a, b, c], and B = ['master', 'FAULT', None] or what ever combination where master occurs only one time and the other states are FAULT|None I want to create C = ['OK', 'CRITICAL', 'CRITICAL'], basically OK in C should be at the same index of master in B, and so on.

The main idea is I am checking the state of hots in list A, A is dynamically created and passed as an argument, so I have no idea about len(A). That is why I need to generate the needed lists at run time. I already handled the case where no host is OK, and when no 'CRITICAL' occurs, now I would like to handle these cases, if one host is OK and the rest are CRITICAL, and accordingly generate my sys.exit() code dynamically according to len(A) and the host index in A to identify which host is in CRITICAL state. A small demonstration:

hosts = [a, b]
state = ['master', 'backup']
msg = ['OK', 'CRITICAL']

if state.count('master') != 1:
    print(', '.join('CRITICAL - host {} state is {}'.format(hosts[i], state[0]) for i in range(len(hosts))))
    sys.exit(2)

# Problem, how many if statements are needed? How can this be created dynamically
elif state[0] == 'master' and 'backup' not in state:
    print(', '.join('{} - host {} state is {}'.format(msg[i], hosts[i], state[i]) for i in range(len(hosts))))
    sys.exit(1)
...........................
...........................
...........................
else:
    print(', '.join("OK - host {} state is {}".format(hosts[i], state[i]) for i in range(len(hosts))))
    sys.exit(0)

After all I need to generate [msg] for the print function, and it should be dynamically, so using [state] to get the matched [msg] according to the OK|master index.

Thanks for any help.

Answer:

a = ['master', 'FAULT', None]
b = ['FAULT', None, 'master']
c = [None, 'master', 'FAULT']


def getMsg(state):
    d = []
    for i in state:
        if i == 'master':
            d.append('OK')
        else:
            d.append('CRITICAL')
    return(d)


print(getMsg(a))  # --> ['OK', 'CRITICAL', 'CRITICAL']
print(getMsg(b))  # --> ['CRITICAL', 'CRITICAL', 'OK']
print(getMsg(c))  # --> ['CRITICAL', 'OK', 'CRITICAL']

and the create a loop:

....................
....................
state = ['master', 'FAULT', None]
msg = getMsg(state)

if 'master' in state and 'backup' not in state:
    print(', '.join('{} - host {} state is {}'.format(msg[i], hosts[i], state[i]) for i in range(len(hosts))))
        sys.exit(1)
....................
....................

Once again sorry for the misleading formulation.

Max
  • 543
  • 6
  • 17
  • 3
    are you just creating diagonal matrices? – chris Nov 19 '19 at 19:41
  • I'll edit my question. – Max Nov 19 '19 at 19:45
  • 1
    Why not use an identity matrix `numpy.identity(n)` where `n` is the `len(a)`. You can interpret 1's as OK and 0's as CRITICAL – Zeeshan Nov 19 '19 at 19:59
  • Thanks Zeeshan. I'll take a look in to numpy. – Max Nov 19 '19 at 20:02
  • Why is this question a duplicate? I took a brief look into the linked question, it doesn't help me at all. – Max Nov 19 '19 at 20:05
  • Because it tells you that it's dangerous and you shouldn't do it. Instead, you should use a dictionary to hold all the lists you need. It also tells you how to create variables dynamically, if you really insist. You can rewrite your question from this point on, if this doesn't help you, but as your post stands now, the responses to the other question answer it. – Al.G. Nov 19 '19 at 21:18
  • I understood that it is dangerous, but how can I use a dictionary when I don't know how many items are held by list A? A is dynamically passed as an argument and it represents a cluster of hosts, which could be two, three or even 10. As @Zeeshan mentioned `numpy.identity()` can generate such a data structure depending on `len(A)`, and with a bit more code I can generate my lists with the strings I need. Thank you @AI.G. – Max Nov 19 '19 at 22:20
  • I think I have it without any complicated ideas. I was blind. A simple function which takes the `[state]` as an argument and generates `[msg]` accordingly. `a = ['m', 'b', 'b'] def getMsg(state): b = [] for i in state: if i == 'm': b.append('OK') else: b.append('CR') return(b) print(getMsg(a))` – Max Nov 20 '19 at 00:18
  • Sorry, my question was misleading. I didn't need all the lists at run time, rather only the matched one. – Max Nov 20 '19 at 00:22

0 Answers0