ssh_crawler/stat/disk_free.go
insanity 3ef6659f46 ing
2017-10-23 18:08:19 +09:00

50 lines
812 B
Go

package stat
import (
"bufio"
"io"
"strings"
)
type DiskFreeStat struct {
filesystem,
size,
used,
available,
usePerc,
mountedOn string
}
func (diskFree DiskFreeStat) Command() string {
return "df -k"
}
func (diskio DiskFreeStat) Parse(r io.Reader) (*[]DiskFreeStat, error) {
var scanner = bufio.NewScanner(r)
var stats = []DiskFreeStat{}
scanner.Scan()
for scanner.Scan() {
parts := strings.Fields(scanner.Text())
stats = append(stats, DiskFreeStat{
filesystem: parts[0],
size: parts[1],
used: parts[2],
available: parts[3],
usePerc: removePercUnit(parts[4]),
mountedOn: parts[5],
})
}
return &stats, scanner.Err()
}
func removePercUnit(str string) string {
if !strings.HasSuffix(str, "%") {
return str
}
str = str[:len(str)-1]
return str
}