Merge pull request #293413 from eclairevoyant/scrutiny

nixos/scrutiny: cleanup; point collector at web app port by default
This commit is contained in:
Jon Seager 2024-03-08 09:11:29 +00:00 committed by GitHub
commit ea45e63959
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,5 +1,11 @@
{ config, lib, pkgs, ... }:
let
inherit (lib) maintainers;
inherit (lib.meta) getExe;
inherit (lib.modules) mkIf;
inherit (lib.options) literalExpression mkEnableOption mkOption mkPackageOption;
inherit (lib.types) bool enum nullOr port str submodule;
cfg = config.services.scrutiny;
# Define the settings format used for this program
settingsFormat = pkgs.formats.yaml { };
@ -7,20 +13,16 @@ in
{
options = {
services.scrutiny = {
enable = lib.mkEnableOption "Enables the scrutiny web application.";
enable = mkEnableOption "Scrutiny, a web application for drive monitoring";
package = lib.mkPackageOptionMD pkgs "scrutiny" { };
package = mkPackageOption pkgs "scrutiny" { };
openFirewall = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Open the default ports in the firewall for Scrutiny.";
};
openFirewall = mkEnableOption "opening the default ports in the firewall for Scrutiny";
influxdb.enable = lib.mkOption {
type = lib.types.bool;
influxdb.enable = mkOption {
type = bool;
default = true;
description = lib.mdDoc ''
description = ''
Enables InfluxDB on the host system using the `services.influxdb2` NixOS module
with default options.
@ -29,127 +31,124 @@ in
'';
};
settings = lib.mkOption {
description = lib.mdDoc ''
settings = mkOption {
description = ''
Scrutiny settings to be rendered into the configuration file.
See https://github.com/AnalogJ/scrutiny/blob/master/example.scrutiny.yaml.
'';
default = { };
type = lib.types.submodule {
type = submodule {
freeformType = settingsFormat.type;
options.web.listen.port = lib.mkOption {
type = lib.types.port;
options.web.listen.port = mkOption {
type = port;
default = 8080;
description = lib.mdDoc "Port for web application to listen on.";
description = "Port for web application to listen on.";
};
options.web.listen.host = lib.mkOption {
type = lib.types.str;
options.web.listen.host = mkOption {
type = str;
default = "0.0.0.0";
description = lib.mdDoc "Interface address for web application to bind to.";
description = "Interface address for web application to bind to.";
};
options.web.listen.basepath = lib.mkOption {
type = lib.types.str;
options.web.listen.basepath = mkOption {
type = str;
default = "";
example = "/scrutiny";
description = lib.mdDoc ''
description = ''
If Scrutiny will be behind a path prefixed reverse proxy, you can override this
value to serve Scrutiny on a subpath.
'';
};
options.log.level = lib.mkOption {
type = lib.types.enum [ "INFO" "DEBUG" ];
options.log.level = mkOption {
type = enum [ "INFO" "DEBUG" ];
default = "INFO";
description = lib.mdDoc "Log level for Scrutiny.";
description = "Log level for Scrutiny.";
};
options.web.influxdb.scheme = lib.mkOption {
type = lib.types.str;
options.web.influxdb.scheme = mkOption {
type = str;
default = "http";
description = lib.mdDoc "URL scheme to use when connecting to InfluxDB.";
description = "URL scheme to use when connecting to InfluxDB.";
};
options.web.influxdb.host = lib.mkOption {
type = lib.types.str;
options.web.influxdb.host = mkOption {
type = str;
default = "0.0.0.0";
description = lib.mdDoc "IP or hostname of the InfluxDB instance.";
description = "IP or hostname of the InfluxDB instance.";
};
options.web.influxdb.port = lib.mkOption {
type = lib.types.port;
options.web.influxdb.port = mkOption {
type = port;
default = 8086;
description = lib.mdDoc "The port of the InfluxDB instance.";
description = "The port of the InfluxDB instance.";
};
options.web.influxdb.tls.insecure_skip_verify = lib.mkOption {
type = lib.types.bool;
default = false;
description = lib.mdDoc "Skip TLS verification when connecting to InfluxDB.";
};
options.web.influxdb.tls.insecure_skip_verify = mkEnableOption "skipping TLS verification when connecting to InfluxDB";
options.web.influxdb.token = lib.mkOption {
type = lib.types.nullOr lib.types.str;
options.web.influxdb.token = mkOption {
type = nullOr str;
default = null;
description = lib.mdDoc "Authentication token for connecting to InfluxDB.";
description = "Authentication token for connecting to InfluxDB.";
};
options.web.influxdb.org = lib.mkOption {
type = lib.types.nullOr lib.types.str;
options.web.influxdb.org = mkOption {
type = nullOr str;
default = null;
description = lib.mdDoc "InfluxDB organisation under which to store data.";
description = "InfluxDB organisation under which to store data.";
};
options.web.influxdb.bucket = lib.mkOption {
type = lib.types.nullOr lib.types.str;
options.web.influxdb.bucket = mkOption {
type = nullOr str;
default = null;
description = lib.mdDoc "InfluxDB bucket in which to store data.";
description = "InfluxDB bucket in which to store data.";
};
};
};
collector = {
enable = lib.mkEnableOption "Enables the scrutiny metrics collector.";
enable = mkEnableOption "the Scrutiny metrics collector";
package = lib.mkPackageOptionMD pkgs "scrutiny-collector" { };
package = mkPackageOption pkgs "scrutiny-collector" { };
schedule = lib.mkOption {
type = lib.types.str;
schedule = mkOption {
type = str;
default = "*:0/15";
description = lib.mdDoc ''
description = ''
How often to run the collector in systemd calendar format.
'';
};
settings = lib.mkOption {
description = lib.mdDoc ''
settings = mkOption {
description = ''
Collector settings to be rendered into the collector configuration file.
See https://github.com/AnalogJ/scrutiny/blob/master/example.collector.yaml.
'';
default = { };
type = lib.types.submodule {
type = submodule {
freeformType = settingsFormat.type;
options.host.id = lib.mkOption {
type = lib.types.nullOr lib.types.str;
options.host.id = mkOption {
type = nullOr str;
default = null;
description = lib.mdDoc "Host ID for identifying/labelling groups of disks";
description = "Host ID for identifying/labelling groups of disks";
};
options.api.endpoint = lib.mkOption {
type = lib.types.str;
default = "http://localhost:8080";
description = lib.mdDoc "Scrutiny app API endpoint for sending metrics to.";
options.api.endpoint = mkOption {
type = str;
default = "http://localhost:${toString cfg.settings.web.listen.port}";
defaultText = literalExpression ''"http://localhost:''${config.services.scrutiny.settings.web.listen.port}"'';
description = "Scrutiny app API endpoint for sending metrics to.";
};
options.log.level = lib.mkOption {
type = lib.types.enum [ "INFO" "DEBUG" ];
options.log.level = mkOption {
type = enum [ "INFO" "DEBUG" ];
default = "INFO";
description = lib.mdDoc "Log level for Scrutiny collector.";
description = "Log level for Scrutiny collector.";
};
};
};
@ -157,14 +156,14 @@ in
};
};
config = lib.mkIf (cfg.enable || cfg.collector.enable) {
config = mkIf (cfg.enable || cfg.collector.enable) {
services.influxdb2.enable = cfg.influxdb.enable;
networking.firewall = lib.mkIf cfg.openFirewall {
networking.firewall = mkIf cfg.openFirewall {
allowedTCPPorts = [ cfg.settings.web.listen.port ];
};
services.smartd = lib.mkIf cfg.collector.enable {
services.smartd = mkIf cfg.collector.enable {
enable = true;
extraOptions = [
"-A /var/log/smartd/"
@ -174,7 +173,7 @@ in
systemd = {
services = {
scrutiny = lib.mkIf cfg.enable {
scrutiny = mkIf cfg.enable {
description = "Hard Drive S.M.A.R.T Monitoring, Historical Trends & Real World Failure Thresholds";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
@ -185,14 +184,14 @@ in
};
serviceConfig = {
DynamicUser = true;
ExecStart = "${lib.getExe cfg.package} start --config ${settingsFormat.generate "scrutiny.yaml" cfg.settings}";
ExecStart = "${getExe cfg.package} start --config ${settingsFormat.generate "scrutiny.yaml" cfg.settings}";
Restart = "always";
StateDirectory = "scrutiny";
StateDirectoryMode = "0750";
};
};
scrutiny-collector = lib.mkIf cfg.collector.enable {
scrutiny-collector = mkIf cfg.collector.enable {
description = "Scrutiny Collector Service";
environment = {
COLLECTOR_VERSION = "1";
@ -200,12 +199,12 @@ in
};
serviceConfig = {
Type = "oneshot";
ExecStart = "${lib.getExe cfg.collector.package} run --config ${settingsFormat.generate "scrutiny-collector.yaml" cfg.collector.settings}";
ExecStart = "${getExe cfg.collector.package} run --config ${settingsFormat.generate "scrutiny-collector.yaml" cfg.collector.settings}";
};
};
};
timers = lib.mkIf cfg.collector.enable {
timers = mkIf cfg.collector.enable {
scrutiny-collector = {
timerConfig = {
OnCalendar = cfg.collector.schedule;
@ -217,5 +216,5 @@ in
};
};
meta.maintainers = [ lib.maintainers.jnsgruk ];
meta.maintainers = [ maintainers.jnsgruk ];
}