ssh_crawler/stat/disk_free.go

50 lines
812 B
Go
Raw Normal View History

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 {
filesystem,
size,
used,
available,
usePerc,
mountedOn string
}
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{
filesystem: parts[0],
2017-10-23 09:08:19 +00:00
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
}