hash_to_int

Table of Contents

Prototype: hash_to_int(lower, upper, string)

Return type: int

Description: Generates an integer between lower and upper range based on hash of string.

Notes:

This function is similar to splayclass() but more widely usable. Anything that involves orchestration of many hosts could use this function, either for evenly spreading out the scheduling, or even for static load balancing. The result would may be coupled with an ifelse() clause of some sort, or just used directly.

Arguments:

  • lower (inclusive): int - Lower inclusive bound - in the range: -99999999999,99999999999
  • upper (exclusive): int - Upper exclusive bound - in the range: -99999999999,99999999999
  • string: string - Input string to hash - in the range: .*

Example:

body common control
{
  bundlesequence => { "example" };
}

bundle agent example
{
  vars:
    "hello"  int => hash_to_int(0, 1000, "hello");
    "world"  int => hash_to_int(0, 1000, "world");

    # Hash can vary on hostkey or policy hub:
    "hour"   int => hash_to_int(0, 24, "$(sys.key_digest)");
    "minute" int => hash_to_int(0, 60, "$(sys.policy_hub)");

  reports:
      "'hello' hashed to: $(hello)";
      "'world' hashed to: $(world)";

}

Output:

R: 'hello' hashed to: 172
R: 'world' hashed to: 760

History:

  • Introduced in 3.12.0.

See also: splayclass()