Sometimes if you must reinventory a huge amount of devices in Check_MK 2.x bulk inventory is not very responsive and could lead to hung up the system. With this little helper you can provide a text list of hostnames and he do an “tabula rasa” for each host one by one.
Host list hosts.txt#
hostA
hostB
hostC
Python script to Check_MK 2.x API#
#!/usr/bin/env python
import requests
import pprint
import sys
import urllib3
urllib3.disable_warnings()
USERNAME = "username"
PASSWORD = "password"
HOST_NAME = "server"
SITE_NAME = "site"
API_URL = f"https://{HOST_NAME}/{SITE_NAME}/check_mk/api/1.0"
########################################################################################################################
def doit(host):
HEADERS = {
"Authorization": f"Bearer {USERNAME} {PASSWORD}",
"Accept": "application/json",
"Content-Type": "application/json"
}
session = requests.session()
session.headers['Authorization'] = f"Bearer {USERNAME} {PASSWORD}"
session.headers['Accept'] = 'application/json'
resp = session.post(
f"{API_URL}/domain-types/service_discovery_run/actions/start/invoke", verify=False, headers=HEADERS,
json={"host_name": host, "mode": "fix_all"}
)
if resp.status_code == 200:
#pprint.pprint(resp.json())
print(f"##### {host} DONE")
else:
raise RuntimeError(pprint.pformat(resp.json()))
if __name__ == '__main__':
with open(sys.argv[1], "r") as f:
for line in f.readlines():
host = line.strip()
try:
doit(host)
except:
print("Error")
Start with#
python tabularasa.py hosts.txt
```