This repository has no description
5.5 kB
194 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.metric_version = 2;
101 kernel_vmstat = { };
102 nginx.urls = lib.mkIf config.services.nginx.statusPage [
103 "http://localhost/nginx_status"
104 ];
105 smart = lib.mkIf (!isVM) {
106 path_smartctl = pkgs.writeShellScript "smartctl" ''
107 exec /run/wrappers/bin/sudo ${pkgs.smartmontools}/bin/smartctl "$@"
108 '';
109 };
110 system = { };
111 mem = { };
112 file = [
113 {
114 data_format = "influx";
115 file_tag = "name";
116 files = [ "/var/log/telegraf/*" ];
117 }
118 ]
119 ++ lib.optional (lib.any (fs: fs == "ext4") (lib.attrNames config.boot.supportedFilesystems)) {
120 name_override = "ext4_errors";
121 files = [ "/sys/fs/ext4/*/errors_count" ];
122 data_format = "value";
123 };
124 exec = [
125 {
126 ## Commands array
127 commands = [ ipv6DadCheck ] ++ zfsChecks ++ nfsChecks;
128 data_format = "influx";
129 }
130 ];
131 systemd_units = { };
132 swap = { };
133 disk.tagdrop = {
134 fstype = [
135 "tmpfs"
136 "ramfs"
137 "devtmpfs"
138 "devfs"
139 "iso9660"
140 "overlay"
141 "aufs"
142 "squashfs"
143 ];
144 device = [
145 "rpc_pipefs"
146 "lxcfs"
147 "nsfs"
148 "borgfs"
149 ];
150 };
151 diskio = { };
152 zfs = {
153 poolMetrics = true;
154 };
155 }
156 //
157 lib.optionalAttrs
158 (
159 if lib.versionAtLeast (lib.versions.majorMinor lib.version) "23.11" then
160 config.boot.swraid.enable
161 else
162 config.boot.initrd.services.swraid.enable
163 )
164 {
165 mdstat = { };
166 };
167 outputs.prometheus_client = {
168 listen = ":9273";
169 metric_version = 2;
170 };
171 };
172 };
173 security.sudo.extraRules = lib.mkIf (!isVM) [
174 {
175 users = [ "telegraf" ];
176 commands = [
177 {
178 command = "${pkgs.smartmontools}/bin/smartctl";
179 options = [ "NOPASSWD" ];
180 }
181 ];
182 }
183 ];
184 networking.firewall.allowedTCPPorts = [ 9273 ];
185
186 # avoid logging sudo use
187 security.sudo.configFile = ''
188 Defaults:telegraf !syslog,!pam_session
189 '';
190 # create dummy file to avoid telegraf errors
191 systemd.tmpfiles.rules = [
192 "f /var/log/telegraf/dummy 0444 root root - -"
193 ];
194}