Created
October 8, 2022 00:10
-
-
Save DerfOh/0de8f892490727d9e7cce8fa411dd7a2 to your computer and use it in GitHub Desktop.
installs windows prometheus exporter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$version = "0.20.0" | |
$AgentURL = "https://github.com/prometheus-community/windows_exporter/releases/download/v${version}/windows_exporter-${version}-amd64.exe" | |
$AbsoluteCurrPath = $(Get-Location).Path | |
$ExporterDirectoryToCreate = "C:\Program Files\windows_exporter\" | |
$AbsolutePathExe = "${ExporterDirectoryToCreate}\windows_exporter.exe" | |
$EnabledCollectors = "cpu,cs,logical_disk,memory,net,os,service,system,tcp,remote_fx,logon,process" | |
$ServiceName = "windows_exporter" | |
# create exporter directory | |
if (-not (Test-Path -LiteralPath $ExporterDirectoryToCreate)) { | |
try { | |
New-Item -Path $ExporterDirectoryToCreate -ItemType Directory -ErrorAction Stop | Out-Null #-Force | |
} catch { | |
Write-Error -Message "Unable to create directory '$ExporterDirectoryToCreate'. Error was: $_" -ErrorAction Stop | |
} | |
"Successfully created directory '$ExporterDirectoryToCreate'." | |
} else { | |
"Directory already exists" | |
} | |
# download specify msi to temp directory | |
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 | |
Invoke-WebRequest $AgentURL -OutFile $AbsolutePathExe | |
# remove the previous MSI install if it exists | |
Uninstall-Package -Name windows_exporter | |
# open firewall rules | |
New-NetFirewallRule -Name "windows_exporter" -DisplayName "windows_exporter port 9182" -Direction inbound -Profile Any -Action Allow -LocalPort 9182 -Protocol TCP | |
$rule = "windows_exporter port 9182" | |
Get-NetFirewallRule -DisplayName $rule | ft -Property Name, DisplayName, @{Name='Protocol';Expression={($PSItem | Get-NetFirewallPortFilter).Protocol}}, @{Name='LocalPort';Expression={($PSItem | Get-NetFirewallPortFilter).LocalPort}}, @{Name='RemotePort';Expression={($PSItem | Get-NetFirewallPortFilter).RemotePort}}, @{Name='RemoteAddress';Expression={($PSItem | Get-NetFirewallAddressFilter).RemoteAddress}}, Enabled, Profile, Direction, Action | |
# Check Status of Service | |
$Service = Get-Service -Name "$ServiceName" | |
if($Service.Status -eq "running"){ | |
Write-Host "$ServiceName is running, stopping..." | |
Stop-Service -Name $ServiceName | |
Write-Host "Removing $ServiceName" | |
sc.exe delete $ServiceName | |
} else { | |
Write-Host "$ServiceName status is: $Service.Status" | |
Write-Host "Removing $ServiceName" | |
sc.exe delete $ServiceName | |
} | |
# enable the service | |
New-Service -name "$ServiceName" ` | |
-BinaryPathName "${AbsolutePathExe} --collectors.enabled `"${EnabledCollectors}`" --collector.service.services-where `"Name LIKE 'windows_%' OR Name LIKE 'WMI%'`"" ` | |
-StartupType Automatic ` | |
-DisplayName "$ServiceName" | |
# start the service | |
Start-Service -Name $ServiceName | |
# Check Status of Service | |
if($Service.Status -eq "running"){ | |
Write-Host "$ServiceName is running" | |
} else { | |
Write-Host "$ServiceName status is: $Service.Status" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment