Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #13571: Applying hosttemplate from generic method fails #106

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 27 additions & 20 deletions centreon/bin/centreon-plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,49 +150,56 @@ def applyRudderMonitoringConfigurations(conf):
register = ConfigParser()
register.read(registerFile)

# list all nodes
nodes_list = {}
data = getRequestToRudderAPI("/nodes?include=minimal")
for node in data['data']['nodes']:
nodes_list[node["id"]] = node["hostname"]

for dir in next(os.walk('/var/rudder/shared-files/root/files'))[1]:
name = nodes_list[dir]
try:
confcsv = csv.reader(open('/var/rudder/shared-files/root/files/' + dir + '/rudder_monitoring.csv'))
except:
print('[!] Node ' + dir + ' has no rudder monitoring config file, considering it empty...')
print('[!] Node ' + name + ' has no rudder monitoring config file, considering it empty...')
confcsv = []

if not any(h['name'] == dir for h in centreon_hosts.list()['result']):
print('[!] Node ' + dir + ' is not registered in Centreon, skipping...')
if not any(h['name'] == name for h in centreon_hosts.list()['result']):
print('[!] Node ' + name + ' is not registered in Centreon, skipping...')
continue

print('[ ] Applying conf to node ' + dir + '...')
print('[ ] Applying conf to node ' + name + '...')
csvTmpList = []
csvMacroKeyList = []
for r in confcsv:
if(r[0] == 'template'):
csvTmpList.append(r[1])
if not any(t['name'] == r[1] for t in centreon_hosts.gettemplate(dir)['result']):
if not any(t['name'] == r[1] for t in centreon_hosts.gettemplate(name)['result']):
try:
centreon_hosts.addtemplate(dir, r[1])
centreon_hosts.applytemplate(dir)
registerTemplate(dir, r[1], register)
centreon_hosts.addtemplate(name, r[1])
centreon_hosts.applytemplate(name)
registerTemplate(name, r[1], register)
except HTTPError:
print('[!] Centreon API error, check if template ' + r[1] + ' exists')
else:
print('[ ] Template ' + r[1] + ' already applied to node ' + dir)
registerTemplate(dir, r[1], register)
print('[ ] Template ' + r[1] + ' already applied to node ' + name)
registerTemplate(name, r[1], register)
elif(r[0] == 'param'):
csvMacroKeyList.append(r[1])
centreon_hosts.setmacro(dir, r[1], ''.join(r[2:]))
registerMacro(dir, r[1], ''.join(r[2:]), register)
centreon_hosts.setmacro(name, r[1], ''.join(r[2:]))
registerMacro(name, r[1], ''.join(r[2:]), register)
else:
print('[!] Incorrect config parameter type ' + r[0] + ', skipping...')

for t in centreon_hosts.gettemplate(dir)['result']:
if not any(t['name'] == n for n in csvTmpList) and templateWasRegistered(dir, t['name'], register):
unregisterTemplate(dir, t['name'], register)
centreon_hosts.deletetemplate(dir, t['name'])
for t in centreon_hosts.gettemplate(name)['result']:
if not any(t['name'] == n for n in csvTmpList) and templateWasRegistered(name, t['name'], register):
unregisterTemplate(name, t['name'], register)
centreon_hosts.deletetemplate(name, t['name'])

for mk in centreon_hosts.getmacro(dir)['result']:
if not any(mk['macro name'] == n for n in csvMacroKeyList) and macroWasRegistered(dir, mk['macro name'], register):
unregisterMacro(dir, mk['macro name'], register)
centreon_hosts.deletemacro(dir, mk['macro name'])
for mk in centreon_hosts.getmacro(name)['result']:
if not any(mk['macro name'] == n for n in csvMacroKeyList) and macroWasRegistered(name, mk['macro name'], register):
unregisterMacro(name, mk['macro name'], register)
centreon_hosts.deletemacro(name, mk['macro name'])

#This ensures we get no duplicates in register in case we have to re-add an already registered
#register[dir]['templates'] = ','.join(list(set(register[dir]['templates'].split(','))))
Expand Down