2022-08-04 17:44:16 +00:00
|
|
|
package yandexcloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
2022-11-30 05:22:12 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
2022-08-04 17:44:16 +00:00
|
|
|
)
|
|
|
|
|
2022-11-30 05:22:12 +00:00
|
|
|
func getInstancesLabels(cfg *apiConfig) ([]*promutils.Labels, error) {
|
2022-08-04 17:44:16 +00:00
|
|
|
organizations, err := cfg.getOrganizations()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
clouds, err := cfg.getClouds(organizations)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
folders, err := cfg.getFolders(clouds)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var instances []instance
|
|
|
|
for _, fld := range folders {
|
|
|
|
inst, err := cfg.getInstances(fld.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
instances = append(instances, inst...)
|
|
|
|
}
|
|
|
|
|
2022-08-04 19:26:38 +00:00
|
|
|
logger.Infof("yandexcloud_sd: collected %d instances", len(instances))
|
2022-08-04 17:44:16 +00:00
|
|
|
|
|
|
|
return addInstanceLabels(instances), nil
|
|
|
|
}
|
|
|
|
|
2022-11-30 05:22:12 +00:00
|
|
|
func addInstanceLabels(instances []instance) []*promutils.Labels {
|
|
|
|
var ms []*promutils.Labels
|
2022-08-04 17:44:16 +00:00
|
|
|
for _, server := range instances {
|
2022-11-30 05:22:12 +00:00
|
|
|
m := promutils.NewLabels(24)
|
|
|
|
m.Add("__address__", server.FQDN)
|
|
|
|
m.Add("__meta_yandexcloud_instance_name", server.Name)
|
|
|
|
m.Add("__meta_yandexcloud_instance_fqdn", server.FQDN)
|
|
|
|
m.Add("__meta_yandexcloud_instance_id", server.ID)
|
|
|
|
m.Add("__meta_yandexcloud_instance_status", server.Status)
|
|
|
|
m.Add("__meta_yandexcloud_instance_platform_id", server.PlatformID)
|
|
|
|
m.Add("__meta_yandexcloud_instance_resources_cores", server.Resources.Cores)
|
|
|
|
m.Add("__meta_yandexcloud_instance_resources_core_fraction", server.Resources.CoreFraction)
|
|
|
|
m.Add("__meta_yandexcloud_instance_resources_memory", server.Resources.Memory)
|
|
|
|
m.Add("__meta_yandexcloud_folder_id", server.FolderID)
|
2022-08-04 17:44:16 +00:00
|
|
|
for k, v := range server.Labels {
|
2022-11-30 05:22:12 +00:00
|
|
|
m.Add(discoveryutils.SanitizeLabelName("__meta_yandexcloud_instance_label_"+k), v)
|
2022-08-04 17:44:16 +00:00
|
|
|
}
|
2022-08-04 19:26:38 +00:00
|
|
|
for _, ni := range server.NetworkInterfaces {
|
|
|
|
privateIPLabel := fmt.Sprintf("__meta_yandexcloud_instance_private_ip_%s", ni.Index)
|
2022-11-30 05:22:12 +00:00
|
|
|
m.Add(privateIPLabel, ni.PrimaryV4Address.Address)
|
2022-08-04 19:26:38 +00:00
|
|
|
if len(ni.PrimaryV4Address.OneToOneNat.Address) > 0 {
|
|
|
|
publicIPLabel := fmt.Sprintf("__meta_yandexcloud_instance_public_ip_%s", ni.Index)
|
2022-11-30 05:22:12 +00:00
|
|
|
m.Add(publicIPLabel, ni.PrimaryV4Address.OneToOneNat.Address)
|
2022-08-04 17:44:16 +00:00
|
|
|
}
|
2022-08-04 19:26:38 +00:00
|
|
|
for j, dnsRecord := range ni.PrimaryV4Address.DNSRecords {
|
2022-08-04 17:44:16 +00:00
|
|
|
dnsRecordLabel := fmt.Sprintf("__meta_yandexcloud_instance_private_dns_%d", j)
|
2022-11-30 05:22:12 +00:00
|
|
|
m.Add(dnsRecordLabel, dnsRecord.FQDN)
|
2022-08-04 17:44:16 +00:00
|
|
|
}
|
2022-08-04 19:26:38 +00:00
|
|
|
for j, dnsRecord := range ni.PrimaryV4Address.OneToOneNat.DNSRecords {
|
2022-08-04 17:44:16 +00:00
|
|
|
dnsRecordLabel := fmt.Sprintf("__meta_yandexcloud_instance_public_dns_%d", j)
|
2022-11-30 05:22:12 +00:00
|
|
|
m.Add(dnsRecordLabel, dnsRecord.FQDN)
|
2022-08-04 17:44:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ms = append(ms, m)
|
|
|
|
}
|
|
|
|
return ms
|
|
|
|
}
|