aboutsummaryrefslogblamecommitdiff
path: root/modules/private/monitoring/to_objects.nix
blob: 12721d2ff6015a5bc08e51c687c494f9befcb530 (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}"
    ) (filterAttrs (k: v: ! builtins.elem k ["passiveInfo" "filter"]) 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)}
    }
    '';
  toOtherNoName = keyname: v: ''
    define ${keyname} {
    ${builtins.concatStringsSep "\n" (mapAttrsToList (kk: vv:
      "  ${pad 30 kk}   ${vv}"
    ) v)}
    }
    '';
  toOthers = keyname: a: builtins.concatStringsSep "\n" (mapAttrsToList (toOther keyname) a);
  toOthersArray = keyname: a: builtins.concatStringsSep "\n" (map (toOtherNoName 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 ["hostgroup" "host" "contactgroup" "contact" "timeperiod" "servicegroup"]
      then toOthers keyname v
    else if builtins.elem keyname ["servicedependency"]
      then toOthersArray keyname v
    else "";
  toObjects = v: builtins.concatStringsSep "\n" (mapAttrsToList toObjects' v);
in
  toObjects