Tag: monitoring

very, very simple PowerShell performance monitoring + ElasitcSearch

Some time ago I’ve promised a friend to post a little “how-to” concerning PowerShell and ElasticSearch.
So here we go: the following code-snipped can be split into two parts: the “elasticsearch api” and a “naive performance monitor” 🙂

Please note that as always, I write my PoSh-Scripts with PowerShell 5 or 5.1 especially in this case I’m using class-objects, so this won’t run on anything below version 5!

param(
    [string]$es_uri = "http://my-elastic-uri:9200",
    [string]$es_perf_index = "perfdata",
    [string]$es_perf_mapping = "base",
    [int]$SecondsDelay = 10,
    [switch]$Verbose
)

$script:beVerbose = $Verbose

##########################################################################################
## basic elasticsearch powershell api
##########################################################################################
class Elastic {

    hidden [string]$es_uri

    hidden [PSCustomObject] _JSONIFY($obj) {
        return ($obj | ConvertTo-Json -Depth 10)
    }
    
    Elastic($ElasitcServerURI) {
        $this.es_uri = $ElasitcServerURI
    }

    [PSCustomObject] __call ($verb, $params, $body) {
        $uri = $this.es_uri
        if ($script:beVerbose) {
            Write-Host "`nCalling [$uri/$params]" -f Green
            if ($body) {
                if ($body) {
                    Write-Host "BODY`n--------------------------------------------`n$body`n--------------------------------------------`n" -f Yellow
                }
            }
        }
        $response = Invoke-WebRequest -Uri "$uri/$params" -method $verb -ContentType 'application/json' -Body $body
        return $response.Content
    }
  
    [PSCustomObject] _get ($params) {
        $params += "?format=json"    
        return ($this.__call("Get", $params, $null) | ConvertFrom-JSon)
    }
    [PSCustomObject] _delete ($params) {
        return $this.__call("Delete", $params, $null)
    }
    [PSCustomObject] _put ($params, $obj) {
        $obj = $this._JSONIFY($obj)
        return $this.__call("Put", $params, $obj)
    }
    [PSCustomObject] _post ($params, $obj) {
        $obj = $this._JSONIFY($obj)
        return $this.__call("Post", $params, $obj)
    }
    [PSCustomObject] catalogs() {
        return $this._get("_cat/indices")
    }
}

##########################################################################################
## naive performance monitor
##########################################################################################
function Get-CPULoad {
    return $(Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Select-Object Average)
}

function Get-MEMLoad {
    $os = Get-WmiObject Win32_OperatingSystem
    return $([math]::Round(100 - ($os.FreePhysicalMemory / $os.TotalVisibleMemorySize) * 100, 2))
} 

while ($true) {
    $entry = @{
        "Time"    = $(Get-Date);
        "CPULoad" = $(Get-CPULoad);
        "MEMLoad" = $(Get-MEMLoad);
    }
    
    [Elastic]::new($es_uri)._post("$es_perf_index/$es_perf_mapping", $entry) > $null
    if ($script:beVerbose) {
        Write-Host "... going to sleep for $SecondsDelay seconds!"
    }
    Start-Sleep -Seconds $SecondsDelay
}

Of course you can also find my spin at ElasticSearch->PowerShell integration at GitHub
happy hacking!
– if you’ve got any questions concerning this sample, feel free to reach out to me any time!