feat(output/prometheus-sd): improve and add labels (#216)
This commit is contained in:
parent
82129764c9
commit
c5703e0975
|
@ -30,6 +30,24 @@ const (
|
||||||
TargetAddressNodeID TargetAddressType = "node_id"
|
TargetAddressNodeID TargetAddressType = "node_id"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type TargetAddressFunc func(*runtime.Node) string
|
||||||
|
|
||||||
|
var TargetAddressTypeFuncs = map[TargetAddressType]TargetAddressFunc{
|
||||||
|
TargetAddressIP: func(n *runtime.Node) string {
|
||||||
|
if addr := n.Address; addr != nil {
|
||||||
|
return addr.IP.String()
|
||||||
|
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
},
|
||||||
|
TargetAddressNodeID: func(n *runtime.Node) string {
|
||||||
|
if ni := n.Nodeinfo; ni != nil {
|
||||||
|
return ni.NodeID
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
func (c Config) TargetAddress() TargetAddressType {
|
func (c Config) TargetAddress() TargetAddressType {
|
||||||
if v, ok := c["target_address"]; ok {
|
if v, ok := c["target_address"]; ok {
|
||||||
return TargetAddressType(v.(string))
|
return TargetAddressType(v.(string))
|
||||||
|
@ -67,27 +85,67 @@ type Targets struct {
|
||||||
Labels map[string]interface{} `json:"labels,omitempty"`
|
Labels map[string]interface{} `json:"labels,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func toTargets(n *runtime.Node, defaultLabels map[string]interface{}, targetFunc TargetAddressFunc) *Targets {
|
||||||
|
target := targetFunc(n)
|
||||||
|
if target == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
labels := map[string]interface{}{}
|
||||||
|
for k, v := range defaultLabels {
|
||||||
|
labels[k] = v
|
||||||
|
}
|
||||||
|
if ni := n.Nodeinfo; ni != nil {
|
||||||
|
labels["node_id"] = ni.NodeID
|
||||||
|
labels["hostname"] = ni.Hostname
|
||||||
|
// model
|
||||||
|
if model := ni.Hardware.Model; model != "" {
|
||||||
|
labels["model"] = model
|
||||||
|
}
|
||||||
|
// system
|
||||||
|
if siteCode := ni.System.SiteCode; siteCode != "" {
|
||||||
|
labels["site_code"] = siteCode
|
||||||
|
}
|
||||||
|
if domainCode := ni.System.DomainCode; domainCode != "" {
|
||||||
|
labels["domain_code"] = domainCode
|
||||||
|
}
|
||||||
|
if primaryDomainCode := ni.System.PrimaryDomainCode; primaryDomainCode != "" {
|
||||||
|
labels["primary_domain_code"] = primaryDomainCode
|
||||||
|
}
|
||||||
|
|
||||||
|
// owner
|
||||||
|
if owner := ni.Owner; owner != nil {
|
||||||
|
labels["owner"] = owner.Contact
|
||||||
|
}
|
||||||
|
|
||||||
|
// wireless - airtime
|
||||||
|
if wifi := ni.Wireless; wifi != nil {
|
||||||
|
labels["wifi_txpower24"] = wifi.TxPower24
|
||||||
|
labels["wifi_channel24"] = wifi.Channel24
|
||||||
|
labels["wifi_txpower5"] = wifi.TxPower5
|
||||||
|
labels["wifi_channel5"] = wifi.Channel5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &Targets{
|
||||||
|
Targets: []string{target},
|
||||||
|
Labels: labels,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (o *Output) Save(nodes *runtime.Nodes) {
|
func (o *Output) Save(nodes *runtime.Nodes) {
|
||||||
nodes.RLock()
|
nodes.RLock()
|
||||||
defer nodes.RUnlock()
|
defer nodes.RUnlock()
|
||||||
|
|
||||||
targets := &Targets{
|
targetFunc, ok := TargetAddressTypeFuncs[o.targetType]
|
||||||
Targets: []string{},
|
if !ok {
|
||||||
Labels: o.labels,
|
return
|
||||||
}
|
}
|
||||||
if o.targetType == TargetAddressNodeID {
|
targets := []*Targets{}
|
||||||
for _, n := range nodes.List {
|
for _, n := range nodes.List {
|
||||||
if ni := n.Nodeinfo; ni != nil {
|
if t := toTargets(n, o.labels, targetFunc); t != nil {
|
||||||
targets.Targets = append(targets.Targets, ni.NodeID)
|
targets = append(targets, t)
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for _, n := range nodes.List {
|
|
||||||
if addr := n.Address; addr != nil {
|
|
||||||
targets.Targets = append(targets.Targets, addr.IP.String())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
runtime.SaveJSON([]interface{}{targets}, o.path)
|
runtime.SaveJSON(targets, o.path)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,9 +5,9 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/FreifunkBremen/yanic/data"
|
"github.com/FreifunkBremen/yanic/data"
|
||||||
"github.com/FreifunkBremen/yanic/runtime"
|
"github.com/FreifunkBremen/yanic/runtime"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestOutput(t *testing.T) {
|
func TestOutput(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue