Forum Discussion

PeterMattsson's avatar
6 years ago

Multi instanced datasource listing Custom Customer Datasources

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.

 

Hope all works well for you.