ssh_crawler/stat/disk_ifree.go

78 lines
1.4 KiB
Go
Raw Normal View History

2017-10-26 02:38:04 +00:00
package stat
import (
"bufio"
"io"
"strings"
"strconv"
"git.loafle.net/overflow/ssh_crawler/util"
)
type DiskIFreeStat struct {
Filesystem,
Size,
Used,
Free,
UsePerc,
MountedOn string
}
func (diskFree DiskIFreeStat) Command() string {
return "df -ik"
}
func (diskio DiskIFreeStat) Read(r io.Reader, keys []string) (*map[string]string, error) {
var scanner = bufio.NewScanner(r)
var stats = []DiskIFreeStat{}
scanner.Scan()
for scanner.Scan() {
parts := strings.Fields(scanner.Text())
stats = append(stats, DiskIFreeStat{
Filesystem: parts[0],
Size: parts[1],
Used: parts[2],
Free: parts[3],
UsePerc: removePercUnit(parts[4]),
MountedOn: parts[5],
})
}
res, err := diskio.parse(keys, stats)
if err != nil {
return nil, err
}
return &res, scanner.Err()
}
func (diskio DiskIFreeStat) parse(keys []string, data []DiskIFreeStat) (map[string]string, error) {
resMap := make(map[string]string)
for _, key := range keys {
t := strings.Split(key, ".")
suffix := t[len(t)-1]
ext := util.ExtractInBracket(key)
idx, _ := strconv.Atoi(ext)
switch suffix {
case "fs":
resMap[key] = data[idx].Filesystem
case "used":
resMap[key] = data[idx].Used
case "free":
resMap[key] = data[idx].Free
case "usedperc":
resMap[key] = data[idx].UsePerc
case "mounted":
resMap[key] = data[idx].MountedOn
default:
}
}
return resMap, nil
}