blob: 7b4b5233b0774dd99eb240d01aa2a67b13a1b23c (
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)}
}
'';
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 ["hostgroup" "host" "contactgroup" "contact" "timeperiod" "servicegroup"]
then toOthers keyname v
else "";
toObjects = v: builtins.concatStringsSep "\n" (mapAttrsToList toObjects' v);
in
toObjects
|