git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #51980 from ToxicFrog/munin-plugins

nixos/munin: New options (and some bugfixes) for service configuration

commitdfce20e4e3b603428a6cb7f13bf7372a60900e22 authorSilvan Mosberger <infinisil@icloud.com> dateTue 5 Feb 2019, 19:35 (8 years ago) parent140a64885cdd06
3 files changed+201−17
nixos/modules/services/monitoring/munin.nix+197−12 nixos/tests/munin.nix+1−3 pkgs/servers/monitoring/munin/default.nix+3−2
nixos/modules/services/monitoring/munin.nix+197−12
@@ -4,7 +4,7 @@
44# TODO: LWP/Pg perl libs aren't recognized
55
66# TODO: support fastcgi
7−# http://munin-monitoring.org/wiki/CgiHowto2
7+# http://guide.munin-monitoring.org/en/latest/example/webserver/apache-cgi.html
88# spawn-fcgi -s /run/munin/fastcgi-graph.sock -U www-data -u munin -g munin /usr/lib/munin/cgi/munin-cgi-graph
99# spawn-fcgi -s /run/munin/fastcgi-html.sock -U www-data -u munin -g munin /usr/lib/munin/cgi/munin-cgi-html
1010# https://paste.sh/vofcctHP#-KbDSXVeWoifYncZmLfZzgum
@@ -24,6 +24,8 @@ let
2424 logdir /var/log/munin
2525 rundir /run/munin
2626
27+ ${lib.optionalString (cronCfg.extraCSS != "") "staticdir ${customStaticDir}"}
28+
2729 ${cronCfg.extraGlobalConfig}
2830
2931 ${cronCfg.hosts}
@@ -63,6 +65,11 @@ let
6365 [ipmi*]
6466 user root
6567 group root
68+
69+ [munin*]
70+ env.UPDATE_STATSFILE /var/lib/munin/munin-update.stats
71+
72+ ${nodeCfg.extraPluginConfig}
6673 '';
6774
6875 pluginConfDir = pkgs.stdenv.mkDerivation {
@@ -72,6 +79,54 @@ let
7279 ln -s ${pluginConf} $out/nixos-config
7380 '';
7481 };
82+
83+ # Copy one Munin plugin into the Nix store with a specific name.
84+ # This is suitable for use with plugins going directly into /etc/munin/plugins,
85+ # i.e. munin.extraPlugins.
86+ internOnePlugin = name: path:
87+ "cp -a '${path}' '${name}'";
88+
89+ # Copy an entire tree of Munin plugins into a single directory in the Nix
90+ # store, with no renaming.
91+ # This is suitable for use with munin-node-configure --suggest, i.e.
92+ # munin.extraAutoPlugins.
93+ internManyPlugins = name: path:
94+ "find '${path}' -type f -perm /a+x -exec cp -a -t . '{}' '+'";
95+
96+ # Use the appropriate intern-fn to copy the plugins into the store and patch
97+ # them afterwards in an attempt to get them to run on NixOS.
98+ internAndFixPlugins = name: intern-fn: paths:
99+ pkgs.runCommand name {} ''
100+ mkdir -p "$out"
101+ cd "$out"
102+ ${lib.concatStringsSep "\n"
103+ (lib.attrsets.mapAttrsToList intern-fn paths)}
104+ chmod -R u+w .
105+ find . -type f -exec sed -E -i '
106+ s,(/usr)?/s?bin/,/run/current-system/sw/bin/,g
107+ ' '{}' '+'
108+ '';
109+
110+ # TODO: write a derivation for munin-contrib, so that for contrib plugins
111+ # you can just refer to them by name rather than needing to include a copy
112+ # of munin-contrib in your nixos configuration.
113+ extraPluginDir = internAndFixPlugins "munin-extra-plugins.d"
114+ internOnePlugin nodeCfg.extraPlugins;
115+
116+ extraAutoPluginDir = internAndFixPlugins "munin-extra-auto-plugins.d"
117+ internManyPlugins
118+ (builtins.listToAttrs
119+ (map
120+ (path: { name = baseNameOf path; value = path; })
121+ nodeCfg.extraAutoPlugins));
122+
123+ customStaticDir = pkgs.runCommand "munin-custom-static-data" {} ''
124+ cp -a "${pkgs.munin}/etc/opt/munin/static" "$out"
125+ cd "$out"
126+ chmod -R u+w .
127+ echo "${cronCfg.extraCSS}" >> style.css
128+ echo "${cronCfg.extraCSS}" >> style-new.css
129+ '';
75130in
76131
77132{
@@ -82,11 +137,12 @@ in
82137
83138 enable = mkOption {
84139 default = false;
140+ type = types.bool;
85141 description = ''
86142 Enable Munin Node agent. Munin node listens on 0.0.0.0 and
87143 by default accepts connections only from 127.0.0.1 for security reasons.
88144
89− See <link xlink:href='http://munin-monitoring.org/wiki/munin-node.conf' />.
145+ See <link xlink:href='http://guide.munin-monitoring.org/en/latest/architecture/index.html' />.
90146 '';
91147 };
92148
@@ -95,18 +151,108 @@ in
95151 type = types.lines;
96152 description = ''
97153 <filename>munin-node.conf</filename> extra configuration. See
98− <link xlink:href='http://munin-monitoring.org/wiki/munin-node.conf' />
154+ <link xlink:href='http://guide.munin-monitoring.org/en/latest/reference/munin-node.conf.html' />
155+ '';
156+ };
157+
158+ extraPluginConfig = mkOption {
159+ default = "";
160+ type = types.lines;
161+ description = ''
162+ <filename>plugin-conf.d</filename> extra plugin configuration. See
163+ <link xlink:href='http://guide.munin-monitoring.org/en/latest/plugin/use.html' />
164+ '';
165+ example = ''
166+ [fail2ban_*]
167+ user root
99168 '';
100169 };
101170
102− # TODO: add option to add additional plugins
171+ extraPlugins = mkOption {
172+ default = {};
173+ type = with types; attrsOf path;
174+ description = ''
175+ Additional Munin plugins to activate. Keys are the name of the plugin
176+ symlink, values are the path to the underlying plugin script. You
177+ can use the same plugin script multiple times (e.g. for wildcard
178+ plugins).
179+
180+ Note that these plugins do not participate in autoconfiguration. If
181+ you want to autoconfigure additional plugins, use
182+ <option>services.munin-node.extraAutoPlugins</option>.
183+
184+ Plugins enabled in this manner take precedence over autoconfigured
185+ plugins.
186+
187+ Plugins will be copied into the Nix store, and it will attempt to
188+ modify them to run properly by fixing hardcoded references to
189+ <literal>/bin</literal>, <literal>/usr/bin</literal>,
190+ <literal>/sbin</literal>, and <literal>/usr/sbin</literal>.
191+ '';
192+ example = literalExample ''
193+ {
194+ zfs_usage_bigpool = /src/munin-contrib/plugins/zfs/zfs_usage_;
195+ zfs_usage_smallpool = /src/munin-contrib/plugins/zfs/zfs_usage_;
196+ zfs_list = /src/munin-contrib/plugins/zfs/zfs_list;
197+ };
198+ '';
199+ };
103200
201+ extraAutoPlugins = mkOption {
202+ default = [];
203+ type = with types; listOf path;
204+ description = ''
205+ Additional Munin plugins to autoconfigure, using
206+ <literal>munin-node-configure --suggest</literal>. These should be
207+ the actual paths to the plugin files (or directories containing them),
208+ not just their names.
209+
210+ If you want to manually enable individual plugins instead, use
211+ <option>services.munin-node.extraPlugins</option>.
212+
213+ Note that only plugins that have the 'autoconfig' capability will do
214+ anything if listed here, since plugins that cannot autoconfigure
215+ won't be automatically enabled by
216+ <literal>munin-node-configure</literal>.
217+
218+ Plugins will be copied into the Nix store, and it will attempt to
219+ modify them to run properly by fixing hardcoded references to
220+ <literal>/bin</literal>, <literal>/usr/bin</literal>,
221+ <literal>/sbin</literal>, and <literal>/usr/sbin</literal>.
222+ '';
223+ example = literalExample ''
224+ [
225+ /src/munin-contrib/plugins/zfs
226+ /src/munin-contrib/plugins/ssh
227+ ];
228+ '';
229+ };
230+
231+ disabledPlugins = mkOption {
232+ # TODO: figure out why Munin isn't writing the log file and fix it.
233+ # In the meantime this at least suppresses a useless graph full of
234+ # NaNs in the output.
235+ default = [ "munin_stats" ];
236+ type = with types; listOf string;
237+ description = ''
238+ Munin plugins to disable, even if
239+ <literal>munin-node-configure --suggest</literal> tries to enable
240+ them. To disable a wildcard plugin, use an actual wildcard, as in
241+ the example.
242+
243+ munin_stats is disabled by default as it tries to read
244+ <literal>/var/log/munin/munin-update.log</literal> for timing
245+ information, and the NixOS build of Munin does not write this file.
246+ '';
247+ example = [ "diskstats" "zfs_usage_*" ];
248+ };
104249 };
105250
106251 services.munin-cron = {
107252
108253 enable = mkOption {
109254 default = false;
255+ type = types.bool;
110256 description = ''
111257 Enable munin-cron. Takes care of all heavy lifting to collect data from
112258 nodes and draws graphs to html. Runs munin-update, munin-limits,
@@ -119,11 +265,12 @@ in
119265
120266 extraGlobalConfig = mkOption {
121267 default = "";
268+ type = types.lines;
122269 description = ''
123270 <filename>munin.conf</filename> extra global configuration.
124− See <link xlink:href='http://munin-monitoring.org/wiki/munin.conf' />.
271+ See <link xlink:href='http://guide.munin-monitoring.org/en/latest/reference/munin.conf.html' />.
125272 Useful to setup notifications, see
126− <link xlink:href='http://munin-monitoring.org/wiki/HowToContact' />
273+ <link xlink:href='http://guide.munin-monitoring.org/en/latest/tutorial/alert.html' />
127274 '';
128275 example = ''
129276 contact.email.command mail -s "Munin notification for ''${var:host}" someone@example.com
@@ -131,14 +278,34 @@ in
131278 };
132279
133280 hosts = mkOption {
281+ default = "";
282+ type = types.lines;
283+ description = ''
284+ Definitions of hosts of nodes to collect data from. Needs at least one
285+ host for cron to succeed. See
286+ <link xlink:href='http://guide.munin-monitoring.org/en/latest/reference/munin.conf.html' />
287+ '';
134288 example = ''
135289 [''${config.networking.hostName}]
136290 address localhost
137291 '';
292+ };
293+
294+ extraCSS = mkOption {
295+ default = "";
296+ type = types.lines;
138297 description = ''
139− Definitions of hosts of nodes to collect data from. Needs at least one
140− hosts for cron to succeed. See
141− <link xlink:href='http://munin-monitoring.org/wiki/munin.conf' />
298+ Custom styling for the HTML that munin-cron generates. This will be
299+ appended to the CSS files used by munin-cron and will thus take
300+ precedence over the builtin styles.
301+ '';
302+ example = ''
303+ /* A simple dark theme. */
304+ html, body { background: #222222; }
305+ #header, #footer { background: #333333; }
306+ img.i, img.iwarn, img.icrit, img.iunkn {
307+ filter: invert(100%) hue-rotate(-30deg);
308+ }
142309 '';
143310 };
144311
@@ -155,6 +322,7 @@ in
155322 description = "Munin monitoring user";
156323 group = "munin";
157324 uid = config.ids.uids.munin;
325+ home = "/var/lib/munin";
158326 }];
159327
160328 users.groups = [{
@@ -173,14 +341,27 @@ in
173341 environment.MUNIN_PLUGSTATE = "/run/munin";
174342 environment.MUNIN_LOGDIR = "/var/log/munin";
175343 preStart = ''
176− echo "updating munin plugins..."
344+ echo "Updating munin plugins..."
177345
178346 mkdir -p /etc/munin/plugins
179347 rm -rf /etc/munin/plugins/*
348+
349+ # Autoconfigure builtin plugins
180350 ${pkgs.munin}/bin/munin-node-configure --suggest --shell --families contrib,auto,manual --config ${nodeConf} --libdir=${pkgs.munin}/lib/plugins --servicedir=/etc/munin/plugins --sconfdir=${pluginConfDir} 2>/dev/null | ${pkgs.bash}/bin/bash
181351
182− # NOTE: we disable disktstats because plugin seems to fail and it hangs html generation (100% CPU + memory leak)
183− rm /etc/munin/plugins/diskstats || true
352+ # Autoconfigure extra plugins
353+ ${pkgs.munin}/bin/munin-node-configure --suggest --shell --families contrib,auto,manual --config ${nodeConf} --libdir=${extraAutoPluginDir} --servicedir=/etc/munin/plugins --sconfdir=${pluginConfDir} 2>/dev/null | ${pkgs.bash}/bin/bash
354+
355+ ${lib.optionalString (nodeCfg.extraPlugins != {}) ''
356+ # Link in manually enabled plugins
357+ ln -f -s -t /etc/munin/plugins ${extraPluginDir}/*
358+ ''}
359+
360+ ${lib.optionalString (nodeCfg.disabledPlugins != []) ''
361+ # Disable plugins
362+ cd /etc/munin/plugins
363+ rm -f ${toString nodeCfg.disabledPlugins}
364+ ''}
184365 '';
185366 serviceConfig = {
186367 ExecStart = "${pkgs.munin}/sbin/munin-node --config ${nodeConf} --servicedir /etc/munin/plugins/ --sconfdir=${pluginConfDir}";
@@ -192,6 +373,10 @@ in
192373
193374 }) (mkIf cronCfg.enable {
194375
376+ # Munin is hardcoded to use DejaVu Mono and the graphs come out wrong if
377+ # it's not available.
378+ fonts.fonts = [ pkgs.dejavu_fonts ];
379+
195380 systemd.timers.munin-cron = {
196381 description = "batch Munin master programs";
197382 wantedBy = [ "timers.target" ];
nixos/tests/munin.nix+1−3
@@ -15,9 +15,7 @@ import ./make-test.nix ({ pkgs, ...} : {
1515 munin-node = {
1616 enable = true;
1717 # disable a failing plugin to prevent irrelevant error message, see #23049
18− extraConfig = ''
19− ignore_file ^apc_nis$
20− '';
18+ disabledPlugins = [ "apc_nis" ];
2119 };
2220 munin-cron = {
2321 enable = true;
pkgs/servers/monitoring/munin/default.nix+3−2
@@ -3,14 +3,14 @@
33}:
44
55stdenv.mkDerivation rec {
6− version = "2.0.37";
6+ version = "2.0.43";
77 name = "munin-${version}";
88
99 src = fetchFromGitHub {
1010 owner = "munin-monitoring";
1111 repo = "munin";
1212 rev = version;
13− sha256 = "10niyzckx90dwdr4d7vj07d1qjy3nk7xzp30nqnlxzbaww7n5v78";
13+ sha256 = "1ydhf9hcb3n5h0ss5f1zf9yz4r4njqxazlz931ixvx5gyhj9gq5l";
1414 };
1515
1616 buildInputs = [
@@ -75,6 +75,7 @@ stdenv.mkDerivation rec {
7575 ];
7676
7777 preBuild = ''
78+ echo "${version}" > RELEASE
7879 substituteInPlace "Makefile" \
7980 --replace "/bin/pwd" "pwd" \
8081 --replace "HTMLOld.3pm" "HTMLOld.3"