aboutsummaryrefslogtreecommitdiff
path: root/modules/private/monitoring/to_objects.nix
blob: 7b4b5233b0774dd99eb240d01aa2a67b13a1b23c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
{ 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