And now, Part IV! Like I’ve mentioned previously, please do yourself a favor and read the previous parts to this series of posts: Part I, Part II, and Part III. This won’t make sense without it. It may not make sense with it, and if that turns out to be true, then let me know by leaving a comment, or reaching me on Twitter. I know it’s possible that Splunk feels like a foreign language. What I know, I learned over the course of a few weeks, and it doesn’t feel like much. I probably shouldn’t be surprised that there’s so much to cover; so much was learned. Maybe you’ll have to do it too: I read the same articles over and over and over. I did have some help from a colleague, but if you don’t have that, then you can consider it me, up to a point,
The next thing we’ll look at is our first POST request to Splunk. This is us, sending in our JSON payload. Notice our first below Invoke-RestMethod command. First, it includes our $HttpRequestEventParams parameter hash table. This includes the Uri, Method, Headers, and the Body parameter and their corresponding parameter values. Here’s a reminder of what’s in that parameter hash table.
URI = @{$EventUri; Method = 'POST'; Headers = $Headers; Body = $Body}
As a part of this Invoke-RestMethod command, we also included the StatusCodeVariable parameter. When our command completes, it will have created the $StatusCode variable which should contain a 200 response, if our message was received by Splunk. Additionally, we have this command writing any output of the command (from Splunk) into the $Ack variable.
#region: Make requests to Splunk REST web services. $Ack = Invoke-RestMethod @HttpRequestEventParams -StatusCodeVariable StatusCode -Verbose $StatusCode $AckBody = "{`"acks`": [$($Ack.ackId)]}" $HttpRequestAckParams = @{URI = $AckUri; Method = 'POST'; Headers = $Headers; Body = $AckBody} Measure-Command -Expression { Do { $AckResponse = Invoke-RestMethod @HttpRequestAckParams -Verbose $AckResponse.acks.0 Start-Sleep -Seconds 30 } Until ($AckResponse.acks.0 -eq $true) } # End Measure-Command #endregion.
Keep in mind here, that if we weren’t using indexer acknowledgment, we’d probably be done. But now, we’ve got to create a second POST request to Splunk, so we can determine when the data that we know was received (200 response), is headed into the Splunk pipeline for processing. It’s a guarantee of data ingestion (not indigestion). But, since we’re going along down the indexer acknowledgment path (for now and only now), let’s walk through what else is happening here.
First, we create $AckBody. It uses the PowerShell object in $Ack and turns it back into JSON. Invoke-RestMethod has this helpful feature of turning JSON into a PowerShell object, so we’ve got to reverse that so we can send it back to Splunk. Once $AckBody is done, we’ll use it as a parameter value in $HttpRequestAckParams. About $AckBody, be sure to read this page (it’s been linked before), under the “Query for indexing status” section. Splunk sends us an ack ID and then we need to send it back with the same headers as in the first Invoke-RestMethod request. Remember, this includes the HEC token (pulled out of our .env file forever ago), and the channel identifier we created as a random GUID.
Following? Again, reach out to me if you need this information and I’m failing at getting it across to you. I’m not too big to help/rewrite/clarify. I get it, this has the potential to make someone launch their laptop out of a second-story window. Luckily, I work on the first floor of my house. Also, print out the Splunk articles I’ve linked and make them a home on the drink table next to your couch (like I did).
Okay, we’re on the last step. Remember, we’re still working as those indexer acknowledgement is enabled. This whole second POST request is irrelevant if you’re not going to use it. As is the channel identifier. Again, I’ll post modified code just as soon as my indexer acknowledgment is disabled.
Measure-Command -Expression { Do { $AckResponse = Invoke-RestMethod @HttpRequestAckParams -Verbose $AckResponse.acks.0 Start-Sleep -Seconds 30 } Until ($AckResponse.acks.0 -eq $true) } # End Measure-Command
I mentioned that I’m not going to be using indexer acknowledgment because of the time it takes; I simply don’t have that. I’m in the business of automating. I record the duration of every function I invoke. It isn’t going to work for me. Anyway, I have my second Invoke-RestMethod request inside a Do-Until loop. This loop continues to run the command every 30 seconds until I get a $true response from Splunk (that means that Splunk is for sure going to index my data). For fun, that’s inside of a Measure-Command Expression parameter. This, is how I determined it took too much time for me. Below I’ll include the entire code as one block from all three posts. In the fifth post, yet to be written, I’ll include the entire code as one block, too. And remember, that post won’t have any requirements on an enabled indexer acknowledgment, the second POST request, the channel identifier, etc.
Thank you for hanging in for this one. Hopefully, it proves helpful for someone. Oh, that reminds me. There were two Splunk functions written to do all this when I started looking around. Maybe you found them too. They made little sense to me until I really took the time to learn what’s happening. Now that you’ve read this series, read over those functions; see what you understand about them now that you wouldn’t have been able to before. It might be much more than you thought it would be.
@torggler https://www.powershellgallery.com/packages/Send-SplunkEvent
@halr9000 https://gist.github.com/halr9000/d7bce26533db7bca1746
#region: Read .env file and create environment variables. $FilterScript = {$_ -ne '' -and $_ -notmatch '^#'} $Content = Get-Content -Path 'C:\Users\tommymaynard\Documents\_Repos\code\functiontemplate\random\telemetry\.env' | Where-Object -FilterScript $FilterScript If ($Content) { Foreach ($Line in $Content) { $KVP = $Line -split '=',2; $Key = $KVP[0].Trim(); $Value = $KVP[1].Trim() Write-Verbose -Message "Adding an environment variable: `$env`:$Key." [Environment]::SetEnvironmentVariable($Key,$Value,'Process') } # End Foreach. } Else { '$Content variable was not set. Do things without the file/the environment variables.' } # End If. #endregion. #region: Read clixml .xml file and obtain hashtable. $HashTablePath = 'C:\Users\tommymaynard\Documents\_Repos\code\functiontemplate\random\telemetry\eventhashtable.xml' $EventHashTable = Import-Clixml -Path $HashTablePath #endregion. #region: Create Splunk / Invoke-RestMethod variables. $EventUri = $env:SplunkUrl + '/services/collector/event' $AckUri = $env:SplunkUrl + '/services/collector/ack' $ChannelIdentifier = (New-Guid).Guid $Headers = @{Authorization = "Splunk $env:SplunkHECToken"; 'X-Splunk-Request-Channel' = $ChannelIdentifier} $Body = ConvertTo-Json -InputObject $EventHashTable $HttpRequestEventParams = @{URI = $EventUri; Method = 'POST'; Headers = $Headers; Body = $Body} #endregion. #region: Make requests to Splunk REST web services. $Ack = Invoke-RestMethod @HttpRequestEventParams -StatusCodeVariable StatusCode -Verbose $StatusCode $AckBody = "{`"acks`": [$($Ack.ackId)]}" $HttpRequestAckParams = @{URI = $AckUri; Method = 'POST'; Headers = $Headers; Body = $AckBody} Measure-Command -Expression { Do { $AckResponse = Invoke-RestMethod @HttpRequestAckParams -Verbose $AckResponse.acks.0 Start-Sleep -Seconds 30 } Until ($AckResponse.acks.0 -eq $true) } # End Measure-Command #endregion.