Skip to content

Instantly share code, notes, and snippets.

@matthewgream
Last active October 31, 2023 20:18
Show Gist options
  • Select an option

  • Save matthewgream/179fc09512451b3362db3e1df2eb4bcb to your computer and use it in GitHub Desktop.

Select an option

Save matthewgream/179fc09512451b3362db3e1df2eb4bcb to your computer and use it in GitHub Desktop.
$source = "source"
$server = "http://server/?ping"
$username = "user"
$password = "pass"
$clusterProcesses = Get-Process -Name cluster* | Select-Object -Property Name, Id, MainWindowTitle, WS
$processDetailsList = @()
foreach ($process in $clusterProcesses) {
$memorySizeMB = [math]::Round($process.WS / 1MB, 2)
$processDetailsList += "$($process.Name),$($process.Id),$($process.MainWindowTitle),$($memorySizeMB)"
}
$processes = $processDetailsList -join ", "
$lastBootUp = Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -Property LastBootUpTime
$bootup = $lastBootUp.LastBootUpTime.ToString("yyyyMMddHHmmss.ffffff+000")
$systemInfo = systeminfo /fo csv | ConvertFrom-Csv
$cpuCounter = Get-Counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 1
$cpuUtil = [math]::Round($cpuCounter.CounterSamples[0].CookedValue, 2)
$memoryTotal = [double]$systemInfo.'Total Physical Memory'.Replace(" MB", "").Replace(",", "")
$memoryAvailable = [double]$systemInfo.'Available Physical Memory'.Replace(" MB", "").Replace(",", "")
$memoryUsed = $memoryTotal - $memoryAvailable
$memoryUtil = [math]::Round((($memoryUsed / $memoryTotal) * 100), 2) # Get percentage
$diskCounter = Get-Counter -Counter "\PhysicalDisk(_Total)\% Disk Time" -SampleInterval 1 -MaxSamples 1
$diskUtil = [math]::Round($diskCounter.CounterSamples[0].CookedValue, 2)
$networkSendCounter = Get-Counter -Counter "\Network Interface(*)\Bytes Sent/sec" -SampleInterval 1 -MaxSamples 1
$networkRecvCounter = Get-Counter -Counter "\Network Interface(*)\Bytes Received/sec" -SampleInterval 1 -MaxSamples 1
$networkSend = 0; foreach ($sample in $networkSendCounter.CounterSamples) { $networkSend += $sample.CookedValue }
$networkRecv = 0; foreach ($sample in $networkRecvCounter.CounterSamples) { $networkRecv += $sample.CookedValue }
$address = Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.InterfaceAlias -eq "tapae0f3d09-53" } | Select-Object -ExpandProperty IPAddress
if (-not $address) {
$address = ""
}
$platform = @{
physicalMemoryTotal = $systemInfo.'Total Physical Memory'.Replace(" MB", "").Replace(",", "")
physicalMemoryAvail = $systemInfo.'Available Physical Memory'.Replace(" MB", "").Replace(",", "")
virtualMemoryLimit = $systemInfo.'Virtual Memory: Max Size'.Replace(" MB", "").Replace(",", "")
virtualMemoryAvail = $systemInfo.'Virtual Memory: Available'.Replace(" MB", "").Replace(",", "")
virtualMemoryInUse = $systemInfo.'Virtual Memory: In Use'.Replace(" MB", "").Replace(",", "")
cpuUtil = "$($cpuUtil)"
memoryUtil = "$($memoryUtil)"
diskUtil = "$($diskUtil)"
networkSend = "$([math]::Round($networkSend/1MB, 6))"
networkRecv = "$([math]::Round($networkRecv/1MB, 6))"
}
$jsonObject = @{
bootup = $bootup
source = $source
processes = $processes
platform = $platform
address = $address
}
$jsonOutput = $jsonObject | ConvertTo-Json -Compress
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password)))
$headers = @{ Authorization = ("Basic {0}" -f $base64AuthInfo) }
try {
$response = Invoke-RestMethod -Uri $server -Headers $headers -Method 'Post' -Body $jsonOutput -ContentType 'application/json'
} catch {
Write-Error "An error occurred while sending data to the server: $_"
}
#!/bin/sh -e
#############################################################################################
export SERVER="http://server/?ping"
export USER=user
export PASS=pass
export DOMAIN=domain
#############################################################################################
CPU=$(top -bn1 | grep "load average:" | awk '{print $(NF-2)}' | awk -F ',' '{print $1*100}')
MEM=$(free | grep Mem | awk '{print $3/$2 * 100.0}')
DSK=$(df -h / | tail -1 | awk '{print $5}' | sed 's/%//g')
NET=0.0
#############################################################################################
export HOST=`hostname`
export FQDN="$HOST.$DOMAIN"
export TIME=`date -Iseconds`
export BOOT=`uptime -s`
export SRCE=$HOST
export TYPE=${1:-"ping"}
export SYST="cpu=$CPU,net=$NET,mem=$MEM,dsk=$DSK"
export ADDR=`curl ifconfig.me 2>/dev/null`
echo PING [$SERVER]: $FQDN ... $BOOT [$TYPE]
# echo "{\"time\":\"$TIME\", \"boot\":\"$BOOT\", \"source\":\"$SRCE\", \"type\":\"$TYPE\", \"system\":\"$SYST\", \"address\":\"$ADDR\"}"
export RESPONSE=`curl --request POST \
--basic --user $USER:$PASS \
--header "Content-Type: application/json" --header "Accept: application/json" \
--data "{\"time\":\"$TIME\", \"boot\":\"$BOOT\", \"source\":\"$SRCE\", \"type\":\"$TYPE\", \"system\":\"$SYST\", \"address\":\"$ADDR\"}" \
--output /dev/null \
--write-out "%{http_code}" \
$SERVER 2> /dev/null`
echo SUCCESS: $RESPONSE
#############################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment