Forum Discussion
@Joe Williams the alerts count paging works differently to other calls. I don't know why, but it does:
https://www.logicmonitor.com/support/rest-api-developers-guide/v1/alerts/get-alerts/
QuoteNote: The response 'total' will be a negative number if there are additional alerts that satisfy the request criteria that weren't included in the request, and that "at least" that number of alerts exist. For example, if you request the first 500 alerts and you have 3000 alerts in your account, the response may include total=-1000 (i.e. you have at least 1000 alerts, but you didn't ask for them all).
Therefore, your recursion to fetch additional alerts should run if the 'total' is a negative number.
Something a bit like this (NOT complete code):
// Enclosure to GET alerts for a Group
def GETGroupAlerts(groupWildvalue,filterString='',offsetPassed=0)
{
/*
... define url including size, fields, filters etc and make API call for alerts.
Initial offset will be zero as per default passed parameter.
Hardcode size to be 1000 as this is the maximum number of results the API will return from one call.
*/
/*
... actually use the above to make the API call...
*/
// Parse the API response and put the results into a map, something like:
if (code == 200)
{
// 200 response code (OK), meaning credentials are good. Slurp...
def allResponse = new JsonSlurper().parseText(responseBody);
def alertCount = allResponse.total;
// LOOP THROUGH RESULTS:
allResponse.items.each
{ alert ->
alertsMap << [
(alert.id) : [
severity : alert.severity,
sdted : alert.sdted,
acked : alert.acked,
],
];
}
if(alertCount < 0)
{
/*
// DEBUG
println 'we ought to go get some more...';
println 'alertCount: ' + alertCount;
println 'size: ' + size;
println 'offset: ' + offset;
println 'size + offset: ' + (size + offset);
// END DEBUG
/**/
alertsMap << GETGroupAlerts(groupWildvalue,filterString,(size + offset));
}
}
return alertsMap;
}
//----------------------------------------------------------------------------------------
Whenever you finally get a response with a positive 'total' number, you're at the end of the alerts list, the recursion will stop, and you'll have one alertsMap object with all the alerts in it, which you can then do whatever you like with.
Note the above bits of code are from a script that uses the API v2 data structure. Note also, the hacked out chunks above are nothing like a complete script.
Note graph values match Alerts tab values:
Related Content
- 2 years ago
- 3 months ago
- 2 years ago
- 2 years ago