Last active
March 19, 2018 19:46
-
-
Save berkus/a63efa6206272292ae19df87b2ee87eb to your computer and use it in GitHub Desktop.
Track connected bluetooth devices Battery status
This file contains hidden or 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
| #!/bin/bash | |
| # Format data suitable for DataDog metric and submit it | |
| data=$(system_profiler -xml SPBluetoothDataType 2>/dev/null \ | |
| | plutil -extract 0._items.0.device_title xml1 -o - - \ | |
| | plutil -convert json -o - - \ | |
| | jq -c --arg hostname $(hostname) --arg timestamp $(date +%s) 'map(to_entries[0]) | |
| | map (.key as $k | .value | select(.device_isconnected=="attrib_Yes" and .device_ispaired=="attrib_Yes") | |
| | { battery: .device_batteryPercent, name: $k }) | |
| | map({metric: "battery.level", tags: [.name], host: $hostname, points:[[($timestamp|tonumber), (.battery|rtrimstr("%")|tonumber)]]}) | |
| | {series: .}') | |
| curl -X POST -H "Content-type: application/json" -d "$data" "https://app.datadoghq.com/api/v1/series?api_key=$YOUR_API_KEY" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment