mirror of
https://github.com/jimeh/dotfiles.git
synced 2026-02-19 13:46:41 +00:00
Add macos-battery-exporter script and launchd config
It reads and exports battery metrics for Prometheus.
This commit is contained in:
81
bin/macos_battery_exporter
Executable file
81
bin/macos_battery_exporter
Executable file
@@ -0,0 +1,81 @@
|
||||
#! /usr/bin/env ruby
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'json'
|
||||
|
||||
class MacOSBatteryExporter
|
||||
def export
|
||||
puts metrics
|
||||
end
|
||||
|
||||
def metrics
|
||||
<<~METRICS
|
||||
# HELP node_battery_charge_percent Battery charge percent
|
||||
# TYPE node_battery_charge_percent gauge
|
||||
node_battery_charge_percent{serial="#{battery_serial_number}"} #{charge_percentage}
|
||||
# HELP node_battery_charge_ampere Battery charge ampere
|
||||
# TYPE node_battery_charge_ampere gauge
|
||||
node_battery_charge_ampere{serial="#{battery_serial_number}"} #{charge_info['sppower_battery_current_capacity'] / 1000.0}
|
||||
# HELP node_battery_full_charge_ampere Battery full charge capacity
|
||||
# TYPE node_battery_full_charge_ampere gauge
|
||||
node_battery_full_charge_ampere{serial="#{battery_serial_number}"} #{charge_info['sppower_battery_max_capacity'] / 1000.0}
|
||||
# HELP node_battery_current_flow_ampere Current flow of ampere in (+) or out (-) of battery
|
||||
# TYPE node_battery_current_flow_ampere gauge
|
||||
node_battery_current_flow_ampere{serial="#{battery_serial_number}"} #{battery_info['sppower_current_amperage'] / 1000.0}
|
||||
# HELP node_battery_charger_connected Is charger connected?
|
||||
# TYPE node_battery_charger_connected gauge
|
||||
node_battery_charger_connected{serial="#{battery_serial_number}"} #{ac_charger_info['sppower_battery_charger_connected'] == 'TRUE' ? 1.0 : 0.0}
|
||||
# HELP node_battery_is_charging Is charger connected?
|
||||
# TYPE node_battery_is_charging gauge
|
||||
node_battery_is_charging{serial="#{battery_serial_number}"} #{ac_charger_info['sppower_battery_is_charging'] == 'TRUE' ? 1.0 : 0.0}
|
||||
# HELP node_battery_charger_watts Watts provided by charger
|
||||
# TYPE node_battery_charger_watts gauge
|
||||
node_battery_charger_watts{serial="#{battery_serial_number}"} #{ac_charger_info['sppower_ac_charger_watts'].to_f}
|
||||
# HELP node_battery_fully_charged Is battery fully charged?
|
||||
# TYPE node_battery_fully_charged gauge
|
||||
node_battery_fully_charged{serial="#{battery_serial_number}"} #{ac_charger_info['sppower_battery_fully_charged'] == 'TRUE' ? 1.0 : 0.0}
|
||||
# HELP node_battery_cycle_count Battery cycle count
|
||||
# TYPE node_battery_cycle_count counter
|
||||
node_battery_cycle_count{serial="#{battery_serial_number}"} #{health_info['sppower_battery_cycle_count']}
|
||||
METRICS
|
||||
end
|
||||
|
||||
def charge_percentage
|
||||
@charge_percentage ||=
|
||||
`pmset -g batt | grep -Eo '\\d+%' | cut -d% -f1`.strip
|
||||
end
|
||||
|
||||
def battery_serial_number
|
||||
battery_info.dig(
|
||||
'sppower_battery_model_info',
|
||||
'sppower_battery_serial_number'
|
||||
)
|
||||
end
|
||||
|
||||
def charge_info
|
||||
@charge_info ||= battery_info['sppower_battery_charge_info']
|
||||
end
|
||||
|
||||
def health_info
|
||||
@health_info ||= battery_info['sppower_battery_health_info']
|
||||
end
|
||||
|
||||
def battery_info
|
||||
@battery_info ||= power_data&.find do |v|
|
||||
v['_name'] == 'spbattery_information'
|
||||
end
|
||||
end
|
||||
|
||||
def ac_charger_info
|
||||
@ac_charger_info ||= power_data&.find do |v|
|
||||
v['_name'] == 'sppower_ac_charger_information'
|
||||
end
|
||||
end
|
||||
|
||||
def power_data
|
||||
@power_data ||= JSON.parse(`system_profiler SPPowerDataType -json`)
|
||||
&.[]('SPPowerDataType')
|
||||
end
|
||||
end
|
||||
|
||||
MacOSBatteryExporter.new.export if __FILE__ == $PROGRAM_NAME
|
||||
25
launch_agents/me.jimeh.macos-battery-exporter.plist
Normal file
25
launch_agents/me.jimeh.macos-battery-exporter.plist
Normal file
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>Disabled</key>
|
||||
<false/>
|
||||
<key>Label</key>
|
||||
<string>me.jimeh.macos-battery-exporter</string>
|
||||
<key>ProgramArguments</key>
|
||||
<array>
|
||||
<string>sh</string>
|
||||
<string>-c</string>
|
||||
<string>$HOME/.dotfiles/bin/macos_battery_exporter > $HOME/.node_metrics/battery.prom</string>
|
||||
</array>
|
||||
<key>RunAtLoad</key>
|
||||
<false/>
|
||||
<key>EnvironmentVariables</key>
|
||||
<dict>
|
||||
<key>PATH</key>
|
||||
<string>/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin</string>
|
||||
</dict>
|
||||
<key>StartInterval</key>
|
||||
<integer>30</integer>
|
||||
</dict>
|
||||
</plist>
|
||||
20
zsh/ruby.zsh
20
zsh/ruby.zsh
@@ -39,19 +39,15 @@ alias cu="bundle exec cucumber"
|
||||
alias scu="RAILS_ENV=cucumber bundle exec spring cucumber"
|
||||
alias va="vagrant"
|
||||
|
||||
alias bc="bundle check"
|
||||
# Bundler aliases
|
||||
alias bcn="bundle clean"
|
||||
alias bco="bundle console"
|
||||
|
||||
if [ -n "$BASH_VERSION" ]; then
|
||||
# Bundler aliases
|
||||
alias be="bundle exec"
|
||||
alias bl="bundle list"
|
||||
alias bp="bundle package"
|
||||
alias bo="bundle open"
|
||||
alias bu="bundle update"
|
||||
alias bi="bundle_install"
|
||||
alias bcn="bundle clean"
|
||||
fi
|
||||
alias be="bundle exec"
|
||||
alias bi="bundle_install"
|
||||
alias bl="bundle list"
|
||||
alias bo="bundle open"
|
||||
alias bp="bundle package"
|
||||
alias bu="bundle update"
|
||||
|
||||
# lazy-load rbenv
|
||||
if [ -d "$HOME/.rbenv/shims" ]; then
|
||||
|
||||
Reference in New Issue
Block a user