metrics_printer.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package metricsutil
  14. import (
  15. "fmt"
  16. "io"
  17. metrics_api "k8s.io/heapster/metrics/apis/metrics/v1alpha1"
  18. "k8s.io/kubernetes/pkg/api"
  19. "k8s.io/kubernetes/pkg/api/resource"
  20. "k8s.io/kubernetes/pkg/kubectl"
  21. )
  22. var (
  23. MeasuredResources = []api.ResourceName{
  24. api.ResourceCPU,
  25. api.ResourceMemory,
  26. }
  27. NodeColumns = []string{"NAME", "CPU(cores)", "CPU%", "MEMORY(bytes)", "MEMORY%"}
  28. PodColumns = []string{"NAME", "CPU(cores)", "MEMORY(bytes)"}
  29. NamespaceColumn = "NAMESPACE"
  30. PodColumn = "POD"
  31. )
  32. type ResourceMetricsInfo struct {
  33. Name string
  34. Metrics api.ResourceList
  35. Available api.ResourceList
  36. }
  37. type TopCmdPrinter struct {
  38. out io.Writer
  39. }
  40. func NewTopCmdPrinter(out io.Writer) *TopCmdPrinter {
  41. return &TopCmdPrinter{out: out}
  42. }
  43. func (printer *TopCmdPrinter) PrintNodeMetrics(metrics []metrics_api.NodeMetrics, availableResources map[string]api.ResourceList) error {
  44. if len(metrics) == 0 {
  45. return nil
  46. }
  47. w := kubectl.GetNewTabWriter(printer.out)
  48. defer w.Flush()
  49. printColumnNames(w, NodeColumns)
  50. var usage api.ResourceList
  51. for _, m := range metrics {
  52. err := api.Scheme.Convert(&m.Usage, &usage, nil)
  53. if err != nil {
  54. return err
  55. }
  56. printMetricsLine(w, &ResourceMetricsInfo{
  57. Name: m.Name,
  58. Metrics: usage,
  59. Available: availableResources[m.Name],
  60. })
  61. }
  62. return nil
  63. }
  64. func (printer *TopCmdPrinter) PrintPodMetrics(metrics []metrics_api.PodMetrics, printContainers bool, withNamespace bool) error {
  65. if len(metrics) == 0 {
  66. return nil
  67. }
  68. w := kubectl.GetNewTabWriter(printer.out)
  69. defer w.Flush()
  70. if withNamespace {
  71. printValue(w, NamespaceColumn)
  72. }
  73. if printContainers {
  74. printValue(w, PodColumn)
  75. }
  76. printColumnNames(w, PodColumns)
  77. for _, m := range metrics {
  78. err := printSinglePodMetrics(w, &m, printContainers, withNamespace)
  79. if err != nil {
  80. return err
  81. }
  82. }
  83. return nil
  84. }
  85. func printColumnNames(out io.Writer, names []string) {
  86. for _, name := range names {
  87. printValue(out, name)
  88. }
  89. fmt.Fprint(out, "\n")
  90. }
  91. func printSinglePodMetrics(out io.Writer, m *metrics_api.PodMetrics, printContainersOnly bool, withNamespace bool) error {
  92. containers := make(map[string]api.ResourceList)
  93. podMetrics := make(api.ResourceList)
  94. for _, res := range MeasuredResources {
  95. podMetrics[res], _ = resource.ParseQuantity("0")
  96. }
  97. var usage api.ResourceList
  98. for _, c := range m.Containers {
  99. err := api.Scheme.Convert(&c.Usage, &usage, nil)
  100. if err != nil {
  101. return err
  102. }
  103. containers[c.Name] = usage
  104. if !printContainersOnly {
  105. for _, res := range MeasuredResources {
  106. quantity := podMetrics[res]
  107. quantity.Add(usage[res])
  108. podMetrics[res] = quantity
  109. }
  110. }
  111. }
  112. if printContainersOnly {
  113. for contName := range containers {
  114. if withNamespace {
  115. printValue(out, m.Namespace)
  116. }
  117. printValue(out, m.Name)
  118. printMetricsLine(out, &ResourceMetricsInfo{
  119. Name: contName,
  120. Metrics: containers[contName],
  121. Available: api.ResourceList{},
  122. })
  123. }
  124. } else {
  125. if withNamespace {
  126. printValue(out, m.Namespace)
  127. }
  128. printMetricsLine(out, &ResourceMetricsInfo{
  129. Name: m.Name,
  130. Metrics: podMetrics,
  131. Available: api.ResourceList{},
  132. })
  133. }
  134. return nil
  135. }
  136. func printMetricsLine(out io.Writer, metrics *ResourceMetricsInfo) {
  137. printValue(out, metrics.Name)
  138. printAllResourceUsages(out, metrics)
  139. fmt.Fprint(out, "\n")
  140. }
  141. func printValue(out io.Writer, value interface{}) {
  142. fmt.Fprintf(out, "%v\t", value)
  143. }
  144. func printAllResourceUsages(out io.Writer, metrics *ResourceMetricsInfo) {
  145. for _, res := range MeasuredResources {
  146. quantity := metrics.Metrics[res]
  147. printSingleResourceUsage(out, res, quantity)
  148. fmt.Fprint(out, "\t")
  149. if available, found := metrics.Available[res]; found {
  150. fraction := float64(quantity.MilliValue()) / float64(available.MilliValue()) * 100
  151. fmt.Fprintf(out, "%d%%\t", int64(fraction))
  152. }
  153. }
  154. }
  155. func printSingleResourceUsage(out io.Writer, resourceType api.ResourceName, quantity resource.Quantity) {
  156. switch resourceType {
  157. case api.ResourceCPU:
  158. fmt.Fprintf(out, "%vm", quantity.MilliValue())
  159. case api.ResourceMemory:
  160. fmt.Fprintf(out, "%vMi", quantity.Value()/(1024*1024))
  161. default:
  162. fmt.Fprintf(out, "%v", quantity.Value())
  163. }
  164. }