|
| 1 | +Function Get-LMLogMessage { |
| 2 | + |
| 3 | + [CmdletBinding(DefaultParameterSetName = "Default")] |
| 4 | + Param ( |
| 5 | + [Datetime]$StartDate, |
| 6 | + |
| 7 | + [Datetime]$EndDate, |
| 8 | + |
| 9 | + [Parameter(ParameterSetName = 'Query')] |
| 10 | + [String]$Query, |
| 11 | + |
| 12 | + [ValidateSet("15min", "30min", "1hour", "3hour", "6hour", "12hour", "24hour", "3day", "7day", "1month", "custom")] |
| 13 | + [String]$Range = "15min", |
| 14 | + |
| 15 | + [Int]$BatchSize = 300, |
| 16 | + |
| 17 | + [Switch]$Async |
| 18 | + ) |
| 19 | + #Check if we are logged in and have valid api creds |
| 20 | + If ($Script:LMAuth.Valid) { |
| 21 | + |
| 22 | + #Build header and uri |
| 23 | + $ResourcePath = "/log/search" |
| 24 | + |
| 25 | + #Initialize vars |
| 26 | + $Results = @() |
| 27 | + |
| 28 | + #Convert to epoch, if not set use defaults |
| 29 | + $CurrentTime = Get-Date |
| 30 | + Switch ($Range) { |
| 31 | + "15min" { |
| 32 | + $StartTime = ([DateTimeOffset]$($CurrentTime.AddMinutes(-15))).ToUnixTimeMilliseconds() |
| 33 | + $EndTime = ([DateTimeOffset]$($CurrentTime)).ToUnixTimeMilliseconds() |
| 34 | + } |
| 35 | + "30min" { |
| 36 | + $StartTime = ([DateTimeOffset]$($CurrentTime.AddMinutes(-30))).ToUnixTimeMilliseconds() |
| 37 | + $EndTime = ([DateTimeOffset]$($CurrentTime)).ToUnixTimeMilliseconds() |
| 38 | + } |
| 39 | + "1hour" { |
| 40 | + $StartTime = ([DateTimeOffset]$($CurrentTime.AddHours(-1))).ToUnixTimeMilliseconds() |
| 41 | + $EndTime = ([DateTimeOffset]$($CurrentTime)).ToUnixTimeMilliseconds() |
| 42 | + } |
| 43 | + "3hour" { |
| 44 | + $StartTime = ([DateTimeOffset]$($CurrentTime.AddHours(-3))).ToUnixTimeMilliseconds() |
| 45 | + $EndTime = ([DateTimeOffset]$($CurrentTime)).ToUnixTimeMilliseconds() |
| 46 | + } |
| 47 | + "6hour" { |
| 48 | + $StartTime = ([DateTimeOffset]$($CurrentTime.AddHours(-6))).ToUnixTimeMilliseconds() |
| 49 | + $EndTime = ([DateTimeOffset]$($CurrentTime)).ToUnixTimeMilliseconds() |
| 50 | + } |
| 51 | + "12hour" { |
| 52 | + $StartTime = ([DateTimeOffset]$($CurrentTime.AddHours(-12))).ToUnixTimeMilliseconds() |
| 53 | + $EndTime = ([DateTimeOffset]$($CurrentTime)).ToUnixTimeMilliseconds() |
| 54 | + } |
| 55 | + "24hour" { |
| 56 | + $StartTime = ([DateTimeOffset]$($CurrentTime.AddDays(-1))).ToUnixTimeMilliseconds() |
| 57 | + $EndTime = ([DateTimeOffset]$($CurrentTime)).ToUnixTimeMilliseconds() |
| 58 | + } |
| 59 | + "3day" { |
| 60 | + $StartTime = ([DateTimeOffset]$($CurrentTime.AddDays(-3))).ToUnixTimeMilliseconds() |
| 61 | + $EndTime = ([DateTimeOffset]$($CurrentTime)).ToUnixTimeMilliseconds() |
| 62 | + } |
| 63 | + "7day" { |
| 64 | + $StartTime = ([DateTimeOffset]$($CurrentTime.AddDays(-7))).ToUnixTimeMilliseconds() |
| 65 | + $EndTime = ([DateTimeOffset]$($CurrentTime)).ToUnixTimeMilliseconds() |
| 66 | + } |
| 67 | + "1month" { |
| 68 | + $StartTime = ([DateTimeOffset]$($CurrentTime.AddDays(-30))).ToUnixTimeMilliseconds() |
| 69 | + $EndTime = ([DateTimeOffset]$($CurrentTime)).ToUnixTimeMilliseconds() |
| 70 | + } |
| 71 | + "custom" { |
| 72 | + $StartTime = ([DateTimeOffset]$($StartDate)).ToUnixTimeMilliseconds() |
| 73 | + $EndTime = ([DateTimeOffset]$($EndDate)).ToUnixTimeMilliseconds() |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + # Build the base query |
| 78 | + $BaseQuery = "_partition=default" |
| 79 | + |
| 80 | + # Add additional query parameters if specified |
| 81 | + if ($Query) { |
| 82 | + $BaseQuery += " $Query" |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + # Build the payload |
| 87 | + $Data = @{ |
| 88 | + meta = @{ |
| 89 | + isAsync = $Async.ToBool() |
| 90 | + perPageCount = $BatchSize |
| 91 | + queryId = $null |
| 92 | + filter = @{ |
| 93 | + query = $BaseQuery |
| 94 | + range = @{ |
| 95 | + startAtMS = $StartTime |
| 96 | + endAtMS = $EndTime |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + $Body = $Data | ConvertTo-Json -Depth 10 |
| 103 | + |
| 104 | + Try { |
| 105 | + $Headers = New-LMHeader -Auth $Script:LMAuth -Method "POST" -ResourcePath $ResourcePath -Version 4 |
| 106 | + $Uri = "https://$($Script:LMAuth.Portal).logicmonitor.com/santaba/rest" + $ResourcePath |
| 107 | + |
| 108 | + Resolve-LMDebugInfo -Url $Uri -Headers $Headers[0] -Command $MyInvocation -Payload $Body |
| 109 | + |
| 110 | + # Issue request |
| 111 | + $Response = Invoke-RestMethod -Uri $Uri -Method "POST" -Headers $Headers[0] -WebSession $Headers[1] -Body $Body |
| 112 | + |
| 113 | + If(!$Async) { |
| 114 | + Return (Add-ObjectTypeInfo -InputObject $Response.data.byId.logs.PSObject.Properties.Value -TypeName "LogicMonitor.LMLogs") |
| 115 | + } |
| 116 | + |
| 117 | + # Handle async response |
| 118 | + If ($Response.meta.queryId -and $Response.meta.progress -eq 0) { |
| 119 | + $QueryId = $Response.meta.queryId |
| 120 | + $Complete = $false |
| 121 | + $Results = @() |
| 122 | + |
| 123 | + # Poll for completion |
| 124 | + While (!$Complete) { |
| 125 | + Start-Sleep -Seconds 2 |
| 126 | + |
| 127 | + # Build the payload |
| 128 | + $Data = @{ |
| 129 | + meta = @{ |
| 130 | + isAsync = $Async.ToBool() |
| 131 | + perPageCount = $BatchSize |
| 132 | + queryType = "raw" |
| 133 | + queryId = $QueryId |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + $Body = $Data | ConvertTo-Json -Depth 10 |
| 138 | + |
| 139 | + $Headers = New-LMHeader -Auth $Script:LMAuth -Method "POST" -ResourcePath $ResourcePath -Version 4 |
| 140 | + $Uri = "https://$($Script:LMAuth.Portal).logicmonitor.com/santaba/rest" + $ResourcePath |
| 141 | + |
| 142 | + Resolve-LMDebugInfo -Url $Uri -Headers $Headers[0] -Command $MyInvocation -Payload $Body |
| 143 | + |
| 144 | + # Issue request |
| 145 | + $Response = Invoke-RestMethod -Uri $Uri -Method "POST" -Headers $Headers[0] -WebSession $Headers[1] -Body $Body |
| 146 | + |
| 147 | + $Results += $Response.data.byId.logs.PSObject.Properties.Value |
| 148 | + |
| 149 | + If($Response.meta.progress -eq 1) { |
| 150 | + $Complete = $true |
| 151 | + } |
| 152 | + } |
| 153 | + Return (Add-ObjectTypeInfo -InputObject $Results -TypeName "LogicMonitor.LMLogs") |
| 154 | + } |
| 155 | + } |
| 156 | + Catch [Exception] { |
| 157 | + $Proceed = Resolve-LMException -LMException $PSItem |
| 158 | + If (!$Proceed) { |
| 159 | + Return |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + Else { |
| 164 | + Write-Error "Please ensure you are logged in before running any commands, use Connect-LMAccount to login and try again." |
| 165 | + } |
| 166 | +} |
0 commit comments