2017-10-23 05:31:04 +00:00
|
|
|
package stat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2017-10-23 09:08:19 +00:00
|
|
|
"io"
|
2017-10-23 05:31:04 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DiskFreeStat struct {
|
2017-10-24 07:09:17 +00:00
|
|
|
Filesystem,
|
|
|
|
Size,
|
|
|
|
Used,
|
|
|
|
Available,
|
|
|
|
UsePerc,
|
|
|
|
MountedOn string
|
2017-10-23 05:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (diskFree DiskFreeStat) Command() string {
|
|
|
|
return "df -k"
|
|
|
|
}
|
|
|
|
|
2017-10-23 09:08:19 +00:00
|
|
|
func (diskio DiskFreeStat) Parse(r io.Reader) (*[]DiskFreeStat, error) {
|
2017-10-23 05:31:04 +00:00
|
|
|
|
|
|
|
var scanner = bufio.NewScanner(r)
|
|
|
|
var stats = []DiskFreeStat{}
|
|
|
|
|
|
|
|
scanner.Scan()
|
|
|
|
for scanner.Scan() {
|
|
|
|
parts := strings.Fields(scanner.Text())
|
|
|
|
stats = append(stats, DiskFreeStat{
|
2017-10-24 07:09:17 +00:00
|
|
|
Filesystem: parts[0],
|
|
|
|
Size: parts[1],
|
|
|
|
Used: parts[2],
|
|
|
|
Available: parts[3],
|
|
|
|
UsePerc: removePercUnit(parts[4]),
|
|
|
|
MountedOn: parts[5],
|
2017-10-23 05:31:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-23 09:08:19 +00:00
|
|
|
return &stats, scanner.Err()
|
2017-10-23 05:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func removePercUnit(str string) string {
|
2017-10-23 09:08:19 +00:00
|
|
|
if !strings.HasSuffix(str, "%") {
|
2017-10-23 05:31:04 +00:00
|
|
|
return str
|
|
|
|
}
|
|
|
|
str = str[:len(str)-1]
|
|
|
|
return str
|
2017-10-23 09:08:19 +00:00
|
|
|
}
|