VictoriaMetrics/lib/promscrape/discovery/openstack/hypervisor.go
Nikolay Khramchikhin cbe3cf683b
Adds openstack sd (#811)
* adds openstack service discovery

https://github.com/VictoriaMetrics/VictoriaMetrics/issues/728

 implemented hypervisors and instance discovery with openstack v3 api.
 Added tests for labeling and data parsing.
 Added token refresh.

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: Aliaksandr Valialkin <valyala@gmail.com>
2020-10-05 16:45:33 +03:00

91 lines
2.3 KiB
Go

package openstack
import (
"encoding/json"
"fmt"
"path"
"strconv"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
)
// https://docs.openstack.org/api-ref/compute/?expanded=list-servers-detailed-detail#list-hypervisors-details
type hypervisorDetail struct {
Hypervisors []hypervisor `json:"hypervisors"`
Links []struct {
HREF string `json:"href"`
Rel string `json:"rel,omitempty"`
} `json:"hypervisors_links,omitempty"`
}
type hypervisor struct {
HostIP string `json:"host_ip"`
ID int `json:"id"`
Hostname string `json:"hypervisor_hostname"`
Status string `json:"status"`
State string `json:"state"`
Type string `json:"hypervisor_type"`
}
func parseHypervisorDetail(data []byte) (*hypervisorDetail, error) {
var hvsd hypervisorDetail
if err := json.Unmarshal(data, &hvsd); err != nil {
return nil, fmt.Errorf("cannot parse hypervisorDetail: %w", err)
}
return &hvsd, nil
}
func (cfg *apiConfig) getHypervisors() ([]hypervisor, error) {
computeURL := *cfg.creds.computeURL
computeURL.Path = path.Join(computeURL.Path, "os-hypervisors", "detail")
nextLink := computeURL.String()
var hvs []hypervisor
for {
resp, err := getAPIResponse(nextLink, cfg)
if err != nil {
return nil, err
}
detail, err := parseHypervisorDetail(resp)
if err != nil {
return nil, err
}
hvs = append(hvs, detail.Hypervisors...)
if len(detail.Links) > 0 {
nextLink = detail.Links[0].HREF
continue
}
return hvs, nil
}
}
func addHypervisorLabels(hvs []hypervisor, port int) []map[string]string {
var ms []map[string]string
for _, hv := range hvs {
addr := discoveryutils.JoinHostPort(hv.HostIP, port)
m := map[string]string{
"__address__": addr,
"__meta_openstack_hypervisor_type": hv.Type,
"__meta_openstack_hypervisor_status": hv.Status,
"__meta_openstack_hypervisor_hostname": hv.Hostname,
"__meta_openstack_hypervisor_state": hv.State,
"__meta_openstack_hypervisor_host_ip": hv.HostIP,
"__meta_openstack_hypervisor_id": strconv.Itoa(hv.ID),
}
ms = append(ms, m)
}
return ms
}
func getHypervisorLabels(cfg *apiConfig) ([]map[string]string, error) {
hvs, err := cfg.getHypervisors()
if err != nil {
return nil, fmt.Errorf("cannot get hypervisors: %w", err)
}
return addHypervisorLabels(hvs, cfg.port), nil
}