/
stats.go
68 lines (55 loc) · 1.99 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package stats
import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"code.cloudfoundry.org/grootfs/groot"
quotapkg "code.cloudfoundry.org/grootfs/store/filesystems/overlayxfs/quota"
"code.cloudfoundry.org/lager"
errorspkg "github.com/pkg/errors"
)
func VolumeStats(logger lager.Logger, imagePath string) (groot.VolumeStats, error) {
logger = logger.Session("overlayxfs-fetching-stats", lager.Data{"imagePath": imagePath})
logger.Debug("starting")
defer logger.Debug("ending")
if _, err := os.Stat(imagePath); os.IsNotExist(err) {
logger.Error("image-path-not-found", err)
return groot.VolumeStats{}, errorspkg.Wrapf(err, "image path (%s) doesn't exist", imagePath)
}
exclusiveSize, err := listQuotaUsage(logger, imagePath)
if err != nil {
logger.Error("list-quota-usage-failed", err)
return groot.VolumeStats{}, errorspkg.Wrapf(err, "listing quota usage %s", imagePath)
}
volumeSize, err := readImageInfo(logger, imagePath)
if err != nil {
logger.Error("reading-image-info-failed", err)
return groot.VolumeStats{}, errorspkg.Wrapf(err, "reading image info %s", imagePath)
}
logger.Debug("usage", lager.Data{"volumeSize": volumeSize, "exclusiveSize": exclusiveSize})
return groot.VolumeStats{
DiskUsage: groot.DiskUsage{
ExclusiveBytesUsed: exclusiveSize,
TotalBytesUsed: volumeSize + exclusiveSize,
},
}, nil
}
func listQuotaUsage(logger lager.Logger, imagePath string) (int64, error) {
logger = logger.Session("listing-quota-usage", lager.Data{"imagePath": imagePath})
logger.Debug("starting")
defer logger.Debug("ending")
quota, err := quotapkg.Get(logger, imagePath)
if err != nil {
logger.Error("getting-quota-failed", err)
return 0, errorspkg.Wrapf(err, "getting quota %s", imagePath)
}
return int64(quota.BCount), nil
}
func readImageInfo(logger lager.Logger, imagePath string) (int64, error) {
contents, err := ioutil.ReadFile(filepath.Join(imagePath, "image_info"))
if err != nil {
return 0, err
}
return strconv.ParseInt(string(contents), 10, 64)
}