]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - modules/naemon/default.nix
Use attrs for secrets instead of lists
[perso/Immae/Config/Nix.git] / modules / naemon / default.nix
CommitLineData
3bc32d9e
IB
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.naemon;
7
8 naemonConfig = pkgs.runCommand "naemon-config" {
9 objectsFile = pkgs.writeText "naemon_objects.cfg" cfg.objectDefs;
10 resourceFile = config.secrets.fullPaths."naemon/resources.cfg";
11 extraConfig = pkgs.writeText "extra.cfg" cfg.extraConfig;
12 inherit (cfg) logDir varDir runDir cacheDir;
13 } ''
14 substituteAll ${./naemon.cfg} $out
15 cat $extraConfig >> $out
16 '';
17in
18{
19 options = {
20 services.naemon = {
21 enable = mkOption {
22 default = false;
23 description = "
24 Whether to use <link
25 xlink:href='http://www.naemon.org/'>Naemon</link> to monitor
26 your system or network.
27 ";
28 };
29
30 objectDefs = mkOption {
31 type = types.lines;
32 default = "";
33 description = "
34 A list of Naemon object configuration that must define
35 the hosts, host groups, services and contacts for the
36 network that you want Naemon to monitor.
37 ";
38 };
39
40 extraResource = mkOption {
41 type = types.lines;
42 default = "";
43 example = ''
44 # Sets $USER2$ to be the path to event handlers
45 #$USER2$=/usr/lib/monitoring-plugins/eventhandlers
46
47 # Store some usernames and passwords (hidden from the CGIs)
48 #$USER3$=someuser
49 #$USER4$=somepassword
50 '';
51 description = "
52 Lines to add to the resource file
53 # You can define $USERx$ macros in this file, which can in turn be used
54 # in command definitions in your host config file(s). $USERx$ macros are
55 # useful for storing sensitive information such as usernames, passwords,
56 # etc. They are also handy for specifying the path to plugins and
57 # event handlers - if you decide to move the plugins or event handlers to
58 # a different directory in the future, you can just update one or two
59 # $USERx$ macros, instead of modifying a lot of command definitions.
60 #
61 # Naemon supports up to 256 $USERx$ macros ($USER1$ through $USER256$)
62 #
63 # Resource files may also be used to store configuration directives for
64 # external data sources like MySQL...
65 #
66 ";
67 };
68
69 extraConfig = mkOption {
70 type = types.lines;
71 default = "";
72 description = "
73 Extra config to append to main config
74 ";
75 };
76
77 user = mkOption {
78 type = types.str;
79 default = "naemon";
80 description = "User for naemon";
81 };
82
83 group = mkOption {
84 type = types.str;
85 default = "naemon";
86 description = "Group for naemon";
87 };
88
89 varDir = mkOption {
90 type = types.path;
91 default = "/var/lib/naemon";
92 description = "The directory where naemon stores its data";
93 };
94
95 cacheDir = mkOption {
96 type = types.path;
97 default = "/var/cache/naemon";
98 description = "The directory where naemon stores its cache";
99 };
100
101 runDir = mkOption {
102 type = types.path;
103 default = "/run/naemon";
104 description = "The directory where naemon stores its runtime files";
105 };
106
107 logDir = mkOption {
108 type = types.path;
109 default = "/var/log/naemon";
110 description = "The directory where naemon stores its log files";
111 };
112
113 package = mkOption {
114 type = types.package;
115 default = pkgs.naemon.override {
116 inherit (cfg) varDir cacheDir logDir runDir user group;
117 };
118 description = ''
119 Naemon package to use
120 '';
121 };
122 };
123 };
124
125
126 config = mkIf cfg.enable {
4c4652aa
IB
127 secrets.keys = {
128 "naemon/resources.cfg" = {
3bc32d9e
IB
129 user = cfg.user;
130 group = cfg.group;
131 permissions = "0400";
132 text = ''
133 $USER1$=${pkgs.monitoring-plugins}/libexec
134 ${cfg.extraResource}
135 '';
4c4652aa
IB
136 };
137 };
3bc32d9e 138
258dd18b
IB
139 users.users = optionalAttrs (cfg.user == "naemon") {
140 naemon = {
3bc32d9e
IB
141 group = cfg.group;
142 uid = config.ids.uids.nagios;
143 extraGroups = [ "keys" ];
258dd18b
IB
144 };
145 };
146 users.groups = optionalAttrs (cfg.user == "naemon") {
147 naemon = {
148 gid = config.ids.gids.nagios;
149 };
150 };
3bc32d9e
IB
151
152 services.filesWatcher.naemon = {
153 paths = [ config.secrets.fullPaths."naemon/resources.cfg" ];
154 };
155 systemd.services.naemon = {
156 description = "Naemon monitoring daemon";
157 path = [ cfg.package pkgs.monitoring-plugins ];
158 wantedBy = [ "multi-user.target" ];
159 after = [ "network.target" ];
160
161 preStart = "${cfg.package}/bin/naemon -vp ${naemonConfig}";
162 script = "${cfg.package}/bin/naemon --daemon ${naemonConfig}";
163 reload = "${pkgs.utillinux}/bin/kill -HUP $MAINPID";
164 serviceConfig = {
165 User = cfg.user;
166 Restart = "always";
167 RestartSec = 2;
168 StandardOutput = "journal";
169 StandardError = "inherit";
170 PIDFile = "${cfg.runDir}/naemon.pid";
171 LogsDirectory = assert lib.strings.hasPrefix "/var/log/" cfg.logDir;
172 lib.strings.removePrefix "/var/log/" cfg.logDir;
173 CacheDirectory = assert lib.strings.hasPrefix "/var/cache/" cfg.cacheDir;
174 let unprefixed = lib.strings.removePrefix "/var/cache/" cfg.cacheDir;
175 in [ unprefixed "${unprefixed}/checkresults" ];
176 StateDirectory = assert lib.strings.hasPrefix "/var/lib/" cfg.varDir;
177 lib.strings.removePrefix "/var/lib/" cfg.varDir;
178 RuntimeDirectory = assert lib.strings.hasPrefix "/run/" cfg.runDir;
179 lib.strings.removePrefix "/run/" cfg.runDir;
180 };
181 };
182 };
183}