Forum Discussion

Cole_McDonald's avatar
Cole_McDonald
Icon for Professor rankProfessor
3 months ago
Solved

RestAPI Alerts access to ExternalTicketID

Has anyone figured out how to get at the ##ExternalTicketID## programatically at all?  Not having access to that is driving me to distraction.  It's in the DB somewhere, but we can't get to it to hel...
  • Dave_Lee's avatar
    Dave_Lee
    2 months ago

    I'm using X-Version=3 as well.  Looks like you're using PowerShell, here's a snippet that's working for me.

    $company = "your-lm-company"  # Replace with your LogicMonitor company name
    $bearerToken = "your-lm-bearer-token" # replace with your LM bearer token
    $daysAgo = 2  # Number of days to look back for alerts
    
    # calculates the epoch time for the start of the query
    $startEpoch = (Get-Date (Get-Date).AddDays(-$daysAgo) -UFormat %s) 
    
    # query string includes the ##EXTERNALTICKETID# custom column and filters for alerts created after the startEpoch.  
    # This Will get the first 1000 results, you'll need to put a loop in to handle paging if you expect  more than that.
    $queryString = ('?customColumns=%23%23EXTERNALTICKETID%23%23&size=1000&offset=0&filter=startEpoch>:' + $startEpoch)
    
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Authorization","Bearer $bearerToken")
    $headers.Add("X-Version",'3')
    $headers.Add("Content-Type",'application/json')
    
    $alerts = (Invoke-RestMethod -Uri "https://$company.logicmonitor.com/santaba/rest/alert/alerts$queryString" -Method "GET" -Headers $headers).items
    
    # Filter the alerts to only include those that went to a rule where the name includes "Service Now" and that were not suppressed (e.g. not SDT at the time)
    # Does a bit of manipulation to extract the ServiceNow Incident ID from the custom column ##EXTERNALTICKETID##
    $alerts | where-object {$_.rule -like "*Service Now*" -And !($_.suppressor)} | Select-Object `
        @{Name="Alert ID";Expression={$_.id}}, `
        @{Name="InternalId";Expression={$_.InternalId}}, `
        @{Name="External_Ticket_ID_RAW";Expression={$_.customColumns.'##EXTERNALTICKETID##'}}, `
        @{Name="SNOWIncidentId";Expression={$_.customColumns.'##EXTERNALTICKETID##'.split(':')[1].trim()}}, `
        @{Name="SNOWLinks";Expression={$_.alertExternalTicketUrl.'servicenowIncidentLinks'}}

    You'll need to change (or remove) the filter I've put in for $_.rule -like "*Service Now*" to match whatever your rules are.  You should get something like the output below.

    Alert ID               : DS8871637
    InternalId             : LMD33542811
    External_Ticket_ID_RAW : ServiceNow xxxx : INC5147309
    SNOWIncidentId         : INC5147309
    SNOWLinks              : @{INC5147309=https://xxxxx.service-now.com/now/nav/ui/classic/params/target/incident.do%3Fsys_id%3D82fa6f601bb22a507f1da608b04bcbb9}
    
    Alert ID               : DS8871639
    InternalId             : LMD33542820
    External_Ticket_ID_RAW : ServiceNow xxxxx : INC5147311
    SNOWIncidentId         : INC5147311
    SNOWLinks              : @{INC5147311=https://xxxxx.service-now.com/now/nav/ui/classic/params/target/incident.do%3Fsys_id%3Dab1b27241b766ad006c94043b24bcb1e}
    
    Alert ID               : DS8871640
    InternalId             : LMD33542817
    External_Ticket_ID_RAW : ServiceNow xxxxx : INC5147312
    SNOWIncidentId         : INC5147312
    SNOWLinks              : @{INC5147312=https://xxxxx.service-now.com/now/nav/ui/classic/params/target/incident.do%3Fsys_id%3Dfd7b6b241bb22a507f1da608b04bcb49}