2021-06-14 12:15:04 +02:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2021-06-25 11:10:20 +02:00
|
|
|
"flag"
|
2021-06-14 12:15:04 +02:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
2021-06-25 11:10:20 +02:00
|
|
|
"time"
|
2021-06-14 12:15:04 +02:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
2022-11-30 06:22:12 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
2021-06-14 12:15:04 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/proxy"
|
|
|
|
)
|
|
|
|
|
2021-06-25 11:10:20 +02:00
|
|
|
// SDCheckInterval defines interval for targets refresh.
|
|
|
|
var SDCheckInterval = flag.Duration("promscrape.digitaloceanSDCheckInterval", time.Minute, "Interval for checking for changes in digital ocean. "+
|
|
|
|
"This works only if digitalocean_sd_configs is configured in '-promscrape.config' file. "+
|
2024-04-18 02:27:47 +02:00
|
|
|
"See https://docs.victoriametrics.com/sd_configs/#digitalocean_sd_configs for details")
|
2021-06-25 11:10:20 +02:00
|
|
|
|
2021-06-14 12:15:04 +02:00
|
|
|
// SDConfig represents service discovery config for digital ocean.
|
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#digitalocean_sd_config
|
|
|
|
type SDConfig struct {
|
|
|
|
Server string `yaml:"server,omitempty"`
|
|
|
|
HTTPClientConfig promauth.HTTPClientConfig `yaml:",inline"`
|
2021-10-26 20:21:08 +02:00
|
|
|
ProxyURL *proxy.URL `yaml:"proxy_url,omitempty"`
|
2021-06-14 12:15:04 +02:00
|
|
|
ProxyClientConfig promauth.ProxyClientConfig `yaml:",inline"`
|
|
|
|
Port int `yaml:"port,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLabels returns Digital Ocean droplet labels according to sdc.
|
2022-11-30 06:22:12 +01:00
|
|
|
func (sdc *SDConfig) GetLabels(baseDir string) ([]*promutils.Labels, error) {
|
2021-06-14 12:15:04 +02:00
|
|
|
cfg, err := getAPIConfig(sdc, baseDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot get API config: %w", err)
|
|
|
|
}
|
|
|
|
droplets, err := getDroplets(cfg.client.GetAPIResponse)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return addDropletLabels(droplets, cfg.port), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://developers.digitalocean.com/documentation/v2/#retrieve-an-existing-droplet-by-id
|
|
|
|
type droplet struct {
|
|
|
|
ID int `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
|
2024-07-09 22:32:54 +02:00
|
|
|
Features []string `json:"features"`
|
|
|
|
Image dropletImage `json:"image"`
|
|
|
|
SizeSlug string `json:"size_slug"`
|
|
|
|
Networks networks `json:"networks"`
|
|
|
|
Region dropletRegion `json:"region"`
|
|
|
|
Tags []string `json:"tags"`
|
|
|
|
VpcUUID string `json:"vpc_uuid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type dropletImage struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Slug string `json:"slug"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type dropletRegion struct {
|
|
|
|
Slug string `json:"slug"`
|
2021-06-14 12:15:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *droplet) getIPByNet(netVersion, netType string) string {
|
|
|
|
var dropletNetworks []network
|
|
|
|
switch netVersion {
|
|
|
|
case "v4":
|
|
|
|
dropletNetworks = d.Networks.V4
|
|
|
|
case "v6":
|
|
|
|
dropletNetworks = d.Networks.V6
|
|
|
|
default:
|
|
|
|
logger.Fatalf("BUG, unexpected network type: %s, want v4 or v6", netVersion)
|
|
|
|
}
|
|
|
|
for _, net := range dropletNetworks {
|
|
|
|
if net.Type == netType {
|
|
|
|
return net.IPAddress
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
type networks struct {
|
|
|
|
V4 []network `json:"v4"`
|
|
|
|
V6 []network `json:"v6"`
|
|
|
|
}
|
|
|
|
type network struct {
|
|
|
|
IPAddress string `json:"ip_address"`
|
|
|
|
// private | public.
|
|
|
|
Type string `json:"type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://developers.digitalocean.com/documentation/v2/#list-all-droplets
|
|
|
|
type listDropletResponse struct {
|
|
|
|
Droplets []droplet `json:"droplets,omitempty"`
|
|
|
|
Links links `json:"links,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type links struct {
|
2024-07-09 22:32:54 +02:00
|
|
|
Pages linksPages `json:"pages,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type linksPages struct {
|
|
|
|
Last string `json:"last,omitempty"`
|
|
|
|
Next string `json:"next,omitempty"`
|
2021-06-14 12:15:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *listDropletResponse) nextURLPath() (string, error) {
|
|
|
|
if r.Links.Pages.Next == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
u, err := url.Parse(r.Links.Pages.Next)
|
|
|
|
if err != nil {
|
2023-10-25 21:24:01 +02:00
|
|
|
return "", fmt.Errorf("cannot parse digital ocean next url: %s: %w", r.Links.Pages.Next, err)
|
2021-06-14 12:15:04 +02:00
|
|
|
}
|
|
|
|
return u.RequestURI(), nil
|
|
|
|
}
|
|
|
|
|
2022-11-30 06:22:12 +01:00
|
|
|
func addDropletLabels(droplets []droplet, defaultPort int) []*promutils.Labels {
|
|
|
|
var ms []*promutils.Labels
|
2021-06-14 12:15:04 +02:00
|
|
|
for _, droplet := range droplets {
|
|
|
|
if len(droplet.Networks.V4) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
privateIPv4 := droplet.getIPByNet("v4", "private")
|
|
|
|
publicIPv4 := droplet.getIPByNet("v4", "public")
|
|
|
|
publicIPv6 := droplet.getIPByNet("v6", "public")
|
|
|
|
|
|
|
|
addr := discoveryutils.JoinHostPort(publicIPv4, defaultPort)
|
2022-11-30 06:22:12 +01:00
|
|
|
m := promutils.NewLabels(16)
|
|
|
|
m.Add("__address__", addr)
|
|
|
|
m.Add("__meta_digitalocean_droplet_id", fmt.Sprintf("%d", droplet.ID))
|
|
|
|
m.Add("__meta_digitalocean_droplet_name", droplet.Name)
|
|
|
|
m.Add("__meta_digitalocean_image", droplet.Image.Slug)
|
|
|
|
m.Add("__meta_digitalocean_image_name", droplet.Image.Name)
|
|
|
|
m.Add("__meta_digitalocean_private_ipv4", privateIPv4)
|
|
|
|
m.Add("__meta_digitalocean_public_ipv4", publicIPv4)
|
|
|
|
m.Add("__meta_digitalocean_public_ipv6", publicIPv6)
|
|
|
|
m.Add("__meta_digitalocean_region", droplet.Region.Slug)
|
|
|
|
m.Add("__meta_digitalocean_size", droplet.SizeSlug)
|
|
|
|
m.Add("__meta_digitalocean_status", droplet.Status)
|
|
|
|
m.Add("__meta_digitalocean_vpc", droplet.VpcUUID)
|
2021-06-14 12:15:04 +02:00
|
|
|
if len(droplet.Features) > 0 {
|
|
|
|
features := fmt.Sprintf(",%s,", strings.Join(droplet.Features, ","))
|
2022-11-30 06:22:12 +01:00
|
|
|
m.Add("__meta_digitalocean_features", features)
|
2021-06-14 12:15:04 +02:00
|
|
|
}
|
|
|
|
if len(droplet.Tags) > 0 {
|
2021-06-17 14:12:20 +02:00
|
|
|
tags := fmt.Sprintf(",%s,", strings.Join(droplet.Tags, ","))
|
2022-11-30 06:22:12 +01:00
|
|
|
m.Add("__meta_digitalocean_tags", tags)
|
2021-06-14 12:15:04 +02:00
|
|
|
}
|
|
|
|
ms = append(ms, m)
|
|
|
|
}
|
|
|
|
return ms
|
|
|
|
}
|
2021-06-25 10:39:18 +02:00
|
|
|
|
|
|
|
// MustStop stops further usage for sdc.
|
|
|
|
func (sdc *SDConfig) MustStop() {
|
2023-07-27 23:47:53 +02:00
|
|
|
v := configMap.Delete(sdc)
|
|
|
|
if v != nil {
|
|
|
|
cfg := v.(*apiConfig)
|
|
|
|
cfg.client.Stop()
|
|
|
|
}
|
2021-06-25 10:39:18 +02:00
|
|
|
}
|