Listing current switch interface status with Flux (Flux)
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
Details
- Language: Flux
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()