Pushgateway windows

From UVOO Tech Wiki
Revision as of 03:10, 12 January 2024 by Busk (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Windows

Windows push

$ErrorActionPreference = "Stop"
$hname = (hostname)
$user = "myuser"
$pass = "mypass"
$secpasswd = ConvertTo-SecureString $pass -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($user, $secpasswd)
$r=(invoke-webrequest http://localhost:9182/metrics)
$metrics = ($r.content.tostring())
Invoke-WebRequest -uri https://pushgateway.example.com/metrics/job/node-windows/instance/$hname -cred $cred -Body $metrics -Method Post

Run as a service

Code

vim C:\app\prompush.ps1

# $ErrorActionPreference = "Stop"
$interval_secs = 30
$job_name = "windows-node"
$hname = (hostname)
$user = "myuser"
$pass = "changeme"
$secpasswd = ConvertTo-SecureString $pass -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($user, $secpasswd)

while($true) {

  $r=(invoke-webrequest http://localhost:9182/metrics)
  $metrics = ($r.content.tostring())
  Invoke-WebRequest -uri https://pushgateway.example.com/metrics/job/$job_name/instance/$hname -cred $cred -Body $metrics -Method Post
  sleep $interval_secs
}

create service

create-service.ps1

$Binary = (Get-Command Powershell).Source
$Arguments = '-ExecutionPolicy Bypass -NoProfile -File "C:\app\prompush.ps1"'
nssm.exe install prompush $Binary $Arguments
choco install nssm
./create-service.ps1
start-service prompush




More stuff

Run every minute

$Action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-NonInteractive  -NoLogo -NoProfile -command C:\bin\prompush.ps1'
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 1)
$Settings = New-ScheduledTaskSettingsSet 
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings
Register-ScheduledTask -TaskName 'prompush every minute' -InputObject $Task -User 'system'

Sandbox not working

$ErrorActionPreference = "Stop"
$r=(invoke-webrequest http://localhost:9182/metrics)
# $metrics = ($r.tostring() -split "[`r`n]" | select-string -pattern "go_|http_request|http_requests|http_response|process" -NotMatch)
$metrics = ($r.content.tostring() -split "[`r`n]" | select-string -pattern "go_|http_request|http_requests|http_response|process" -NotMatch)
# $metrics = ($metrics -replace '(?m)^\s*?\n'| out-string )
$metrics = ($metrics -replace '(?m)^\s*?\n')
# echo $metrics | more
# $metrics = ($r.tostring())
Invoke-WebRequest -uri https://example.uvoo.io/metrics/job/windows-nodes/instance/winhost -cred $cred -Body $metrics -Method Post