<commit_before>{ config, lib, pkgs, ... }:

# TODO: support munin-async
# TODO: LWP/Pg perl libs aren't recognized

# TODO: support fastcgi
# http://munin-monitoring.org/wiki/CgiHowto2
# spawn-fcgi -s /var/run/munin/fastcgi-graph.sock -U www-data   -u munin -g munin /usr/lib/munin/cgi/munin-cgi-graph
# spawn-fcgi -s /var/run/munin/fastcgi-html.sock  -U www-data   -u munin -g munin /usr/lib/munin/cgi/munin-cgi-html
# https://paste.sh/vofcctHP#-KbDSXVeWoifYncZmLfZzgum
# nginx http://munin.readthedocs.org/en/latest/example/webserver/nginx.html


with lib;

let
  nodeCfg = config.services.munin-node;
  cronCfg = config.services.munin-cron;

  muninPlugins = pkgs.stdenv.mkDerivation {
    name = "munin-available-plugins";
    buildCommand = ''
      mkdir -p $out

      cp --preserve=mode ${pkgs.munin}/lib/plugins/* $out/

      for file in $out/*; do
        case "$file" in
            plugin.sh) continue;;
        esac

        # read magic makers from the file
        family=$(sed -nr 's/.*#%#\s+family\s*=\s*(\S+)\s*/\1/p' $file)
        cap=$(sed -nr 's/.*#%#\s+capabilities\s*=\s*(.+)/\1/p' $file)

        wrapProgram $file \
          --set PATH "/run/wrappers/bin:/run/current-system/sw/bin:/run/current-system/sw/bin" \
          --set MUNIN_LIBDIR "${pkgs.munin}/lib" \
          --set MUNIN_PLUGSTATE "/var/run/munin"

        # munin uses markers to tell munin-node-configure what a plugin can do
        echo "#%# family=$family" >> $file
        echo "#%# capabilities=$cap" >> $file
      done

      # NOTE: we disable disktstats because plugin seems to fail and it hangs html generation (100% CPU + memory leak)
      rm -f $out/diskstats
    '';
    buildInputs = [ pkgs.makeWrapper ];
  };

  muninConf = pkgs.writeText "munin.conf"
    ''
      dbdir     /var/lib/munin
      htmldir   /var/www/munin
      logdir    /var/log/munin
      rundir    /var/run/munin

      ${cronCfg.extraGlobalConfig}

      ${cronCfg.hosts}
    '';

  nodeConf = pkgs.writeText "munin-node.conf"
    ''
      log_level 3
      log_file Sys::Syslog
      port 4949
      host *
      background 0
      user root
      group root
      host_name ${config.networking.hostName}
      setsid 0

      # wrapped plugins by makeWrapper being with dots
      ignore_file ^\.

      allow ^127\.0\.0\.1$

      ${nodeCfg.extraConfig}
    '';
in

{

  options = {

    services.munin-node = {
<commit_msg>munin service: listen on IPv6 loopback as well (#23012)<commit_after>{ config, lib, pkgs, ... }:

# TODO: support munin-async
# TODO: LWP/Pg perl libs aren't recognized

# TODO: support fastcgi
# http://munin-monitoring.org/wiki/CgiHowto2
# spawn-fcgi -s /var/run/munin/fastcgi-graph.sock -U www-data   -u munin -g munin /usr/lib/munin/cgi/munin-cgi-graph
# spawn-fcgi -s /var/run/munin/fastcgi-html.sock  -U www-data   -u munin -g munin /usr/lib/munin/cgi/munin-cgi-html
# https://paste.sh/vofcctHP#-KbDSXVeWoifYncZmLfZzgum
# nginx http://munin.readthedocs.org/en/latest/example/webserver/nginx.html


with lib;

let
  nodeCfg = config.services.munin-node;
  cronCfg = config.services.munin-cron;

  muninPlugins = pkgs.stdenv.mkDerivation {
    name = "munin-available-plugins";
    buildCommand = ''
      mkdir -p $out

      cp --preserve=mode ${pkgs.munin}/lib/plugins/* $out/

      for file in $out/*; do
        case "$file" in
            plugin.sh) continue;;
        esac

        # read magic makers from the file
        family=$(sed -nr 's/.*#%#\s+family\s*=\s*(\S+)\s*/\1/p' $file)
        cap=$(sed -nr 's/.*#%#\s+capabilities\s*=\s*(.+)/\1/p' $file)

        wrapProgram $file \
          --set PATH "/run/wrappers/bin:/run/current-system/sw/bin:/run/current-system/sw/bin" \
          --set MUNIN_LIBDIR "${pkgs.munin}/lib" \
          --set MUNIN_PLUGSTATE "/var/run/munin"

        # munin uses markers to tell munin-node-configure what a plugin can do
        echo "#%# family=$family" >> $file
        echo "#%# capabilities=$cap" >> $file
      done

      # NOTE: we disable disktstats because plugin seems to fail and it hangs html generation (100% CPU + memory leak)
      rm -f $out/diskstats
    '';
    buildInputs = [ pkgs.makeWrapper ];
  };

  muninConf = pkgs.writeText "munin.conf"
    ''
      dbdir     /var/lib/munin
      htmldir   /var/www/munin
      logdir    /var/log/munin
      rundir    /var/run/munin

      ${cronCfg.extraGlobalConfig}

      ${cronCfg.hosts}
    '';

  nodeConf = pkgs.writeText "munin-node.conf"
    ''
      log_level 3
      log_file Sys::Syslog
      port 4949
      host *
      background 0
      user root
      group root
      host_name ${config.networking.hostName}
      setsid 0

      # wrapped plugins by makeWrapper being with dots
      ignore_file ^\.

      allow ^::1$
      allow ^127\.0\.0\.1$

      ${nodeCfg.extraConfig}
    '';
in

{

  options = {

    services.munin-node = {

{'fraction_non_alphanumeric': 0.14555145551455514, 'fraction_numerical': 0.00922509225092251, 'mean_word_length': 2.2077580539119, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5385165', 'n_tokens_mistral': 1904, 'n_tokens_neox': 1761, 'n_words': 376}