Reads output of linux “df” command (usage of filesystems) as splits it to the parts using regular expression.

package main

import (
	"fmt"
	"log"
	"os/exec"
	"regexp"
	"strconv"
	"strings"
	"time"
)

type dfdata struct {
	fs        string
	size      int64
	used      int64
	available int64
	percents  int64
	mount     string
}

var (
	printDebugMsg = false
)

func main() {
	cmd := "df"
	pattern := `(?P<filesystem>[a-z0-9/]*)\s*(?P<size>[0-9]*)\s*(?P<used>[0-9]*)\s*(?P<available>[0-9]*)\s*(?P<percents>[0-9]*)% (?P<mount>.*)`
	output, err := exec.Command(cmd).CombinedOutput()
	if err != nil {
		log.Fatal()
	}
	dfLines := strings.Split(string(output), "\n")
	var f dfdata
	for i := range dfLines {
		if i > 0 {
			dfline := dfLines[i]
			debugMsg(fmt.Sprint(i, ":", dfline))
			re := regexp.MustCompile(pattern)
			match := re.FindStringSubmatch(dfline)
			pm := make(map[string]string)
			for i, name := range re.SubexpNames() {
				if i > 0 && i <= len(match) {
					pm[name] = match[i]
				}
			}
			f.fs = pm["filesystem"]
			f.size, err = strconv.ParseInt(pm["size"], 10, 64)
			f.used, err = strconv.ParseInt(pm["used"], 10, 64)
			f.available, err = strconv.ParseInt(pm["available"], 10, 64)
			f.percents, err = strconv.ParseInt(pm["percents"], 10, 64)
			f.mount = pm["mount"]
			fmt.Println(f)
		}
	}
}

func curTime() string {
	return time.Now().Format(time.RFC3339Nano) + ": "
}

func debugMsg(t string) {
	if printDebugMsg == true {
		fmt.Println(curTime(), t)
	}
}