Send Powershell Counters to statsd Grafana server

grafana1.png

 

Hi

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: http://docs.grafana.org/

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

http://en.community.dell.com/techcenter/powergui/m/bits/20439049

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-172.16.150.19 - Remote Desktop Connection.png

—- Begin of the script —-

<#
.SYNOPSIS

Send metrics to statsd server.

.DESCRIPTION

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

.PARAMETER data

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

.PARAMETER ip

IP address for statsd server

.PARAMETER port

Port that statsd server is listening to

.EXAMPLE

Send-Statsd “my_metric:123|g”

.EXAMPLE

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

.EXAMPLE

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

.EXAMPLE

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

#>
function send_metric ($metric, $stat){

# Ip of the server
$ip=”10.128.0.149″

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

echo $data

#Statsd running on localhost on port 8125
$ipAddress=[System.Net.IPAddress]::Parse($ip)
#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
$encodedData=[System.Text.Encoding]::ASCII.GetBytes($data)
$bytesSent=$udpclient.Send($encodedData,$encodedData.length,$endPoint)

#Cleanup after yourself
$udpclient.Close()

}

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
send_disk_percent
Start-Sleep -s 60

}

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

 

—– END OF SCRIPT —–

As you can see this is the result:

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

 

 

 

 

Enjoy !!!

 

 

 

Advertisement

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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.