Log Analytics Data Collector API

You can send practically any data to Azure Log Analytics (aka. OMS). As long as you can convert it to JSON format prior to sending it to your Log Analytics workspace. Here’s the official write up on it https://docs.microsoft.com/en-us/azure/log-analytics/log-analytics-data-collector-api.

Data that you upload gets sent to a custom log, with the characters ‘_CL‘ at the end, these characters are appended automatically.


#region variables
# Replace with your Workspace ID
$CustomerID = 'Your_Workspace_Key'
# Replace with your Primary Key
$SharedKey = 'Your_OMS_Key'
#Specify the name of the record type that we'll be creating.
$LogType = 'Your_Log_Name' # To be used to search for as a custom log e.g. Type=iTunesPopCharts2_CL
# Specify a time in the format YYYY-MM-DDThh:mm:ssZ to specify a created time for the records
$TimeStampField = ''
#endregion
#region functions
function iHeartRadioUS
{
$iwr1 = Invoke-WebRequest UseBasicParsing Uri $url
$null = $iwr1 -match 'http.*[\r|\n].*'; $url2 = $matches[0]
$iwr2 = Invoke-WebRequest UseBasicParsing Uri $url2
$null = $iwr2.RawContent -match 'EXTINF:10,title="(.*?)".*,artist="(.*?)".*"song_spot=\\"(.*?)\\"'
$title = $matches[1]
$artist = $matches[2]
$global:spot = $matches[3]
$RadioSong = "$artist$title"
if((($RadioSong -notmatch $CallSign1) -and ($RadioSong -notmatch $CallSign2) -and ($RadioSong -notmatch $CallSign3)) `
-and ($RadioSong -match '..\s-\s..'))
{
#Write-Output "Passed the last check"
#Region Do stuff with the song
$Cleansong = $RadioSong -replace '\(.*\)', ' ' -replace('&', '&') -replace ' ', ' ' -replace ' feat. ', ' ' `
-replace ' ft. ', ' ' -replace ' ft ', ' ' `
-replace '[\xC0-\xC5]', 'A' -replace '[\xC8-\xCB]', 'E' -replace '[\xCC-\xCF]', 'I' `
-replace '[\xD2-\xD6]', 'O' -replace '[\xD9-\xDC]', 'U' -replace '[\xe8-\xEB]', 'e' `
-replace '[\xEC-\xEF]', 'i' -replace '[\xF2-\xF6]', 'o' -replace '[\xF9-\xFC]', 'u' `
-replace '[\xE0-\xE5]', 'o' -replace '[\xD1]', 'N' -replace '[\x91\x92\x60]', '\x27' `
-replace '[\x93\x94]', '\x22' -replace '[^\x30-\x39\x41-\x5A\x61-\x7A\x20\x2D\x26\x27\x22\x2E\x2C]+', ' ' `
-replace ' ', ' ' -replace 'The ', '' -replace '\s$' -replace '^\s'
$Cleansong
}
}
# Function to create the authorization signature.
Function New-Signature ($CustomerID, $SharedKey, $date, $contentLength, $method, $contentType, $resource)
{
$xHeaders = 'x-ms-date:' + $date
$stringToHash = $method + "`n" + $contentLength + "`n" + $contentType + "`n" + $xHeaders + "`n" + $resource
$bytesToHash = [Text.Encoding]::UTF8.GetBytes($stringToHash)
$keyBytes = [Convert]::FromBase64String($SharedKey)
$sha256 = New-Object TypeName System.Security.Cryptography.HMACSHA256
$sha256.Key = $keyBytes
$calculatedHash = $sha256.ComputeHash($bytesToHash)
$encodedHash = [Convert]::ToBase64String($calculatedHash)
$authorization = 'SharedKey {0}:{1}' -f $CustomerID, $encodedHash
return $authorization
}
# Function to create and post the request
Function Send-OMSData($CustomerID, $SharedKey, $body, $LogType)
{
$method = 'POST'
$contentType = 'application/json'
$resource = '/api/logs'
$rfc1123date = [DateTime]::UtcNow.ToString('r')
$contentLength = $body.Length
$signature = New-Signature `
customerId $CustomerID `
sharedKey $SharedKey `
date $rfc1123date `
contentLength $contentLength `
method $method `
contentType $contentType `
resource $resource
$omsuri = 'https://' + $CustomerID + '.ods.opinsights.azure.com' + $resource + '?api-version=2016-04-01'
$headers = @{
'Authorization' = $signature
'Log-Type' = $LogType
'x-ms-date' = $rfc1123date
'time-generated-field' = $TimeStampField
}
$response = Invoke-WebRequest Uri $omsuri Method $method ContentType $contentType Headers $headers Body $body UseBasicParsing
return $response.StatusCode
}
#endregion
# Flush the variables
Get-Variable *result | Remove-Variable
while($true)
{
$Results = @()
# 93.3 FLZ – Tampa Bay
$URL = 'https://c4.prod.playlists.ihrhls.com/681/playlist.m3u8'
$song = $null
$CallSign1 = "FLZ"
$CallSign2 = "Tampa Bay"
$CallSign3 = "text="
$song = iHeartRadioUS
if($song -and ($FLZTamparesult -ne $song) -and ($global:spot -eq 'M'))
{
$FLZTamparesult = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = '93.3 FLZ – Tampa Bay'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song NoNewline ForegroundColor Yellow;Write-Host " $global:spot" ForegroundColor Green
}
# 103.5 Kiss Fm – Chicago
$URL = 'http://c5icyelb.prod.playlists.ihrhls.com/849/playlist.m3u8'
$song = $null
$CallSign1 = "Kiss FM"
$CallSign2 = "Chicago"
$CallSign3 = "text="
$song = iHeartRadioUS
if($song -and ($KissChicagoresult -ne $song) -and ($global:spot -eq 'M'))
{
$KissChicagoresult = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = '103.5 Kiss Fm – Chicago'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song NoNewline ForegroundColor Yellow;Write-Host " $global:spot" ForegroundColor Green
}
# WILD 94.9 – San Francisco
$URL = 'http://c4icy.prod.playlists.ihrhls.com/305/playlist.m3u8'
$song = $null
$CallSign1 = "Wild 949"
$CallSign2 = "Bays Hit Music"
$CallSign3 = "text="
$song = iHeartRadioUS
if($song -and ($WILDresult -ne $song) -and ($global:spot -eq 'M'))
{
$WILDresult = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = 'Wild 94.9 – San Francisco'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song NoNewline ForegroundColor Yellow;Write-Host " $global:spot" ForegroundColor Green
}
# Z100 – New York
$URL = 'https://c5.prod.playlists.ihrhls.com/1469/playlist.m3u8'
$song = $null
$CallSign1 = "Z100"
$CallSign2 = "Hit Music Station"
$CallSign3 = "text="
$song = iHeartRadioUS
if($song -and ($Z100result -ne $song) -and ($global:spot -eq 'M'))
{
$Z100result = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = 'Z100 – New York'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song NoNewline ForegroundColor Yellow;Write-Host " $global:spot" ForegroundColor Green
}
# Kiis 102.7 – LA
$URL = 'https://c2.prod.playlists.ihrhls.com/185/playlist.m3u8'
$song = $null
$CallSign1 = "102.7 KIIS FM"
$CallSign2 = "KIIS FM"
$CallSign3 = "text="
$song = iHeartRadioUS
if($song -and ($KiisLAresult -ne $song) -and ($global:spot -eq 'M'))
{
$KiisLAresult = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = 'Kiis 102.7 – LA'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song NoNewline ForegroundColor Yellow;Write-Host " $global:spot" ForegroundColor Green
}
if($Results)
{
$json = $Results | ConvertTo-Json
Write-Output InputObject $json
Send-OMSData customerId $customerId sharedKey $sharedKey body $json logType $logType
}
}

Then, within your Azure Log Analytics, run the following query to see the data:
Your_Log_Name_CL

| project Station_s, Song_s, TimeGenerated

| sort by TimeGenerated desc

Cool URLs on the subject:

Leave a Reply

Please log in using one of these methods to post your comment:

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