Created
June 5, 2022 20:24
-
-
Save noahp/46e45f458aa2151fc9f77a7ddae2502e to your computer and use it in GitHub Desktop.
Memfault Devices API python processing for last_seen field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Small python program to load json data generated by the Memfault "list Devices" | |
API: | |
https://api-docs.memfault.com/#2634f3fb-d137-42a8-b093-e05d420b8932 | |
And spit out a json object with the following format: | |
{ | |
"device_serial": "last_seen_timestamp", | |
... | |
} | |
For example, can be used in a one-liner with curl: | |
❯ curl -sSL --user ":${MEMFAULT_ORG_TOKEN}" --location --request GET --header \ | |
"Content-Type: application/json" \ | |
'https://api.memfault.com/api/v0/organizations/${MEMFAULT_ORG}/projects/${MEMFAULT_PROJ}/devices?per_page=1000' \ | |
| ./last_seen.py | jq | |
{ | |
"0025CA37A463": "2022-03-04T21:39:20.056866+00:00", | |
... | |
} | |
The resulting json object can be used for example to find devices that have not | |
been seen within a window. | |
""" | |
import json | |
import pprint | |
import sys | |
def process(infile): | |
data = json.load(infile) | |
d = {} | |
for item in data["data"]: | |
d[item["device_serial"]] = item["last_seen"] | |
print(json.dumps(d)) | |
def main(): | |
for infile in sys.argv[1:]: | |
with open(infile) as infile: | |
process(infile) | |
if not sys.stdin.isatty(): | |
process(sys.stdin) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment