PeterMattsson

Multi instanced datasource listing Custom Customer Datasources

Recommended Posts

Hello,

We needed a datasource to show custom datasources named Mon_[CustomerName]* from all our customers in a datasource. This to be able to show big numbers on each customers dashboard and also a summary on our Sales Dashboard. It is important to know that this datasource calculates from every customer so be careful which server you appy this to. Best is to have an internal server of your own.

First i created the datasource and made it a multi instance datasource with Active Discovery. Below is the script fetching our customers names from device groups. Please notice that we've got all our customers inside a group called customers. We search inside that group by telling Powershell to look inside that group by its DeviceGroup ID.

 

CollectorScript

##########################################################################################

<# Use TLS 1.2 #>
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
 
<# account info #>
$accessId = '[AccessID]'
$accessKey = '[AccessKey]'
$company = '[Your Company]'
 
<# request details #>
$httpVerb = 'GET'
$resourcePath = '/device/groups'
$queryParams = '?filter=parentId~[ParentGroupID]' # Here we put the group ID of our Customers group.

<# Construct URL #>
$url = 'https://' + $company + '.logicmonitor.com/santaba/rest' + $resourcePath + $queryParams

<# Get current time in milliseconds #>
$epoch = [Math]::Round((New-TimeSpan -start (Get-Date -Date "1/1/1970") -end (Get-Date).ToUniversalTime()).TotalMilliseconds)
 
<# Concatenate Request Details #>
$requestVars = $httpVerb + $epoch + $resourcePath
 
<# Construct Signature #>
$hmac = New-Object System.Security.Cryptography.HMACSHA256
$hmac.Key = [Text.Encoding]::UTF8.GetBytes($accessKey)
$signatureBytes = $hmac.ComputeHash([Text.Encoding]::UTF8.GetBytes($requestVars))
$signatureHex = [System.BitConverter]::ToString($signatureBytes) -replace '-'
$signature = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($signatureHex.ToLower()))
 
<# Construct Headers #>
$auth = 'LMv1 ' + $accessId + ':' + $signature + ':' + $epoch
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization",$auth)
$headers.Add("Content-Type",'application/json')

<# Make Request #>
$response = Invoke-RestMethod -Uri $url -Method $httpVerb -Header $headers 

<# Create Body of Data  #>
$rawbody = $response.data

$items = $rawbody.items

foreach($item in $items){
    
    $customername = $($item.name.Split(" ")[0]) # this filter the first word in the customer name Which is all we want.
    
  

    write-host "$customername##$customername" # Should be ID##Name##Description but then i got problems with the ##Wildvalue## Token
    
}

EXIT 0;

##########################################################################################

 

After this we added the script below to count the amount of custom datasources for each customer in collection above.

 

##########################################################################################

<# Use TLS 1.2 #>
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
 
<# account info #>
$accessId = '[AccessId]'
$accessKey = '[AccessKey]'
$company = '[Company]'
 

    $httpVerb = 'GET'
    $resourcePath = '/setting/datasources'
    $queryParams = "?size=500&filter=name~MON_##WILDVALUE##" # Search for a datasource called *MON_Customername*
    <# Construct URL #>
    $url = 'https://' + $company + '.logicmonitor.com/santaba/rest' + $resourcePath + $queryParams
 
    <# Get current time in milliseconds #>
    $epoch = [Math]::Round((New-TimeSpan -start (Get-Date -Date "1/1/1970") -end (Get-Date).ToUniversalTime()).TotalMilliseconds)
 
    <# Concatenate Request Details #>
    $requestVars = $httpVerb + $epoch + $resourcePath
 
    <# Construct Signature #>
    $hmac = New-Object System.Security.Cryptography.HMACSHA256
    $hmac.Key = [Text.Encoding]::UTF8.GetBytes($accessKey)
    $signatureBytes = $hmac.ComputeHash([Text.Encoding]::UTF8.GetBytes($requestVars))
    $signatureHex = [System.BitConverter]::ToString($signatureBytes) -replace '-'
    $signature = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($signatureHex.ToLower()))
 
    <# Construct Headers #>
    $auth = 'LMv1 ' + $accessId + ':' + $signature + ':' + $epoch
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Authorization",$auth)
    $headers.Add("Content-Type",'application/json')
 
    <# Make Request #>
    $response = Invoke-RestMethod -Uri $url -Method $httpVerb -Header $headers 
    $rawbody = $response.data
    $items = $rawbody.items

   $monitors=$items.Count
   write-host "CustomMonitors=$($monitors)"

##########################################################################################

Create a Datapoint with the following settings.

image.png.1698fc001f57b8016cdb20ff3d6d27d6.png

 

Hope all works well for you.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now