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"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-06-11 09:17:59 -07:00
|
|
|
"github.com/go-kit/log"
|
2020-08-31 04:24:39 -07:00
|
|
|
"github.com/prometheus/common/config"
|
2020-08-21 06:49:19 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2020-10-05 04:34:41 -07:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
hetznerRobotLabelPrefix = hetznerLabelPrefix + "robot_"
|
|
|
|
hetznerLabelRobotProduct = hetznerRobotLabelPrefix + "product"
|
|
|
|
hetznerLabelRobotCancelled = hetznerRobotLabelPrefix + "cancelled"
|
|
|
|
)
|
|
|
|
|
2020-10-05 04:34:41 -07:00
|
|
|
var userAgent = fmt.Sprintf("Prometheus/%s", version.Version)
|
|
|
|
|
2020-08-21 06:49:19 -07:00
|
|
|
// Discovery periodically performs Hetzner Robot requests. It implements
|
|
|
|
// the Discoverer interface.
|
|
|
|
type robotDiscovery struct {
|
|
|
|
*refresh.Discovery
|
|
|
|
client *http.Client
|
|
|
|
port int
|
|
|
|
endpoint string
|
|
|
|
}
|
|
|
|
|
|
|
|
// newRobotDiscovery returns a new robotDiscovery which periodically refreshes its targets.
|
2023-04-12 04:05:41 -07:00
|
|
|
func newRobotDiscovery(conf *SDConfig, _ log.Logger) (*robotDiscovery, error) {
|
2020-08-21 06:49:19 -07:00
|
|
|
d := &robotDiscovery{
|
|
|
|
port: conf.Port,
|
|
|
|
endpoint: conf.robotEndpoint,
|
|
|
|
}
|
|
|
|
|
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 = &http.Client{
|
|
|
|
Transport: rt,
|
|
|
|
Timeout: time.Duration(conf.RefreshInterval),
|
|
|
|
}
|
|
|
|
|
|
|
|
return d, nil
|
|
|
|
}
|
2021-10-22 01:06:44 -07:00
|
|
|
|
2023-04-12 04:05:41 -07:00
|
|
|
func (d *robotDiscovery) refresh(context.Context) ([]*targetgroup.Group, error) {
|
2020-10-05 04:34:41 -07:00
|
|
|
req, err := http.NewRequest("GET", d.endpoint+"/server", nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("User-Agent", userAgent)
|
|
|
|
|
|
|
|
resp, err := d.client.Do(req)
|
2020-08-21 06:49:19 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-31 04:24:39 -07:00
|
|
|
|
2020-08-21 06:49:19 -07:00
|
|
|
defer func() {
|
2022-04-27 02:24:36 -07:00
|
|
|
io.Copy(io.Discard, resp.Body)
|
2020-08-21 06:49:19 -07:00
|
|
|
resp.Body.Close()
|
|
|
|
}()
|
2020-08-31 04:24:39 -07:00
|
|
|
|
|
|
|
if resp.StatusCode/100 != 2 {
|
2022-06-03 04:47:14 -07:00
|
|
|
return nil, fmt.Errorf("non 2xx status '%d' response during hetzner service discovery with role robot", resp.StatusCode)
|
2020-08-31 04:24:39 -07:00
|
|
|
}
|
|
|
|
|
2022-04-27 02:24:36 -07:00
|
|
|
b, err := io.ReadAll(resp.Body)
|
2021-07-02 01:38:14 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-21 06:49:19 -07:00
|
|
|
var servers serversList
|
2021-07-02 01:38:14 -07:00
|
|
|
err = json.Unmarshal(b, &servers)
|
2020-08-21 06:49:19 -07:00
|
|
|
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(HetznerRoleRobot),
|
2020-08-21 06:49:19 -07:00
|
|
|
hetznerLabelServerID: model.LabelValue(strconv.Itoa(server.Server.ServerNumber)),
|
|
|
|
hetznerLabelServerName: model.LabelValue(server.Server.ServerName),
|
|
|
|
hetznerLabelDatacenter: model.LabelValue(strings.ToLower(server.Server.Dc)),
|
|
|
|
hetznerLabelPublicIPv4: model.LabelValue(server.Server.ServerIP),
|
|
|
|
hetznerLabelServerStatus: model.LabelValue(server.Server.Status),
|
|
|
|
hetznerLabelRobotProduct: model.LabelValue(server.Server.Product),
|
|
|
|
hetznerLabelRobotCancelled: model.LabelValue(fmt.Sprintf("%t", server.Server.Canceled)),
|
|
|
|
|
|
|
|
model.AddressLabel: model.LabelValue(net.JoinHostPort(server.Server.ServerIP, strconv.FormatUint(uint64(d.port), 10))),
|
|
|
|
}
|
|
|
|
for _, subnet := range server.Server.Subnet {
|
|
|
|
ip := net.ParseIP(subnet.IP)
|
|
|
|
if ip.To4() == nil {
|
|
|
|
labels[hetznerLabelPublicIPv6Network] = model.LabelValue(fmt.Sprintf("%s/%s", subnet.IP, subnet.Mask))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
targets[i] = labels
|
|
|
|
}
|
|
|
|
return []*targetgroup.Group{{Source: "hetzner", Targets: targets}}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type serversList []struct {
|
|
|
|
Server struct {
|
|
|
|
ServerIP string `json:"server_ip"`
|
|
|
|
ServerNumber int `json:"server_number"`
|
|
|
|
ServerName string `json:"server_name"`
|
|
|
|
Dc string `json:"dc"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
Product string `json:"product"`
|
|
|
|
Canceled bool `json:"cancelled"`
|
|
|
|
Subnet []struct {
|
|
|
|
IP string `json:"ip"`
|
|
|
|
Mask string `json:"mask"`
|
|
|
|
} `json:"subnet"`
|
|
|
|
} `json:"server"`
|
|
|
|
}
|