2020-08-21 06:49:19 -07: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 hetzner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2021-06-11 09:17:59 -07:00
|
|
|
"github.com/go-kit/log"
|
2023-07-17 05:21:18 -07:00
|
|
|
"github.com/hetznercloud/hcloud-go/v2/hcloud"
|
2020-08-21 06:49:19 -07:00
|
|
|
"github.com/prometheus/common/config"
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
"github.com/prometheus/common/version"
|
2020-10-22 02:00:08 -07:00
|
|
|
|
2020-08-21 06:49:19 -07:00
|
|
|
"github.com/prometheus/prometheus/discovery/refresh"
|
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
|
|
|
"github.com/prometheus/prometheus/util/strutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
hetznerHcloudLabelPrefix = hetznerLabelPrefix + "hcloud_"
|
|
|
|
hetznerLabelHcloudImageName = hetznerHcloudLabelPrefix + "image_name"
|
|
|
|
hetznerLabelHcloudImageDescription = hetznerHcloudLabelPrefix + "image_description"
|
|
|
|
hetznerLabelHcloudImageOSVersion = hetznerHcloudLabelPrefix + "image_os_version"
|
|
|
|
hetznerLabelHcloudImageOSFlavor = hetznerHcloudLabelPrefix + "image_os_flavor"
|
|
|
|
hetznerLabelHcloudPrivateIPv4 = hetznerHcloudLabelPrefix + "private_ipv4_"
|
|
|
|
hetznerLabelHcloudDatacenterLocation = hetznerHcloudLabelPrefix + "datacenter_location"
|
|
|
|
hetznerLabelHcloudDatacenterLocationNetworkZone = hetznerHcloudLabelPrefix + "datacenter_location_network_zone"
|
|
|
|
hetznerLabelHcloudCPUCores = hetznerHcloudLabelPrefix + "cpu_cores"
|
|
|
|
hetznerLabelHcloudCPUType = hetznerHcloudLabelPrefix + "cpu_type"
|
|
|
|
hetznerLabelHcloudMemoryGB = hetznerHcloudLabelPrefix + "memory_size_gb"
|
|
|
|
hetznerLabelHcloudDiskGB = hetznerHcloudLabelPrefix + "disk_size_gb"
|
|
|
|
hetznerLabelHcloudType = hetznerHcloudLabelPrefix + "server_type"
|
|
|
|
hetznerLabelHcloudLabel = hetznerHcloudLabelPrefix + "label_"
|
2021-07-02 16:51:50 -07:00
|
|
|
hetznerLabelHcloudLabelPresent = hetznerHcloudLabelPrefix + "labelpresent_"
|
2020-08-21 06:49:19 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Discovery periodically performs Hetzner Cloud requests. It implements
|
|
|
|
// the Discoverer interface.
|
|
|
|
type hcloudDiscovery struct {
|
|
|
|
*refresh.Discovery
|
|
|
|
client *hcloud.Client
|
|
|
|
port int
|
|
|
|
}
|
|
|
|
|
|
|
|
// newHcloudDiscovery returns a new hcloudDiscovery which periodically refreshes its targets.
|
2023-04-12 04:05:41 -07:00
|
|
|
func newHcloudDiscovery(conf *SDConfig, _ log.Logger) (*hcloudDiscovery, error) {
|
2020-08-21 06:49:19 -07:00
|
|
|
d := &hcloudDiscovery{
|
|
|
|
port: conf.Port,
|
|
|
|
}
|
|
|
|
|
2021-09-26 14:16:12 -07:00
|
|
|
rt, err := config.NewRoundTripperFromConfig(conf.HTTPClientConfig, "hetzner_sd")
|
2020-08-21 06:49:19 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
d.client = hcloud.NewClient(
|
|
|
|
hcloud.WithApplication("Prometheus", version.Version),
|
|
|
|
hcloud.WithHTTPClient(&http.Client{
|
|
|
|
Transport: rt,
|
|
|
|
Timeout: time.Duration(conf.RefreshInterval),
|
|
|
|
}),
|
|
|
|
hcloud.WithEndpoint(conf.hcloudEndpoint),
|
|
|
|
)
|
|
|
|
return d, nil
|
|
|
|
}
|
2021-10-22 01:06:44 -07:00
|
|
|
|
2020-08-21 06:49:19 -07:00
|
|
|
func (d *hcloudDiscovery) refresh(ctx context.Context) ([]*targetgroup.Group, error) {
|
|
|
|
servers, err := d.client.Server.All(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
networks, err := d.client.Network.All(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
targets := make([]model.LabelSet, len(servers))
|
|
|
|
for i, server := range servers {
|
|
|
|
labels := model.LabelSet{
|
2023-07-31 03:51:41 -07:00
|
|
|
hetznerLabelRole: model.LabelValue(HetznerRoleHcloud),
|
2024-05-13 08:36:19 -07:00
|
|
|
hetznerLabelServerID: model.LabelValue(strconv.FormatInt(server.ID, 10)),
|
2020-08-21 06:49:19 -07:00
|
|
|
hetznerLabelServerName: model.LabelValue(server.Name),
|
|
|
|
hetznerLabelDatacenter: model.LabelValue(server.Datacenter.Name),
|
|
|
|
hetznerLabelPublicIPv4: model.LabelValue(server.PublicNet.IPv4.IP.String()),
|
|
|
|
hetznerLabelPublicIPv6Network: model.LabelValue(server.PublicNet.IPv6.Network.String()),
|
|
|
|
hetznerLabelServerStatus: model.LabelValue(server.Status),
|
|
|
|
|
|
|
|
hetznerLabelHcloudDatacenterLocation: model.LabelValue(server.Datacenter.Location.Name),
|
|
|
|
hetznerLabelHcloudDatacenterLocationNetworkZone: model.LabelValue(server.Datacenter.Location.NetworkZone),
|
|
|
|
hetznerLabelHcloudType: model.LabelValue(server.ServerType.Name),
|
2024-05-13 08:36:19 -07:00
|
|
|
hetznerLabelHcloudCPUCores: model.LabelValue(strconv.Itoa(server.ServerType.Cores)),
|
2020-08-21 06:49:19 -07:00
|
|
|
hetznerLabelHcloudCPUType: model.LabelValue(server.ServerType.CPUType),
|
2024-05-13 08:36:19 -07:00
|
|
|
hetznerLabelHcloudMemoryGB: model.LabelValue(strconv.Itoa(int(server.ServerType.Memory))),
|
|
|
|
hetznerLabelHcloudDiskGB: model.LabelValue(strconv.Itoa(server.ServerType.Disk)),
|
2020-08-21 06:49:19 -07:00
|
|
|
|
|
|
|
model.AddressLabel: model.LabelValue(net.JoinHostPort(server.PublicNet.IPv4.IP.String(), strconv.FormatUint(uint64(d.port), 10))),
|
|
|
|
}
|
|
|
|
|
|
|
|
if server.Image != nil {
|
|
|
|
labels[hetznerLabelHcloudImageName] = model.LabelValue(server.Image.Name)
|
|
|
|
labels[hetznerLabelHcloudImageDescription] = model.LabelValue(server.Image.Description)
|
|
|
|
labels[hetznerLabelHcloudImageOSVersion] = model.LabelValue(server.Image.OSVersion)
|
|
|
|
labels[hetznerLabelHcloudImageOSFlavor] = model.LabelValue(server.Image.OSFlavor)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, privateNet := range server.PrivateNet {
|
|
|
|
for _, network := range networks {
|
|
|
|
if privateNet.Network.ID == network.ID {
|
|
|
|
networkLabel := model.LabelName(hetznerLabelHcloudPrivateIPv4 + strutil.SanitizeLabelName(network.Name))
|
|
|
|
labels[networkLabel] = model.LabelValue(privateNet.IP.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for labelKey, labelValue := range server.Labels {
|
2021-07-02 16:51:50 -07:00
|
|
|
presentLabel := model.LabelName(hetznerLabelHcloudLabelPresent + strutil.SanitizeLabelName(labelKey))
|
|
|
|
labels[presentLabel] = model.LabelValue("true")
|
|
|
|
|
2020-08-21 06:49:19 -07:00
|
|
|
label := model.LabelName(hetznerLabelHcloudLabel + strutil.SanitizeLabelName(labelKey))
|
|
|
|
labels[label] = model.LabelValue(labelValue)
|
|
|
|
}
|
|
|
|
targets[i] = labels
|
|
|
|
}
|
|
|
|
return []*targetgroup.Group{{Source: "hetzner", Targets: targets}}, nil
|
|
|
|
}
|