Skip to content

Commit

Permalink
Coax ups.status to an integer
Browse files Browse the repository at this point in the history
  • Loading branch information
DRuggeri committed Dec 21, 2020
1 parent 9847740 commit c908cf5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ The variables exposed to a NUT client by the NUT system are the lifeblood of a d
* Not all driver and UPS implementations provide all variables. Run this exporter with log.level at debug or use the `LIST VAR` upsc command to see available variables for your UPS
* All number-like values are coaxed to the appropriate go type by the library and are set as the value of the exported metric
* Boolean values are coaxed to 0 (false) or 1 (true)
* The special `ups.status` variable is returned by NUT as a string. It is coaxed to an integer by this exporter to enable use of alerting on status changes. The values are:
* `OL`: Online - `0`
* `OB`: On Battery - `1`
* `LB`: Low Battery - `2`
* Any other value: Unknown - `3`

### Example Scrape Configuration
Note that this exporter will scrape only one UPS per scrape invocation. If there are multiple UPS devices visible to NUT, you MUST ensure that you set up different scrape configs for each UPS device. Here is an example configuration for such a use case:
Expand Down
15 changes: 15 additions & 0 deletions collectors/nut_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,21 @@ func (c *NutCollector) Collect(ch chan<- prometheus.Metric) {
log.Debugf(" Export the variable? true")
value := float64(0)

/* Manually coax critical vaules to floats */
if variable.Name == "ups.status" {
variable.Type = "INTEGER"
switch {
case variable.Value == "OL":
variable.Value = float64(0)
case variable.Value == "OB":
variable.Value = float64(1)
case variable.Value == "LB":
variable.Value = float64(2)
default:
variable.Value = float64(3)
}
}

/* All numbers are coaxed to native types by the library, so at this point we know
we cannot set this value because a string will never be a float-like number */
if strings.ToLower(variable.Type) == "string" {
Expand Down

0 comments on commit c908cf5

Please sign in to comment.