Send Powershell Counters to statsd Grafana server




There is a nice script that sends  different data to a grafana server in powershell like the CPU , disk usage and other counters ( Monitoring Purposes )

Info about grafana:

This script later can be deployed as service with the software Powergui

Here is a little video about the software

Also you can deploy the service with advanced installer via GPO – Deploy or you can also launch scheduled inmediate task to the machines that needs that service

2016-01-19 16_52_03-.png


2016-01-19 16_53_15- - Remote Desktop Connection.png

—- Begin of the script —-


Send metrics to statsd server.


PowerShell cmdlet to send metric data to statsd server. Unless IP or port is passed, tries the default IP of and port of 8125.


Metric data to send to statsd. If string is not enclosed in quotes (single or double), the pipe character needs to be escaped.


IP address for statsd server


Port that statsd server is listening to


Send-Statsd “my_metric:123|g”


Send-Statsd “my_metric:123|g” -ip -port 8125


Send-Statsd my_metric:321`|g -ip -port 8180


Send-Statsd ‘my_metric:321|g’ -ip -port 8180

function send_metric ($metric, $stat){

# Ip of the server

#Port of the server
$stat = $stat
$metric = $metric
$data = $metric+$stat+’|g’

echo $data

#Statsd running on localhost on port 8125
#Create endpoint and udp client
$endPoint=New-Object System.Net.IPEndPoint($ipAddress, $port)
$udpclient=New-Object System.Net.Sockets.UdpClient

#Encode and send the data

#Cleanup after yourself


function get_procesor_usage ()
# Cast Counter to integer
[int]$pt = (Get-Counter -Counter “\Processor(_Total)\% Processor Time”).CounterSamples.CookedValue

#send_metric $server $pt

return $pt


function getram_percent ()

$TotalRAM = (get-WMIObject win32_operatingsystem | Measure-Object TotalVisibleMemorySize -sum).sum / 1024
$FreeRAM = ((get-WMIObject -class win32_operatingsystem).freephysicalmemory) / 1024
$UsedRAM = (($TotalRAM) – ($FreeRAM))
$RAMPercentUsed = ([math]::truncate(($UsedRAM) / ($TotalRAM) * 100))

return $RAMPercentUsed


function send_disk_percent ()

#Metrica de disco
$server_metric_disk= “Windows.”+$server+”.disk”
$colDisks = get-wmiobject Win32_LogicalDisk -Filter “DriveType = 3”
# For each disk calculate the free space
foreach ($disk in $colDisks) {
$server_metric_disk= “Windows.”+$server+”.disk”+”.”+$disk.Deviceid
$PercentFree = [Math]::round((($disk.freespace/$disk.size) * 100))
$used = 100 – $PercentFree

send_metric $server_metric_disk $used



## Main

$server= “$env:computername”

# Metricas cpu y ram
$server_metric_cpu = “Windows.”+$server+”.cpu:”
$server_metric_ram = “Windows.”+$server+”.ram:”

while ($true)


$ram = getram_percent
$cpu = get_procesor_usage
send_metric $server_metric_cpu $cpu
send_metric $server_metric_ram $ram
Start-Sleep -s 60


# Get-Counter -ListSet *
#$cpu= $cpu.trim()
#echo $cpu
#send_metric “my_metric:” $cpu.tostring()



As you can see this is the result:

2016-01-19 17_02_43-Grafana - wintel.png





Enjoy !!!





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.