about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/monitoring/ocsinventory-agent.nix
blob: a36375587759c71c7069dc942f36e518b5f5a392 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
{ config, lib, pkgs, ... }:

let
  cfg = config.services.ocsinventory-agent;

  settingsFormat = pkgs.formats.keyValue {
    mkKeyValue = lib.generators.mkKeyValueDefault { } "=";
  };

in
{
  meta = {
    doc = ./ocsinventory-agent.md;
    maintainers = with lib.maintainers; [ anthonyroussel ];
  };

  options = {
    services.ocsinventory-agent = {
      enable = lib.mkEnableOption (lib.mdDoc "OCS Inventory Agent");

      package = lib.mkPackageOption pkgs "ocsinventory-agent" { };

      settings = lib.mkOption {
        type = lib.types.submodule {
          freeformType = settingsFormat.type.nestedTypes.elemType;

          options = {
            server = lib.mkOption {
              type = lib.types.nullOr lib.types.str;
              example = "https://ocsinventory.localhost:8080/ocsinventory";
              default = null;
              description = lib.mdDoc ''
                The URI of the OCS Inventory server where to send the inventory file.

                This option is ignored if {option}`services.ocsinventory-agent.settings.local` is set.
              '';
            };

            local = lib.mkOption {
              type = lib.types.nullOr lib.types.path;
              example = "/var/lib/ocsinventory-agent/reports";
              default = null;
              description = lib.mdDoc ''
                If specified, the OCS Inventory Agent will run in offline mode
                and the resulting inventory file will be stored in the specified path.
              '';
            };

            ca = lib.mkOption {
              type = lib.types.path;
              default = "/etc/ssl/certs/ca-certificates.crt";
              description = lib.mdDoc ''
                Path to CA certificates file in PEM format, for server
                SSL certificate validation.
              '';
            };

            tag = lib.mkOption {
              type = lib.types.nullOr lib.types.str;
              default = null;
              example = "01234567890123";
              description = lib.mdDoc "Tag for the generated inventory.";
            };

            debug = lib.mkEnableOption (lib.mdDoc "debug mode");
          };
        };
        default = { };
        example = {
          ca = "/etc/ssl/certs/ca-certificates.crt";
          debug = true;
          server = "https://ocsinventory.localhost:8080/ocsinventory";
          tag = "01234567890123";
        };
        description = lib.mdDoc ''
          Configuration for /etc/ocsinventory-agent/ocsinventory-agent.cfg.

          Refer to
          {manpage}`ocsinventory-agent(1)` for available options.
        '';
      };

      interval = lib.mkOption {
        type = lib.types.str;
        default = "daily";
        example = "06:00";
        description = lib.mdDoc ''
          How often we run the ocsinventory-agent service. Runs by default every daily.

          The format is described in
          {manpage}`systemd.time(7)`.
        '';
      };
    };
  };

  config =
    let
      configFile = settingsFormat.generate "ocsinventory-agent.cfg" cfg.settings;

    in lib.mkIf cfg.enable {
      # Path of the configuration file is hard-coded and cannot be changed
      # https://github.com/OCSInventory-NG/UnixAgent/blob/v2.10.0/lib/Ocsinventory/Agent/Config.pm#L78
      #
      environment.etc."ocsinventory-agent/ocsinventory-agent.cfg".source = configFile;

      systemd.services.ocsinventory-agent = {
        description = "OCS Inventory Agent service";
        wantedBy = [ "multi-user.target" ];
        after = [ "network.target" ];

        reloadTriggers = [ configFile ];

        serviceConfig = {
          ExecStart = lib.getExe cfg.package;
          ConfigurationDirectory = "ocsinventory-agent";
          StateDirectory = "ocsinventory-agent";
        };
      };

      systemd.timers.ocsinventory-agent = {
        description = "Launch OCS Inventory Agent regularly";
        wantedBy = [ "timers.target" ];

        timerConfig = {
          OnCalendar = cfg.interval;
          AccuracySec = "1h";
          RandomizedDelaySec = 240;
          Persistent = true;
          Unit = "ocsinventory-agent.service";
        };
      };
    };
}