// freeMemOrSwap is an abstraction of FreeMemory and FreeSwap, which measures // if the desired resource has a quantity free above the amount specified func freeMemOrSwap(input string, swapOrMem string) (int, string, error) { amount, units, err := chkutil.SeparateByteUnits(input) if err != nil { log.WithFields(log.Fields{ "err": err.Error(), }).Fatal("Couldn't separate string into a scalar and units") } var actualAmount int switch strings.ToLower(swapOrMem) { case "memory": actualAmount, err = memstatus.FreeMemory(units) case "swap": actualAmount, err = memstatus.FreeSwap(units) default: log.Fatalf("Invalid option passed to freeMemoOrSwap: %s", swapOrMem) } if err != nil { return 1, "", err } else if actualAmount > amount { return errutil.Success() } msg := "Free " + swapOrMem + " lower than defined threshold" actualString := fmt.Sprint(actualAmount) + units return errutil.GenericError(msg, input, []string{actualString}) }
func (chk FreeSwap) New(params []string) (chkutil.Check, error) { if len(params) != 1 { return chk, errutil.ParameterLengthError{1, params} } _, _, err := chkutil.SeparateByteUnits(params[0]) if err != nil { return chk, errutil.ParameterTypeError{params[0], "amount"} } chk.amount = params[0] return chk, nil }