This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

superconfig / 2configs / monitoring / telegraf.nix
5.7 kB 198 lines
1{ 2 pkgs, 3 lib, 4 config, 5 ... 6}: 7# To use this module you also need to allow port 9273 either on the internet or on a vpn interface 8# i.e. networking.firewall.interfaces."vpn0".allowedTCPPorts = [ 9273 ]; 9# Example prometheus alert rules: 10# - https://github.com/Mic92/dotfiles/blob/master/nixos/eva/modules/prometheus/alert-rules.nix 11let 12 isVM = lib.any ( 13 mod: mod == "xen-blkfront" || mod == "virtio_console" 14 ) config.boot.initrd.kernelModules; 15 # potentially wrong if the nvme is not used at boot... 16 hasNvme = lib.any (m: m == "nvme") config.boot.initrd.availableKernelModules; 17 18 ipv6DadCheck = pkgs.writeShellScript "ipv6-dad-check" '' 19 ${pkgs.iproute2}/bin/ip --json addr | \ 20 ${pkgs.jq}/bin/jq -r 'map(.addr_info) | flatten(1) | map(select(.dadfailed == true)) | map(.local) | @text "ipv6_dad_failures count=\(length)i"' 21 ''; 22 23 zfsChecks = 24 lib.optional (lib.any (fs: fs == "zfs") (lib.attrNames config.boot.supportedFilesystems)) 25 ( 26 pkgs.writeScript "zpool-health" '' 27 #!${pkgs.gawk}/bin/awk -f 28 BEGIN { 29 while ("${pkgs.zfs}/bin/zpool status" | getline) { 30 if ($1 ~ /pool:/) { printf "zpool_status,name=%s ", $2 } 31 if ($1 ~ /state:/) { printf " state=\"%s\",", $2 } 32 if ($1 ~ /errors:/) { 33 if (index($2, "No")) printf "errors=0i\n"; else printf "errors=%di\n", $2 34 } 35 } 36 } 37 '' 38 ); 39 40 nfsChecks = 41 let 42 collectHosts = 43 shares: fs: 44 if 45 builtins.elem fs.fsType [ 46 "nfs" 47 "nfs3" 48 "nfs4" 49 ] 50 then 51 shares 52 // ( 53 let 54 # also match ipv6 addresses 55 group = builtins.match "\\[?([^\]]+)]?:([^:]+)$" fs.device; 56 host = builtins.head group; 57 path = builtins.elemAt group 1; 58 in 59 { 60 ${host} = (shares.${host} or [ ]) ++ [ path ]; 61 } 62 ) 63 else 64 shares; 65 nfsHosts = lib.foldl collectHosts { } (builtins.attrValues config.fileSystems); 66 in 67 lib.mapAttrsToList ( 68 host: args: 69 (pkgs.writeScript "nfs-health" '' 70 #!${pkgs.gawk}/bin/awk -f 71 BEGIN { 72 for (i = 2; i < ARGC; i++) { 73 mounts[ARGV[i]] = 1 74 } 75 while ("${pkgs.nfs-utils}/bin/showmount -e " ARGV[1] | getline) { 76 if (NR == 1) { continue } 77 if (mounts[$1] == 1) { 78 printf "nfs_export,host=%s,path=%s present=1\n", ARGV[1], $1 79 } 80 delete mounts[$1] 81 } 82 for (mount in mounts) { 83 printf "nfs_export,host=%s,path=%s present=0\n", ARGV[1], $1 84 } 85 } 86 '') 87 + " ${host} ${builtins.concatStringsSep " " args}" 88 ) nfsHosts; 89 90in 91{ 92 93 systemd.services.telegraf.path = lib.optional (!isVM && hasNvme) pkgs.nvme-cli; 94 95 services.telegraf = { 96 enable = true; 97 extraConfig = { 98 agent.interval = "60s"; 99 inputs = { 100 prometheus.urls = lib.mkIf config.services.promtail.enable [ 101 # default promtail port 102 "http://localhost:9080/metrics" 103 ]; 104 prometheus.metric_version = 2; 105 kernel_vmstat = { }; 106 nginx.urls = lib.mkIf config.services.nginx.statusPage [ 107 "http://localhost/nginx_status" 108 ]; 109 smart = lib.mkIf (!isVM) { 110 path_smartctl = pkgs.writeShellScript "smartctl" '' 111 exec /run/wrappers/bin/sudo ${pkgs.smartmontools}/bin/smartctl "$@" 112 ''; 113 }; 114 system = { }; 115 mem = { }; 116 file = [ 117 { 118 data_format = "influx"; 119 file_tag = "name"; 120 files = [ "/var/log/telegraf/*" ]; 121 } 122 ] 123 ++ lib.optional (lib.any (fs: fs == "ext4") (lib.attrNames config.boot.supportedFilesystems)) { 124 name_override = "ext4_errors"; 125 files = [ "/sys/fs/ext4/*/errors_count" ]; 126 data_format = "value"; 127 }; 128 exec = [ 129 { 130 ## Commands array 131 commands = [ ipv6DadCheck ] ++ zfsChecks ++ nfsChecks; 132 data_format = "influx"; 133 } 134 ]; 135 systemd_units = { }; 136 swap = { }; 137 disk.tagdrop = { 138 fstype = [ 139 "tmpfs" 140 "ramfs" 141 "devtmpfs" 142 "devfs" 143 "iso9660" 144 "overlay" 145 "aufs" 146 "squashfs" 147 ]; 148 device = [ 149 "rpc_pipefs" 150 "lxcfs" 151 "nsfs" 152 "borgfs" 153 ]; 154 }; 155 diskio = { }; 156 zfs = { 157 poolMetrics = true; 158 }; 159 } 160 // 161 lib.optionalAttrs 162 ( 163 if lib.versionAtLeast (lib.versions.majorMinor lib.version) "23.11" then 164 config.boot.swraid.enable 165 else 166 config.boot.initrd.services.swraid.enable 167 ) 168 { 169 mdstat = { }; 170 }; 171 outputs.prometheus_client = { 172 listen = ":9273"; 173 metric_version = 2; 174 }; 175 }; 176 }; 177 security.sudo.extraRules = lib.mkIf (!isVM) [ 178 { 179 users = [ "telegraf" ]; 180 commands = [ 181 { 182 command = "${pkgs.smartmontools}/bin/smartctl"; 183 options = [ "NOPASSWD" ]; 184 } 185 ]; 186 } 187 ]; 188 networking.firewall.allowedTCPPorts = [ 9273 ]; 189 190 # avoid logging sudo use 191 security.sudo.configFile = '' 192 Defaults:telegraf !syslog,!pam_session 193 ''; 194 # create dummy file to avoid telegraf errors 195 systemd.tmpfiles.rules = [ 196 "f /var/log/telegraf/dummy 0444 root root - -" 197 ]; 198}