Skip to content

Commit

Permalink
Refactor push.go to include filesystem usage data
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasbacsai committed Oct 22, 2024
1 parent 20c4803 commit 4bed0fa
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion push.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"log"
"net/http"
"syscall"
"time"

"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -51,9 +52,16 @@ func getPushData() (map[string]interface{}, error) {
log.Printf("Error getting containers data: %v", err)
return nil, err
}
filesystemUsageRoot, err := filesystemUsageRoot()
if err != nil {
log.Printf("Error getting disk usage: %v", err)
return nil, err
}
data := map[string]interface{}{
"containers": containersData,
"containers": containersData,
"filesystem_usage_root": filesystemUsageRoot,
}
fmt.Printf("Pushing data: %v\n", data)
jsonData, err := JSON.Marshal(data)
if err != nil {
log.Printf("Error marshalling data: %v", err)
Expand All @@ -80,6 +88,22 @@ func getPushData() (map[string]interface{}, error) {
return data, nil
}

func filesystemUsageRoot() (map[string]interface{}, error) {
fs := syscall.Statfs_t{}
err := syscall.Statfs("/", &fs)
if err != nil {
return nil, err
}
totalSpace := fs.Blocks * uint64(fs.Bsize)
freeSpace := fs.Bfree * uint64(fs.Bsize)
usedSpace := totalSpace - freeSpace
usedPercentage := float64(usedSpace) / float64(totalSpace) * 100

return map[string]interface{}{
"used_percentage": fmt.Sprintf("%d", int(usedPercentage)),
}, nil
}

func containerData() ([]Container, error) {
resp, err := makeDockerRequest("/containers/json?all=true")
if err != nil {
Expand Down

0 comments on commit 4bed0fa

Please sign in to comment.