fix check datascale command for https endpoints

This commit is contained in:
Saeid Bostandoust 2021-04-16 03:51:04 +04:30
parent c73da740fa
commit a9c4301c1e
2 changed files with 20 additions and 5 deletions

View File

@ -311,6 +311,8 @@ func newCheckDatascaleCommand(cmd *cobra.Command, args []string) {
ExitWithError(ExitError, errEndpoints) ExitWithError(ExitError, errEndpoints)
} }
sec := secureCfgFromCmd(cmd)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
resp, err := clients[0].Get(ctx, checkDatascalePrefix, v3.WithPrefix(), v3.WithLimit(1)) resp, err := clients[0].Get(ctx, checkDatascalePrefix, v3.WithPrefix(), v3.WithLimit(1))
cancel() cancel()
@ -329,7 +331,7 @@ func newCheckDatascaleCommand(cmd *cobra.Command, args []string) {
wg.Add(len(clients)) wg.Add(len(clients))
// get the process_resident_memory_bytes and process_virtual_memory_bytes before the put operations // get the process_resident_memory_bytes and process_virtual_memory_bytes before the put operations
bytesBefore := endpointMemoryMetrics(eps[0]) bytesBefore := endpointMemoryMetrics(eps[0], sec)
if bytesBefore == 0 { if bytesBefore == 0 {
fmt.Println("FAIL: Could not read process_resident_memory_bytes before the put operations.") fmt.Println("FAIL: Could not read process_resident_memory_bytes before the put operations.")
os.Exit(ExitError) os.Exit(ExitError)
@ -367,7 +369,7 @@ func newCheckDatascaleCommand(cmd *cobra.Command, args []string) {
s := <-sc s := <-sc
// get the process_resident_memory_bytes after the put operations // get the process_resident_memory_bytes after the put operations
bytesAfter := endpointMemoryMetrics(eps[0]) bytesAfter := endpointMemoryMetrics(eps[0], sec)
if bytesAfter == 0 { if bytesAfter == 0 {
fmt.Println("FAIL: Could not read process_resident_memory_bytes after the put operations.") fmt.Println("FAIL: Could not read process_resident_memory_bytes after the put operations.")
os.Exit(ExitError) os.Exit(ExitError)

View File

@ -15,6 +15,7 @@
package command package command
import ( import (
"crypto/tls"
"context" "context"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
@ -90,14 +91,26 @@ func isCommandTimeoutFlagSet(cmd *cobra.Command) bool {
return commandTimeoutFlag.Changed return commandTimeoutFlag.Changed
} }
// get the process_resident_memory_bytes from <server:2379>/metrics // get the process_resident_memory_bytes from <server>/metrics
func endpointMemoryMetrics(host string) float64 { func endpointMemoryMetrics(host string, scfg *secureCfg) float64 {
residentMemoryKey := "process_resident_memory_bytes" residentMemoryKey := "process_resident_memory_bytes"
var residentMemoryValue string var residentMemoryValue string
if !strings.HasPrefix(host, `http://`) { if !strings.HasPrefix(host, "http://") && !strings.HasPrefix(host, "https://") {
host = "http://" + host host = "http://" + host
} }
url := host + "/metrics" url := host + "/metrics"
if strings.HasPrefix(host, "https://") {
// load client certificate
cert, err := tls.LoadX509KeyPair(scfg.cert, scfg.key)
if err != nil {
fmt.Println(fmt.Sprintf("client certificate error: %v", err))
return 0.0
}
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{
Certificates: []tls.Certificate{cert},
InsecureSkipVerify: scfg.insecureSkipVerify,
}
}
resp, err := http.Get(url) resp, err := http.Get(url)
if err != nil { if err != nil {
fmt.Println(fmt.Sprintf("fetch error: %v", err)) fmt.Println(fmt.Sprintf("fetch error: %v", err))