I am looking for a way to set downtime on a list of hosts via ansible.
I did some web search and only found how to set downtime on a single host using an url like
{Checkmk_base}/check_mk/view.php?_do_confirm=yes&transit=-1&view_name=hoststatus&site=&output_format=JSON&_username={uname}&_secret={passwd}&host={hostn}&_down_from_now=From+now+for&_down_minutes=10&_down_comment=test
I feel it’s a bit much calling this URL for every single host in the list. Is there a way to form an url that handles multiple hosts at once like with the host_regex parameter instead of just “host”? Or is there a way to do this with a single REST API call?
Because if you have 50 Servers/Services and the Script has to make 50 individual API calls and each call needs 0.5s, then that is very slow and inefficient
it would be much easier and faster if you could pass a list of hosts/services in one API call.
I was afraid that’s the only option. Since the host list is dynamically created, that’s at least inconvenient.
You already named the reason why I prefer a single API call, but if there’s really no other way I think making one call per host is what I’d prefer here.
So far we had no group that could not be predefined. But if the groups are only known at runtime and cannot be meaningfully predefined, such as “all Linux servers at location x” or "all servers with tag/label me2, then you probably have no other choice than to call the API for each host individually (You could parallelise the API calls to a certain extent).
Please keep in mind that the Web API no longer exists as of 2.2. With the host_by_query option in the REST-API it should be possible to pass an adhoc group of hosts:
A query expression of the Livestatus ‘hosts’ table in nested dictionary form. If you want to use multiple expressions, nest them with the AND/OR operators.
Well, if we are already using the REST API, why don’t we dynamically create the hostgroup from the list of hosts as well? The API has the necessary endpoints.
hello @LaSoe i have create the below powershell script and it works perfectly for 1 host: if i manually specify it (dxbesx1)
but if i uncomment the line 10 to get the content of a txt file each ost on seperate line, i got error that the bad request status 400 host_name not a valid string
below is the script:
#Put the Hosts in checkmk Downtime
#Disable SSL Verification
[System.Net.ServicePointmanager]::ServerCertificateValidationCallback = {$true}
$username = "api-user"
$secret = "apisecret"
####Get the list of hosts entered in the DTESXList.txt
#$hostlist = get-content -Path "C:\nca\PowerCLI PS\BOS Patches\DTESXList.txt"
#write-host $hostlist
$hostname = "dxbesx1"
#### Time Zone Variables
$Delta = 60
$StartTime = Get-Date
$EndTime = $StartTime.AddMinutes($Delta)
$StartTimeREST = [System.String]$(Get-Date $StartTime -Format 'yyyy-MM-ddTHH:mm:ssK')
$EndTimeREST = [System.String]$(Get-Date $EndTime -Format 'yyyy-MM-ddTHH:mm:ssK')
####Header Inputs
$Header = @{
"Authorization" = "Bearer $username $secret"
}
foreach ($hostname in $hostlist) {
###### Hashtable
##### the Z at the end is for UTC Time Zone
$Data = @{
"start_time" = $startTimerest
"end_time" = $endTimerest
"recur" = "fixed"
"comment" = "Security updates"
"downtime_type" = "host"
"host_name" = $hostname
} | ConvertTo-Json
$Parameters = @{
Method = "POST"
Uri = "https://checkmk.xx.com/ncamaster/check_mk/api/1.0/domain-types/downtime/collections/host"
Headers = $Header
Body = $data
ContentType = "application/json"
}
Invoke-RestMethod @Parameters
}
===================================
error:
Invoke-RestMethod @Parameters
}
Invoke-RestMethod : {"title": "Bad Request", "status": 400, "detail": "These fields have problems: host_name", "fields": {"host_name": ["Not a valid string."]}}
At line:51 char:1
Since not all of us are so experienced API users as you, a small code example would be very helpful to better understand how something like this could be accomplished.
hi @LaSoe and @elias.voelker thanks for the quick reply, i am not an expert on API hehe learning as you all do, i was able to fix it in a way as become as below:
#Put the Hosts in checkmk Downtime
#Disable SSL Verification
[System.Net.ServicePointmanager]::ServerCertificateValidationCallback = {$true}
$username = "api-user"
$secret = "key"
####Get the list of hosts entered in the DTESXList.txt
$hostlist = get-content -Path "C:\nca\PowerCLI PS\BOS Patches\DTESXList.txt"
#write-host $hostlist
#$hostname = "dxbesx1"
#### Time Zone Variables
$Delta = 60
$StartTime = Get-Date
$EndTime = $StartTime.AddMinutes($Delta)
$StartTimeREST = [System.String]$(Get-Date $StartTime -Format 'yyyy-MM-ddTHH:mm:ssK')
$EndTimeREST = [System.String]$(Get-Date $EndTime -Format 'yyyy-MM-ddTHH:mm:ssK')
####Header Inputs
$Header = @{
"Authorization" = "Bearer $username $secret"
}
foreach ($hostname in $hostlist) {
###### Hashtable
##### the Z at the end is for UTC Time Zone
$Data = @{
"start_time" = $startTimerest
"end_time" = $endTimerest
"recur" = "fixed"
"comment" = "Security updates"
"downtime_type" = "host"
"host_name" = "$hostname" (this was the issue i had to put double quotes on the variable itself)
} | ConvertTo-Json
$Parameters = @{
Method = "POST"
Uri = "https://checkmk.xx.com/ncamaster/check_mk/api/1.0/domain-types/downtime/collections/host"
Headers = $Header
Body = $data
ContentType = "application/json"
}
Invoke-RestMethod @Parameters
}