int MemoryAbove::init()

in src/oomd/plugins/MemoryAbove.cpp [35:98]


int MemoryAbove::init(
    const Engine::PluginArgs& args,
    const PluginConstructionContext& context) {
  // load `meminfo` outside of the PluginArgParser.
  // Because the threshold arg parsing depends on meminfo, to avoid making the
  // parser over complicated for this specific case. We handle it as a special
  // case and make a copy of args with `meminfo` removed.
  auto meminfoMaybe = args.find("meminfo_location") != args.end()
      ? Fs::getMeminfo(args.at("meminfo_location"))
      : Fs::getMeminfo();

  if (!meminfoMaybe) {
    OLOG << "Could not read meminfo " << meminfoMaybe.error().what();
    return 1;
  } else if (!meminfoMaybe->count("MemTotal")) {
    OLOG << "meminfo does not contain MemTotal";
    return 1;
  }
  auto memTotal = (*meminfoMaybe)["MemTotal"];

  // erase meminfo_location since we already loaded it
  auto argsCopy = args;
  argsCopy.erase("meminfo_location");

  // by default we're looking for `threshold`.
  // if `threshold_anon` is passed in, use it instead
  auto thresholdArgName = "threshold";
  if (argsCopy.find("threshold_anon") != argsCopy.end()) {
    thresholdArgName = "threshold_anon";
    // remove threshold so that we don't need to handle it in the parser
    argsCopy.erase("threshold");
    is_anon_ = true;
  }

  argParser_.addArgumentCustom(
      "cgroup",
      cgroups_,
      [context](const std::string& cgroupStr) {
        return PluginArgParser::parseCgroup(context, cgroupStr);
      },
      true);

  argParser_.addArgumentCustom(
      thresholdArgName,
      threshold_,
      [memTotal](const std::string& str) {
        int64_t res = 0;
        if (Util::parseSizeOrPercent(str, &res, memTotal) != 0) {
          throw std::invalid_argument("Failed to parse threshold: " + str);
        }
        return res;
      },
      true);

  argParser_.addArgument("duration", duration_, true);
  argParser_.addArgument("debug", debug_);

  if (!argParser_.parse(argsCopy)) {
    return 1;
  }

  // Success
  return 0;
}