Windows Agent : MySQL service + Powershell Installation

Hi there,

I have written a PowerShell to install the agent in Base64 format and then create and register the host against the master CheckMK site. There is an optional at the end to remove any legacy agents if they are still used.

It’s only rough as it is still a work in progress but ultimately it’s functional. You would just need to enter your personal CheckMK details in the top variables under the “CheckMK Variables” section and then convert your agent to Base64 format.

This is based off of the CheckMK Enterprise version.

Clear-Host

#region CheckMK Variables
#Force the hostname to be lowercase - this is important as check_mk is case sensitive, all systems in check_mk should be creared as lowercase!
$hostName = "$env:COMPUTERNAME".ToLower()
$Url = "<URL Here>"
$Site = "<Site Here>"
$SecretKey = "<Secret Here>"
#endregion

#region Server Variables
#Enable TLSv1.2 Connections To The Internet
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#Set Progress Preference
$ProgressPreference = 'SilentlyContinue'

$cmk = "C:\ProgramData\checkmk\agent\plugins\cmk-update-agent.exe"
#endregion

#region Check If CheckMK Agent Exists And Install If Appropriate
IF (Test-Path $cmk){
Write-Host " As we have found existing CheckMK files we will not reinstall the Bakery version" -ForegroundColor Green
Start-Sleep 2
}
Else
{
#Remove Proxy Settings If They Exist
$RegKey ="HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings"
Set-ItemProperty -path $RegKey -name ProxyEnable -value 0

Write-Host ""Removing $regkey..."" -ForegroundColor Green
Start-Sleep 5

#Install The Agent Silently
$Base64 = "TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA..........+DD"
$Content = [System.Convert]::FromBase64String($Base64)
Set-Content -Path $env:temp\CheckMK.msi -Value $Content -Encoding Byte

Start-Process -FilePath msiexec -ArgumentList /norestart, /i, $env:temp\CheckMK.msi, /qb -Wait

Start-Sleep -Seconds 5

Remove-Item $env:temp\CheckMK.msi

Start-Sleep -Seconds 1

#Register the agent with the master CheckMK
 & $cmk register -s $Url -v -i $Site -p https -H $hostName -U register -S $SecretKey

Start-Sleep -Seconds 2

#Force a reinstall from the server to ensure it is working and up to date
 & $cmk -v -f

Start-Sleep -Seconds 2
}
#endregion

#region Add Host to CheckMK Monitoring 
#Find FQDN of running host
$myFQDN=(Get-WmiObject win32_computersystem).DNSHostName+"."+(Get-WmiObject win32_computersystem).Domain

#Resolve hosts IP via DNS
$myIP = Test-Connection -ComputerName $myFQDN -Count 1  | Select-Object -ExpandProperty IPV4Address
$myIPv4 = $myIP.IPAddressToString
IF ($myIPv4 -like "10*" -OR $myIPv4 -like "172*")
{
$myIP = $myIPv4
Start-Sleep 5
}

Else 
{
$myIP=Resolve-DnsName $myFQDN -DnsOnly |Select-Object -ExpandProperty IPAddress
Write-Host " The IP address of this server is $myIP" -ForegroundColor Green
Start-Sleep 5
}

#region CheckMK Description (Alias)
#Create Description From AD For Server
$Description = "(&(objectCategory=computer)(objectClass=computer)(cn=$hostName))"
$CheckMKDescription = ([adsisearcher]$Description).FindOne().Properties.description

$inputData = @"
request=
{"hostname":"$hostname","folder":"Discovered","attributes":{"alias":"$CheckMKDescription","ipaddress":"$myIP"}}
"@ 

$ADD = "https://$url/$site/check_mk/webapi.py?action=add_host&_username=register&_secret=$SecretKey" 

#Connect to check_mk and register the server if the hostname does not already exist in Discovered Folder
Invoke-RestMethod $ADD -Method Post -Body $inputData  -TimeoutSec 3 -ErrorAction:Stop

Start-Sleep 3

$Get_Description = "https://$url/$site/check_mk/webapi.py?action=get_host&_username=register&_secret=$SecretKey&output_format=python&request_format=python" 

$inputData = @"
request=
{"hostname":"$hostname"}
"@  

$FoundDescription = Invoke-RestMethod $Get_Description -Method Post -Body $inputData  -TimeoutSec 3 -ErrorAction:Stop
#$FoundDescription
$MatchDescription = $FoundDescription -match "(?<=alias': u')($CheckMKDescription)(?=',)"

Write-Host ""Finding Description in AD... Please Wait..."" -ForegroundColor Green
Start-Sleep 1
Write-Host ""Found $FoundDescription for $hostname"" -ForegroundColor Green
Start-Sleep 1
Write-Host " Checking to see if the description matches..." -ForegroundColor Green
Start-Sleep 1
Write-Host ""$MatchDescription"" -ForegroundColor Green
Start-Sleep 1

if($MatchDescription -eq $true){
Write-Host " The correct description is already in Check_MK so we will not update at this time" -ForegroundColor Red
Start-Sleep 2
}
Else
{
$Add_Description = "https://$url/$site/check_mk/webapi.py?action=edit_host&_username=register&_secret=SecretKey"

$inputData = @"
request=
{"hostname":"$hostname","attributes":{"alias":"$CheckMKDescription","ipaddress":"$myIP"}}
"@  

Invoke-RestMethod $Add_Description -Method Post -Body $inputData  -TimeoutSec 3 -ErrorAction:Stop

Write-Host " Couldn't find a matching description so we will update to the latest in AD" -ForegroundColor Red
Write-Host ""Updated CheckMK description to $CheckMKDescription"" -ForegroundColor Red
Start-Sleep 3
}
#endregion

#region CheckMK IP Address (IPv4)
$Get_IP = "https://$url/$site/check_mk/webapi.py?action=get_host&_username=register&_secret=SecretKey&output_format=python&request_format=python" 

$inputData = @"
request=
{"hostname":"$hostname"}
"@  

$FoundIP = Invoke-RestMethod $Get_IP -Method Post -Body $inputData  -TimeoutSec 3 -ErrorAction:Stop
#$FoundDescription
$MatchIP = $FoundIP -match "(?<=ipaddress': u')($myIP)(?='},)"

Write-Host ""Finding IP... Please Wait..."" -ForegroundColor Green
Start-Sleep 2
Write-Host ""Found $FoundIP for $hostname"" -ForegroundColor Green
Start-Sleep 2
Write-Host " Checking to see if the IP matches that of the server..." -ForegroundColor Green
Start-Sleep 2
Write-Host ""$MatchIP"" -ForegroundColor Green
Start-Sleep 2

if($MatchIP -eq $true){

Write-Host " The correct IPv4 is already present in CheckMK so we will not update" -ForegroundColor Green
Start-Sleep 2
}
Else
{
$Add_IP = "https://$url/$site/check_mk/webapi.py?action=edit_host&_username=register&_secret=SecretKey"

$inputData = @"
request=
{"hostname":"$hostname","attributes":{"ipaddress":"$myIP"}}
"@  

Invoke-RestMethod $Add_IP -Method Post -Body $inputData  -TimeoutSec 3 -ErrorAction:Stop

Write-Host " Couldn't find a matching IP so we will update it for you" -ForegroundColor Red
Write-Host ""Updated Check_MK description to $myIP"" -ForegroundColor Red
Start-Sleep 2
}
#endregion
#endregion

#region Restart The CheckMK Service
#Check if the Check_MK service exists
$CheckMKService = Get-WmiObject -Class Win32_Service -Filter "Name='CheckMkService'" -ErrorAction SilentlyContinue

IF($CheckMKService.State -eq 'Running')
{
net stop CheckMkService
Start-Sleep -Seconds 5
}

IF($CheckMKService.State -ne 'Running')
{
net start CheckMkService
}
#endregion

#region Uninstall Check_MK Version 1.5
$app = Get-WmiObject -Class Win32_Product | Where-Object { 
    $_.Name -match "Check_MK Agent" 
}

IF ($null -eq $app)
{
Write-Host "No Legacy CheckMK Agent Found"
}
Else
{
Write-Host "Removing Legacy CheckMK Agent"
$app.Uninstall()
}

#endregion
1 Like