我正在编写一个Go程序,以便从我的扩展底座主机获取以GB为单位的总磁盘使用量.为此,我使用来自Go lib的Func DiskUsage()
:
查看代码,此函数将调用docker API端点/system/df
:
然而,当我使用这个库与使用命令docker system df
计算GB时,我注意到一个奇怪的行为:
-
docker system df
输出:$ docker system df TYPE TOTAL ACTIVE SIZE RECLAIMABLE Images 223 4 21.02GB 20.7GB (98%) Containers 6 0 0B 0B Local Volumes 13 1 536.4MB 340.4MB (63%) Build Cache 954 0 13.51GB 13.51GB
- 我的GO应用程序输出:
$ go run ./cmd/main.go Images: TOTAL (223), 17GB Build Cache: TOTAL (954), 29GB
正如您所看到的,我们在两个输出之间存在差异.I would like a help to understand if I am doing something wrong with this calculation which gets data from the 100 endpoint.谢谢:)
GO申请:
package main
import (
"context"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
func main() {
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
panic(err)
}
diskUsg, err := cli.DiskUsage(context.Background(), types.DiskUsageOptions{})
if err != nil {
panic(err)
}
b := float64(0)
for _, ch := range diskUsg.BuildCache {
b = b + float64(ch.Size)
}
b2 := float64(0)
for _, ch := range diskUsg.Images {
if ch.Size > ch.SharedSize {
b2 = b2 + (float64(ch.Size) - float64(ch.SharedSize))
continue
}
b2 = b2 + (float64(ch.SharedSize) - float64(ch.Size))
}
fmt.Printf("Images: TOTAL (%d), %2.fGB\n", len(diskUsg.Images), float64(b2)/(1<<30))
fmt.Printf("Build Cache: TOTAL (%d), %2.fGB\n", len(diskUsg.BuildCache), float64(b)/(1<<30))
}