Merge pull request #105 from oGi4i/feature/interface_type

Added more interface labels
This commit is contained in:
Steve Brunton 2021-03-25 20:27:04 -04:00 committed by GitHub
commit 8e7b67527a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 32 additions and 21 deletions

View File

@ -21,11 +21,11 @@ func newInterfaceCollector() routerOSCollector {
}
func (c *interfaceCollector) init() {
c.props = []string{"name", "comment", "rx-byte", "tx-byte", "rx-packet", "tx-packet", "rx-error", "tx-error", "rx-drop", "tx-drop"}
c.props = []string{"name", "type", "disabled", "comment", "slave", "actual-mtu", "running", "rx-byte", "tx-byte", "rx-packet", "tx-packet", "rx-error", "tx-error", "rx-drop", "tx-drop"}
labelNames := []string{"name", "address", "interface", "comment"}
labelNames := []string{"name", "address", "interface", "type", "disabled", "comment", "running", "slave"}
c.descriptions = make(map[string]*prometheus.Desc)
for _, p := range c.props[1:] {
for _, p := range c.props[5:] {
c.descriptions[p] = descriptionForPropertyName("interface", p, labelNames)
}
}
@ -50,7 +50,7 @@ func (c *interfaceCollector) collect(ctx *collectorContext) error {
}
func (c *interfaceCollector) fetch(ctx *collectorContext) ([]*proto.Sentence, error) {
reply, err := ctx.client.Run("/interface/print", "?disabled=false", "?running=true", "=.proplist="+strings.Join(c.props, ","))
reply, err := ctx.client.Run("/interface/print", "=.proplist="+strings.Join(c.props, ","))
if err != nil {
log.WithFields(log.Fields{
"device": ctx.device.Name,
@ -63,28 +63,39 @@ func (c *interfaceCollector) fetch(ctx *collectorContext) ([]*proto.Sentence, er
}
func (c *interfaceCollector) collectForStat(re *proto.Sentence, ctx *collectorContext) {
name := re.Map["name"]
comment := re.Map["comment"]
for _, p := range c.props[2:] {
c.collectMetricForProperty(p, name, comment, re, ctx)
for _, p := range c.props[5:] {
c.collectMetricForProperty(p, re, ctx)
}
}
func (c *interfaceCollector) collectMetricForProperty(property, iface, comment string, re *proto.Sentence, ctx *collectorContext) {
func (c *interfaceCollector) collectMetricForProperty(property string, re *proto.Sentence, ctx *collectorContext) {
desc := c.descriptions[property]
if value := re.Map[property]; value != "" {
v, err := strconv.ParseFloat(value, 64)
if err != nil {
log.WithFields(log.Fields{
"device": ctx.device.Name,
"interface": iface,
"property": property,
"value": value,
"error": err,
}).Error("error parsing interface metric value")
return
var (
v float64
err error
)
switch property {
case "running":
if value == "true" {
v = 1
} else {
v = 0
}
default:
v, err = strconv.ParseFloat(value, 64)
if err != nil {
log.WithFields(log.Fields{
"device": ctx.device.Name,
"interface": re.Map["name"],
"property": property,
"value": value,
"error": err,
}).Error("error parsing interface metric value")
return
}
}
ctx.ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, v, ctx.device.Name, ctx.device.Address, iface, comment)
ctx.ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, v, ctx.device.Name, ctx.device.Address,
re.Map["name"], re.Map["type"], re.Map["disabled"], re.Map["comment"], re.Map["running"], re.Map["slave"])
}
}