package stat import ( "bufio" "io" "strings" "strconv" "git.loafle.net/overflow/ssh_crawler/util" ) type DiskFreeStat struct { Filesystem, Size, Used, Available, UsePerc, MountedOn string } func (diskFree DiskFreeStat) Command() string { return "df -k" } func (diskio DiskFreeStat) Read(r io.Reader, keys []string) (*map[string]string, 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], }) } res, err := diskio.parse(keys, stats) if err != nil { return nil, err } return &res, scanner.Err() } func removePercUnit(str string) string { if !strings.HasSuffix(str, "%") { return str } str = str[:len(str)-1] return str } 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 }