Skip to content

Instantly share code, notes, and snippets.

@berkus
Last active March 19, 2018 19:46
Show Gist options
  • Select an option

  • Save berkus/a63efa6206272292ae19df87b2ee87eb to your computer and use it in GitHub Desktop.

Select an option

Save berkus/a63efa6206272292ae19df87b2ee87eb to your computer and use it in GitHub Desktop.
Track connected bluetooth devices Battery status
#!/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