ssh_crawler/stat/disk_free.go

49 lines
791 B
Go
Raw Normal View History

2017-10-23 05:31:04 +00:00
package stat
import (
"io"
"bufio"
"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
}