Example #1
0
// returns a column of the routing table as a slice of strings
func routingTableColumn(name string) []string {
	cmd := exec.Command("route", "-n")
	out := wrkutils.CommandOutput(cmd)
	table := tabular.ProbabalisticSplit(out)
	if len(table) < 1 {
		log.WithFields(log.Fields{
			"column": name,
			"table":  "\n" + tabular.ToString(table),
		}).Fatal("Routing table was not available or not properly parsed")
	}
	finalTable := table[1:] // has extra line before headers
	return tabular.GetColumnByHeader(name, finalTable)
}
Example #2
0
// getSwapOrMemory returns output from `free`, it is an abstraction of
// getSwap and getMemory. inputs: status: free | used | total
// swapOrMem: memory | swap, units: b | kb | mb | gb | tb
// TODO: support kib/gib style units, with proper transformations.
func getSwapOrMemory(status string, swapOrMem string, units string) int {
	statusToColumn := map[string]int{
		"total": 1,
		"used":  2,
		"free":  3,
	}
	unitsToFlag := map[string]string{
		"b":  "--bytes",
		"kb": "--kilo",
		"mb": "--mega",
		"gb": "--giga",
		"tb": "--tera",
	}
	typeToRow := map[string]int{
		"memory": 0,
		"swap":   1,
	}
	// check to see that our keys are really in our dict
	if _, ok := statusToColumn[status]; !ok {
		log.WithFields(log.Fields{
			"status":   status,
			"expected": []string{"total", "used", "free"},
		}).Fatal("Internal error: invalid status in getSwapOrMemory")
	} else if _, ok := unitsToFlag[units]; !ok {
		log.WithFields(log.Fields{
			"units":    units,
			"expected": []string{"b", "kb", "mb", "gb", "tb"},
		}).Fatal("Internal error: invalid units in getSwapOrMemory")
	} else if _, ok := typeToRow[swapOrMem]; !ok {
		log.WithFields(log.Fields{
			"option":   swapOrMem,
			"expected": []string{"memory", "swap"},
		}).Fatal("Internal error: invalid option in getSwapOrMemory")
	}
	// execute free and return the appropriate output
	cmd := exec.Command("free", unitsToFlag[units])
	outStr := wrkutils.CommandOutput(cmd)
	table := tabular.ProbabalisticSplit(outStr)
	column := tabular.GetColumnByHeader(status, table)
	row := typeToRow[swapOrMem]
	// check for errors in output of `free`
	if column == nil {
		log.WithFields(log.Fields{
			"header": status,
			"table":  "\n" + tabular.ToString(table),
		}).Fatal("Free column was empty")
	}
	if row >= len(column) {
		log.WithFields(log.Fields{
			"output": outStr,
			"column": column,
			"row":    row,
		}).Fatal("`free` didn't output enough rows")
	}
	toReturn, err := strconv.ParseInt(column[row], 10, 64)
	if err != nil {
		log.WithFields(log.Fields{
			"cell":   column[row],
			"error":  err.Error(),
			"output": outStr,
		}).Fatal("Couldn't parse output of `free` as an int")
	}
	return int(toReturn)
}