From a4a2b2f1503b217e8b721f356ee1466a007f55ad Mon Sep 17 00:00:00 2001 From: Matt Layher Date: Mon, 8 Jun 2020 09:41:16 -0400 Subject: [PATCH] apcupsd_exporter: modernize and tidy Signed-off-by: Matt Layher --- .github/workflows/linux-test.yml | 29 ++++++++++++ .github/workflows/static-analysis.yml | 35 +++++++++++++++ .travis.yml | 14 ------ LICENSE.md | 5 +-- README.md | 18 ++------ apcupsdexporter.go | 14 +++--- apcupsdexporter_test.go | 18 ++++---- cmd/apcupsd_exporter/main.go | 7 +-- go.mod | 12 ++--- go.sum | 63 ++++++++++++++++++++++++--- upscollector.go | 36 ++++++++------- upscollector_test.go | 6 +-- 12 files changed, 179 insertions(+), 78 deletions(-) create mode 100644 .github/workflows/linux-test.yml create mode 100644 .github/workflows/static-analysis.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/linux-test.yml b/.github/workflows/linux-test.yml new file mode 100644 index 0000000..9ca36aa --- /dev/null +++ b/.github/workflows/linux-test.yml @@ -0,0 +1,29 @@ +name: Linux Test + +on: + push: + branches: + - master + pull_request: + branches: + - '*' + +jobs: + build: + strategy: + matrix: + go-version: [1.14] + runs-on: ubuntu-latest + + steps: + - name: Set up Go + uses: actions/setup-go@v1 + with: + go-version: ${{ matrix.go-version }} + id: go + + - name: Check out code into the Go module directory + uses: actions/checkout@v1 + + - name: Run tests + run: go test -race ./... diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml new file mode 100644 index 0000000..d802690 --- /dev/null +++ b/.github/workflows/static-analysis.yml @@ -0,0 +1,35 @@ +name: Static Analysis + +on: + push: + branches: + - master + pull_request: + branches: + - '*' + +jobs: + build: + strategy: + matrix: + go-version: [1.14] + runs-on: ubuntu-latest + + steps: + - name: Set up Go + uses: actions/setup-go@v1 + with: + go-version: ${{ matrix.go-version }} + id: go + + - name: Check out code into the Go module directory + uses: actions/checkout@v1 + + - name: Print staticcheck version + run: go run honnef.co/go/tools/cmd/staticcheck -version + + - name: Run staticcheck + run: go run honnef.co/go/tools/cmd/staticcheck -- ./... + + - name: Run go vet + run: go vet ./... diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 2582a35..0000000 --- a/.travis.yml +++ /dev/null @@ -1,14 +0,0 @@ -language: go -go: - - 1.x -env: - - GO111MODULE=on -before_install: - - go get golang.org/x/lint/golint -before_script: - - go get -d ./... -script: - - go build ./... - - golint ./... - - go vet ./... - - go test -v ./... diff --git a/LICENSE.md b/LICENSE.md index 031350d..7e70b05 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,7 +1,6 @@ -MIT License -=========== +# MIT License -Copyright (C) 2016 Matt Layher +Copyright (C) 2016-2020 Matt Layher Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/README.md b/README.md index 6e3959e..c831cc7 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,10 @@ -apcupsd_exporter [![Build Status](https://travis-ci.org/mdlayher/apcupsd_exporter.svg?branch=master)](https://travis-ci.org/mdlayher/apcupsd_exporter) [![GoDoc](http://godoc.org/github.com/mdlayher/apcupsd_exporter?status.svg)](http://godoc.org/github.com/mdlayher/apcupsd_exporter) -================ +# apcupsd_exporter [![Linux Test Status](https://github.com/mdlayher/apcupsd_exporter/workflows/Linux%20Test/badge.svg)](https://github.com/mdlayher/apcupsd_exporter/actions) [![GoDoc](http://godoc.org/github.com/mdlayher/apcupsd_exporter?status.svg)](http://godoc.org/github.com/mdlayher/apcupsd_exporter) Command `apcupsd_exporter` provides a Prometheus exporter for the -[apcupsd](http://www.apcupsd.org/) Network Information Server (NIS). -MIT Licensed. +[apcupsd](http://www.apcupsd.org/) Network Information Server (NIS). MIT +Licensed. -Usage ------ +## Usage Available flags for `apcupsd_exporter` include: @@ -22,11 +20,3 @@ Usage of ./apcupsd_exporter: -telemetry.path string URL path for surfacing collected metrics (default "/metrics") ``` - -Sample ------- - -Here is a screenshot of an example grafana dashboard using metrics from -`apcupsd_exporter`. - -![sample](https://cloud.githubusercontent.com/assets/1926905/18330011/55c49eca-7524-11e6-8152-717bf4bc75c0.png) diff --git a/apcupsdexporter.go b/apcupsdexporter.go index 6193300..0cbaedd 100644 --- a/apcupsdexporter.go +++ b/apcupsdexporter.go @@ -3,7 +3,9 @@ package apcupsdexporter import ( + "context" "log" + "time" "github.com/mdlayher/apcupsd" "github.com/prometheus/client_golang/prometheus" @@ -29,7 +31,7 @@ var _ prometheus.Collector = &Exporter{} // A ClientFunc is a function which can return an apcupsd NIS client. // ClientFuncs are invoked on each Prometheus scrape, so that connections // can be short-lived and less likely to time out or fail. -type ClientFunc func() (*apcupsd.Client, error) +type ClientFunc func(ctx context.Context) (*apcupsd.Client, error) // New creates a new Exporter which collects metrics by creating a apcupsd // client using the input ClientFunc. @@ -63,17 +65,19 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) { // collectors. It invokes the input closure and then cleans up after the // closure returns. func (e *Exporter) withCollectors(fn func(cs []prometheus.Collector)) { - c, err := e.clientFn() + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + c, err := e.clientFn(ctx) if err != nil { - log.Printf("[ERROR] error creating apcupsd client: %v", err) + log.Printf("error creating apcupsd client: %v", err) return } + defer c.Close() cs := []prometheus.Collector{ NewUPSCollector(c), } fn(cs) - - _ = c.Close() } diff --git a/apcupsdexporter_test.go b/apcupsdexporter_test.go index ed6e95d..6102b1b 100644 --- a/apcupsdexporter_test.go +++ b/apcupsdexporter_test.go @@ -5,23 +5,25 @@ import ( "net/http" "net/http/httptest" "testing" + "time" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" ) func testCollector(t *testing.T, collector prometheus.Collector) []byte { - if err := prometheus.Register(collector); err != nil { - t.Fatalf("failed to register Prometheus collector: %v", err) - } - defer prometheus.Unregister(collector) + t.Helper() - promServer := httptest.NewServer(promhttp.Handler()) - defer promServer.Close() + reg := prometheus.NewPedanticRegistry() + reg.MustRegister(collector) - resp, err := http.Get(promServer.URL) + srv := httptest.NewServer(promhttp.HandlerFor(reg, promhttp.HandlerOpts{})) + defer srv.Close() + + c := &http.Client{Timeout: 1 * time.Second} + resp, err := c.Get(srv.URL) if err != nil { - t.Fatalf("failed to GET data from prometheus: %v", err) + t.Fatalf("failed to HTTP GET data from prometheus: %v", err) } defer resp.Body.Close() diff --git a/cmd/apcupsd_exporter/main.go b/cmd/apcupsd_exporter/main.go index 63cd2c6..160ce85 100644 --- a/cmd/apcupsd_exporter/main.go +++ b/cmd/apcupsd_exporter/main.go @@ -2,6 +2,7 @@ package main import ( + "context" "flag" "log" "net/http" @@ -44,8 +45,8 @@ func main() { } } -func newClient(network string, addr string) apcupsdexporter.ClientFunc { - return func() (*apcupsd.Client, error) { - return apcupsd.Dial(network, addr) +func newClient(network, addr string) apcupsdexporter.ClientFunc { + return func(ctx context.Context) (*apcupsd.Client, error) { + return apcupsd.DialContext(ctx, network, addr) } } diff --git a/go.mod b/go.mod index e546cb3..b26554e 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,11 @@ module github.com/mdlayher/apcupsd_exporter go 1.14 require ( - github.com/golang/protobuf v1.4.0 // indirect - github.com/mdlayher/apcupsd v0.0.0-20200427150358-149abe0674c2 - github.com/prometheus/client_golang v1.5.1 - github.com/prometheus/procfs v0.0.11 // indirect - golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f // indirect + github.com/golang/protobuf v1.4.2 // indirect + github.com/mdlayher/apcupsd v0.0.0-20200608131503-2bf01da7bf1b + github.com/prometheus/client_golang v1.6.0 + github.com/prometheus/common v0.10.0 // indirect + github.com/prometheus/procfs v0.1.0 // indirect + golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 // indirect + google.golang.org/protobuf v1.24.0 // indirect ) diff --git a/go.sum b/go.sum index e086706..06aa91b 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= @@ -7,16 +9,22 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24 github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -27,10 +35,16 @@ github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrU github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= github.com/golang/protobuf v1.4.0 h1:oOuy+ugB+P/kBdUnG5QaMXSIyJ1q38wWSojYCb3z5VQ= github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= +github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.4.1 h1:/exdXoGamhu5ONeUJH0deniYLWYvQwW66yvlfiiKTu0= +github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= @@ -42,8 +56,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= -github.com/mdlayher/apcupsd v0.0.0-20200427150358-149abe0674c2 h1:/6kamK5dQLpKSHNH47TqdeDLorFi1rpIeajupArlxxI= -github.com/mdlayher/apcupsd v0.0.0-20200427150358-149abe0674c2/go.mod h1:izgPry3rVq8ix9cave/cFB9ZtlrWMa0bUOEOO4Q85x0= +github.com/mdlayher/apcupsd v0.0.0-20200608131503-2bf01da7bf1b h1:Kcr+kPbkWZHFHXwl87quXUAmavS4/IMgu2zck3aiE7k= +github.com/mdlayher/apcupsd v0.0.0-20200608131503-2bf01da7bf1b/go.mod h1:WYK/Z/aXq9cbMFIL5ihcA4sX/r/3/WCas/Qvs/2fXcA= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= @@ -54,21 +68,25 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= -github.com/prometheus/client_golang v1.5.1 h1:bdHYieyGlH+6OLEk2YQha8THib30KP0/yD0YH9m6xcA= -github.com/prometheus/client_golang v1.5.1/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU= +github.com/prometheus/client_golang v1.6.0 h1:YVPodQOcK15POxhgARIvnDRVpLcuK8mglnMrWfyrw6A= +github.com/prometheus/client_golang v1.6.0/go.mod h1:ZLOG9ck3JLRdB5MgO8f+lLTe83AXG6ro35rLTxvnIl4= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 h1:idejC8f05m9MGOsuEi1ATq9shN03HrxNkD/luQvxCv8= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M= github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= github.com/prometheus/common v0.9.1 h1:KOMtN28tlbam3/7ZKEYKHhKoJZYYj3gMH4uc62x7X7U= github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8bs7vj7HSQ4= +github.com/prometheus/common v0.10.0 h1:RyRA7RzGXQZiW+tGMr7sxa85G1z0yOpM1qq5c8lNawc= +github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo= github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= -github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A= github.com/prometheus/procfs v0.0.11 h1:DhHlBtkHWPYi8O2y31JkK0TF+DGM+51OopZjH/Ia5qI= github.com/prometheus/procfs v0.0.11/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= +github.com/prometheus/procfs v0.1.0 h1:jhMy6QXfi3y2HEzFoyuCj40z4OZIIHHPtFyCMftmvKA= +github.com/prometheus/procfs v0.1.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -78,30 +96,59 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f h1:Bl/8QSvNqXvPGPGXa2z5xUTmV7VDcZyvRZ+QQXkXTZQ= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200122134326-e047566fdf82 h1:ywK/j/KkyTHcdyYSZNXGjMwgmDSfjglYZ3vStQ/gSCU= -golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f h1:gWF768j/LaZugp8dyS4UwsslYCYz9XgFxvlgsn0n9H8= golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 h1:OjiUf46hAmXblsZdnoSXsEUSKU8r1UEzcL5RVZ4gO9Y= +golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= google.golang.org/protobuf v1.21.0 h1:qdOKuR/EIArgaWNjetjgTzgVTAZ+S/WXVrq9HW9zimw= google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.24.0 h1:UhZDfRO8JRQru4/+LlLE0BRKGF8L+PICnvYZmx/fEGA= +google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= @@ -109,3 +156,5 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/upscollector.go b/upscollector.go index 0a25a5a..dfdab3a 100644 --- a/upscollector.go +++ b/upscollector.go @@ -30,9 +30,9 @@ type UPSCollector struct { BatteryTimeLeftSeconds *prometheus.Desc BatteryTimeOnSeconds *prometheus.Desc BatteryCumulativeTimeOnSecondsTotal *prometheus.Desc - LastTransferOnBattery *prometheus.Desc - LastTransferOffBattery *prometheus.Desc - LastSelftest *prometheus.Desc + LastTransferOnBatteryTimeSeconds *prometheus.Desc + LastTransferOffBatteryTimeSeconds *prometheus.Desc + LastSelftestTimeSeconds *prometheus.Desc NominalPowerWatts *prometheus.Desc ss StatusSource @@ -122,29 +122,29 @@ func NewUPSCollector(ss StatusSource) *UPSCollector { nil, ), - LastTransferOnBattery: prometheus.NewDesc( - prometheus.BuildFQName(namespace, "", "apcupsd_last_transfer_on_battery"), - "Time of last transfer to battery since apcupsd startup.", + LastTransferOnBatteryTimeSeconds: prometheus.NewDesc( + prometheus.BuildFQName(namespace, "", "last_transfer_on_battery_time_seconds"), + "UNIX timestamp of last transfer to battery since apcupsd startup.", labels, nil, ), - LastTransferOffBattery: prometheus.NewDesc( - prometheus.BuildFQName(namespace, "", "apcupsd_last_transfer_off_battery"), - "Time of last transfer from battery since apcupsd startup.", + LastTransferOffBatteryTimeSeconds: prometheus.NewDesc( + prometheus.BuildFQName(namespace, "", "last_transfer_off_battery_time_seconds"), + "UNIX timestamp of last transfer from battery since apcupsd startup.", labels, nil, ), - LastSelftest: prometheus.NewDesc( - prometheus.BuildFQName(namespace, "", "apcupsd_last_selftest"), - "Time of last selftest since apcupsd startup.", + LastSelftestTimeSeconds: prometheus.NewDesc( + prometheus.BuildFQName(namespace, "", "last_selftest_time_seconds"), + "UNIX timestamp of last selftest since apcupsd startup.", labels, nil, ), NominalPowerWatts: prometheus.NewDesc( - prometheus.BuildFQName(namespace, "", "apcupsd_nominal_power_watts"), + prometheus.BuildFQName(namespace, "", "nominal_power_watts"), "Nominal power output in watts.", labels, nil, @@ -169,6 +169,10 @@ func (c *UPSCollector) Describe(ch chan<- *prometheus.Desc) { c.BatteryTimeLeftSeconds, c.BatteryTimeOnSeconds, c.BatteryCumulativeTimeOnSecondsTotal, + c.LastTransferOnBatteryTimeSeconds, + c.LastTransferOffBatteryTimeSeconds, + c.LastSelftestTimeSeconds, + c.NominalPowerWatts, } for _, d := range ds { @@ -264,21 +268,21 @@ func (c *UPSCollector) Collect(ch chan<- prometheus.Metric) { ) ch <- prometheus.MustNewConstMetric( - c.LastTransferOnBattery, + c.LastTransferOnBatteryTimeSeconds, prometheus.GaugeValue, timestamp(s.XOnBattery), s.UPSName, ) ch <- prometheus.MustNewConstMetric( - c.LastTransferOffBattery, + c.LastTransferOffBatteryTimeSeconds, prometheus.GaugeValue, timestamp(s.XOffBattery), s.UPSName, ) ch <- prometheus.MustNewConstMetric( - c.LastSelftest, + c.LastSelftestTimeSeconds, prometheus.GaugeValue, timestamp(s.LastSelftest), s.UPSName, diff --git a/upscollector_test.go b/upscollector_test.go index 31329a1..aa968f4 100644 --- a/upscollector_test.go +++ b/upscollector_test.go @@ -56,9 +56,9 @@ func TestUPSCollector(t *testing.T) { regexp.MustCompile(`apcupsd_line_nominal_volts{ups="bar"} 120`), regexp.MustCompile(`apcupsd_line_volts{ups="bar"} 121.1`), regexp.MustCompile(`apcupsd_ups_load_percent{ups="bar"} 16`), - regexp.MustCompile(`apcupsd_last_transfer_on_battery{ups="bar"} 100001`), - regexp.MustCompile(`apcupsd_last_transfer_off_battery{ups="bar"} 100002`), - regexp.MustCompile(`apcupsd_last_selftest{ups="bar"} 100003`), + regexp.MustCompile(`apcupsd_last_transfer_on_battery_time_seconds{ups="bar"} 100001`), + regexp.MustCompile(`apcupsd_last_transfer_off_battery_time_seconds{ups="bar"} 100002`), + regexp.MustCompile(`apcupsd_last_selftest_time_seconds{ups="bar"} 100003`), regexp.MustCompile(`apcupsd_nominal_power_watts{ups="bar"} 50`), }, },