Log Analytics Data Collector API

Grab data/information from anywhere and store this in Log Analytics. Basically any log, any OS, any type of data can be captured on a loop and sent to a Log Analytics workspace on a constant basis, then can be used to pull out reports etc.

My example below pulls the ‘Current Playing Song‘ out of radio station websites, it runs through many radio stations, at the end converts the aggravated results to JSON, then the data is sent to the Custom Log in Log Analytics using the API.

You can run the script below on a regular basis, e.g. set this up as a scheduled task.

To set things up and to get underway, there’s a few variables to set at the top of the script.

Both the $CustomerID and $SharedKey can be found from the Advanced Settings area of your Log Analytics workspace in the Azure Portal as shown below….

2018-05-31_1030

2018-05-31_1027

The $LogType is the name of your custom log file, you don’t need to create this prior to tunning the script, the Custom Log will be created the very first time you run the script under the name you give it here.

#region variables
# Replace with your Log Analytics Workspace ID
$CustomerID = '***************My Customer ID********************'
# Replace with your Log Analytics Workspace Primary Key
$SharedKey = '***************Massive Long Key********************'
# Specify the custom log name.
$LogType = '***************CustomLogName********************' # Name of the custom log Type=CustomLog_CL without the top and tail e.g. 'CustomLog'
# Specify a time in the format YYYY-MM-DDThh:mm:ssZ to specify a created time for the records
$TimeStampField = ''
#endregion
#region functions
$AustereoURL = 'http://legacy.scahw.com.au'
$ARNParentURL = 'http://media.arn.com.au'
$SmoothParentURL = 'http://www.smooth.com.au'
function Austereo {
[CmdletBinding()]
Param()
Remove-Variable Name Cleansong ea 0
Remove-Variable Name RadioSong ea 0
Remove-Variable Name Title ea 0
Remove-Variable Name Artist ea 0
Start-Sleep Seconds 1
# Get the initial metadata for the stream
$iwr = Invoke-RestMethod Uri $url
# Build up the .Net web client
$HttpCompletionOption = 'ResponseContentRead'
Add-Type AssemblyName System.Net.Http
$webClient = New-Object System.Net.Http.HttpClient
$webclient.DefaultRequestHeaders.Add('Icy-MetaData', '1')
# Get the Stream URL
$null = $iwr.InnerXml -match '<location>(?<location>.*)<\/location>'
$location = $matches.location
# Fire up the stream
$response = $webClient.GetAsync($location,$HttpCompletionOption)
$null = $webclient.DefaultRequestHeaders.Remove('Icy-MetaData')
# Pause until the stream title actually fires up
Start-Sleep Seconds 2
# Grab the song
$iwr = Invoke-RestMethod Uri $url
$null = $iwr.InnerXml -match '<title>(?<song>.*)<\/title>'
# Kill the stream
$webclient.Dispose()
$RadioSong = $matches.song
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('&amp;', '&') -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 AustralianRadioNetwork
{
[CmdletBinding()]
Param()
Remove-Variable Name Cleansong ea 0
Remove-Variable Name RadioSong ea 0
#WSFM – Sydney
$matches = @()
Start-Sleep Seconds 1
$URI = (Invoke-WebRequest Uri $URL).content
Start-Sleep Seconds 1
$null = $URI -match '<now_playing>(.|\n)*artist><!\[CDATA\[([^]]*)\]\]></artist(.|\n)*</now_playing>'
$Artist = $matches[2]
$null = $URI -match '<now_playing>(.|\n)*title\sgeneric="False"><!\[CDATA\[([^]]*)\]\]></title(.|\n)*</now_playing>'
$title = $matches[2]
$RadioSong = '{0} – {1}' -f $Artist, $title
#Write-Host -NoNewline "Now playing on wsFM " -ForegroundColor Cyan;Write-Host $wsfmsong -ForegroundColor Yellow
if(($RadioSong -notmatch $CallSign1) -and `
($RadioSong -match '..\s-\s..') -and `
($RadioSong -notmatch 'Audio Type Changed'))
{
#query table entities
#Region Do stuff with the song
$Cleansong = $RadioSong -replace '\(.*\)', ' ' -replace('&amp;', '&') -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 ', ''
$Cleansong
}
}
function SmoothNetwork
{
[CmdletBinding()]
Param()
Remove-Variable Name Cleansong ea 0
Remove-Variable Name RadioSong ea 0
#Smooth FM
$matches = @()
Start-Sleep Seconds 1
$URI = Invoke-WebRequest Uri $URL UseBasicParsing
Start-Sleep Seconds 1
$null = ($URI.Content) -match 'smooth-media-bar-now-playing-artist"><a class="smooth-media-bar-link" href="">(.*)<.*><\/span'
$Artist = $matches[1]
$null = ($URI.Content) -match 'smooth-media-bar-now-playing-track"><a class="smooth-media-bar-link" href="">(.*)<.*>.<\/span'
$title = $matches[1]
$RadioSong = '{0} – {1}' -f $Artist, $title
#Write-Host -NoNewline "Now playing on Smooth FM Sydney " -ForegroundColor Cyan;Write-Host $smooth953song -ForegroundColor Yellow
if(($RadioSong -notmatch $CallSign1) -and `
($RadioSong -match '..\s-\s..') -and `
($RadioSong -notmatch 'Audio Type Changed'))
{
#Region Do stuff with the song
$Cleansong = $RadioSong -replace '\(.*\)', ' ' -replace('&amp;', '&') -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 ', ''
$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 = @()
# Triple M Brisbane
$LeafURL = '4mmm_128.xspf'
$URL = "$AustereoURL/$LeafURL"
$CallSign1 = "Brisbane's Best Rock"
$CallSign2 = "Triple M"
$CallSign3 = "104.5 Triple M Brisbane"
$song = $null
$song = Austereo
if($song -and ($TripleMBneresult -ne $song))
{
$TripleMBneresult = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = 'Triple M Brisbane'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song ForegroundColor Yellow}
# Triple M Sydney
$LeafURL = '2mmm_128.xspf'
$URL = "$AustereoURL/$LeafURL"
$CallSign1 = "Sydney's Best Rock"
$CallSign2 = "Triple M"
$CallSign3 = "———"
$song = $null
$song = Austereo
if($song -and ($TripleMSydresult -ne $song))
{
$TripleMSydresult = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = 'Triple M Sydney'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song ForegroundColor Yellow}
# Triple M Melbourne
$LeafURL = '3mmm_128.xspf'
$URL = "$AustereoURL/$LeafURL"
$CallSign1 = "Melbourne's Best Rock"
$CallSign2 = "Triple M"
$CallSign3 = "———"
$song = $null
$song = Austereo
if($song -and ($TripleMMelresult -ne $song))
{
$TripleMMelresult = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = 'Triple M Melbourne'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song ForegroundColor Yellow}
# Triple M Darling Downs
$LeafURL = '4gr_128.xspf'
$URL = "$AustereoURL/$LeafURL"
$CallSign1 = "Darling Downs"
$CallSign2 = "Triple M"
$CallSign3 = "———"
$song = $null
$song = Austereo
if($song -and ($TripleMDDresult -ne $song))
{
$TripleMDDresult = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = 'Triple M Darling Downs'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song ForegroundColor Yellow}
# Triple M Cairns
$LeafURL = '4hot_128.xspf'
$URL = "$AustereoURL/$LeafURL"
$CallSign1 = "Cairns Best Rock"
$CallSign2 = "Triple M"
$CallSign3 = "———"
$song = $null
$song = Austereo
if($song -and ($TripleMCairnsresult -ne $song))
{
$TripleMCairnsresult = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = 'Triple M Cairns'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song ForegroundColor Yellow}
# Triple M Modern Rock Digital
$LeafURL = 'modernrock_128.xspf'
$URL = "$AustereoURL/$LeafURL"
$CallSign1 = "Sydney's Best Rock"
$CallSign2 = "Triple M"
$CallSign3 = "———"
$song = $null
$song = Austereo
if($song -and ($ModernRockresult -ne $song))
{
$ModernRockresult = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = 'Triple M Modern Rock Digital'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song ForegroundColor Yellow}
# Triple M Classic Rock Digital
$LeafURL = 'classicrock_128.xspf'
$URL = "$AustereoURL/$LeafURL"
$CallSign1 = "Sydney's Best Rock"
$CallSign2 = "Triple M"
$CallSign3 = "Classic Rock"
$song = $null
$song = Austereo
if($song -and ($ClassicRockresult -ne $song))
{
$ClassicRockresult = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = 'Triple M Classic Rock Digital'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song ForegroundColor Yellow}
# Triple M Greatest Hits Digital
$LeafURL = 'greatesthits_128.xspf'
$URL = "$AustereoURL/$LeafURL"
$CallSign1 = "Sydney's Best Rock"
$CallSign2 = "Triple M"
$CallSign3 = "———"
$song = $null
$song = Austereo
if($song -and ($GreatestHitsresult -ne $song))
{
$GreatestHitsresult = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = 'Triple M Greatest Hits Digital'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song ForegroundColor Yellow}
# Easy Hits Digital
$LeafURL = 'stardust_128.xspf'
$URL = "$AustereoURL/$LeafURL"
$CallSign1 = "Easy Hits"
$CallSign2 = "Easy Radio"
$CallSign3 = "———"
$song = $null
$song = Austereo
if($song -and ($EasyHitsresult -ne $song))
{
$EasyHitsresult = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = 'Easy Hits Digital'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song ForegroundColor Yellow}
# 2-Day FM
$LeafURL = '2day_128.xspf'
$URL = "$AustereoURL/$LeafURL"
$CallSign1 = "Hits & Old School"
$CallSign2 = "2Day FM"
$CallSign3 = "2DayFM"
$song = $null
$song = Austereo
if($song -and ($2DayFMSYDresult -ne $song))
{
$2DayFMSYDresult = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = '2Day FM Sydney'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song ForegroundColor Yellow}
# Fox FM
$LeafURL = '3fox_128.xspf'
$URL = "$AustereoURL/$LeafURL"
$CallSign1 = "Hits & Old School"
$CallSign2 = "Fox FM"
$CallSign3 = "Hits and Old School"
$song = $null
$song = Austereo
if($song -and ($FoxFMMELresult -ne $song))
{
$FoxFMMELresult = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = 'Fox FM Melbourne'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song ForegroundColor Yellow}
# B105
$LeafURL = '4bbb_128.xspf'
$URL = "$AustereoURL/$LeafURL"
$CallSign1 = "Hits & Old School"
$CallSign2 = "Hit 105"
$CallSign3 = "Hits and Old School"
$song = $null
$song = Austereo
if($song -and ($B105BNEresult -ne $song))
{
$B105BNEresult = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = 'B105 Brisbane'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song ForegroundColor Yellow}
# PMFM
$LeafURL = '6ppm_128.xspf'
$URL = "$AustereoURL/$LeafURL"
$CallSign1 = "Hits & Old School"
$CallSign2 = "Hit929"
$CallSign3 = "Hits and Old School"
$song = $null
$song = Austereo
if($song -and ($PMFMresult -ne $song))
{
$PMFMresult = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = 'PMFM Perth'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song ForegroundColor Yellow}
# SAFM
$LeafURL = '5ssa_128.xspf'
$URL = "$AustereoURL/$LeafURL"
$CallSign1 = "Hits & Old School"
$CallSign2 = "Hit 107"
$CallSign3 = "Hits and Old School"
$song = $null
$song = Austereo
if($song -and ($SAFMresult -ne $song))
{
$SAFMresult = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = 'SAFM Adelaide'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song ForegroundColor Yellow}
# WSFM – Sydney
$LeafURL = 'xml/wsfm1017_now.xml'
$URL = "$ARNParentURL/$LeafURL"
$CallSign1 = "WS FM101.7"
$song = $null
$song = AustralianRadioNetwork
if($song -and ($WSFMresult -ne $song))
{
$WSFMresult = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = 'WSFM Sydney'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song ForegroundColor Yellow}
# Gold FM – Melbourne
$LeafURL = 'xml/gold1043_now.xml'
$URL = "$ARNParentURL/$LeafURL"
$CallSign1 = "Gold 104.3"
$song = $null
$song = AustralianRadioNetwork
if($song -and ($GoldFMresult -ne $song))
{
$GoldFMresult = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = 'Gold FM Melbourne'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song ForegroundColor Yellow}
# Kiis – Melbourne
$LeafURL = 'xml/KIIS1011_now.xml'
$URL = "$ARNParentURL/$LeafURL"
$CallSign1 = "Kiis"
$song = $null
$song = AustralianRadioNetwork
if($song -and ($kiis1011result -ne $song))
{
$kiis1011result = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = 'Kiis Sydney'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song ForegroundColor Yellow}
# Kiis – Sydney
$LeafURL = 'xml/mix1065_now.xml'
$URL = "$ARNParentURL/$LeafURL"
$CallSign1 = "Kiis"
$song = $null
$song = AustralianRadioNetwork
if($song -and ($kiis1065result -ne $song))
{
$kiis1065result = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = 'Kiis Melbourne'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song ForegroundColor Yellow}
# Smooth FM – Sydney
$LeafURL = 'smooth953-player'
$URL = "$SmoothParentURL/$LeafURL"
$CallSign1 = "Smooth FM"
$song = $null
$song = SmoothNetwork
if($song -and ($SmoothFMSYDresult -ne $song))
{
$SmoothFMSYDresult = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = 'Smooth FM Sydney'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song ForegroundColor Yellow}
# Smooth FM – Melbourne
$LeafURL = 'smooth915-player'
$URL = "$SmoothParentURL/$LeafURL"
$CallSign1 = "Smooth FM"
$song = $null
$song = SmoothNetwork
if($song -and ($SmoothFMMELresult -ne $song))
{
$SmoothFMMELresult = $song
$x = New-Object TypeName psObject | Select-Object Property Station, Song
$x.Station = 'Smooth FM Melbourne'
$x.Song = $song
$Results += $x
Write-Host "$($x.Station): " NoNewline ForegroundColor Cyan;Write-Host $x.Song ForegroundColor Yellow}
if($Results)
{
$json = $Results | ConvertTo-Json
Write-Output InputObject $json
Send-OMSData customerId $customerId sharedKey $sharedKey body $json logType $logType
}
}

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 )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s