Listing current switch interface status with Flux



Published: 2021-10-30 09:41:48 +0000
Categories: Flux,

Language

Flux

Description

If you collect stats from a switch with SNMP, the field ifOperStatus will be numeric - 1 for up, 2 for down, looking something like this.

This snippet provides some Flux to pull current statuses out of InfluxDB and map the value over to a more human readable format

Snippet

from(bucket: "<bucket>")
  |> range(start: <start time>)
  |> filter(fn: (r) => r._measurement == "interface" and r._field == "ifOperStatus")
  |> filter(fn: (r) => r.hostname == "<SNMP Hostname>")
  |> group(columns: ["ifDescr"], mode: "by")
  |> last()
  // This map will drop all columns except those listed
  // use "r with" if you want to preserve other tags
  |> map(fn: (r) => ({ifDescr: r.ifDescr,
                      _time: r._time,
                      _value: if (r._value == 1.00) then "up" else "down"
                      }))
  |> group()

Usage Example

from(bucket: "telegraf/autogen")
  |> range(start: v.timeRangeStart)
  |> filter(fn: (r) => r._measurement == "interface" and r._field == "ifOperStatus")
  |> filter(fn: (r) => r.hostname == "netgear-switch")
  |> group(columns: ["ifDescr"], mode: "by")
  |> last()
  |> map(fn: (r) => ({ifDescr: r.ifDescr,
                      _time: r._time,
                      _value: if (r._value == 1.00) then "up" else "down"
                      }))
  |> group()

Keywords

influxdb, flux, ifOperStatus, map, query,

Latest Posts


Copyright © 2021 Ben Tasker | Sitemap | Privacy Policy
Available at snippets.bentasker.co.uk and http://phecoopwm6x7azx26ctuqcp6673bbqkrqfeoiz2wwk36sady5tqbdpqd.onion