metrics_du.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. Copyright 2014 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 volume
  14. import (
  15. "k8s.io/apimachinery/pkg/api/resource"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "k8s.io/kubernetes/pkg/volume/util/fs"
  18. )
  19. var _ MetricsProvider = &metricsDu{}
  20. // metricsDu represents a MetricsProvider that calculates the used and
  21. // available Volume space by calling fs.DiskUsage() and gathering
  22. // filesystem info for the Volume path.
  23. type metricsDu struct {
  24. // the directory path the volume is mounted to.
  25. path string
  26. }
  27. // NewMetricsDu creates a new metricsDu with the Volume path.
  28. func NewMetricsDu(path string) MetricsProvider {
  29. return &metricsDu{path}
  30. }
  31. // GetMetrics calculates the volume usage and device free space by executing "du"
  32. // and gathering filesystem info for the Volume path.
  33. // See MetricsProvider.GetMetrics
  34. func (md *metricsDu) GetMetrics() (*Metrics, error) {
  35. metrics := &Metrics{Time: metav1.Now()}
  36. if md.path == "" {
  37. return metrics, NewNoPathDefinedError()
  38. }
  39. err := md.runDiskUsage(metrics)
  40. if err != nil {
  41. return metrics, err
  42. }
  43. err = md.runFind(metrics)
  44. if err != nil {
  45. return metrics, err
  46. }
  47. err = md.getFsInfo(metrics)
  48. if err != nil {
  49. return metrics, err
  50. }
  51. return metrics, nil
  52. }
  53. // runDiskUsage gets disk usage of md.path and writes the results to metrics.Used
  54. func (md *metricsDu) runDiskUsage(metrics *Metrics) error {
  55. used, err := fs.DiskUsage(md.path)
  56. if err != nil {
  57. return err
  58. }
  59. metrics.Used = used
  60. return nil
  61. }
  62. // runFind executes the "find" command and writes the results to metrics.InodesUsed
  63. func (md *metricsDu) runFind(metrics *Metrics) error {
  64. inodesUsed, err := fs.Find(md.path)
  65. if err != nil {
  66. return err
  67. }
  68. metrics.InodesUsed = resource.NewQuantity(inodesUsed, resource.BinarySI)
  69. return nil
  70. }
  71. // getFsInfo writes metrics.Capacity and metrics.Available from the filesystem
  72. // info
  73. func (md *metricsDu) getFsInfo(metrics *Metrics) error {
  74. available, capacity, _, inodes, inodesFree, _, err := fs.FsInfo(md.path)
  75. if err != nil {
  76. return NewFsInfoFailedError(err)
  77. }
  78. metrics.Available = resource.NewQuantity(available, resource.BinarySI)
  79. metrics.Capacity = resource.NewQuantity(capacity, resource.BinarySI)
  80. metrics.Inodes = resource.NewQuantity(inodes, resource.BinarySI)
  81. metrics.InodesFree = resource.NewQuantity(inodesFree, resource.BinarySI)
  82. return nil
  83. }