Find event occurrences across devices
Example: Find which devices have sent a specific event in the last hour​
Scenario
A user notices an event in the Cumulocity UI which related to an error condition. They would like to know how many other devices have also sent this event recently, to know how wide-spread the problem is.
Goal
Get a list of devices where a specific event has been sent to Cumulocity in the last hour.
Procedure
-
Get the event type of the event that you want to search for.
In this example we searching for the type
device_disconnected. -
Find how many events with this type
device_disconnectedwere created in the last 1 hour- Shell
- PowerShell (native)
- PowerShell (PSc8y)
c8y events list --type device_disconnected --dateFrom "-1h" -p 1 --withTotalPagesc8y events list --type device_disconnected --dateFrom "-1h" -p 1 --withTotalPagesGet-EventCollection -Type device_disconnected -DateFrom "-1h" -PageSize 1 -WithTotalPagesOutput| totalPages | pageSize | currentPage |
|------------|------------|-------------|
| 245 | 1 | 1 |The
totalPagesproperty on the console show the total number of events found using the search criteria. However some of these events may have come from the same devices.To get a list of the unique devices, native commands can be used.
We will set the
pageSizeparameter to something higher than thetotalPagesresponse, so that we can be sure we have all the result, then we assign the result back to a variable.We use the assigned results in
$eventsand use dot notation to reference the.source.idproperty of every item in the$eventsarray. This result is then piped to theSort-Objectcmdlet which removes- Shell
- PowerShell (native)
- PowerShell (PSc8y)
c8y events list \
--type device_disconnected \
--dateFrom "-1h" \
-p 2000 \
--select source.id -o csv | sort --unique | wc -lc8y events list `
--type device_disconnected `
--dateFrom "-1h" `
-p 2000 `
--select source.id -o csv | sort --unique | wc -l$events = Get-EventCollection -Type device_disconnected -DateFrom "-1h" -PageSize 2000
# Get unique list of ids
$events.source.id | Sort-Object -Unique | Measure-ObjectOutput147The results show that 147 devices have sent this event in the last hour.
If you want the ids or names of the devices then just leave out the last pipe
| wc -l.- Shell
- PowerShell (native)
- PowerShell (PSc8y)
c8y events list \
--type device_disconnected \
--dateFrom "-1h" \
-p 2000 \
--select "source.id,source.name" -o csv | sort --uniquec8y events list `
--type device_disconnected `
--dateFrom "-1h" `
-p 2000 `
--select "source.id,source.name" -o csv | sort --uniqueGet-EventCollection `
-Type device_disconnected `
-DateFrom "-1h" `
-PageSize 2000 `
-Select "source.id,source.name" -Output csv | sort -UniqueNotes
- The events API is restricted to a max page size of 2000 events, but you can use the
includeAllparameter to fetch all of the results as go-c8y-cli will look after the paging for you.