2021-10-18 23:00:44 +00:00
|
|
|
// Copyright 2020 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package uyuni
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-06-03 11:47:14 +00:00
|
|
|
"errors"
|
2021-10-18 23:00:44 +00:00
|
|
|
"fmt"
|
2024-09-10 01:41:53 +00:00
|
|
|
"log/slog"
|
2021-10-18 23:00:44 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
2024-05-13 15:36:19 +00:00
|
|
|
"strconv"
|
2021-10-18 23:00:44 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/kolo/xmlrpc"
|
2023-10-23 13:55:36 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2021-10-18 23:00:44 +00:00
|
|
|
"github.com/prometheus/common/config"
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/discovery"
|
|
|
|
"github.com/prometheus/prometheus/discovery/refresh"
|
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
uyuniXMLRPCAPIPath = "/rpc/api"
|
|
|
|
|
|
|
|
uyuniMetaLabelPrefix = model.MetaLabelPrefix + "uyuni_"
|
|
|
|
uyuniLabelMinionHostname = uyuniMetaLabelPrefix + "minion_hostname"
|
|
|
|
uyuniLabelPrimaryFQDN = uyuniMetaLabelPrefix + "primary_fqdn"
|
2024-09-10 20:32:03 +00:00
|
|
|
uyuniLabelSystemID = uyuniMetaLabelPrefix + "system_id"
|
|
|
|
uyuniLabelGroups = uyuniMetaLabelPrefix + "groups"
|
|
|
|
uyuniLabelEndpointName = uyuniMetaLabelPrefix + "endpoint_name"
|
|
|
|
uyuniLabelExporter = uyuniMetaLabelPrefix + "exporter"
|
2021-10-18 23:00:44 +00:00
|
|
|
uyuniLabelProxyModule = uyuniMetaLabelPrefix + "proxy_module"
|
|
|
|
uyuniLabelMetricsPath = uyuniMetaLabelPrefix + "metrics_path"
|
|
|
|
uyuniLabelScheme = uyuniMetaLabelPrefix + "scheme"
|
2022-02-15 15:38:38 +00:00
|
|
|
|
|
|
|
tokenDuration = 10 * time.Minute
|
2021-10-18 23:00:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultSDConfig is the default Uyuni SD configuration.
|
|
|
|
var DefaultSDConfig = SDConfig{
|
2022-06-08 09:29:44 +00:00
|
|
|
Entitlement: "monitoring_entitled",
|
|
|
|
Separator: ",",
|
|
|
|
RefreshInterval: model.Duration(1 * time.Minute),
|
|
|
|
HTTPClientConfig: config.DefaultHTTPClientConfig,
|
2021-10-18 23:00:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
discovery.RegisterConfig(&SDConfig{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SDConfig is the configuration for Uyuni based service discovery.
|
|
|
|
type SDConfig struct {
|
2021-12-04 20:14:47 +00:00
|
|
|
Server string `yaml:"server"`
|
2021-10-18 23:00:44 +00:00
|
|
|
Username string `yaml:"username"`
|
|
|
|
Password config.Secret `yaml:"password"`
|
|
|
|
HTTPClientConfig config.HTTPClientConfig `yaml:",inline"`
|
|
|
|
Entitlement string `yaml:"entitlement,omitempty"`
|
|
|
|
Separator string `yaml:"separator,omitempty"`
|
|
|
|
RefreshInterval model.Duration `yaml:"refresh_interval,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type systemGroupID struct {
|
|
|
|
GroupID int `xmlrpc:"id"`
|
|
|
|
GroupName string `xmlrpc:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type networkInfo struct {
|
|
|
|
SystemID int `xmlrpc:"system_id"`
|
|
|
|
Hostname string `xmlrpc:"hostname"`
|
|
|
|
PrimaryFQDN string `xmlrpc:"primary_fqdn"`
|
|
|
|
IP string `xmlrpc:"ip"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type endpointInfo struct {
|
|
|
|
SystemID int `xmlrpc:"system_id"`
|
|
|
|
EndpointName string `xmlrpc:"endpoint_name"`
|
|
|
|
Port int `xmlrpc:"port"`
|
|
|
|
Path string `xmlrpc:"path"`
|
|
|
|
Module string `xmlrpc:"module"`
|
|
|
|
ExporterName string `xmlrpc:"exporter_name"`
|
|
|
|
TLSEnabled bool `xmlrpc:"tls_enabled"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discovery periodically performs Uyuni API requests. It implements the Discoverer interface.
|
|
|
|
type Discovery struct {
|
|
|
|
*refresh.Discovery
|
2022-02-15 15:38:38 +00:00
|
|
|
apiURL *url.URL
|
|
|
|
roundTripper http.RoundTripper
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
token string
|
|
|
|
tokenExpiration time.Time
|
|
|
|
entitlement string
|
|
|
|
separator string
|
|
|
|
interval time.Duration
|
2024-09-10 01:41:53 +00:00
|
|
|
logger *slog.Logger
|
2021-10-18 23:00:44 +00:00
|
|
|
}
|
|
|
|
|
2024-01-23 15:53:55 +00:00
|
|
|
// NewDiscovererMetrics implements discovery.Config.
|
|
|
|
func (*SDConfig) NewDiscovererMetrics(reg prometheus.Registerer, rmi discovery.RefreshMetricsInstantiator) discovery.DiscovererMetrics {
|
|
|
|
return &uyuniMetrics{
|
|
|
|
refreshMetrics: rmi,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 23:00:44 +00:00
|
|
|
// Name returns the name of the Config.
|
|
|
|
func (*SDConfig) Name() string { return "uyuni" }
|
|
|
|
|
|
|
|
// NewDiscoverer returns a Discoverer for the Config.
|
|
|
|
func (c *SDConfig) NewDiscoverer(opts discovery.DiscovererOptions) (discovery.Discoverer, error) {
|
2024-01-23 15:53:55 +00:00
|
|
|
return NewDiscovery(c, opts.Logger, opts.Metrics)
|
2021-10-18 23:00:44 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 09:29:44 +00:00
|
|
|
// SetDirectory joins any relative file paths with dir.
|
|
|
|
func (c *SDConfig) SetDirectory(dir string) {
|
|
|
|
c.HTTPClientConfig.SetDirectory(dir)
|
|
|
|
}
|
|
|
|
|
2021-10-18 23:00:44 +00:00
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
|
|
|
func (c *SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
*c = DefaultSDConfig
|
|
|
|
type plain SDConfig
|
|
|
|
err := unmarshal((*plain)(c))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-04 20:14:47 +00:00
|
|
|
if c.Server == "" {
|
2021-10-18 23:00:44 +00:00
|
|
|
return errors.New("Uyuni SD configuration requires server host")
|
|
|
|
}
|
|
|
|
|
2021-12-04 20:14:47 +00:00
|
|
|
_, err = url.Parse(c.Server)
|
2021-10-18 23:00:44 +00:00
|
|
|
if err != nil {
|
2022-06-03 11:47:14 +00:00
|
|
|
return fmt.Errorf("Uyuni Server URL is not valid: %w", err)
|
2021-10-18 23:00:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Username == "" {
|
|
|
|
return errors.New("Uyuni SD configuration requires a username")
|
|
|
|
}
|
|
|
|
if c.Password == "" {
|
|
|
|
return errors.New("Uyuni SD configuration requires a password")
|
|
|
|
}
|
2023-09-08 11:59:49 +00:00
|
|
|
return c.HTTPClientConfig.Validate()
|
2021-10-18 23:00:44 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 15:38:38 +00:00
|
|
|
func login(rpcclient *xmlrpc.Client, user, pass string, duration int) (string, error) {
|
2021-10-18 23:00:44 +00:00
|
|
|
var result string
|
2022-02-15 15:38:38 +00:00
|
|
|
err := rpcclient.Call("auth.login", []interface{}{user, pass, duration}, &result)
|
2021-10-18 23:00:44 +00:00
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2021-10-22 08:06:44 +00:00
|
|
|
func getSystemGroupsInfoOfMonitoredClients(rpcclient *xmlrpc.Client, token, entitlement string) (map[int][]systemGroupID, error) {
|
2021-10-18 23:00:44 +00:00
|
|
|
var systemGroupsInfos []struct {
|
|
|
|
SystemID int `xmlrpc:"id"`
|
|
|
|
SystemGroups []systemGroupID `xmlrpc:"system_groups"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err := rpcclient.Call("system.listSystemGroupsForSystemsWithEntitlement", []interface{}{token, entitlement}, &systemGroupsInfos)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make(map[int][]systemGroupID)
|
|
|
|
for _, systemGroupsInfo := range systemGroupsInfos {
|
|
|
|
result[systemGroupsInfo.SystemID] = systemGroupsInfo.SystemGroups
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNetworkInformationForSystems(rpcclient *xmlrpc.Client, token string, systemIDs []int) (map[int]networkInfo, error) {
|
|
|
|
var networkInfos []networkInfo
|
|
|
|
err := rpcclient.Call("system.getNetworkForSystems", []interface{}{token, systemIDs}, &networkInfos)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make(map[int]networkInfo)
|
|
|
|
for _, networkInfo := range networkInfos {
|
|
|
|
result[networkInfo.SystemID] = networkInfo
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEndpointInfoForSystems(
|
|
|
|
rpcclient *xmlrpc.Client,
|
|
|
|
token string,
|
|
|
|
systemIDs []int,
|
|
|
|
) ([]endpointInfo, error) {
|
|
|
|
var endpointInfos []endpointInfo
|
|
|
|
err := rpcclient.Call(
|
|
|
|
"system.monitoring.listEndpoints",
|
|
|
|
[]interface{}{token, systemIDs}, &endpointInfos)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return endpointInfos, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDiscovery returns a uyuni discovery for the given configuration.
|
2024-09-10 01:41:53 +00:00
|
|
|
func NewDiscovery(conf *SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*Discovery, error) {
|
2024-01-23 15:53:55 +00:00
|
|
|
m, ok := metrics.(*uyuniMetrics)
|
|
|
|
if !ok {
|
2024-11-03 12:15:51 +00:00
|
|
|
return nil, errors.New("invalid discovery metrics type")
|
2024-01-23 15:53:55 +00:00
|
|
|
}
|
|
|
|
|
2021-12-04 20:14:47 +00:00
|
|
|
apiURL, err := url.Parse(conf.Server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-18 23:00:44 +00:00
|
|
|
apiURL.Path = path.Join(apiURL.Path, uyuniXMLRPCAPIPath)
|
|
|
|
|
2021-10-19 19:26:39 +00:00
|
|
|
rt, err := config.NewRoundTripperFromConfig(conf.HTTPClientConfig, "uyuni_sd")
|
2021-10-18 23:00:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
d := &Discovery{
|
|
|
|
apiURL: apiURL,
|
|
|
|
roundTripper: rt,
|
|
|
|
username: conf.Username,
|
|
|
|
password: string(conf.Password),
|
|
|
|
entitlement: conf.Entitlement,
|
|
|
|
separator: conf.Separator,
|
|
|
|
interval: time.Duration(conf.RefreshInterval),
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Discovery = refresh.NewDiscovery(
|
2023-10-23 13:55:36 +00:00
|
|
|
refresh.Options{
|
2024-01-23 15:53:55 +00:00
|
|
|
Logger: logger,
|
|
|
|
Mech: "uyuni",
|
|
|
|
Interval: time.Duration(conf.RefreshInterval),
|
|
|
|
RefreshF: d.refresh,
|
|
|
|
MetricsInstantiator: m.refreshMetrics,
|
2023-10-23 13:55:36 +00:00
|
|
|
},
|
2021-10-18 23:00:44 +00:00
|
|
|
)
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Discovery) getEndpointLabels(
|
|
|
|
endpoint endpointInfo,
|
|
|
|
systemGroupIDs []systemGroupID,
|
|
|
|
networkInfo networkInfo,
|
|
|
|
) model.LabelSet {
|
|
|
|
var addr, scheme string
|
|
|
|
managedGroupNames := getSystemGroupNames(systemGroupIDs)
|
|
|
|
addr = fmt.Sprintf("%s:%d", networkInfo.Hostname, endpoint.Port)
|
|
|
|
if endpoint.TLSEnabled {
|
|
|
|
scheme = "https"
|
|
|
|
} else {
|
|
|
|
scheme = "http"
|
|
|
|
}
|
|
|
|
|
|
|
|
result := model.LabelSet{
|
|
|
|
model.AddressLabel: model.LabelValue(addr),
|
|
|
|
uyuniLabelMinionHostname: model.LabelValue(networkInfo.Hostname),
|
|
|
|
uyuniLabelPrimaryFQDN: model.LabelValue(networkInfo.PrimaryFQDN),
|
2024-09-10 20:32:03 +00:00
|
|
|
uyuniLabelSystemID: model.LabelValue(strconv.Itoa(endpoint.SystemID)),
|
|
|
|
uyuniLabelGroups: model.LabelValue(strings.Join(managedGroupNames, d.separator)),
|
|
|
|
uyuniLabelEndpointName: model.LabelValue(endpoint.EndpointName),
|
|
|
|
uyuniLabelExporter: model.LabelValue(endpoint.ExporterName),
|
2021-10-18 23:00:44 +00:00
|
|
|
uyuniLabelProxyModule: model.LabelValue(endpoint.Module),
|
|
|
|
uyuniLabelMetricsPath: model.LabelValue(endpoint.Path),
|
|
|
|
uyuniLabelScheme: model.LabelValue(scheme),
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSystemGroupNames(systemGroupsIDs []systemGroupID) []string {
|
|
|
|
managedGroupNames := make([]string, 0, len(systemGroupsIDs))
|
|
|
|
for _, systemGroupInfo := range systemGroupsIDs {
|
|
|
|
managedGroupNames = append(managedGroupNames, systemGroupInfo.GroupName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return managedGroupNames
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Discovery) getTargetsForSystems(
|
|
|
|
rpcClient *xmlrpc.Client,
|
|
|
|
entitlement string,
|
|
|
|
) ([]model.LabelSet, error) {
|
|
|
|
result := make([]model.LabelSet, 0)
|
|
|
|
|
2022-02-15 15:38:38 +00:00
|
|
|
systemGroupIDsBySystemID, err := getSystemGroupsInfoOfMonitoredClients(rpcClient, d.token, entitlement)
|
2021-10-18 23:00:44 +00:00
|
|
|
if err != nil {
|
2022-06-03 11:47:14 +00:00
|
|
|
return nil, fmt.Errorf("unable to get the managed system groups information of monitored clients: %w", err)
|
2021-10-18 23:00:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
systemIDs := make([]int, 0, len(systemGroupIDsBySystemID))
|
|
|
|
for systemID := range systemGroupIDsBySystemID {
|
|
|
|
systemIDs = append(systemIDs, systemID)
|
|
|
|
}
|
|
|
|
|
2022-02-15 15:38:38 +00:00
|
|
|
endpointInfos, err := getEndpointInfoForSystems(rpcClient, d.token, systemIDs)
|
2021-10-18 23:00:44 +00:00
|
|
|
if err != nil {
|
2022-06-03 11:47:14 +00:00
|
|
|
return nil, fmt.Errorf("unable to get endpoints information: %w", err)
|
2021-10-18 23:00:44 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 15:38:38 +00:00
|
|
|
networkInfoBySystemID, err := getNetworkInformationForSystems(rpcClient, d.token, systemIDs)
|
2021-10-18 23:00:44 +00:00
|
|
|
if err != nil {
|
2022-06-03 11:47:14 +00:00
|
|
|
return nil, fmt.Errorf("unable to get the systems network information: %w", err)
|
2021-10-18 23:00:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, endpoint := range endpointInfos {
|
|
|
|
systemID := endpoint.SystemID
|
|
|
|
labels := d.getEndpointLabels(
|
|
|
|
endpoint,
|
|
|
|
systemGroupIDsBySystemID[systemID],
|
|
|
|
networkInfoBySystemID[systemID])
|
|
|
|
result = append(result, labels)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-02-15 15:38:38 +00:00
|
|
|
func (d *Discovery) refresh(_ context.Context) ([]*targetgroup.Group, error) {
|
2021-10-18 23:00:44 +00:00
|
|
|
rpcClient, err := xmlrpc.NewClient(d.apiURL.String(), d.roundTripper)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rpcClient.Close()
|
|
|
|
|
2022-02-15 15:38:38 +00:00
|
|
|
if time.Now().After(d.tokenExpiration) {
|
|
|
|
// Uyuni API takes duration in seconds.
|
|
|
|
d.token, err = login(rpcClient, d.username, d.password, int(tokenDuration.Seconds()))
|
|
|
|
if err != nil {
|
2022-06-03 11:47:14 +00:00
|
|
|
return nil, fmt.Errorf("unable to login to Uyuni API: %w", err)
|
2021-10-18 23:00:44 +00:00
|
|
|
}
|
2022-02-15 15:38:38 +00:00
|
|
|
// Login again at half the token lifetime.
|
|
|
|
d.tokenExpiration = time.Now().Add(tokenDuration / 2)
|
|
|
|
}
|
2021-10-18 23:00:44 +00:00
|
|
|
|
2022-02-15 15:38:38 +00:00
|
|
|
targetsForSystems, err := d.getTargetsForSystems(rpcClient, d.entitlement)
|
2021-10-18 23:00:44 +00:00
|
|
|
if err != nil {
|
2022-02-15 15:38:38 +00:00
|
|
|
// Force the renewal of the token on next refresh.
|
|
|
|
d.tokenExpiration = time.Now()
|
2021-10-18 23:00:44 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return []*targetgroup.Group{{Targets: targetsForSystems, Source: d.apiURL.String()}}, nil
|
|
|
|
}
|