I am getting a json object back from the “Show the monitored services of a host” call which looks like this but for every service in a host:
{"links": [{"domainType": "link", "rel": "self", "href": "<url>/check_mk/api/1.0/objects/service/checkmk-dev-OMD%2520bvd_lan%2520status", "method": "GET", "type": "application/json"}], "domainType": "service", "id": "checkmk-dev-OMD bvd_lan status", "title": "Service OMD bvd_lan status", "members": {}, "extensions": {"description": "OMD bvd_lan status", "host_name": "checkmk-dev", "state_type": 1, "state": 0, "last_check": 1691735710}}
I get the “state” value which you can see here is 0. And most of the time it is 0 for all services.
Here is maybe a bit of a cleaner look:
Here is also some relevant skript parts:
def execute_a_service_discovery(url: str, hostname: str, mode: str):
option = "/domain-types/service_discovery_run/actions/start/invoke"
url += option
payload = {
'host_name': hostname,
'mode': mode
}
r = requests.post(url, headers=headers, json=payload)
return r
def show_the_monitored_services(url: str, hostname: str, service_description: str):
option = "/objects/host/"+hostname+"/actions/show_service/invoke?service_description="+service_description
url += option
r = requests.get(url, headers=headers)
return r
def update_the_phase_service(url: str, hostname: str, check_type: str, service_item: str, target_phase: str):
option = "/objects/host/"+hostname+"/actions/update_discovery_phase/invoke"
url += option
payload = {
'check_type': check_type,
'service_item': service_item,
'target_phase': target_phase
}
print("UPDATE: ", json.dumps(payload), "FOR HOST: ", hostname)
r = requests.put(url, headers=headers, json=(payload))
return r
def service_monitor(hostname: str):
service_to_body = {}
#execute_a_service_discovery(url, hostname, "refresh")
tmp = execute_a_service_discovery(url, hostname, "fix_all").text
activate_pending_changes(url)
json_response_extensions = find_json_values_by_key('extensions',tmp)
for index,x in enumerate(json_response_extensions):
if index == len(json_response_extensions)-1:
continue
# TODO: Expectation that all hosts have an agent installed on them for the following line to work
service_to_body[x['service_name']] = x
#print("RESPOND ACTIVATE CHANGES 1: ", activate_pending_changes(url))
return service_to_body
def get_all_hosts(url: str, effective_attributes='false'):
option = "/domain-types/host_config/collections/all?effective_attributes="+effective_attributes
url += option
r = requests.get(url, headers=headers)
return r
def monitor_initial_cleanup():
hosts = find_json_values_by_key('id', get_all_hosts(url).text)
print("HOSTS: ", hosts)
for hostname in hosts:
services = service_monitor(hostname)
for service_name, body in services.items():
monitored_response = show_the_monitored_services(url, hostname, service_name).text
#print("MONITORED RESPONSE: ", monitored_response)
#activate_pending_changes(url)
#sleep(2)
state = find_json_values_by_key('state', monitored_response)
if len(state) != 0:
print("HOST: ", hostname, " SERVICE: ", service_name, " STATUS: ", state[0])
if len(state) != 0 and state[0] != 0:
print("RESPONSE: ", update_the_phase_service(url, hostname, body['check_plugin_name'], body['service_item'], 'undecided'))
if __name__ == '__main__':
monitor_initial_cleanup()