2021-06-25 10:20:18 +00:00
|
|
|
package dockerswarm
|
2020-10-12 10:38:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
2022-11-30 05:22:12 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
2020-10-12 10:38:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// See https://docs.docker.com/engine/api/v1.40/#tag/Node
|
|
|
|
type node struct {
|
|
|
|
ID string
|
|
|
|
Spec struct {
|
|
|
|
Labels map[string]string
|
|
|
|
Role string
|
|
|
|
Availability string
|
|
|
|
}
|
|
|
|
Description struct {
|
|
|
|
Hostname string
|
|
|
|
Platform struct {
|
|
|
|
Architecture string
|
|
|
|
OS string
|
|
|
|
}
|
|
|
|
Engine struct {
|
|
|
|
EngineVersion string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Status struct {
|
|
|
|
State string
|
|
|
|
Message string
|
|
|
|
Addr string
|
|
|
|
}
|
2020-10-12 13:12:36 +00:00
|
|
|
ManagerStatus struct {
|
2020-10-12 10:38:21 +00:00
|
|
|
Leader bool
|
|
|
|
Reachability string
|
|
|
|
Addr string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 05:22:12 +00:00
|
|
|
func getNodesLabels(cfg *apiConfig) ([]*promutils.Labels, error) {
|
2020-10-12 10:38:21 +00:00
|
|
|
nodes, err := getNodes(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return addNodeLabels(nodes, cfg.port), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNodes(cfg *apiConfig) ([]node, error) {
|
2023-01-12 06:50:34 +00:00
|
|
|
filtersQueryArg := ""
|
2023-01-12 07:02:45 +00:00
|
|
|
if cfg.role == "nodes" {
|
2023-01-12 06:50:34 +00:00
|
|
|
filtersQueryArg = cfg.filtersQueryArg
|
|
|
|
}
|
|
|
|
resp, err := cfg.getAPIResponse("/nodes", filtersQueryArg)
|
2020-10-12 10:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot query dockerswarm api for nodes: %w", err)
|
|
|
|
}
|
|
|
|
return parseNodes(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseNodes(data []byte) ([]node, error) {
|
|
|
|
var nodes []node
|
|
|
|
if err := json.Unmarshal(data, &nodes); err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse nodes: %w", err)
|
|
|
|
}
|
|
|
|
return nodes, nil
|
|
|
|
}
|
|
|
|
|
2022-11-30 05:22:12 +00:00
|
|
|
func addNodeLabels(nodes []node, port int) []*promutils.Labels {
|
|
|
|
var ms []*promutils.Labels
|
2020-10-12 10:38:21 +00:00
|
|
|
for _, node := range nodes {
|
2022-11-30 05:22:12 +00:00
|
|
|
m := promutils.NewLabels(16)
|
|
|
|
m.Add("__address__", discoveryutils.JoinHostPort(node.Status.Addr, port))
|
|
|
|
m.Add("__meta_dockerswarm_node_address", node.Status.Addr)
|
|
|
|
m.Add("__meta_dockerswarm_node_availability", node.Spec.Availability)
|
|
|
|
m.Add("__meta_dockerswarm_node_engine_version", node.Description.Engine.EngineVersion)
|
|
|
|
m.Add("__meta_dockerswarm_node_hostname", node.Description.Hostname)
|
|
|
|
m.Add("__meta_dockerswarm_node_id", node.ID)
|
|
|
|
m.Add("__meta_dockerswarm_node_manager_address", node.ManagerStatus.Addr)
|
|
|
|
m.Add("__meta_dockerswarm_node_manager_leader", fmt.Sprintf("%t", node.ManagerStatus.Leader))
|
|
|
|
m.Add("__meta_dockerswarm_node_manager_reachability", node.ManagerStatus.Reachability)
|
|
|
|
m.Add("__meta_dockerswarm_node_platform_architecture", node.Description.Platform.Architecture)
|
|
|
|
m.Add("__meta_dockerswarm_node_platform_os", node.Description.Platform.OS)
|
|
|
|
m.Add("__meta_dockerswarm_node_role", node.Spec.Role)
|
|
|
|
m.Add("__meta_dockerswarm_node_status", node.Status.State)
|
2020-10-12 10:38:21 +00:00
|
|
|
for k, v := range node.Spec.Labels {
|
2022-11-30 05:22:12 +00:00
|
|
|
m.Add(discoveryutils.SanitizeLabelName("__meta_dockerswarm_node_label_"+k), v)
|
2020-10-12 10:38:21 +00:00
|
|
|
}
|
|
|
|
ms = append(ms, m)
|
|
|
|
}
|
|
|
|
return ms
|
|
|
|
}
|