Script automatically adds the computer to the hostlist on the checkmk server. Works on Windows 10, 7(with KB3191566 update).
- Create
.ps1
file with following code. - Change server and client(optional) variables.
- Run
Script:
# Server
$server = "https://monitoring.example.com/cmk"
$username = "automation"
$secret = "your_secret"
# Client
$hostname = hostname
$folder = "Workstations"
$site = "cmk"
$tag_agent = "cmk-agent"
$create_folders = 1
###
### Don't change the code below
###
# Ignore self-signed certificates
Add-Type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@ -ea SilentlyContinue -wa SilentlyContinue
<#
.Synopsis
Send action to check_mk server
#>
function Send-Action
{
Param
(
[Parameter(Mandatory=$true, Position=0)]
[string] $action,
[Parameter(Mandatory=$true, Position=1)]
[string] $json
)
$uri = "$server/check_mk/webapi.py?action=$action&_username=$username&_secret=$secret"
$body = "request=$json"
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
Invoke-RestMethod -Uri $uri -Method Post -Body $body -Verbose:$false
}
<#
.Synopsis
https://docs.checkmk.com/latest/en/web_api_references.html#heading_add_host
Function adds host to check_mk
#>
function Add-Host {
$action = "add_host"
$json = @"
{
"hostname":"$hostname",
"folder":"$folder",
"attributes":{
"site":"$site",
"tag_agent":"$tag_agent"
},
"create_folders":"$create_folders"
}
"@
Send-Action $action $json
}
<#
.Synopsis
https://docs.checkmk.com/latest/en/web_api_references.html#heading_activate_changes
All changes made to the Checkmk-Server can be activated with the activate_changes command.
#>
function Activate-Changes {
$action = "activate_changes"
$json = @"
{
"sites":["$site"],
"allow_foreign_changes":"0"
}
"@
Send-Action $action $json
}
Add-Host
Activate-Changes
Registers a host on the checkmk server using the computer name. More info on https://docs.checkmk.com/latest/en/agent_deployment.html#heading_register_agent
- Create
.bat
file with following code: - Change server(-s) and credentials(-U, -P).
- Run script as admin
Script:
"C:\Program Files (x86)\checkmk\service\check_mk_agent.exe" updater register -s monitoring.example.com -i cmk -H %COMPUTERNAME% -p https -U automation -P your_secret -v
"C:\Program Files (x86)\checkmk\service\check_mk_agent.exe" updater -v
pause