fail2ban: Add metrics
This commit is contained in:
		
							parent
							
								
									cb4527d525
								
							
						
					
					
						commit
						3de03a32ac
					
				
					 10 changed files with 176 additions and 24 deletions
				
			
		|  | @ -16,9 +16,10 @@ | |||
| 
 | ||||
|     ./services/backups.nix | ||||
|     ./services/conduit.nix | ||||
|     ./services/fail2ban.nix | ||||
|     ./services/foundryvtt.nix | ||||
|     ./services/gitea.nix | ||||
|     ./services/metrics.nix | ||||
|     ./services/metrics | ||||
|     ./services/nextcloud.nix | ||||
|     ./services/webserver.nix | ||||
|     ./services/wireguard.nix | ||||
|  | @ -176,27 +177,6 @@ | |||
|     acceptTerms = true; | ||||
|   }; | ||||
| 
 | ||||
|   services.fail2ban = { | ||||
|     enable = true; | ||||
|     extraPackages = [pkgs.ipset]; | ||||
|     banaction = "iptables-ipset-proto6-allports"; | ||||
|     bantime-increment.enable = true; | ||||
| 
 | ||||
|     jails = { | ||||
|       nginx-botsearch = '' | ||||
|         enabled = true | ||||
|         logpath = /var/log/nginx/access.log | ||||
|       ''; | ||||
|     }; | ||||
| 
 | ||||
|     ignoreIP = [ | ||||
|       "127.0.0.0/8" | ||||
|       "10.0.0.0/8" | ||||
|       "172.16.0.0/12" | ||||
|       "192.168.0.0/16" | ||||
|     ]; | ||||
|   }; | ||||
| 
 | ||||
|   # Remove some unneeded packages | ||||
|   environment.defaultPackages = []; | ||||
| 
 | ||||
|  |  | |||
							
								
								
									
										42
									
								
								configuration/services/fail2ban.nix
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								configuration/services/fail2ban.nix
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,42 @@ | |||
| {pkgs, ...}: { | ||||
|   services.fail2ban = { | ||||
|     enable = true; | ||||
|     extraPackages = [pkgs.ipset]; | ||||
|     banaction = "iptables-ipset-proto6-allports"; | ||||
|     bantime-increment.enable = true; | ||||
| 
 | ||||
|     jails = { | ||||
|       nginx-botsearch = '' | ||||
|         enabled = true | ||||
|         logpath = /var/log/nginx/access.log | ||||
|       ''; | ||||
|     }; | ||||
| 
 | ||||
|     ignoreIP = [ | ||||
|       "127.0.0.0/8" | ||||
|       "10.0.0.0/8" | ||||
|       "172.16.0.0/12" | ||||
|       "192.168.0.0/16" | ||||
|     ]; | ||||
|   }; | ||||
| 
 | ||||
|   # Allow metrics services to connect to the socket as well | ||||
|   users.groups.fail2ban = {}; | ||||
|   systemd.services.fail2ban.serviceConfig = { | ||||
|     ExecStartPost = | ||||
|       "+" | ||||
|       + (pkgs.writeShellScript "fail2ban-post-start" '' | ||||
|         while ! [ -S /var/run/fail2ban/fail2ban.sock ]; do | ||||
|             sleep 1 | ||||
|         done | ||||
| 
 | ||||
|         while ! ${pkgs.netcat}/bin/nc -zU /var/run/fail2ban/fail2ban.sock; do | ||||
|             sleep 1 | ||||
|         done | ||||
| 
 | ||||
|         ${pkgs.coreutils}/bin/chown root:fail2ban /var/run/fail2ban /var/run/fail2ban/fail2ban.sock | ||||
|         ${pkgs.coreutils}/bin/chmod 660 /var/run/fail2ban/fail2ban.sock | ||||
|         ${pkgs.coreutils}/bin/chmod 710 /var/run/fail2ban | ||||
|       ''); | ||||
|   }; | ||||
| } | ||||
|  | @ -7,6 +7,10 @@ | |||
|   domain = "metrics.${config.services.nginx.domain}"; | ||||
|   yaml = pkgs.formats.yaml {}; | ||||
| in { | ||||
|   imports = [ | ||||
|     ./exporters.nix | ||||
|   ]; | ||||
| 
 | ||||
|   services.victoriametrics.enable = true; | ||||
| 
 | ||||
|   services.grafana = { | ||||
|  | @ -83,6 +87,28 @@ in { | |||
|     }; | ||||
|   }; | ||||
| 
 | ||||
|   services.prometheus.local-exporters = { | ||||
|     prometheus-fail2ban-exporter = rec { | ||||
|       enable = true; | ||||
|       after = ["fail2ban.service"]; | ||||
| 
 | ||||
|       port = 9191; | ||||
|       listenAddress = "127.0.0.1"; | ||||
| 
 | ||||
|       serviceConfig = { | ||||
|         Group = "fail2ban"; | ||||
| 
 | ||||
|         RestrictAddressFamilies = ["AF_UNIX" "AF_INET" "AF_INET6"]; | ||||
| 
 | ||||
|         ExecStart = lib.concatStringsSep " " [ | ||||
|           "${pkgs.local.prometheus-fail2ban-exporter}/bin/fail2ban-prometheus-exporter" | ||||
|           "--collector.f2b.socket=/var/run/fail2ban/fail2ban.sock" | ||||
|           "--web.listen-address='${listenAddress}:${toString port}'" | ||||
|         ]; | ||||
|       }; | ||||
|     }; | ||||
|   }; | ||||
| 
 | ||||
|   systemd.services.export-to-victoriametrics = let | ||||
|     promscrape = yaml.generate "prometheus.yml" { | ||||
|       scrape_configs = [ | ||||
|  | @ -93,7 +119,7 @@ in { | |||
|               targets = | ||||
|                 lib.mapAttrsToList (name: exporter: "${exporter.listenAddress}:${toString exporter.port}") | ||||
|                 (lib.filterAttrs (name: exporter: (builtins.isAttrs exporter) && exporter.enable) | ||||
|                   config.services.prometheus.exporters); | ||||
|                   (config.services.prometheus.exporters // config.services.prometheus.local-exporters)); | ||||
|             } | ||||
|           ]; | ||||
|         } | ||||
							
								
								
									
										45
									
								
								configuration/services/metrics/exporters.nix
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								configuration/services/metrics/exporters.nix
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,45 @@ | |||
| { | ||||
|   config, | ||||
|   lib, | ||||
|   ... | ||||
| }: { | ||||
|   options.services.prometheus.local-exporters = lib.mkOption { | ||||
|     type = lib.types.anything; | ||||
|   }; | ||||
| 
 | ||||
|   config.systemd.services = lib.mapAttrs (_: exporter: | ||||
|     lib.mkMerge [ | ||||
|       { | ||||
|         wantedBy = ["multi-user.target"]; | ||||
|         after = ["network.target"]; | ||||
| 
 | ||||
|         serviceConfig = { | ||||
|           Restart = "always"; | ||||
|           PrivateTmp = true; | ||||
|           WorkingDirectory = "/tmp"; | ||||
|           DynamicUser = true; | ||||
|           LockPersonality = true; | ||||
|           MemoryDenyWriteExecute = true; | ||||
|           NonNewPrivileges = true; | ||||
|           PrivateDevices = true; | ||||
|           ProtectClock = true; | ||||
|           ProtectControlGroups = true; | ||||
|           ProtectHome = true; | ||||
|           ProtectHostname = true; | ||||
|           ProtectKernelLogs = true; | ||||
|           ProtectKernelModules = true; | ||||
|           ProtectKernelTunables = true; | ||||
|           ProtectSystem = "strict"; | ||||
|           RemoveIPC = true; | ||||
|           RestrictAddressFamilies = lib.mkDefault ["AF_INET" "AF_INET6"]; | ||||
|           RestrictNamespaces = true; | ||||
|           RestrictRealtime = true; | ||||
|           RestrictSUIDSGID = true; | ||||
|           SystemCallArchitectures = "native"; | ||||
|           UMask = "0077"; | ||||
|         }; | ||||
|       } | ||||
|       (removeAttrs exporter ["port" "listenAddress"]) | ||||
|     ]) | ||||
|   config.services.prometheus.local-exporters; | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue