func UsedMemory(units string) (int, error) {
	if strings.ToLower(units) == "percent" {
		used, err := swapOrMemory("used", "memory", "b")
		if err != nil {
			return 0, err
		}
		total, err := swapOrMemory("total", "memory", "b")
		if err != nil {
			return 0, err
		}
		return int((float32(used) / float32(total)) * 100), nil
	}
	return swapOrMemory("used", "memory", units)
}
FreeMemory returns the amount of memory that's currently unoccupied.
units : b, kb, mb, gb, tb, percent
package memstatus
import (
	"errors"
	"os/exec"
	"regexp"
	"strconv"
	"strings"
	"github.com/CiscoCloud/distributive/tabular"
)
func swapOrMemory(status string, swapOrMem string, units string) (int, error) {
	unitsToFlag := map[string]string{
		"b":  "--bytes",
		"kb": "--kilo",
		"mb": "--mega",
		"gb": "--giga",
		"tb": "--tera",
	}
	typeToRow := map[string]int{
		"memory": 0,
		"swap":   1,
	}
	if _, ok := unitsToFlag[units]; !ok {
		return 0, errors.New("Invalid units in swapOrMemory: " + units)
	} else if _, ok := typeToRow[swapOrMem]; !ok {
		return 0, errors.New("Invalid option in swapOrMemory: " + swapOrMem)
	}
	cmd := exec.Command("free", unitsToFlag[units])
	out, err := cmd.CombinedOutput()
	if err != nil {
		return 0, err
	}
	colSep := regexp.MustCompile(`\s+`)
	rowSep := regexp.MustCompile(`\n+`)
	table := tabular.SeparateString(rowSep, colSep, string(out))
	column := tabular.GetColumnByHeader(status, table)
	for i, row := range table {
		if len(row) > 0 && strings.Contains(row[0], "-/+") {
			table = append(table[:i], table[i+1:]...)
		}
	}
	row := typeToRow[swapOrMem]
	if column == nil || len(column) < 1 {
		errors.New("Free column was empty")
	}
	if row >= len(column) {
		errors.New("`free` didn't output enough rows")
	}
	toReturn, err := strconv.ParseInt(column[row], 10, 64)
	if err != nil {
		errors.New("Couldn't parse output of `free` as an int")
	}
	return int(toReturn), nil
}
func FreeMemory(units string) (int, error) {
	if strings.ToLower(units) == "percent" {
		free, err := swapOrMemory("free", "memory", "b")
		if err != nil {
			return 0, err
		}
		total, err := swapOrMemory("total", "memory", "b")
		if err != nil {
			return 0, err
		}
		return int((float32(free) / float32(total)) * 100), nil
	}
	return swapOrMemory("free", "memory", units)
}