Windows Agent : MySQL service + Powershell Installation

Hello Guys,

I’ve recently installed the CheckMK agent on a Windows server, but the MySQL service is not monitoring correctly for some reason. I’ve done the following steps so far:

  • Installed the Windows .msi agent on the server

  • Copied mk_mysql.vbs file from C:\Program Files (x86)\checkmk\service\plugins to C:\ProgramData\checkmk\agent\plugins folder.

  • Created mysql.ini file with the below content at C:\ProgramData\checkmk\agent\config folder.

    [client]
    user=
    password=

The username and password entered correctly, but no MySQL service detecting on discovery and also no mention of “<<>>” while checking CheckMK check output.

Also, I would like to set up a Powershell script to automate the Windows agent installation and its setup so do anyone have a basic Powershell script to begin this?

Please advise on the above two, that would be much appreciated, :slight_smile:

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

Thanks @MattBailey , I’ll look at this script here. I’m not actually familiar with Powershell, so can you assist me to set up a new one with my requirements? Its not this much complex, rather too simple and straight forward one. Let me know if you could help, so I could share my Skype or email via DM

Also, you have any idea of the first issue I’ve mentioned here. The MySQL service one, as this isn’t auto discovering even after placing its VBS file in plugins folder and create its INI file.

Any help would be much appreciated! Thanks again :slight_smile:

Any update on that Windows MySQL agent issue mentioned?

Please advise with that @andreas-doehler (you were always helpful with CheckMK) when you get a chance! :slight_smile:

MySQL on Windows is a little bit tricky :smiley:
First step is that the script must find your running instance and the corresponding port.
What i would do first is running the mk_mysql.vbs manually and before you do this export the environment variable “MK_CONFDIR” with the folder of your ini file.
Now you can test if this works and insert some debug messages in the script to have a look where there is a problem.

@andreas-doehler I tried to run with “cscript mk_mysql.vbs” or “WSCript mk_myql.vbs” but nothing shows up in output. I’m not familiar with the Windows side of things, so can you please advise how we could run VBS script manually from command line in Windows and or how to export environment variable in that ini. I’m completely lost here, so any help would be appreciated.

The server has the MariaDB running on 3306 port locally there.

Thanks

wscript will not work as it opens it’s own session and you see nothing. Use cscript to run the vbs.
For the environment variables you use “set” on Windows.

Can you share the exact step by step, as I’m completely lost here. Elaborated answer would be helpful for me here.

Thanks @andreas-doehler as always :slight_smile:

All depends on your system. As i don’t know your setup i cannot help there.
But you should have an Windows admin available who knows your systems and can help.

There is no Windows admin now. This is standard Windows server 2016 OS installed with Plesk control panel. The service MariaDB is running fine on 3306 and the sites are fine.

@andreas-doehler :slight_smile:

Hmmm, I wonder why you use MySQL/MariaDB on Windows then, and not on a platform that you are more comfortable using and that is more commonly used for this application …

Please keep in mind that this is a community forum, not a paid support channel. Especially not if the topic is primarily about Windows administration tasks.

I’m not a Windows admin myself, but I guess that using the hints Andreas gave you already (“cscript”, “set”, “MK_CONFDIR”) and some Google, you should be able to run the mk_mysql.vbs script and see if it produces the expected output or perhaps some helpful error message. Perhaps there is some MySQL error.log (or messages visible in Event Viewer) that show if the login succeeds.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.