volume_stats.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Copyright 2018 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 collectors
  14. import (
  15. "github.com/prometheus/client_golang/prometheus"
  16. "k8s.io/apimachinery/pkg/util/sets"
  17. "k8s.io/klog"
  18. stats "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
  19. "k8s.io/kubernetes/pkg/kubelet/metrics"
  20. serverstats "k8s.io/kubernetes/pkg/kubelet/server/stats"
  21. )
  22. var (
  23. volumeStatsCapacityBytesDesc = prometheus.NewDesc(
  24. prometheus.BuildFQName("", metrics.KubeletSubsystem, metrics.VolumeStatsCapacityBytesKey),
  25. "Capacity in bytes of the volume",
  26. []string{"namespace", "persistentvolumeclaim"}, nil,
  27. )
  28. volumeStatsAvailableBytesDesc = prometheus.NewDesc(
  29. prometheus.BuildFQName("", metrics.KubeletSubsystem, metrics.VolumeStatsAvailableBytesKey),
  30. "Number of available bytes in the volume",
  31. []string{"namespace", "persistentvolumeclaim"}, nil,
  32. )
  33. volumeStatsUsedBytesDesc = prometheus.NewDesc(
  34. prometheus.BuildFQName("", metrics.KubeletSubsystem, metrics.VolumeStatsUsedBytesKey),
  35. "Number of used bytes in the volume",
  36. []string{"namespace", "persistentvolumeclaim"}, nil,
  37. )
  38. volumeStatsInodesDesc = prometheus.NewDesc(
  39. prometheus.BuildFQName("", metrics.KubeletSubsystem, metrics.VolumeStatsInodesKey),
  40. "Maximum number of inodes in the volume",
  41. []string{"namespace", "persistentvolumeclaim"}, nil,
  42. )
  43. volumeStatsInodesFreeDesc = prometheus.NewDesc(
  44. prometheus.BuildFQName("", metrics.KubeletSubsystem, metrics.VolumeStatsInodesFreeKey),
  45. "Number of free inodes in the volume",
  46. []string{"namespace", "persistentvolumeclaim"}, nil,
  47. )
  48. volumeStatsInodesUsedDesc = prometheus.NewDesc(
  49. prometheus.BuildFQName("", metrics.KubeletSubsystem, metrics.VolumeStatsInodesUsedKey),
  50. "Number of used inodes in the volume",
  51. []string{"namespace", "persistentvolumeclaim"}, nil,
  52. )
  53. )
  54. type volumeStatsCollector struct {
  55. statsProvider serverstats.Provider
  56. }
  57. // NewVolumeStatsCollector creates a volume stats prometheus collector.
  58. func NewVolumeStatsCollector(statsProvider serverstats.Provider) prometheus.Collector {
  59. return &volumeStatsCollector{statsProvider: statsProvider}
  60. }
  61. // Describe implements the prometheus.Collector interface.
  62. func (collector *volumeStatsCollector) Describe(ch chan<- *prometheus.Desc) {
  63. ch <- volumeStatsCapacityBytesDesc
  64. ch <- volumeStatsAvailableBytesDesc
  65. ch <- volumeStatsUsedBytesDesc
  66. ch <- volumeStatsInodesDesc
  67. ch <- volumeStatsInodesFreeDesc
  68. ch <- volumeStatsInodesUsedDesc
  69. }
  70. // Collect implements the prometheus.Collector interface.
  71. func (collector *volumeStatsCollector) Collect(ch chan<- prometheus.Metric) {
  72. podStats, err := collector.statsProvider.ListPodStats()
  73. if err != nil {
  74. return
  75. }
  76. addGauge := func(desc *prometheus.Desc, pvcRef *stats.PVCReference, v float64, lv ...string) {
  77. lv = append([]string{pvcRef.Namespace, pvcRef.Name}, lv...)
  78. metric, err := prometheus.NewConstMetric(desc, prometheus.GaugeValue, v, lv...)
  79. if err != nil {
  80. klog.Warningf("Failed to generate metric: %v", err)
  81. return
  82. }
  83. ch <- metric
  84. }
  85. allPVCs := sets.String{}
  86. for _, podStat := range podStats {
  87. if podStat.VolumeStats == nil {
  88. continue
  89. }
  90. for _, volumeStat := range podStat.VolumeStats {
  91. pvcRef := volumeStat.PVCRef
  92. if pvcRef == nil {
  93. // ignore if no PVC reference
  94. continue
  95. }
  96. pvcUniqStr := pvcRef.Namespace + "/" + pvcRef.Name
  97. if allPVCs.Has(pvcUniqStr) {
  98. // ignore if already collected
  99. continue
  100. }
  101. addGauge(volumeStatsCapacityBytesDesc, pvcRef, float64(*volumeStat.CapacityBytes))
  102. addGauge(volumeStatsAvailableBytesDesc, pvcRef, float64(*volumeStat.AvailableBytes))
  103. addGauge(volumeStatsUsedBytesDesc, pvcRef, float64(*volumeStat.UsedBytes))
  104. addGauge(volumeStatsInodesDesc, pvcRef, float64(*volumeStat.Inodes))
  105. addGauge(volumeStatsInodesFreeDesc, pvcRef, float64(*volumeStat.InodesFree))
  106. addGauge(volumeStatsInodesUsedDesc, pvcRef, float64(*volumeStat.InodesUsed))
  107. allPVCs.Insert(pvcUniqStr)
  108. }
  109. }
  110. }