mirror of
https://github.com/jimeh/macos-battery-exporter.git
synced 2026-02-19 09:26:40 +00:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
67890d936c
|
|||
|
|
a1d4f70d98 | ||
|
7b2e98e150
|
|||
|
3c272d6d3d
|
|||
|
dc62db2ace
|
|||
|
|
39e3b69c82 | ||
|
148994ca50
|
|||
|
436e4a4b01
|
|||
|
24bd7d6af3
|
|||
|
|
3791f9772c | ||
|
bf0da03a3f
|
|||
|
4a0aa2daba
|
|||
|
2ac3ecb555
|
|||
|
597779d04f
|
2
.github/.release-please-manifest.json
vendored
2
.github/.release-please-manifest.json
vendored
@@ -1,3 +1,3 @@
|
|||||||
{
|
{
|
||||||
".": "0.0.3"
|
".": "0.0.6"
|
||||||
}
|
}
|
||||||
|
|||||||
20
.github/workflows/ci.yml
vendored
20
.github/workflows/ci.yml
vendored
@@ -16,6 +16,24 @@ jobs:
|
|||||||
- name: Print version
|
- name: Print version
|
||||||
run: |
|
run: |
|
||||||
./bin/macos-battery-exporter -v
|
./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:
|
release-please:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@@ -35,7 +53,7 @@ jobs:
|
|||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
- uses: actions/setup-go@v4
|
- uses: actions/setup-go@v5
|
||||||
with:
|
with:
|
||||||
go-version: "1.21"
|
go-version: "1.21"
|
||||||
- name: Run GoReleaser
|
- 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
|
||||||
@@ -44,6 +44,6 @@ brews:
|
|||||||
system "#{bin}/macos-battery-exporter -v"
|
system "#{bin}/macos-battery-exporter -v"
|
||||||
repository:
|
repository:
|
||||||
owner: jimeh
|
owner: jimeh
|
||||||
name: homebrew-macos-battery-exporter
|
name: homebrew-tap
|
||||||
branch: main
|
branch: main
|
||||||
token: "{{ .Env.BREW_TAP_TOKEN }}"
|
token: "{{ .Env.BREW_TAP_TOKEN }}"
|
||||||
|
|||||||
21
CHANGELOG.md
21
CHANGELOG.md
@@ -1,5 +1,26 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## [0.0.6](https://github.com/jimeh/macos-battery-exporter/compare/v0.0.5...v0.0.6) (2023-12-21)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* **output:** write log output to stderr by default instead of stdout ([7b2e98e](https://github.com/jimeh/macos-battery-exporter/commit/7b2e98e150f9ee6d630023879395df0912047667))
|
||||||
|
|
||||||
|
## [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)
|
## [0.0.3](https://github.com/jimeh/macos-battery-exporter/compare/v0.0.2...v0.0.3) (2023-12-16)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
88
README.md
88
README.md
@@ -10,14 +10,48 @@
|
|||||||
|
|
||||||
<p align="center">
|
<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://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/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/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>
|
<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>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
A Prometheus exporter for macOS which exposes most useful details available from
|
A Prometheus exporter for macOS battery information, exposing wide range of
|
||||||
`ioreg`. Includes a lot more details than what `node_exporter` supports via it's
|
details available from `ioreg` about batteries. Includes a lot more details than
|
||||||
`node_power_supply_*` metrics.
|
what `node_exporter` supports via its `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/tap`](https://github.com/jimeh/homebrew-tap)
|
||||||
|
Tap:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
brew install jimeh/tap/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
|
## Usage
|
||||||
|
|
||||||
@@ -28,19 +62,22 @@ macos-battery-exporter -h
|
|||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Usage of bin/macos-battery-exporter:
|
usage: macos-battery-exporter [<options>]
|
||||||
|
|
||||||
-b string
|
-b string
|
||||||
Bind address to run server on (default "127.0.0.1")
|
Bind address to run server on (default "127.0.0.1")
|
||||||
|
-d string
|
||||||
|
Log output device (stderr or stdout) (default "stderr")
|
||||||
-l string
|
-l string
|
||||||
Log level (default "info")
|
Log level (default "info")
|
||||||
-n string
|
-n string
|
||||||
Namespace for metrics (default "macos")
|
Namespace for metrics (default "macos")
|
||||||
-o string
|
-o string
|
||||||
Output file to write to in Prometheus format
|
Output file to write to in Prometheus format
|
||||||
-p int
|
-p int
|
||||||
Port to run server on (default 9108)
|
Port to run server on (default 9108)
|
||||||
-s Run as a Prometheus metrics server
|
-s Run as a Prometheus metrics server
|
||||||
-v Print version and exit
|
-v Print version and exit
|
||||||
```
|
```
|
||||||
|
|
||||||
### Print to STDOUT
|
### Print to STDOUT
|
||||||
@@ -55,19 +92,22 @@ macos-battery-exporter
|
|||||||
macos_battery_cell_disconnect_count{serial="ZTMDHJEZ8JKMYVAJKU"} 0
|
macos_battery_cell_disconnect_count{serial="ZTMDHJEZ8JKMYVAJKU"} 0
|
||||||
# HELP macos_battery_charge_rate_amps Current charge rate in Ah.
|
# HELP macos_battery_charge_rate_amps Current charge rate in Ah.
|
||||||
# TYPE macos_battery_charge_rate_amps gauge
|
# 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.
|
# HELP macos_battery_charge_rate_watts Current charge rate in Wh.
|
||||||
# TYPE macos_battery_charge_rate_watts gauge
|
# 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.
|
# HELP macos_battery_current_capacity_amps Current charge capacity in Ah.
|
||||||
# TYPE macos_battery_current_capacity_amps gauge
|
# 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.
|
# HELP macos_battery_current_capacity_watts Current charge capacity in Wh.
|
||||||
# TYPE macos_battery_current_capacity_watts gauge
|
# 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.
|
# HELP macos_battery_current_percentage Current battery charge percentage.
|
||||||
# TYPE macos_battery_current_percentage gauge
|
# 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.
|
# HELP macos_battery_cycle_count Current battery cycle count.
|
||||||
# TYPE macos_battery_cycle_count counter
|
# TYPE macos_battery_cycle_count counter
|
||||||
macos_battery_cycle_count{serial="ZTMDHJEZ8JKMYVAJKU"} 15
|
macos_battery_cycle_count{serial="ZTMDHJEZ8JKMYVAJKU"} 15
|
||||||
@@ -76,7 +116,7 @@ macos_battery_cycle_count{serial="ZTMDHJEZ8JKMYVAJKU"} 15
|
|||||||
macos_battery_design_capacity_amps{serial="ZTMDHJEZ8JKMYVAJKU"} 6.249
|
macos_battery_design_capacity_amps{serial="ZTMDHJEZ8JKMYVAJKU"} 6.249
|
||||||
# HELP macos_battery_design_capacity_watts Design capacity in Wh.
|
# HELP macos_battery_design_capacity_watts Design capacity in Wh.
|
||||||
# TYPE macos_battery_design_capacity_watts gauge
|
# 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.
|
# HELP macos_battery_fully_charged Indicates if the battery is fully charged.
|
||||||
# TYPE macos_battery_fully_charged gauge
|
# TYPE macos_battery_fully_charged gauge
|
||||||
macos_battery_fully_charged{serial="ZTMDHJEZ8JKMYVAJKU"} 0
|
macos_battery_fully_charged{serial="ZTMDHJEZ8JKMYVAJKU"} 0
|
||||||
@@ -91,19 +131,19 @@ macos_battery_info{built_in="true",device_name="ayzo3hgs",serial="ZTMDHJEZ8JKMYV
|
|||||||
macos_battery_is_charging{serial="ZTMDHJEZ8JKMYVAJKU"} 0
|
macos_battery_is_charging{serial="ZTMDHJEZ8JKMYVAJKU"} 0
|
||||||
# HELP macos_battery_max_capacity_amps Design capacity in Ah.
|
# HELP macos_battery_max_capacity_amps Design capacity in Ah.
|
||||||
# TYPE macos_battery_max_capacity_amps gauge
|
# 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.
|
# HELP macos_battery_max_capacity_watts Design capacity in Wh.
|
||||||
# TYPE macos_battery_max_capacity_watts gauge
|
# 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.
|
# HELP macos_battery_temperature_celsius Current battery temperature in °C.
|
||||||
# TYPE macos_battery_temperature_celsius gauge
|
# 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.
|
# HELP macos_battery_time_remaining_seconds Estimated time remaining until battery is fully charged or discharged.
|
||||||
# TYPE macos_battery_time_remaining_seconds gauge
|
# 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.
|
# HELP macos_battery_voltage_volts Current battery voltage in V.
|
||||||
# TYPE macos_battery_voltage_volts gauge
|
# 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
|
### Write to File
|
||||||
@@ -121,3 +161,7 @@ macos-battery-exporter -s
|
|||||||
```bash
|
```bash
|
||||||
curl http://localhost:9108/metrics
|
curl http://localhost:9108/metrics
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
[MIT](https://github.com/jimeh/macos-battery-exporter/blob/main/LICENSE)
|
||||||
|
|||||||
@@ -15,12 +15,12 @@ type Battery struct {
|
|||||||
// BuiltIn indicates if the battery is built-in or not.
|
// BuiltIn indicates if the battery is built-in or not.
|
||||||
BuiltIn bool
|
BuiltIn bool
|
||||||
|
|
||||||
// ChargeRateAmps is the current charge rate in mAh. Negative values indicate
|
// ChargeRateAmps is the current charge rate in mAh. Negative values
|
||||||
// discharge, positive values indicate charging.
|
// indicate discharge, positive values indicate charging.
|
||||||
ChargeRateAmps int64
|
ChargeRateAmps int64
|
||||||
|
|
||||||
// ChargeRateWatts is the current charge rate in mWh. Negative values indicate
|
// ChargeRateWatts is the current charge rate in mWh. Negative values
|
||||||
// discharge, positive values indicate charging.
|
// indicate discharge, positive values indicate charging.
|
||||||
ChargeRateWatts float64
|
ChargeRateWatts float64
|
||||||
|
|
||||||
// CurrentCapacityAmps is the current battery capacity in mAh.
|
// CurrentCapacityAmps is the current battery capacity in mAh.
|
||||||
@@ -76,6 +76,7 @@ type Battery struct {
|
|||||||
func newBattery(b *batteryRaw) *Battery {
|
func newBattery(b *batteryRaw) *Battery {
|
||||||
volts := float64(b.Voltage) / 1000
|
volts := float64(b.Voltage) / 1000
|
||||||
|
|
||||||
|
//nolint:lll
|
||||||
return &Battery{
|
return &Battery{
|
||||||
BatteryCellDisconnectCount: b.BatteryCellDisconnectCount,
|
BatteryCellDisconnectCount: b.BatteryCellDisconnectCount,
|
||||||
BuiltIn: b.BuiltIn,
|
BuiltIn: b.BuiltIn,
|
||||||
@@ -101,29 +102,31 @@ func newBattery(b *batteryRaw) *Battery {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Get() (*Battery, error) {
|
func Get() (*Battery, error) {
|
||||||
batteriesRaw, err := getAllRaw()
|
raw, err := getAllRaw()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return newBattery(batteriesRaw[0]), nil
|
return newBattery(raw[0]), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAll() ([]*Battery, error) {
|
func GetAll() ([]*Battery, error) {
|
||||||
batteriesRaw, err := getAllRaw()
|
raw, err := getAllRaw()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
batteries := []*Battery{}
|
batteries := []*Battery{}
|
||||||
for _, b := range batteriesRaw {
|
for _, b := range raw {
|
||||||
batteries = append(batteries, newBattery(b))
|
batteries = append(batteries, newBattery(b))
|
||||||
}
|
}
|
||||||
|
|
||||||
return batteries, nil
|
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 {
|
func roundTo(value float64, places int) float64 {
|
||||||
shift := math.Pow(10, float64(places))
|
shift := math.Pow(10, float64(places))
|
||||||
return math.Round(value*shift) / shift
|
return math.Round(value*shift) / shift
|
||||||
|
|||||||
148
main.go
148
main.go
@@ -5,104 +5,170 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/jimeh/macos-battery-exporter/prom"
|
"github.com/jimeh/macos-battery-exporter/prombat"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/prometheus/common/expfmt"
|
"github.com/prometheus/common/expfmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
name = "macos-battery-exporter"
|
||||||
version = "0.0.0-dev"
|
version = "0.0.0-dev"
|
||||||
commit = "unknown"
|
commit = "unknown"
|
||||||
|
)
|
||||||
|
|
||||||
outputFlag = flag.String(
|
type configuration struct {
|
||||||
|
Output string
|
||||||
|
Server bool
|
||||||
|
Bind string
|
||||||
|
Port int
|
||||||
|
Namespace string
|
||||||
|
LogLevel string
|
||||||
|
LogDevice string
|
||||||
|
PrintVersion bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func configure() (*configuration, *flag.FlagSet, error) {
|
||||||
|
fs := flag.NewFlagSet(name, flag.ExitOnError)
|
||||||
|
fs.Usage = func() {
|
||||||
|
fmt.Fprintf(fs.Output(),
|
||||||
|
"usage: %s [<options>]\n\n", fs.Name(),
|
||||||
|
)
|
||||||
|
fs.PrintDefaults()
|
||||||
|
}
|
||||||
|
|
||||||
|
config := &configuration{}
|
||||||
|
|
||||||
|
fs.StringVar(&config.Output,
|
||||||
"o", "", "Output file to write to in Prometheus format",
|
"o", "", "Output file to write to in Prometheus format",
|
||||||
)
|
)
|
||||||
serverFlag = flag.Bool("s", false, "Run as a Prometheus metrics server")
|
fs.BoolVar(&config.Server,
|
||||||
bindFlag = flag.String(
|
"s", false, "Run as a Prometheus metrics server",
|
||||||
|
)
|
||||||
|
fs.StringVar(&config.Bind,
|
||||||
"b", "127.0.0.1", "Bind address to run server on",
|
"b", "127.0.0.1", "Bind address to run server on",
|
||||||
)
|
)
|
||||||
portFlag = flag.Int("p", 9108, "Port to run server on")
|
fs.IntVar(&config.Port, "p", 9108, "Port to run server on")
|
||||||
namespaceFlag = flag.String(
|
fs.StringVar(&config.Namespace,
|
||||||
"n", prom.DefaultNamespace, "Namespace for metrics",
|
"n", prombat.DefaultNamespace, "Namespace for metrics",
|
||||||
)
|
)
|
||||||
logLevelFlag = flag.String("l", "info", "Log level")
|
fs.StringVar(&config.LogLevel,
|
||||||
versionFlag = flag.Bool("v", false, "Print version and exit")
|
"l", "info", "Log level",
|
||||||
)
|
)
|
||||||
|
fs.StringVar(&config.LogDevice,
|
||||||
|
"d", "stderr", "Log output device (stderr or stdout)",
|
||||||
|
)
|
||||||
|
fs.BoolVar(&config.PrintVersion,
|
||||||
|
"v", false, "Print version and exit",
|
||||||
|
)
|
||||||
|
|
||||||
|
err := fs.Parse(os.Args[1:])
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return config, fs, nil
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if err := mainE(); err != nil {
|
if err := mainE(); err != nil {
|
||||||
log.Fatal(err)
|
slog.Error(err.Error())
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func mainE() error {
|
func mainE() error {
|
||||||
flag.Parse()
|
config, _, err := configure()
|
||||||
|
|
||||||
err := setupSLog(*logLevelFlag)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if *versionFlag {
|
err = setupSLog(config.LogDevice, config.LogLevel)
|
||||||
fmt.Printf("macos-battery-exporter %s (%s)\n", version, commit)
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.PrintVersion {
|
||||||
|
fmt.Printf("%s %s (%s)\n", name, version, commit)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if *serverFlag {
|
if config.Server {
|
||||||
opts := prom.ServerOptions{
|
return runServer(config)
|
||||||
Bind: *bindFlag,
|
|
||||||
Port: *portFlag,
|
|
||||||
}
|
|
||||||
|
|
||||||
return prom.RunServer(
|
|
||||||
*namespaceFlag,
|
|
||||||
prometheus.DefaultRegisterer.(*prometheus.Registry),
|
|
||||||
opts,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
registry := prometheus.NewRegistry()
|
metrics, err := renderMetrics(config)
|
||||||
err = registry.Register(prom.NewCollector(*namespaceFlag))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if config.Output != "" {
|
||||||
|
return writeToFile(metrics, config.Output)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Print(metrics)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func runServer(config *configuration) error {
|
||||||
|
opts := prombat.ServerOptions{
|
||||||
|
Bind: config.Bind,
|
||||||
|
Port: config.Port,
|
||||||
|
}
|
||||||
|
|
||||||
|
return prombat.RunServer(
|
||||||
|
config.Namespace,
|
||||||
|
prometheus.DefaultRegisterer.(*prometheus.Registry),
|
||||||
|
opts,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func renderMetrics(config *configuration) (string, error) {
|
||||||
|
registry := prometheus.NewRegistry()
|
||||||
|
err := registry.Register(prombat.NewCollector(config.Namespace))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
gatherers := prometheus.Gatherers{registry}
|
gatherers := prometheus.Gatherers{registry}
|
||||||
metricFamilies, err := gatherers.Gather()
|
metricFamilies, err := gatherers.Gather()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
var sb strings.Builder
|
var sb strings.Builder
|
||||||
for _, mf := range metricFamilies {
|
for _, mf := range metricFamilies {
|
||||||
_, err := expfmt.MetricFamilyToText(&sb, mf)
|
_, err := expfmt.MetricFamilyToText(&sb, mf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if *outputFlag != "" {
|
return sb.String(), nil
|
||||||
return writeToFile(sb.String(), *outputFlag)
|
|
||||||
} else {
|
|
||||||
fmt.Print(sb.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupSLog(levelStr string) error {
|
func setupSLog(device string, levelStr string) error {
|
||||||
|
var w io.Writer
|
||||||
|
switch device {
|
||||||
|
case "stderr":
|
||||||
|
w = os.Stderr
|
||||||
|
case "stdout":
|
||||||
|
w = os.Stdout
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("invalid log device: %s", device)
|
||||||
|
}
|
||||||
|
|
||||||
var level slog.Level
|
var level slog.Level
|
||||||
err := level.UnmarshalText([]byte(levelStr))
|
err := level.UnmarshalText([]byte(levelStr))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
handler := slog.NewTextHandler(w, &slog.HandlerOptions{
|
||||||
Level: level,
|
Level: level,
|
||||||
})
|
})
|
||||||
logger := slog.New(handler)
|
logger := slog.New(handler)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
//go:build darwin
|
//go:build darwin
|
||||||
|
|
||||||
package prom
|
package prombat
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log/slog"
|
"log/slog"
|
||||||
@@ -367,7 +367,9 @@ func boolToFloat64(b bool) float64 {
|
|||||||
return 0
|
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 {
|
func roundTo(value float64, places int) float64 {
|
||||||
shift := math.Pow(10, float64(places))
|
shift := math.Pow(10, float64(places))
|
||||||
return math.Round(value*shift) / shift
|
return math.Round(value*shift) / shift
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
//go:build darwin
|
//go:build darwin
|
||||||
|
|
||||||
package prom
|
package prombat
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -14,23 +14,17 @@ import (
|
|||||||
|
|
||||||
const DefaultNamespace = "macos"
|
const DefaultNamespace = "macos"
|
||||||
|
|
||||||
type Registry interface {
|
|
||||||
prometheus.Registerer
|
|
||||||
prometheus.Gatherer
|
|
||||||
}
|
|
||||||
|
|
||||||
type ServerOptions struct {
|
type ServerOptions struct {
|
||||||
Bind string
|
Bind string
|
||||||
Port int
|
Port int
|
||||||
ReadTimeout time.Duration
|
ReadTimeout time.Duration
|
||||||
WriteTimeout time.Duration
|
WriteTimeout time.Duration
|
||||||
IdleTimeout time.Duration
|
IdleTimeout time.Duration
|
||||||
Logger *slog.Logger
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
*http.Server
|
*http.Server
|
||||||
registry Registry
|
registry *prometheus.Registry
|
||||||
mux *http.ServeMux
|
mux *http.ServeMux
|
||||||
}
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user