mirror of
https://github.com/jimeh/macos-battery-exporter.git
synced 2026-02-19 09:26:40 +00:00
Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
39e3b69c82 | ||
|
148994ca50
|
|||
|
436e4a4b01
|
|||
|
24bd7d6af3
|
|||
|
|
3791f9772c | ||
|
bf0da03a3f
|
|||
|
4a0aa2daba
|
|||
|
2ac3ecb555
|
|||
|
597779d04f
|
|||
|
|
329a991bb7 | ||
|
9a168f9ff9
|
|||
|
|
cdac9396fc | ||
|
993b036d99
|
2
.github/.release-please-manifest.json
vendored
2
.github/.release-please-manifest.json
vendored
@@ -1,3 +1,3 @@
|
||||
{
|
||||
".": "0.0.1"
|
||||
".": "0.0.5"
|
||||
}
|
||||
|
||||
20
.github/workflows/ci.yml
vendored
20
.github/workflows/ci.yml
vendored
@@ -16,6 +16,24 @@ jobs:
|
||||
- name: Print version
|
||||
run: |
|
||||
./bin/macos-battery-exporter -v
|
||||
./bin/macos-battery-exporter
|
||||
|
||||
lint:
|
||||
name: Lint
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: "1.21"
|
||||
cache: false
|
||||
- name: golangci-lint
|
||||
uses: golangci/golangci-lint-action@v3
|
||||
with:
|
||||
version: v1.55
|
||||
env:
|
||||
VERBOSE: "true"
|
||||
GOOS: "darwin"
|
||||
|
||||
release-please:
|
||||
runs-on: ubuntu-latest
|
||||
@@ -35,7 +53,7 @@ jobs:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- uses: actions/setup-go@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: "1.21"
|
||||
- name: Run GoReleaser
|
||||
|
||||
87
.golangci.yml
Normal file
87
.golangci.yml
Normal file
@@ -0,0 +1,87 @@
|
||||
linters-settings:
|
||||
funlen:
|
||||
lines: 300
|
||||
statements: 450
|
||||
golint:
|
||||
min-confidence: 0
|
||||
govet:
|
||||
enable-all: true
|
||||
disable:
|
||||
- fieldalignment
|
||||
- shadow
|
||||
lll:
|
||||
line-length: 80
|
||||
tab-width: 4
|
||||
maligned:
|
||||
suggest-new: true
|
||||
misspell:
|
||||
locale: US
|
||||
|
||||
linters:
|
||||
disable-all: true
|
||||
enable:
|
||||
- asciicheck
|
||||
- bodyclose
|
||||
- durationcheck
|
||||
- errcheck
|
||||
- errorlint
|
||||
- exhaustive
|
||||
- exportloopref
|
||||
- funlen
|
||||
- gochecknoinits
|
||||
- goconst
|
||||
- gocritic
|
||||
- godot
|
||||
- goimports
|
||||
- gomodguard
|
||||
- goprintffuncname
|
||||
- gosec
|
||||
- gosimple
|
||||
- govet
|
||||
- importas
|
||||
- ineffassign
|
||||
- lll
|
||||
- misspell
|
||||
- nakedret
|
||||
- nilerr
|
||||
- noctx
|
||||
- nolintlint
|
||||
- prealloc
|
||||
- predeclared
|
||||
- revive
|
||||
- staticcheck
|
||||
- typecheck
|
||||
- unconvert
|
||||
- unparam
|
||||
- unused
|
||||
- whitespace
|
||||
|
||||
issues:
|
||||
exclude:
|
||||
- Using the variable on range scope `tt` in function literal
|
||||
- Using the variable on range scope `tc` in function literal
|
||||
exclude-rules:
|
||||
- path: "_test\\.go"
|
||||
linters:
|
||||
- funlen
|
||||
- dupl
|
||||
- source: "^//go:generate "
|
||||
linters:
|
||||
- lll
|
||||
- source: "`env:"
|
||||
linters:
|
||||
- lll
|
||||
- source: "`json:"
|
||||
linters:
|
||||
- lll
|
||||
- source: "`xml:"
|
||||
linters:
|
||||
- lll
|
||||
- source: "`yaml:"
|
||||
linters:
|
||||
- lll
|
||||
|
||||
run:
|
||||
timeout: 2m
|
||||
allow-parallel-runners: true
|
||||
modules-download-mode: readonly
|
||||
@@ -39,7 +39,7 @@ brews:
|
||||
license: "MIT"
|
||||
skip_upload: auto
|
||||
service: |
|
||||
run [bin/"macos-battery-exporter"]
|
||||
run [bin/"macos-battery-exporter", "-s"]
|
||||
test: |
|
||||
system "#{bin}/macos-battery-exporter -v"
|
||||
repository:
|
||||
|
||||
28
CHANGELOG.md
28
CHANGELOG.md
@@ -1,5 +1,33 @@
|
||||
# Changelog
|
||||
|
||||
## [0.0.5](https://github.com/jimeh/macos-battery-exporter/compare/v0.0.4...v0.0.5) (2023-12-16)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* **go:** rename prom package to suitable prombat ([436e4a4](https://github.com/jimeh/macos-battery-exporter/commit/436e4a4b01d96654b7012f795f2d305ca4084681))
|
||||
|
||||
## [0.0.4](https://github.com/jimeh/macos-battery-exporter/compare/v0.0.3...v0.0.4) (2023-12-16)
|
||||
|
||||
|
||||
### Continuous Integration
|
||||
|
||||
* **lint:** setup golangci-lint ([2ac3ecb](https://github.com/jimeh/macos-battery-exporter/commit/2ac3ecb555e0f6eea369516328f1f03da7d61251))
|
||||
|
||||
## [0.0.3](https://github.com/jimeh/macos-battery-exporter/compare/v0.0.2...v0.0.3) (2023-12-16)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **package:** resolve issue with running as a homebrew service ([9a168f9](https://github.com/jimeh/macos-battery-exporter/commit/9a168f9ff918f6539ca85d43202759197ed952b3))
|
||||
|
||||
## [0.0.2](https://github.com/jimeh/macos-battery-exporter/compare/v0.0.1...v0.0.2) (2023-12-16)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **battery:** find ioreg executable more reliably ([993b036](https://github.com/jimeh/macos-battery-exporter/commit/993b036d99362b6bebd36545fc34d325863421d5))
|
||||
|
||||
## 0.0.1 (2023-12-16)
|
||||
|
||||
|
||||
|
||||
62
README.md
62
README.md
@@ -10,6 +10,7 @@
|
||||
|
||||
<p align="center">
|
||||
<a href="https://github.com/jimeh/macos-battery-exporter/releases"><img src="https://img.shields.io/github/v/tag/jimeh/macos-battery-exporter?label=release" alt="GitHub tag (latest SemVer)"></a>
|
||||
<a href="https://pkg.go.dev/github.com/jimeh/macos-battery-exporter"><img src="https://img.shields.io/badge/%E2%80%8B-reference-387b97.svg?logo=go&logoColor=white" alt="Go Reference"></a>
|
||||
<a href="https://github.com/jimeh/macos-battery-exporter/issues"><img src="https://img.shields.io/github/issues-raw/jimeh/macos-battery-exporter.svg?style=flat&logo=github&logoColor=white" alt="GitHub issues"></a>
|
||||
<a href="https://github.com/jimeh/macos-battery-exporter/pulls"><img src="https://img.shields.io/github/issues-pr-raw/jimeh/macos-battery-exporter.svg?style=flat&logo=github&logoColor=white" alt="GitHub pull requests"></a>
|
||||
<a href="https://github.com/jimeh/macos-battery-exporter/blob/main/LICENSE"><img src="https://img.shields.io/github/license/jimeh/macos-battery-exporter.svg?style=flat" alt="License Status"></a>
|
||||
@@ -19,6 +20,40 @@ A Prometheus exporter for macOS which exposes most useful details available from
|
||||
`ioreg`. Includes a lot more details than what `node_exporter` supports via it's
|
||||
`node_power_supply_*` metrics.
|
||||
|
||||
## Installation
|
||||
|
||||
### Manually
|
||||
|
||||
Binary releases are available on the
|
||||
[Releases](https://github.com/jimeh/macos-battery-exporter/releases) page.
|
||||
|
||||
### Homebrew
|
||||
|
||||
You can install it from the
|
||||
[`jimeh/macos-battery-exporter`](https://github.com/jimeh/homebrew-macos-battery-exporter)
|
||||
Tap:
|
||||
|
||||
```bash
|
||||
brew install jimeh/macos-battery-exporter/macos-battery-exporter
|
||||
```
|
||||
|
||||
#### Service
|
||||
|
||||
The homebrew formula has a service configuration, which can be started with:
|
||||
|
||||
```bash
|
||||
brew services start macos-battery-exporter
|
||||
```
|
||||
|
||||
After which battery metrics are available on
|
||||
[`http://localhost:9108/metrics`](http://localhost:9108/metrics).
|
||||
|
||||
### Go
|
||||
|
||||
```bash
|
||||
go install github.com/jimeh/macos-battery-exporter@latest
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Help
|
||||
@@ -28,7 +63,7 @@ macos-battery-exporter -h
|
||||
```
|
||||
|
||||
```
|
||||
Usage of bin/macos-battery-exporter:
|
||||
Usage of macos-battery-exporter:
|
||||
-b string
|
||||
Bind address to run server on (default "127.0.0.1")
|
||||
-l string
|
||||
@@ -55,19 +90,22 @@ macos-battery-exporter
|
||||
macos_battery_cell_disconnect_count{serial="ZTMDHJEZ8JKMYVAJKU"} 0
|
||||
# HELP macos_battery_charge_rate_amps Current charge rate in Ah.
|
||||
# TYPE macos_battery_charge_rate_amps gauge
|
||||
macos_battery_charge_rate_amps{serial="ZTMDHJEZ8JKMYVAJKU"} -0.927
|
||||
macos_battery_charge_rate_amps{serial="ZTMDHJEZ8JKMYVAJKU"} -1.004
|
||||
# HELP macos_battery_charge_rate_watts Current charge rate in Wh.
|
||||
# TYPE macos_battery_charge_rate_watts gauge
|
||||
macos_battery_charge_rate_watts{serial="ZTMDHJEZ8JKMYVAJKU"} -10.297116
|
||||
macos_battery_charge_rate_watts{serial="ZTMDHJEZ8JKMYVAJKU"} -12.712648
|
||||
# HELP macos_battery_count Total number of batteries.
|
||||
# TYPE macos_battery_count gauge
|
||||
macos_battery_count 1
|
||||
# HELP macos_battery_current_capacity_amps Current charge capacity in Ah.
|
||||
# TYPE macos_battery_current_capacity_amps gauge
|
||||
macos_battery_current_capacity_amps{serial="ZTMDHJEZ8JKMYVAJKU"} 1.127
|
||||
macos_battery_current_capacity_amps{serial="ZTMDHJEZ8JKMYVAJKU"} 5.39
|
||||
# HELP macos_battery_current_capacity_watts Current charge capacity in Wh.
|
||||
# TYPE macos_battery_current_capacity_watts gauge
|
||||
macos_battery_current_capacity_watts{serial="ZTMDHJEZ8JKMYVAJKU"} 12.518716
|
||||
macos_battery_current_capacity_watts{serial="ZTMDHJEZ8JKMYVAJKU"} 68.24818
|
||||
# HELP macos_battery_current_percentage Current battery charge percentage.
|
||||
# TYPE macos_battery_current_percentage gauge
|
||||
macos_battery_current_percentage{serial="ZTMDHJEZ8JKMYVAJKU"} 18
|
||||
macos_battery_current_percentage{serial="ZTMDHJEZ8JKMYVAJKU"} 91
|
||||
# HELP macos_battery_cycle_count Current battery cycle count.
|
||||
# TYPE macos_battery_cycle_count counter
|
||||
macos_battery_cycle_count{serial="ZTMDHJEZ8JKMYVAJKU"} 15
|
||||
@@ -76,7 +114,7 @@ macos_battery_cycle_count{serial="ZTMDHJEZ8JKMYVAJKU"} 15
|
||||
macos_battery_design_capacity_amps{serial="ZTMDHJEZ8JKMYVAJKU"} 6.249
|
||||
# HELP macos_battery_design_capacity_watts Design capacity in Wh.
|
||||
# TYPE macos_battery_design_capacity_watts gauge
|
||||
macos_battery_design_capacity_watts{serial="ZTMDHJEZ8JKMYVAJKU"} 69.413892
|
||||
macos_battery_design_capacity_watts{serial="ZTMDHJEZ8JKMYVAJKU"} 79.124838
|
||||
# HELP macos_battery_fully_charged Indicates if the battery is fully charged.
|
||||
# TYPE macos_battery_fully_charged gauge
|
||||
macos_battery_fully_charged{serial="ZTMDHJEZ8JKMYVAJKU"} 0
|
||||
@@ -91,19 +129,19 @@ macos_battery_info{built_in="true",device_name="ayzo3hgs",serial="ZTMDHJEZ8JKMYV
|
||||
macos_battery_is_charging{serial="ZTMDHJEZ8JKMYVAJKU"} 0
|
||||
# HELP macos_battery_max_capacity_amps Design capacity in Ah.
|
||||
# TYPE macos_battery_max_capacity_amps gauge
|
||||
macos_battery_max_capacity_amps{serial="ZTMDHJEZ8JKMYVAJKU"} 6.262
|
||||
macos_battery_max_capacity_amps{serial="ZTMDHJEZ8JKMYVAJKU"} 6.271
|
||||
# HELP macos_battery_max_capacity_watts Design capacity in Wh.
|
||||
# TYPE macos_battery_max_capacity_watts gauge
|
||||
macos_battery_max_capacity_watts{serial="ZTMDHJEZ8JKMYVAJKU"} 69.558296
|
||||
macos_battery_max_capacity_watts{serial="ZTMDHJEZ8JKMYVAJKU"} 79.403402
|
||||
# HELP macos_battery_temperature_celsius Current battery temperature in °C.
|
||||
# TYPE macos_battery_temperature_celsius gauge
|
||||
macos_battery_temperature_celsius{serial="ZTMDHJEZ8JKMYVAJKU"} 30.47
|
||||
macos_battery_temperature_celsius{serial="ZTMDHJEZ8JKMYVAJKU"} 30.53
|
||||
# HELP macos_battery_time_remaining_seconds Estimated time remaining until battery is fully charged or discharged.
|
||||
# TYPE macos_battery_time_remaining_seconds gauge
|
||||
macos_battery_time_remaining_seconds{serial="ZTMDHJEZ8JKMYVAJKU"} 3540
|
||||
macos_battery_time_remaining_seconds{serial="ZTMDHJEZ8JKMYVAJKU"} 14040
|
||||
# HELP macos_battery_voltage_volts Current battery voltage in V.
|
||||
# TYPE macos_battery_voltage_volts gauge
|
||||
macos_battery_voltage_volts{serial="ZTMDHJEZ8JKMYVAJKU"} 11.108
|
||||
macos_battery_voltage_volts{serial="ZTMDHJEZ8JKMYVAJKU"} 12.662
|
||||
```
|
||||
|
||||
### Write to File
|
||||
|
||||
@@ -15,12 +15,12 @@ type Battery struct {
|
||||
// BuiltIn indicates if the battery is built-in or not.
|
||||
BuiltIn bool
|
||||
|
||||
// ChargeRateAmps is the current charge rate in mAh. Negative values indicate
|
||||
// discharge, positive values indicate charging.
|
||||
// ChargeRateAmps is the current charge rate in mAh. Negative values
|
||||
// indicate discharge, positive values indicate charging.
|
||||
ChargeRateAmps int64
|
||||
|
||||
// ChargeRateWatts is the current charge rate in mWh. Negative values indicate
|
||||
// discharge, positive values indicate charging.
|
||||
// ChargeRateWatts is the current charge rate in mWh. Negative values
|
||||
// indicate discharge, positive values indicate charging.
|
||||
ChargeRateWatts float64
|
||||
|
||||
// CurrentCapacityAmps is the current battery capacity in mAh.
|
||||
@@ -76,6 +76,7 @@ type Battery struct {
|
||||
func newBattery(b *batteryRaw) *Battery {
|
||||
volts := float64(b.Voltage) / 1000
|
||||
|
||||
//nolint:lll
|
||||
return &Battery{
|
||||
BatteryCellDisconnectCount: b.BatteryCellDisconnectCount,
|
||||
BuiltIn: b.BuiltIn,
|
||||
@@ -101,29 +102,31 @@ func newBattery(b *batteryRaw) *Battery {
|
||||
}
|
||||
|
||||
func Get() (*Battery, error) {
|
||||
batteriesRaw, err := getAllRaw()
|
||||
raw, err := getAllRaw()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return newBattery(batteriesRaw[0]), nil
|
||||
return newBattery(raw[0]), nil
|
||||
}
|
||||
|
||||
func GetAll() ([]*Battery, error) {
|
||||
batteriesRaw, err := getAllRaw()
|
||||
raw, err := getAllRaw()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
batteries := []*Battery{}
|
||||
for _, b := range batteriesRaw {
|
||||
for _, b := range raw {
|
||||
batteries = append(batteries, newBattery(b))
|
||||
}
|
||||
|
||||
return batteries, nil
|
||||
}
|
||||
|
||||
// roundTo rounds a float64 to 'places' decimal places
|
||||
// roundTo rounds a float64 to 'places' decimal places.
|
||||
//
|
||||
//nolint:unparam
|
||||
func roundTo(value float64, places int) float64 {
|
||||
shift := math.Pow(10, float64(places))
|
||||
return math.Round(value*shift) / shift
|
||||
|
||||
@@ -34,7 +34,15 @@ type batteryRaw struct {
|
||||
}
|
||||
|
||||
func getAllRaw() ([]*batteryRaw, error) {
|
||||
b, err := exec.Command("ioreg", "-ra", "-c", "AppleSmartBattery").Output()
|
||||
ioreg, err := exec.LookPath("ioreg")
|
||||
if err != nil {
|
||||
ioreg, err = exec.LookPath("/usr/sbin/ioreg")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
b, err := exec.Command(ioreg, "-ra", "-c", "AppleSmartBattery").Output()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
14
main.go
14
main.go
@@ -10,7 +10,7 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/jimeh/macos-battery-exporter/prom"
|
||||
"github.com/jimeh/macos-battery-exporter/prombat"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/common/expfmt"
|
||||
)
|
||||
@@ -28,7 +28,7 @@ var (
|
||||
)
|
||||
portFlag = flag.Int("p", 9108, "Port to run server on")
|
||||
namespaceFlag = flag.String(
|
||||
"n", prom.DefaultNamespace, "Namespace for metrics",
|
||||
"n", prombat.DefaultNamespace, "Namespace for metrics",
|
||||
)
|
||||
logLevelFlag = flag.String("l", "info", "Log level")
|
||||
versionFlag = flag.Bool("v", false, "Print version and exit")
|
||||
@@ -54,12 +54,12 @@ func mainE() error {
|
||||
}
|
||||
|
||||
if *serverFlag {
|
||||
opts := prom.ServerOptions{
|
||||
opts := prombat.ServerOptions{
|
||||
Bind: *bindFlag,
|
||||
Port: *portFlag,
|
||||
}
|
||||
|
||||
return prom.RunServer(
|
||||
return prombat.RunServer(
|
||||
*namespaceFlag,
|
||||
prometheus.DefaultRegisterer.(*prometheus.Registry),
|
||||
opts,
|
||||
@@ -67,7 +67,7 @@ func mainE() error {
|
||||
}
|
||||
|
||||
registry := prometheus.NewRegistry()
|
||||
err = registry.Register(prom.NewCollector(*namespaceFlag))
|
||||
err = registry.Register(prombat.NewCollector(*namespaceFlag))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -88,10 +88,10 @@ func mainE() error {
|
||||
|
||||
if *outputFlag != "" {
|
||||
return writeToFile(sb.String(), *outputFlag)
|
||||
} else {
|
||||
fmt.Print(sb.String())
|
||||
}
|
||||
|
||||
fmt.Print(sb.String())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//go:build darwin
|
||||
|
||||
package prom
|
||||
package prombat
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
@@ -367,7 +367,9 @@ func boolToFloat64(b bool) float64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// roundTo rounds a float64 to 'places' decimal places
|
||||
// roundTo rounds a float64 to 'places' decimal places.
|
||||
//
|
||||
//nolint:unparam
|
||||
func roundTo(value float64, places int) float64 {
|
||||
shift := math.Pow(10, float64(places))
|
||||
return math.Round(value*shift) / shift
|
||||
@@ -1,6 +1,6 @@
|
||||
//go:build darwin
|
||||
|
||||
package prom
|
||||
package prombat
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -14,23 +14,17 @@ import (
|
||||
|
||||
const DefaultNamespace = "macos"
|
||||
|
||||
type Registry interface {
|
||||
prometheus.Registerer
|
||||
prometheus.Gatherer
|
||||
}
|
||||
|
||||
type ServerOptions struct {
|
||||
Bind string
|
||||
Port int
|
||||
ReadTimeout time.Duration
|
||||
WriteTimeout time.Duration
|
||||
IdleTimeout time.Duration
|
||||
Logger *slog.Logger
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
*http.Server
|
||||
registry Registry
|
||||
registry *prometheus.Registry
|
||||
mux *http.ServeMux
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user