Skip to content
Permalink
Browse files

Pr 456 (#503)

* Adding usage statistic to system mount interface

Include syscall to call statfs on each mount type. Use the output from
statfs to calculate percentage of filesystem that is currently being
used. Save this percentage as a uint64 in the mount interface.

* Add usage validate and addition to mount resource

* Update docs/manual with mountpoint usage statistic

* Add tests for disk usage
  • Loading branch information
aelsabbahy committed Nov 25, 2019
1 parent ed6e4cd commit 282afa896a11a78cc743fff846bcb843324e0e83
Showing with 47 additions and 2 deletions.
  1. +2 −0 docs/manual.md
  2. +6 −0 integration-tests/goss/goss-shared.yaml
  3. +2 −2 integration-tests/test.sh
  4. +4 −0 resource/mount.go
  5. +33 −0 system/mount.go
@@ -636,6 +636,8 @@ mount:
- relatime
source: /dev/mapper/fedora-home
filesystem: xfs
usage: #% of blocks used in this mountpoint
lt: 95
```
### matching
@@ -163,6 +163,12 @@ mount:
- nosuid
source: tmpfs
filesystem: tmpfs
"/":
exists: true
usage:
and:
- lt: 25
- gt: 0
interface:
eth0:
exists: true
@@ -44,9 +44,9 @@ out=$(docker_exec "/goss/$os/goss-linux-$arch" --vars "/goss/vars.yaml" -g "/gos
echo "$out"

if [[ $os == "arch" ]]; then
egrep -q 'Count: 79, Failed: 0, Skipped: 3' <<<"$out"
egrep -q 'Count: 81, Failed: 0, Skipped: 3' <<<"$out"
else
egrep -q 'Count: 95, Failed: 0, Skipped: 5' <<<"$out"
egrep -q 'Count: 97, Failed: 0, Skipped: 5' <<<"$out"
fi

if [[ ! $os == "arch" ]]; then
@@ -14,6 +14,7 @@ type Mount struct {
Source matcher `json:"source,omitempty" yaml:"source,omitempty"`
Filesystem matcher `json:"filesystem,omitempty" yaml:"filesystem,omitempty"`
Skip bool `json:"skip,omitempty" yaml:"skip,omitempty"`
Usage matcher `json:"usage,omitempty" yaml:"usage,omitempty"`
}

func (m *Mount) ID() string { return m.MountPoint }
@@ -45,6 +46,9 @@ func (m *Mount) Validate(sys *system.System) []TestResult {
if m.Filesystem != nil {
results = append(results, ValidateValue(m, "filesystem", m.Filesystem, sysMount.Filesystem, skip))
}
if m.Usage != nil {
results = append(results, ValidateValue(m, "usage", m.Usage, sysMount.Usage, skip))
}
return results
}

@@ -2,7 +2,9 @@ package system

import (
"fmt"
"math"
"strings"
"syscall"

"github.com/aelsabbahy/goss/util"
"github.com/docker/docker/pkg/mount"
@@ -14,13 +16,15 @@ type Mount interface {
Opts() ([]string, error)
Source() (string, error)
Filesystem() (string, error)
Usage() (int, error)
}

type DefMount struct {
mountPoint string
loaded bool
exists bool
mountInfo *mount.Info
usage int
err error
}

@@ -44,6 +48,14 @@ func (m *DefMount) setup() error {
}
m.mountInfo = mountInfo
m.exists = true

usage, err := getUsage(m.mountPoint)
if err != nil {
m.err = err
return m.err
}
m.usage = usage

return nil
}

@@ -87,6 +99,14 @@ func (m *DefMount) Filesystem() (string, error) {
return m.mountInfo.Fstype, nil
}

func (m *DefMount) Usage() (int, error) {
if err := m.setup(); err != nil {
return -1, err
}

return m.usage, nil
}

func getMount(mountpoint string) (*mount.Info, error) {
entries, err := mount.GetMounts()
if err != nil {
@@ -101,3 +121,16 @@ func getMount(mountpoint string) (*mount.Info, error) {
}
return nil, fmt.Errorf("Mountpoint not found")
}

func getUsage(mountpoint string) (int, error) {
statfsOut := &syscall.Statfs_t{}
err := syscall.Statfs(mountpoint, statfsOut)
if err != nil {
return -1, err
}

percentageFree := float64(statfsOut.Bfree) / float64(statfsOut.Blocks)
usage := math.Round((1 - percentageFree) * 100)

return int(usage), nil
}

0 comments on commit 282afa8

Please sign in to comment.
You can’t perform that action at this time.