Merge branch 'develop' of github.com:ClusterCockpit/cc-metric-collector into develop

This commit is contained in:
Thomas Roehl 2022-07-26 14:59:27 +02:00
commit 96b4a2aec1

View File

@ -1,8 +1,10 @@
package receivers package receivers
import ( import (
"crypto/tls"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http"
"strconv" "strconv"
"sync" "sync"
"time" "time"
@ -18,9 +20,19 @@ import (
type RedfishReceiver struct { type RedfishReceiver struct {
receiver receiver
config struct { config struct {
Type string `json:"type"` Type string `json:"type"`
Fanout int `json:"fanout,omitempty"` // Default fanout: 64
Interval int `json:"interval,omitempty"` // Default interval: 30s // Maximum number of simultaneous redfish connections (default: 64)
Fanout int `json:"fanout,omitempty"`
// How often the redfish power metrics should be read and send to the sink (default: 30 s)
IntervalString string `json:"interval,omitempty"`
Interval time.Duration
// Control whether a client verifies the server's certificate (default: true)
HttpInsecure bool `json:"http_insecure,omitempty"`
// Time limit for requests made by this HTTP client (default: 10 s)
HttpTimeoutString string `json:"http_timeout,omitempty"`
HttpTimeout time.Duration
// Client config for each redfish service // Client config for each redfish service
ClientConfigs []struct { ClientConfigs []struct {
@ -28,7 +40,6 @@ type RedfishReceiver struct {
Username *string `json:"username"` Username *string `json:"username"`
Password *string `json:"password"` Password *string `json:"password"`
Endpoint *string `json:"endpoint"` Endpoint *string `json:"endpoint"`
Insecure *bool `json:"insecure,omitempty"`
ExcludeMetrics []string `json:"exclude_metrics,omitempty"` ExcludeMetrics []string `json:"exclude_metrics,omitempty"`
gofish gofish.ClientConfig gofish gofish.ClientConfig
} `json:"client_config"` } `json:"client_config"`
@ -42,7 +53,7 @@ type RedfishReceiver struct {
func (r *RedfishReceiver) Start() { func (r *RedfishReceiver) Start() {
cclog.ComponentDebug(r.name, "START") cclog.ComponentDebug(r.name, "START")
// readPowerMetric reads readfish power metric from the endpoint configured in conf // readPowerMetric reads redfish power metric from the endpoint configured in conf
readPowerMetric := func(clientConfigIndex int) error { readPowerMetric := func(clientConfigIndex int) error {
clientConfig := &r.config.ClientConfigs[clientConfigIndex] clientConfig := &r.config.ClientConfigs[clientConfigIndex]
@ -50,18 +61,14 @@ func (r *RedfishReceiver) Start() {
// Connect to redfish service // Connect to redfish service
c, err := gofish.Connect(clientConfig.gofish) c, err := gofish.Connect(clientConfig.gofish)
if err != nil { if err != nil {
c := struct { return fmt.Errorf(
Username string "readPowerMetric: gofish.Connect({Username: %v, Endpoint: %v, BasicAuth: %v, HttpTimeout: %v, HttpInsecure: %v}) failed: %v",
Endpoint string clientConfig.gofish.Username,
BasicAuth bool clientConfig.gofish.Endpoint,
Insecure bool clientConfig.gofish.BasicAuth,
}{ clientConfig.gofish.HTTPClient.Timeout,
Username: clientConfig.gofish.Username, clientConfig.gofish.HTTPClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify,
Endpoint: clientConfig.gofish.Endpoint, err)
BasicAuth: clientConfig.gofish.BasicAuth,
Insecure: clientConfig.gofish.Insecure,
}
return fmt.Errorf("readPowerMetric: gofish.Connect(%+v) failed: %v", c, err)
} }
defer c.Logout() defer c.Logout()
@ -115,15 +122,19 @@ func (r *RedfishReceiver) Start() {
tags := map[string]string{ tags := map[string]string{
"hostname": *clientConfig.Hostname, "hostname": *clientConfig.Hostname,
"type": "node", "type": "node",
// ChassisType shall indicate the physical form factor for the type of chassis
"chassis_typ": string(chassis.ChassisType),
// Chassis name
"chassis_name": chassis.Name,
// ID uniquely identifies the resource // ID uniquely identifies the resource
"id": pc.ID, "power_control_id": pc.ID,
// MemberID shall uniquely identify the member within the collection. For // MemberID shall uniquely identify the member within the collection. For
// services supporting Redfish v1.6 or higher, this value shall be the // services supporting Redfish v1.6 or higher, this value shall be the
// zero-based array index. // zero-based array index.
"member_id": pc.MemberID, "power_control_member_id": pc.MemberID,
// PhysicalContext shall be a description of the affected device(s) or region // PhysicalContext shall be a description of the affected device(s) or region
// within the chassis to which this power control applies. // within the chassis to which this power control applies.
"physical_context": string(pc.PhysicalContext), "power_control_physical_context": string(pc.PhysicalContext),
// Name // Name
"power_control_name": pc.Name, "power_control_name": pc.Name,
} }
@ -226,7 +237,7 @@ func (r *RedfishReceiver) Start() {
defer r.wg.Done() defer r.wg.Done()
// Create ticker // Create ticker
ticker := time.NewTicker(time.Duration(r.config.Interval) * time.Second) ticker := time.NewTicker(r.config.Interval)
defer ticker.Stop() defer ticker.Stop()
for { for {
@ -271,7 +282,9 @@ func NewRedfishReceiver(name string, config json.RawMessage) (Receiver, error) {
// Set defaults in r.config // Set defaults in r.config
// Allow overwriting these defaults by reading config JSON // Allow overwriting these defaults by reading config JSON
r.config.Fanout = 64 r.config.Fanout = 64
r.config.Interval = 30 r.config.IntervalString = "30s"
r.config.HttpTimeoutString = "10s"
r.config.HttpInsecure = true
// Read the redfish receiver specific JSON config // Read the redfish receiver specific JSON config
if len(config) > 0 { if len(config) > 0 {
@ -282,6 +295,41 @@ func NewRedfishReceiver(name string, config json.RawMessage) (Receiver, error) {
} }
} }
// interval duration
var err error
r.config.Interval, err = time.ParseDuration(r.config.IntervalString)
if err != nil {
err := fmt.Errorf(
"Failed to parse duration string interval='%s': %w",
r.config.IntervalString,
err,
)
cclog.Error(r.name, err)
return nil, err
}
// HTTP timeout duration
r.config.HttpTimeout, err = time.ParseDuration(r.config.HttpTimeoutString)
if err != nil {
err := fmt.Errorf(
"Failed to parse duration string http_timeout='%s': %w",
r.config.HttpTimeoutString,
err,
)
cclog.Error(r.name, err)
return nil, err
}
// Create new http client
customTransport := http.DefaultTransport.(*http.Transport).Clone()
customTransport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: r.config.HttpInsecure,
}
httpClient := &http.Client{
Timeout: r.config.HttpTimeout,
Transport: customTransport,
}
// Create gofish client config // Create gofish client config
for i := range r.config.ClientConfigs { for i := range r.config.ClientConfigs {
clientConfig := &r.config.ClientConfigs[i] clientConfig := &r.config.ClientConfigs[i]
@ -314,10 +362,7 @@ func NewRedfishReceiver(name string, config json.RawMessage) (Receiver, error) {
} }
gofishConfig.Password = *clientConfig.Password gofishConfig.Password = *clientConfig.Password
gofishConfig.Insecure = true gofishConfig.HTTPClient = httpClient
if clientConfig.Insecure != nil {
gofishConfig.Insecure = *clientConfig.Insecure
}
} }
return r, nil return r, nil