util.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package command
  15. import (
  16. "context"
  17. "encoding/hex"
  18. "fmt"
  19. "io/ioutil"
  20. "net/http"
  21. "regexp"
  22. "strconv"
  23. "strings"
  24. pb "github.com/coreos/etcd/internal/mvcc/mvccpb"
  25. "github.com/spf13/cobra"
  26. )
  27. func printKV(isHex bool, valueOnly bool, kv *pb.KeyValue) {
  28. k, v := string(kv.Key), string(kv.Value)
  29. if isHex {
  30. k = addHexPrefix(hex.EncodeToString(kv.Key))
  31. v = addHexPrefix(hex.EncodeToString(kv.Value))
  32. }
  33. if !valueOnly {
  34. fmt.Println(k)
  35. }
  36. fmt.Println(v)
  37. }
  38. func addHexPrefix(s string) string {
  39. ns := make([]byte, len(s)*2)
  40. for i := 0; i < len(s); i += 2 {
  41. ns[i*2] = '\\'
  42. ns[i*2+1] = 'x'
  43. ns[i*2+2] = s[i]
  44. ns[i*2+3] = s[i+1]
  45. }
  46. return string(ns)
  47. }
  48. func argify(s string) []string {
  49. r := regexp.MustCompile(`"(?:[^"\\]|\\.)*"|'[^']*'|[^'"\s]\S*[^'"\s]?`)
  50. args := r.FindAllString(s, -1)
  51. for i := range args {
  52. if len(args[i]) == 0 {
  53. continue
  54. }
  55. if args[i][0] == '\'' {
  56. // 'single-quoted string'
  57. args[i] = args[i][1 : len(args)-1]
  58. } else if args[i][0] == '"' {
  59. // "double quoted string"
  60. if _, err := fmt.Sscanf(args[i], "%q", &args[i]); err != nil {
  61. ExitWithError(ExitInvalidInput, err)
  62. }
  63. }
  64. }
  65. return args
  66. }
  67. func commandCtx(cmd *cobra.Command) (context.Context, context.CancelFunc) {
  68. timeOut, err := cmd.Flags().GetDuration("command-timeout")
  69. if err != nil {
  70. ExitWithError(ExitError, err)
  71. }
  72. return context.WithTimeout(context.Background(), timeOut)
  73. }
  74. // get the process_resident_memory_bytes from <server:2379>/metrics
  75. func endpointMemoryMetrics(host string) float64 {
  76. residentMemoryKey := "process_resident_memory_bytes"
  77. var residentMemoryValue string
  78. if !strings.HasPrefix(host, `http://`) {
  79. host = "http://" + host
  80. }
  81. url := host + "/metrics"
  82. resp, err := http.Get(url)
  83. if err != nil {
  84. fmt.Println(fmt.Sprintf("fetch error: %v", err))
  85. return 0.0
  86. }
  87. byts, readerr := ioutil.ReadAll(resp.Body)
  88. resp.Body.Close()
  89. if readerr != nil {
  90. fmt.Println(fmt.Sprintf("fetch error: reading %s: %v", url, readerr))
  91. return 0.0
  92. }
  93. for _, line := range strings.Split(string(byts), "\n") {
  94. if strings.HasPrefix(line, residentMemoryKey) {
  95. residentMemoryValue = strings.TrimSpace(strings.TrimPrefix(line, residentMemoryKey))
  96. break
  97. }
  98. }
  99. if residentMemoryValue == "" {
  100. fmt.Println(fmt.Sprintf("could not find: %v", residentMemoryKey))
  101. return 0.0
  102. }
  103. residentMemoryBytes, parseErr := strconv.ParseFloat(residentMemoryValue, 64)
  104. if parseErr != nil {
  105. fmt.Println(fmt.Sprintf("parse error: %v", parseErr))
  106. return 0.0
  107. }
  108. return residentMemoryBytes
  109. }