ssh_crawler/stat/disk_free.go

86 lines
1.5 KiB
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"
2017-10-24 09:23:56 +00:00
"strconv"
"git.loafle.net/overflow/ssh_crawler/util"
2017-10-23 05:31:04 +00:00
)
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-24 09:23:56 +00:00
func (diskio DiskFreeStat) Read(r io.Reader, keys []string) (*map[string]string, 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-24 09:23:56 +00:00
res, err := diskio.parse(keys, stats)
if err != nil {
return nil, err
}
return &res, 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
}
2017-10-24 09:23:56 +00:00
func (diskio DiskFreeStat) parse(keys []string, data []DiskFreeStat) (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 "available":
resMap[key] = data[idx].Available
case "usedperc":
resMap[key] = data[idx].UsePerc
case "mounted":
resMap[key] = data[idx].MountedOn
default:
}
}
return resMap, nil
}