aboutsummaryrefslogblamecommitdiff
path: root/modules/private/monitoring/to_objects.nix
blob: 5ad76e042b1074c553e0cbe29d0ae1d3a3013525 (plain) (tree)


































































                                                                                                    
{ lib }:
  with lib.attrsets;
  with lib.strings;
  with lib.lists;
  with lib.trivial;
let
  pad = width: str: let
      padWidth = width - stringLength str;
      padding = concatStrings (genList (const " ") padWidth);
    in str + optionalString (padWidth > 0) padding;
  toStr = k: v:
    if k == "check_command" && builtins.isList v
    then builtins.concatStringsSep "!" v
    else v;

  toService = service: ''
    define service {
    ${builtins.concatStringsSep "\n" (mapAttrsToList (k: v:
      "  ${pad 30 k}   ${toStr k v}"
    ) service)}
    }
    '';
  toServices = services: builtins.concatStringsSep "\n" (map toService services);

  toCommand = k: v: ''
    define command {
      ${pad 30 "command_name"}   ${k}
      ${pad 30 "command_line"}   ${v}
    }
    '';
  toCommands = a: builtins.concatStringsSep "\n" (mapAttrsToList toCommand a);

  toOther = keyname: k: v: ''
    define ${keyname} {
      ${pad 30 "${keyname}_name"}   ${k}
    ${builtins.concatStringsSep "\n" (mapAttrsToList (kk: vv:
      "  ${pad 30 kk}   ${vv}"
    ) v)}
    }
    '';
  toOthers = keyname: a: builtins.concatStringsSep "\n" (mapAttrsToList (toOther keyname) a);

  toTemplate = keyname: k: v: ''
    define ${keyname} {
      ${pad 30 "name"}   ${k}
      ${pad 30 "register"}   0
    ${builtins.concatStringsSep "\n" (mapAttrsToList (kk: vv:
      "  ${pad 30 kk}   ${vv}"
    ) v)}
    }
    '';
  toTemplates' = keyname: a: builtins.concatStringsSep "\n" (mapAttrsToList (toTemplate keyname) a);
  toTemplates = v: builtins.concatStringsSep "\n" (mapAttrsToList toTemplates' v);

  toObjects' = keyname: v:
    if keyname == "service"
      then toServices v
    else if keyname == "command"
      then toCommands v
    else if keyname == "templates"
      then toTemplates v
    else if builtins.elem keyname ["host" "contactgroup" "contact" "timeperiod"]
      then toOthers keyname v
    else "";
  toObjects = v: builtins.concatStringsSep "\n" (mapAttrsToList toObjects' v);
in
  toObjects