Hi,
I don’t know Powershell. But I think it’s possible to retrieve information via URLs. You can use the posted URLs in your script. Instead of json, it’s also possible to get python or csv data.
This is very promising - thanks.
I have two items though.
I had to add -UseBasicParsing in the line below due to a message about Internet Explorer. I’m not sure if this will affect anything, or more specifically #2 below. I have another script where I interrogate webpages where I had to do this with no other issues.
$result = Invoke-WebRequest -Uri $URI -Method Post -Body $postParams -UseBasicParsing
I’m getting a web response of:
StatusCode : 200
StatusDescription : OK
Content : ERROR: Invalid automation secret for user myusername
I 100% know that the password I’m using is correct.
I assume $mycheckmkuser and $mycheckmsecret are to be set with the username and password I use to log into CheckMK.
#######################
# variable definition #
#######################
# declare return variable for the function as hashtable
$return = @{}
# decide wether to use https or not
if ($https)
{
$URI = "https://"
}
else
{
$URI = "http://"
}
$URI += "$checkmkserver/$checkmkinstance/check_mk/view.py"
# build the web request for displaying the host with its status
$postParams = @{
_username=$checkmkuser
_secret=$checkmksecret
_transid='-1'
host_regex=$hostname # the hostname to search for
view_name='allhosts'
output_format='json'
}
# execute the web request
try
{
if ($usebasicparsing)
{
$result = Invoke-WebRequest -Uri $URI -Method Post -Body $postParams -UseBasicParsing #DPD 11-24-2020 added -UseBasicParsing
}
else
{
$result = Invoke-WebRequest -Uri $URI -Method Post -Body $postParams
}
}
catch
{
$return.status = 1
$return.msg = "error while connecting the check_mk api.`r`n$($error[0])"
# return the error
Return $return
}
# check if a respones was getting back
if (!$result)
{
Write-Host "error while connecting the check_mk api."
Write-Host
$return.status = 1
$return.msg = "error while connecting the check_mk api."
# return the error
Return $return
}
# check if the web request was successful
if ($result.StatusCode -ne 200 -or $result.Content.StartsWith('ERROR:'))
{
Write-Host "error in the response from the check_mk api."
Write-Host
Write-Host $result
$return.status = 1
$return.msg = "error in the response from the check_mk api."
# return the error
Return $return
}
# convert the string content of the response into a powershell json object
$json = $result.content | convertfrom-json
# build array of property names
$field = @('')*($json[0].Count)
for ($i=0; $i -lt $json[0].Count; $i++ )
{
$field[$i] = $json[0][$i]
}
# loop thru all hosts in results to build array of host objects
$hosts = @()
for ($j = 1; $j -lt $json.Count; $j++)
{
$obj = New-Object PSObject
for ($i=0; $i -lt $field.Count; $i++)
{
Add-Member -InputObject $obj -MemberType NoteProperty -Name $field[$i] -Value $json[$j][$i]
}
$hosts += @($obj)
}
$return.status = 0
$return.msg = "found no errors with host $hostname"
$return.host = $hosts
# return the error or the success message
Return $return
}
###########End of function declarations#######################
############
Example
############
connection variables
$mycheckmkserver = “insert checkmk servername”
$mycheckmkinstance = “insert checkmk instance”
$mycheckmkuser = “insert checkmk username using automation secret”
$mycheckmksecret = “insert checkmk automation secret of the username”
host to be checked, set downtime, patched, removed downtime and checked again
New issue, in checkmkdowntime at:
$URI += “$checkmkserver/$checkmkinstance/check_mk/webapi.py” #build the web request for checking if the host exists in check_mk
$Body = @{
action=“get_host”
_username=$checkmkuser
_secret=$checkmksecret
request_format=“json”
output_format=“json”
hostname=$hostname
}