]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - modules/private/monitoring/to_objects.nix
Add monitoring host
[perso/Immae/Config/Nix.git] / modules / private / monitoring / to_objects.nix
CommitLineData
eb071dd4
IB
1{ lib }:
2 with lib.attrsets;
3 with lib.strings;
4 with lib.lists;
5 with lib.trivial;
6let
7 pad = width: str: let
8 padWidth = width - stringLength str;
9 padding = concatStrings (genList (const " ") padWidth);
10 in str + optionalString (padWidth > 0) padding;
11 toStr = k: v:
12 if k == "check_command" && builtins.isList v
13 then builtins.concatStringsSep "!" v
14 else v;
15
16 toService = service: ''
17 define service {
18 ${builtins.concatStringsSep "\n" (mapAttrsToList (k: v:
19 " ${pad 30 k} ${toStr k v}"
e820134d 20 ) (filterAttrs (k: v: ! builtins.elem k ["passiveInfo" "filter"]) service))}
eb071dd4
IB
21 }
22 '';
23 toServices = services: builtins.concatStringsSep "\n" (map toService services);
24
25 toCommand = k: v: ''
26 define command {
27 ${pad 30 "command_name"} ${k}
28 ${pad 30 "command_line"} ${v}
29 }
30 '';
31 toCommands = a: builtins.concatStringsSep "\n" (mapAttrsToList toCommand a);
32
33 toOther = keyname: k: v: ''
34 define ${keyname} {
35 ${pad 30 "${keyname}_name"} ${k}
36 ${builtins.concatStringsSep "\n" (mapAttrsToList (kk: vv:
37 " ${pad 30 kk} ${vv}"
38 ) v)}
39 }
40 '';
41 toOthers = keyname: a: builtins.concatStringsSep "\n" (mapAttrsToList (toOther keyname) a);
42
43 toTemplate = keyname: k: v: ''
44 define ${keyname} {
45 ${pad 30 "name"} ${k}
46 ${pad 30 "register"} 0
47 ${builtins.concatStringsSep "\n" (mapAttrsToList (kk: vv:
48 " ${pad 30 kk} ${vv}"
49 ) v)}
50 }
51 '';
52 toTemplates' = keyname: a: builtins.concatStringsSep "\n" (mapAttrsToList (toTemplate keyname) a);
53 toTemplates = v: builtins.concatStringsSep "\n" (mapAttrsToList toTemplates' v);
54
55 toObjects' = keyname: v:
56 if keyname == "service"
57 then toServices v
58 else if keyname == "command"
59 then toCommands v
60 else if keyname == "templates"
61 then toTemplates v
e820134d 62 else if builtins.elem keyname ["hostgroup" "host" "contactgroup" "contact" "timeperiod" "servicegroup"]
eb071dd4
IB
63 then toOthers keyname v
64 else "";
65 toObjects = v: builtins.concatStringsSep "\n" (mapAttrsToList toObjects' v);
66in
67 toObjects