Skip to content

Instantly share code, notes, and snippets.

@josy1024
Last active January 6, 2023 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josy1024/7a6edb09a8af9270bef847c2b3f51e53 to your computer and use it in GitHub Desktop.
Save josy1024/7a6edb09a8af9270bef847c2b3f51e53 to your computer and use it in GitHub Desktop.
# CREDIT:
# CODE SAMPLE FROM: https://github.com/EsOsO/RRDTool
# GERMAN Get-Counter Values
# Database
$DataSource1 = New-RRDDataSource -Name cpu_kernel -DataSourceType GAUGE -Heartbeat 120 -Min 0 -Max 100
$DataSource2 = New-RRDDataSource -Name cpu_user -DataSourceType GAUGE -Heartbeat 120 -Min 0 -Max 100
$DataSource3 = New-RRDDataSource -Name cpu_queue -DataSourceType GAUGE -Heartbeat 120 -Min 0
$AVERAGE_0 = New-RRDRoundRobinArchive -ConsolidationFunction AVERAGE -Steps 1 -Rows 1440 # 1 day, 1 min pdp
$AVERAGE_1 = New-RRDRoundRobinArchive -ConsolidationFunction AVERAGE -Steps 5 -Rows 2016 # 1 week, 5 min pdp
$AVERAGE_2 = New-RRDRoundRobinArchive -ConsolidationFunction AVERAGE -Steps 60 -Rows 720 # 1 month, 1 hour pdp
$AVERAGE_3 = New-RRDRoundRobinArchive -ConsolidationFunction AVERAGE -Steps 60 -Rows 8760 # 1 year, 1 hour pdp
New-RRD -FileName C:\Temp\cpu.rrd `
-Step 60 `
-DS $Datasource1, $DataSource2, $DataSource3 `
-RRA $AVERAGE_0, $AVERAGE_1, $AVERAGE_2, $AVERAGE_3
#UPDATE
$computername = $env:computername
#english
#$Values = Get-Counter -ComputerName $ComputerName -Counter '\Processor(_total)\% Privileged Time', '\Processor(_total)\% User Time', '\System\Processor Queue Length' | select -ExpandProperty CounterSamples | select -ExpandProperty CookedValue
$Values = Get-Counter -computername $computername -Counter '\prozessor(_total)\prozessorzeit (%)', '\prozessor(_total)\benutzerzeit (%)', '\System\Prozessor-Warteschlangenlänge' | Select-Object -ExpandProperty CounterSamples | Select-Object -ExpandProperty CookedValue
#$valdot = $values -replace ",", "."
Write-customLOG -logtext $Values
Update-RRD -FileName C:\Temp\cpu.rrd -Value $Values
# GRAPH
$CpuRRD = "C:\Temp\cpu.rrd"
$Elements = @(
New-RRDDef -Name 'cpu_kernel' -FileName $CpuRRD -DataSource 'cpu_kernel' -ConsolidationFunction AVERAGE
New-RRDDef -Name 'cpu_user' -FileName $CpuRRD -DataSource 'cpu_user' -ConsolidationFunction AVERAGE
New-RRDDef -Name 'cpu_queue' -FileName $CpuRRD -DataSource 'cpu_queue' -ConsolidationFunction AVERAGE
New-RRDLine -Name 'cpu_kernel' -Color '#FE343E' -Legend '% CPU Kernel Time'
New-RRDLine -Name 'cpu_user' -Color '#6B46EC' -Legend '% CPU User Time'
New-RRDLine -Name 'cpu_queue' -Color '#44F432' -Legend 'Processor Queue Lenght'
New-RRDVdef -Name 'cpukernel_max' -RPNExpression 'cpu_kernel,MAXIMUM'
New-RRDVdef -Name 'cpukernel_avg' -RPNExpression 'cpu_kernel,AVERAGE'
New-RRDVdef -Name 'cpukernel_last' -RPNExpression 'cpu_kernel,LAST'
New-RRDComment -Text '\n'
New-RRDComment -Text '% CPU Kernel Time:'
New-RRDComment -Text '\n'
New-RRDGPrint -Name 'cpukernel_max' -Format 'Max:\t%12.2lf%s'
New-RRDGPrint -Name 'cpukernel_avg' -Format 'Avg:\t%12.2lf%s'
New-RRDGPrint -Name 'cpukernel_last' -Format 'Last:\t%12.2lf%s'
New-RRDVdef -Name 'cpuuser_max' -RPNExpression 'cpu_user,MAXIMUM'
New-RRDVdef -Name 'cpuuser_avg' -RPNExpression 'cpu_user,AVERAGE'
New-RRDVdef -Name 'cpuuser_last' -RPNExpression 'cpu_user,LAST'
New-RRDComment -Text '\n'
New-RRDComment -Text '% CPU User Time'
New-RRDComment -Text '\n'
New-RRDGPrint -Name 'cpuuser_max' -Format 'Max:\t%12.2lf%s'
New-RRDGPrint -Name 'cpuuser_avg' -Format 'Avg:\t%12.2lf%s'
New-RRDGPrint -Name 'cpuuser_last' -Format 'Last:\t%12.2lf%s'
New-RRDVdef -Name 'cpuqueued_max' -RPNExpression 'cpu_queue,MAXIMUM'
New-RRDVdef -Name 'cpuqueued_avg' -RPNExpression 'cpu_queue,AVERAGE'
New-RRDVdef -Name 'cpuqueued_last' -RPNExpression 'cpu_queue,LAST'
New-RRDComment -Text '\n'
New-RRDComment -Text 'Processor Queue Length:'
New-RRDComment -Text '\n'
New-RRDGPrint -Name 'cpuqueued_max' -Format 'Max:\t%12.2lf%s'
New-RRDGPrint -Name 'cpuqueued_avg' -Format 'Avg:\t%12.2lf%s'
New-RRDGPrint -Name 'cpuqueued_last' -Format 'Last:\t%12.2lf%s'
)
Out-RRDGraph -FileName C:\Temp\cpu4h.png -Title 'CPU Usage - Last 4 hours' -Start (Get-Date).AddHours(-4) -LowerLimit 0 -UpperLimit 100 -Rigid -Elements $Elements -Verbose
Out-RRDGraph -FileName C:\Temp\cpu24h.png -Title 'CPU Usage - Last 24 hours' -Start (Get-Date).AddHours(-25) -LowerLimit 0 -UpperLimit 100 -Rigid -Elements $Elements -Verbose
Out-RRDGraph -FileName C:\Temp\cpu7d.png -Title 'CPU Usage - Last day' -Start (Get-Date).AddDays(-7) -LowerLimit 0 -UpperLimit 100 -Rigid -Elements $Elements -Verbose
# FETCH DATA:
$rrd = "C:\Temp\cpu.rrd"
#-r resolution 60 sec$st
#-s start time from (24h) -86400
#$data = Invoke-Expression "/bin/rrdtool.exe fetch $rrd AVERAGE -r 60 -s -86400"
$data = Invoke-Expression "./bin/rrdtool.exe fetch $rrd AVERAGE -r 60 -s -3600" | Select-Object -Skip 2
# Split the string into an array
$string = $data[61]
$array = $string.Split(':')
# Create a new empty named array
$namedArray = @{}
# Iterate through the array and add the values to the named array
foreach ($item in $array) {
if ($item -match '^\d+$') {
$namedArray['Timestamp'] = get-date ($start.ticks + $item)
#$namedArray['Timestamp'] = $item
} else {
$namedArray['Values'] = $item.trimstart(' ').Split(' ')
}
}
$namedArray['Timestamp']
$cpu = $namedArray.Values[0]
$cpuUser = $namedArray.Values[1]
$queue = $namedArray.Values[2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment