CMK version:2.0.0p26
OS version:Linux RHEL8
Hello all
Has somebody a working Python script which is detecting the ETag and using it to update a host with some attributes?
Please let me know.
Thanks
Best regards
JP
CMK version:2.0.0p26
OS version:Linux RHEL8
Hello all
Has somebody a working Python script which is detecting the ETag and using it to update a host with some attributes?
Please let me know.
Thanks
Best regards
JP
Hi please try the following to get the etag:
HOST_NAME = "yourhost"
SITE_NAME = "yoursite"
API_URL = f"https://{HOST_NAME}/{SITE_NAME}/check_mk/api/1.0"
USERNAME = "youruser"
PASSWORD = "yoursecret"
HOSTNAME = "hostnameToQuery"
query_string = urllib.parse.urlencode({
})
request = urllib.request.Request(
f"{API_URL}/objects/host_config/{HOSTNAME}?{query_string}",
method="GET",
headers={
"Authorization": f"Bearer {USERNAME} {PASSWORD}",
"Accept": "application/json",
},
)
response = urllib.request.urlopen(request)
if response.status == 200:
etag = str(response.headers["ETag"])
elif response.status == 204:
print("Done")
else:
raise RuntimeError(response.read())
You can then use the etag variable for the “If-Match” Header.
Hello
Thanks, the ETag I got now via:
print(“================================”)
for (key, value) in resp.headers.items():
if key == “ETag”:
ETAG_X = value
ETAG = ETAG_X.replace(‘"’, ‘’)
print(‘ETag:’ + ETAG)
print(“================================”)
But has somebody a working Host-Update Python script?
The provided example by the 2.0.0p26 documentation does not work in my env.
I used:
json={
‘folder’:‘/ww-intern/test’,
‘host_name’: ‘xxxxxx2.ww-intern.de’,
‘attributes’: {
‘ipaddress’: ‘127.0.0.1’
},
‘update_attributes’: {
‘ipaddress’: ‘127.0.0.2’,
‘alias’: ‘Test update’,
},
#‘remove_attributes’: [‘tag_foobar’]
},
But no changes are done.
Thanks
Philipp
Hi Philipp,
this is my full example including the update:
#!/usr/bin/env python3
import pprint
import json
import urllib.request
HOST_NAME = "yourhost"
SITE_NAME = "yoursite"
API_URL = f"https://{HOST_NAME}/{SITE_NAME}/check_mk/api/1.0"
USERNAME = "youruser"
PASSWORD = "yoursecret"
HOSTNAME = "hostnameToQuery"
query_string = urllib.parse.urlencode({
})
request = urllib.request.Request(
f"{API_URL}/objects/host_config/{HOSTNAME}?{query_string}",
method="GET",
headers={
"Authorization": f"Bearer {USERNAME} {PASSWORD}",
"Accept": "application/json",
},
)
response = urllib.request.urlopen(request)
if response.status == 200:
etag = str(response.headers["ETag"])
elif response.status == 204:
print("Done")
else:
raise RuntimeError(response.read())
request = urllib.request.Request(
f"{API_URL}/objects/host_config/{HOSTNAME}",
method="PUT",
headers={
"Authorization": f"Bearer {USERNAME} {PASSWORD}",
"Accept": "application/json",
"If-Match": etag, # (required) The value of the, to be modified, object's ETag header.
"Content-Type": 'application/json', # (required) A header specifying which type of content is in the request/response body.
},
data=json.dumps({
'update_attributes': {
'tag_service': 'Standard'
}
}).encode('utf-8'),
)
response = urllib.request.urlopen(request)
if response.status == 200:
pprint.pprint(json.loads(response.read()))
elif response.status == 204:
print("Done")
else:
raise RuntimeError(response.read())
This example works perfect in my environment.
Regards Michael
Hello Michael
Thanks for your knowledge.
I got this problem in my env.:
./api_host_update_test.py
Traceback (most recent call last):
File "./api_host_update_test.py", line 54, in <module>
response_2 = urllib.request.urlopen(request_2)
File "/omd/sites/WWIPU01/lib/python3.8/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/omd/sites/WWIPU01/lib/python3.8/urllib/request.py", line 531, in open
response = meth(req, response)
File "/omd/sites/WWIPU01/lib/python3.8/urllib/request.py", line 640, in http_response
response = self.parent.error(
File "/omd/sites/WWIPU01/lib/python3.8/urllib/request.py", line 563, in error
result = self._call_chain(*args)
File "/omd/sites/WWIPU01/lib/python3.8/urllib/request.py", line 502, in _call_chain
result = func(*args)
File "/omd/sites/WWIPU01/lib/python3.8/urllib/request.py", line 734, in http_error_302
new = self.redirect_request(req, fp, code, msg, headers, newurl)
File "/omd/sites/WWIPU01/lib/python3.8/urllib/request.py", line 672, in redirect_request
raise HTTPError(req.full_url, code, msg, headers, fp)
urllib.error.HTTPError: HTTP Error 302: Found
by using this code:
Do you’ve any solution?
I’m just beginning with Python sorry.
Thanks for your help!
Probably a wrong API_URL. Is your Site reachable via https? Otherwise please modify the API_URL variable to “http” instead of “https”. If this is not the problem please test the value of your API_URL variable in a webbrowser.
Regards Michael
Hello Michael
Yea! perfect, yes it was the API_URL with http instead of https which we only accept hand has implemented a redirect which caused maybe the error output we’ve got…
Thanks so much!
The weekend is saved
Have a nice weekend!
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.