-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckWazuhVersion.ps1
62 lines (53 loc) · 2.56 KB
/
CheckWazuhVersion.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#Checks if latest Wazuh Wazuh is installed
[System.UriBuilder]$LatestVersionURL = 'https://github.com/wazuh/wazuh/releases/latest'
$WazuhVersionFile = ${env:ProgramFiles(x86)} + '\' + 'ossec-agent' + '\' + 'VERSION'
#If more than X minor versions behind, report as critical
#Example if $MaxMinorDif = 4: Latest = 4.8.0, Installed = 4.6.0, reports as warning. If Latest = 4.8.0, Installed = 4.1.0, then it will report as critical
#Being behind by 1 or more major versions is automatically returns CRITICAL
#Being behind by 1 or more build numbers is automatically returns WARNING
$MaxMinorDif = 4
#-----------
if (!(Test-Path $WazuhVersionFile)) {
Write-Output 'UNKNOWN: Version file could not be found'
$LASTEXITCODE = 3
exit $LASTEXITCODE
}
#Forces TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#Find latest version from GitHub
$WebPage = New-Object -ComObject "HTMLFile"
$Content = (Invoke-WebRequest -Uri $LatestVersionURL.Uri -UseBasicParsing).Content.ToString()
$WebPage.write([ref]$Content)
[string]$LatestVersion = ($WebPage.body.innerText -split '\n') | Where-Object {($_ -cmatch 'Wazuh v') -and ($_ -cmatch 'Latest')} | Select-Object -First 1
[version]$LatestVersion = ($LatestVersion -split ' ')[1].TrimStart('v')
#Find currently installed version
[version]$InstalledVersion = (Get-Content $WazuhVersionFile).TrimStart('v')
#Determine difference in version numbers
$MajorDif = $LatestVersion.Major - $InstalledVersion.Major
$MinorDif = $LatestVersion.Minor - $InstalledVersion.Minor
#Report
if ($LatestVersion -eq $InstalledVersion) {
Write-Output 'OK: Wazuh is up to date'
Write-Output "Latest Version: $LatestVersion"
Write-Output "Current Version: $InstalledVersion"
$LASTEXITCODE = 0
} elseif (($MajorDif -le 0) -and ($MinorDif -gt 0) -and ($MinorDif -lt $MaxMinorDif)) {
Write-Output 'WARNING: Wazuh is out of date'
Write-Output "Latest Version: $LatestVersion"
Write-Output "Current Version: $InstalledVersion"
$LASTEXITCODE = 1
} elseif (($MajorDif -gt 0) -or ($MinorDif -ge $MaxMinorDif)) {
Write-Output 'CRITICAL: Wazuh is VERY out of date'
Write-Output "Latest Version: $LatestVersion"
Write-Output "Current Version: $InstalledVersion"
$LASTEXITCODE = 2
} elseif ($LatestVersion -gt $InstalledVersion) {
Write-Output 'WARNING: Wazuh is out of date'
Write-Output "Latest Version: $LatestVersion"
Write-Output "Current Version: $InstalledVersion"
$LASTEXITCODE = 1
} else {
Write-Output 'UNKNOWN: Issue with determining versions'
$LASTEXITCODE = 3
}
exit $LASTEXITCODE