Skip to content

Instantly share code, notes, and snippets.

@BercX
Created December 3, 2021 12:53
Show Gist options
  • Save BercX/d01e782ff9ddbb09d00b1b5dafe3a5e8 to your computer and use it in GitHub Desktop.
Save BercX/d01e782ff9ddbb09d00b1b5dafe3a5e8 to your computer and use it in GitHub Desktop.
CheckMK Scripts

Check MK Scripts

Add host to checkmk server:

Script automatically adds the computer to the hostlist on the checkmk server. Works on Windows 10, 7(with KB3191566 update).

  1. Create .ps1 file with following code.
  2. Change server and client(optional) variables.
  3. 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

Register checkmk update server:

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

  1. Create .bat file with following code:
  2. Change server(-s) and credentials(-U, -P).
  3. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment