Hi Community,
i want to create Host Attributes with the RestAPI.
I did not found a function for that. Is there any function to solve that?
Thank you and best regards ![]()
Hi Community,
i want to create Host Attributes with the RestAPI.
I did not found a function for that. Is there any function to solve that?
Thank you and best regards ![]()
No experience, but local Rest API docs: https://domain/site/check_mk/openapi/#operation/cmk.gui.plugins.openapi.endpoints.host_config.update_host suggests it’s possible.
@Yggy No experience, but local Rest API docs:
https://domain/site/check_mk/openapi/#operation/cmk.gui.plugins.openapi.endpoints.host_config.update_hostsuggests it’s possible.
This is just the API Reference how to Update a Host with an Custom Attribute. But the step before ist creating this Custom Attributes as Simple Text in CheckMK:
I did not find a way how to do that with Rest-API
In 2.3 there is no endpoint for the custom host attributes inside the API.
As far as i know ![]()
oh no! : @andreas-doehler smiley:
Any dirty workaround with file editing or similar
?
I read the documentation as:
attributes is for creating Custom Attribute (and deleting those not defined), so your ‘step before’update_attributes for updating existing (and not touching those not defined)And apparently it can only be added on an existing host, not created on the spot when creating a new host.
No it’s for overwriting all current attributes.
This means you can set/change only some attributes.
Is there a file somewhere i can edit manually to add my attributes?
I think yes - if you create one manually, you should find it inside the wato folder.
An easy solution might be this one:
def get_custom_hostattributes():
from cmk.gui.watolib.custom_attributes import load_custom_attrs_from_mk_file
host_attributes = load_custom_attrs_from_mk_file(lock=False)["host"]
return host_attributes
def get_missing_custom_hostattributes(existing, new):
missing = [x for x in new if x not in existing]
return missing
def new_list_custom_hostattributes(missing, existing):
import json
new_list = missing + existing
####Remove Duplicate Entries and Replace with new ones if duplicate is available###
unique_elements = []
cleaned_data = []
keys = []
for i, j in enumerate(new_list):
if new_list[i]["name"] not in unique_elements:
unique_elements.append(new_list[i]["name"])
keys.append(i)
for key in keys:
cleaned_data.append(new_list[key])
return cleaned_data
def save_custom_hostattributes(hostattr):
from cmk.gui.watolib.custom_attributes import save_custom_attrs_to_mk_file
saved_hostattributes = save_custom_attrs_to_mk_file({"host": hostattr})
return saved_hostattributes
#####Code####
new_hostattributes=[
{
"add_custom_macro": True,
"help": "",
"name": "Field3",
"show_in_table": True,
"title": "Field3",
"topic": "custom_attributes",
"type": "TextAscii"
},
{
"add_custom_macro": True,
"help": "",
"name": "Field2",
"show_in_table": True,
"title": "Field2",
"topic": "custom_attributes",
"type": "TextAscii"
},
{
"add_custom_macro": True,
"help": "",
"name": "Field1",
"show_in_table": True,
"title": "Field1",
"topic": "custom_attributes",
"type": "TextAscii"
}
]
existing_hostattributes = get_custom_hostattributes()
print (f"Existing Custom Host Attributes: {existing_hostattributes}")
missing_hostattributes = get_missing_custom_hostattributes(existing_hostattributes,new_hostattributes)
print (f"Missing Custom Host Attributes: {missing_hostattributes}")
new_list_hostattributes = new_list_custom_hostattributes(missing_hostattributes,existing_hostattributes)
print (f"New List with Custom Host Attributes: {new_list_hostattributes}")
save_hostattributes = save_custom_hostattributes(new_list_hostattributes)
print(save_hostattributes)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed. Contact an admin if you think this should be re-opened.