summary refs log tree commit diff
path: root/nixos/modules/services/admin/oxidized.nix
blob: da81be3f23e85a6287afb7905fd12f51b045923d (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
{ config, pkgs, lib, ... }:

with lib;

let
  cfg = config.services.oxidized;
in
{
  options.services.oxidized = {
    enable = mkEnableOption "the oxidized configuration backup service";

    user = mkOption {
      type = types.str;
      default = "oxidized";
      description = ''
        User under which the oxidized service runs.
      '';
    };

    group = mkOption {
      type = types.str;
      default = "oxidized";
      description = ''
        Group under which the oxidized service runs.
      '';
    };

    dataDir = mkOption {
      type = types.path;
      default = "/var/lib/oxidized";
      description = "State directory for the oxidized service.";
    };

    configFile = mkOption {
      type = types.path;
      example = literalExample ''
        pkgs.writeText "oxidized-config.yml" '''
          ---
          debug: true
          use_syslog: true
          input:
            default: ssh
            ssh:
              secure: true
          interval: 3600
          model_map:
            dell: powerconnect
            hp: procurve
          source:
            default: csv
            csv:
              delimiter: !ruby/regexp /:/
              file: "/var/lib/oxidized/.config/oxidized/router.db"
              map:
                name: 0
                model: 1
                username: 2
                password: 3
          pid: "/var/lib/oxidized/.config/oxidized/pid"
          rest: 127.0.0.1:8888
          retries: 3
          # ... additional config
        ''';
      '';
      description = ''
        Path to the oxidized configuration file.
      '';
    };

    routerDB = mkOption {
      type = types.path;
      example = literalExample ''
        pkgs.writeText "oxidized-router.db" '''
          hostname-sw1:powerconnect:username1:password2
          hostname-sw2:procurve:username2:password2
          # ... additional hosts
        '''
      '';
      description = ''
        Path to the file/database which contains the targets for oxidized.
      '';
    };
  };

  config = mkIf cfg.enable {
    users.groups.${cfg.group} = { };
    users.users.${cfg.user} = {
      description = "Oxidized service user";
      group = cfg.group;
      home = cfg.dataDir;
      createHome = true;
      isSystemUser = true;
    };

    systemd.services.oxidized = {
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];

      preStart = ''
        mkdir -p ${cfg.dataDir}/.config/oxidized
        ln -f -s ${cfg.routerDB} ${cfg.dataDir}/.config/oxidized/router.db
        ln -f -s ${cfg.configFile} ${cfg.dataDir}/.config/oxidized/config
      '';

      serviceConfig = {
        ExecStart = "${pkgs.oxidized}/bin/oxidized";
        User = cfg.user;
        Group = cfg.group;
        UMask = "0077";
        NoNewPrivileges = true;
        Restart  = "always";
        WorkingDirectory = cfg.dataDir;
        KillSignal = "SIGKILL";
      };
    };
  };
}