e1e0.net

sources for e1e0 website
git clone https://git.e1e0.net/e1e0.net.git
Log | Files | Refs

upsc-prometheus-exporter.md (3562B)


      1 Title: upsc (NUT) Prometheus Exporter
      2 Author: paco
      3 Date: 2020-01-17
      4 Type: article
      5 
      6 I have a cheap [UPS][1] that provides backup power to my main workstation and some
      7 other equipment.  This device has a USB interface that provides some data about
      8 the status, battery charge, etc.  That means only one device can have that
      9 information and react accordingly when there's a power outage and the battery
     10 goes near depletion.
     11 
     12 To address that I use [NUT][2], so the other devices attached to that ups can
     13 also react on power events.
     14 
     15 There's plenty information on the net about this (probably the hardest part is
     16 finding the driver for your particular unit ...), so I won't talk about that on
     17 this article.
     18 
     19 NUT has a cli tool that can print some data to stdout like this:
     20 
     21     $ upsc ups
     22     Init SSL without certificate database
     23     battery.charge: 100
     24     battery.voltage: 27.40
     25     battery.voltage.high: 26.00
     26     battery.voltage.low: 20.80
     27     battery.voltage.nominal: 24.0
     28     device.type: ups
     29     driver.name: blazer_usb
     30     driver.parameter.pollinterval: 2
     31     driver.parameter.port: auto
     32     driver.parameter.synchronous: no
     33     driver.version: 2.7.4
     34     driver.version.internal: 0.12
     35     input.current.nominal: 5.0
     36     input.frequency: 49.6
     37     input.frequency.nominal: 50
     38     input.voltage: 232.8
     39     input.voltage.fault: 232.8
     40     input.voltage.nominal: 230
     41     output.voltage: 232.8
     42     ups.beeper.status: enabled
     43     ups.delay.shutdown: 30
     44     ups.delay.start: 180
     45     ups.load: 10
     46     ups.productid: 5161
     47     ups.status: OL
     48     ups.type: offline / line interactive
     49     ups.vendorid: 0665
     50 
     51 That's a lot of interesting information.  What I wanted was a way to monitor
     52 the data exposed by NUT in a way that could be consumed by prometheus, as I use
     53 it for monitoring on my internal network.
     54 
     55 The only thing I could find was [this hack][3] that uses `docker` + `inetd(8)`
     56 + wrapper scripts + `awk(1)` for parsing and serving the data on a Prometheus
     57 format.
     58 
     59 Although I have to admit that I like simple command line solutions, I think this
     60 is a bit too far for my taste, so I've made a super dumb program in Go that does
     61 exactly that, listens on a port (http) and for any incoming request on
     62 `/metrics` calls `upsc(8)`, parses its output and returns it formatted in a way
     63 Prometheus can understand.
     64 
     65 On a surge of originality, the small program is called `prometheus-upsc-exporter`.
     66 The source code can be found [here][4].  I've also built it for linux arm
     67 (raspberry pi for instance) and created a [repo][5] for it (well, I may use it
     68 in the future for other builds).  So it can be downloaded and used directly,
     69 just be sure to check the checksum and signature.
     70 
     71 It produces metrics of the form:
     72 
     73     upsc_battery_charge{instance="nut.host:8081",job="ups"}
     74     upsc_battery_voltage{instance="nut.host:8081",job="ups"}
     75     ...
     76 
     77 With that and a bit of "grafana dashboard fu" I now have metrics on my cheap UPS
     78 and can even generate alerts based on them.
     79 
     80 If you decide to use it, I recommend to take a look at the code first, as
     81 I made some parsing decisions and ignore some info I do not find useful and you
     82 may want to include.
     83 
     84 Take a look at the included man page and the `systemd(1)` servide definition
     85 example for more info.
     86 
     87 Hope somebody find it useful and, as usual, any patches and suggestions are
     88 welcome.
     89 
     90 [1]: https://en.wikipedia.org/wiki/Uninterruptible_power_supply
     91 [2]: https://networkupstools.org/
     92 [3]: https://github.com/jzck/prometheus-upsc-exporter
     93 [4]: https://git.e1e0.net/prometheus-upsc-exporter/log.html
     94 [5]: https://e1e0.net/repo/