mirror of
https://github.com/jimeh/macos-battery-exporter.git
synced 2026-02-19 09:26:40 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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.4"
|
||||||
}
|
}
|
||||||
|
|||||||
19
.github/workflows/ci.yml
vendored
19
.github/workflows/ci.yml
vendored
@@ -17,6 +17,23 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
./bin/macos-battery-exporter -v
|
./bin/macos-battery-exporter -v
|
||||||
|
|
||||||
|
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
|
||||||
if: github.ref == 'refs/heads/main'
|
if: github.ref == 'refs/heads/main'
|
||||||
@@ -35,7 +52,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
|
||||||
@@ -1,5 +1,12 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## [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)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
36
README.md
36
README.md
@@ -19,6 +19,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
|
`ioreg`. Includes a lot more details than what `node_exporter` supports via it's
|
||||||
`node_power_supply_*` metrics.
|
`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
|
## Usage
|
||||||
|
|
||||||
### Help
|
### Help
|
||||||
@@ -28,7 +62,7 @@ macos-battery-exporter -h
|
|||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Usage of bin/macos-battery-exporter:
|
Usage of macos-battery-exporter:
|
||||||
-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")
|
||||||
-l string
|
-l string
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
4
main.go
4
main.go
@@ -88,10 +88,10 @@ func mainE() error {
|
|||||||
|
|
||||||
if *outputFlag != "" {
|
if *outputFlag != "" {
|
||||||
return writeToFile(sb.String(), *outputFlag)
|
return writeToFile(sb.String(), *outputFlag)
|
||||||
} else {
|
|
||||||
fmt.Print(sb.String())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Print(sb.String())
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user