2024-02-19 10:46:23 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
2024-03-05 06:07:08 +01:00
|
|
|
inherit (lib) maintainers;
|
|
|
|
inherit (lib.meta) getExe;
|
2024-03-16 17:37:27 +01:00
|
|
|
inherit (lib.modules) mkIf mkMerge;
|
2024-03-05 06:23:04 +01:00
|
|
|
inherit (lib.options) literalExpression mkEnableOption mkOption mkPackageOption;
|
2024-03-05 06:07:08 +01:00
|
|
|
inherit (lib.types) bool enum nullOr port str submodule;
|
|
|
|
|
2024-02-19 10:46:23 +01:00
|
|
|
cfg = config.services.scrutiny;
|
|
|
|
# Define the settings format used for this program
|
|
|
|
settingsFormat = pkgs.formats.yaml { };
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.scrutiny = {
|
2024-03-05 06:09:45 +01:00
|
|
|
enable = mkEnableOption "Scrutiny, a web application for drive monitoring";
|
2024-02-19 10:46:23 +01:00
|
|
|
|
2024-03-05 06:07:08 +01:00
|
|
|
package = mkPackageOption pkgs "scrutiny" { };
|
2024-02-19 10:46:23 +01:00
|
|
|
|
2024-03-05 06:09:45 +01:00
|
|
|
openFirewall = mkEnableOption "opening the default ports in the firewall for Scrutiny";
|
2024-02-19 10:46:23 +01:00
|
|
|
|
2024-03-05 06:07:08 +01:00
|
|
|
influxdb.enable = mkOption {
|
|
|
|
type = bool;
|
2024-02-19 10:46:23 +01:00
|
|
|
default = true;
|
2024-03-05 06:03:03 +01:00
|
|
|
description = ''
|
2024-02-19 10:46:23 +01:00
|
|
|
Enables InfluxDB on the host system using the `services.influxdb2` NixOS module
|
|
|
|
with default options.
|
|
|
|
|
|
|
|
If you already have InfluxDB configured, or wish to connect to an external InfluxDB
|
|
|
|
instance, disable this option.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-03-05 06:07:08 +01:00
|
|
|
settings = mkOption {
|
2024-03-05 06:03:03 +01:00
|
|
|
description = ''
|
2024-02-19 10:46:23 +01:00
|
|
|
Scrutiny settings to be rendered into the configuration file.
|
|
|
|
|
|
|
|
See https://github.com/AnalogJ/scrutiny/blob/master/example.scrutiny.yaml.
|
|
|
|
'';
|
|
|
|
default = { };
|
2024-03-05 06:07:08 +01:00
|
|
|
type = submodule {
|
2024-02-19 10:46:23 +01:00
|
|
|
freeformType = settingsFormat.type;
|
|
|
|
|
2024-03-05 06:07:08 +01:00
|
|
|
options.web.listen.port = mkOption {
|
|
|
|
type = port;
|
2024-02-19 10:46:23 +01:00
|
|
|
default = 8080;
|
2024-03-05 06:03:03 +01:00
|
|
|
description = "Port for web application to listen on.";
|
2024-02-19 10:46:23 +01:00
|
|
|
};
|
|
|
|
|
2024-03-05 06:07:08 +01:00
|
|
|
options.web.listen.host = mkOption {
|
|
|
|
type = str;
|
2024-02-19 10:46:23 +01:00
|
|
|
default = "0.0.0.0";
|
2024-03-05 06:03:03 +01:00
|
|
|
description = "Interface address for web application to bind to.";
|
2024-02-19 10:46:23 +01:00
|
|
|
};
|
|
|
|
|
2024-03-05 06:07:08 +01:00
|
|
|
options.web.listen.basepath = mkOption {
|
|
|
|
type = str;
|
2024-02-19 10:46:23 +01:00
|
|
|
default = "";
|
|
|
|
example = "/scrutiny";
|
2024-03-05 06:03:03 +01:00
|
|
|
description = ''
|
2024-02-19 10:46:23 +01:00
|
|
|
If Scrutiny will be behind a path prefixed reverse proxy, you can override this
|
|
|
|
value to serve Scrutiny on a subpath.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-03-05 06:07:08 +01:00
|
|
|
options.log.level = mkOption {
|
|
|
|
type = enum [ "INFO" "DEBUG" ];
|
2024-02-19 10:46:23 +01:00
|
|
|
default = "INFO";
|
2024-03-05 06:03:03 +01:00
|
|
|
description = "Log level for Scrutiny.";
|
2024-02-19 10:46:23 +01:00
|
|
|
};
|
|
|
|
|
2024-03-05 06:07:08 +01:00
|
|
|
options.web.influxdb.scheme = mkOption {
|
|
|
|
type = str;
|
2024-02-19 10:46:23 +01:00
|
|
|
default = "http";
|
2024-03-05 06:03:03 +01:00
|
|
|
description = "URL scheme to use when connecting to InfluxDB.";
|
2024-02-19 10:46:23 +01:00
|
|
|
};
|
|
|
|
|
2024-03-05 06:07:08 +01:00
|
|
|
options.web.influxdb.host = mkOption {
|
|
|
|
type = str;
|
2024-02-19 10:46:23 +01:00
|
|
|
default = "0.0.0.0";
|
2024-03-05 06:03:03 +01:00
|
|
|
description = "IP or hostname of the InfluxDB instance.";
|
2024-02-19 10:46:23 +01:00
|
|
|
};
|
|
|
|
|
2024-03-05 06:07:08 +01:00
|
|
|
options.web.influxdb.port = mkOption {
|
|
|
|
type = port;
|
2024-02-19 10:46:23 +01:00
|
|
|
default = 8086;
|
2024-03-05 06:03:03 +01:00
|
|
|
description = "The port of the InfluxDB instance.";
|
2024-02-19 10:46:23 +01:00
|
|
|
};
|
|
|
|
|
2024-03-05 06:09:45 +01:00
|
|
|
options.web.influxdb.tls.insecure_skip_verify = mkEnableOption "skipping TLS verification when connecting to InfluxDB";
|
2024-02-19 10:46:23 +01:00
|
|
|
|
2024-03-05 06:07:08 +01:00
|
|
|
options.web.influxdb.token = mkOption {
|
|
|
|
type = nullOr str;
|
2024-02-19 10:46:23 +01:00
|
|
|
default = null;
|
2024-03-05 06:03:03 +01:00
|
|
|
description = "Authentication token for connecting to InfluxDB.";
|
2024-02-19 10:46:23 +01:00
|
|
|
};
|
|
|
|
|
2024-03-05 06:07:08 +01:00
|
|
|
options.web.influxdb.org = mkOption {
|
|
|
|
type = nullOr str;
|
2024-02-19 10:46:23 +01:00
|
|
|
default = null;
|
2024-03-05 06:03:03 +01:00
|
|
|
description = "InfluxDB organisation under which to store data.";
|
2024-02-19 10:46:23 +01:00
|
|
|
};
|
|
|
|
|
2024-03-05 06:07:08 +01:00
|
|
|
options.web.influxdb.bucket = mkOption {
|
|
|
|
type = nullOr str;
|
2024-02-19 10:46:23 +01:00
|
|
|
default = null;
|
2024-03-05 06:03:03 +01:00
|
|
|
description = "InfluxDB bucket in which to store data.";
|
2024-02-19 10:46:23 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
collector = {
|
2024-07-22 08:38:45 +02:00
|
|
|
enable = mkEnableOption "the Scrutiny metrics collector" // {
|
|
|
|
default = cfg.enable;
|
|
|
|
defaultText = lib.literalExpression "config.services.scrutiny.enable";
|
|
|
|
};
|
2024-02-19 10:46:23 +01:00
|
|
|
|
2024-03-05 06:07:08 +01:00
|
|
|
package = mkPackageOption pkgs "scrutiny-collector" { };
|
2024-02-19 10:46:23 +01:00
|
|
|
|
2024-03-05 06:07:08 +01:00
|
|
|
schedule = mkOption {
|
|
|
|
type = str;
|
2024-02-19 10:46:23 +01:00
|
|
|
default = "*:0/15";
|
2024-03-05 06:03:03 +01:00
|
|
|
description = ''
|
2024-02-19 10:46:23 +01:00
|
|
|
How often to run the collector in systemd calendar format.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-03-05 06:07:08 +01:00
|
|
|
settings = mkOption {
|
2024-03-05 06:03:03 +01:00
|
|
|
description = ''
|
2024-02-19 10:46:23 +01:00
|
|
|
Collector settings to be rendered into the collector configuration file.
|
|
|
|
|
|
|
|
See https://github.com/AnalogJ/scrutiny/blob/master/example.collector.yaml.
|
|
|
|
'';
|
|
|
|
default = { };
|
2024-03-05 06:07:08 +01:00
|
|
|
type = submodule {
|
2024-02-19 10:46:23 +01:00
|
|
|
freeformType = settingsFormat.type;
|
|
|
|
|
2024-03-05 06:07:08 +01:00
|
|
|
options.host.id = mkOption {
|
|
|
|
type = nullOr str;
|
2024-02-19 10:46:23 +01:00
|
|
|
default = null;
|
2024-03-05 06:03:03 +01:00
|
|
|
description = "Host ID for identifying/labelling groups of disks";
|
2024-02-19 10:46:23 +01:00
|
|
|
};
|
|
|
|
|
2024-03-05 06:07:08 +01:00
|
|
|
options.api.endpoint = mkOption {
|
|
|
|
type = str;
|
2024-06-10 19:52:03 +02:00
|
|
|
default = "http://${cfg.settings.web.listen.host}:${toString cfg.settings.web.listen.port}";
|
|
|
|
defaultText = literalExpression ''"http://''${config.services.scrutiny.settings.web.listen.host}:''${config.services.scrutiny.settings.web.listen.port}"'';
|
2024-03-05 06:03:03 +01:00
|
|
|
description = "Scrutiny app API endpoint for sending metrics to.";
|
2024-02-19 10:46:23 +01:00
|
|
|
};
|
|
|
|
|
2024-03-05 06:07:08 +01:00
|
|
|
options.log.level = mkOption {
|
|
|
|
type = enum [ "INFO" "DEBUG" ];
|
2024-02-19 10:46:23 +01:00
|
|
|
default = "INFO";
|
2024-03-05 06:03:03 +01:00
|
|
|
description = "Log level for Scrutiny collector.";
|
2024-02-19 10:46:23 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-03-16 17:37:27 +01:00
|
|
|
config = mkMerge [
|
|
|
|
(mkIf cfg.enable {
|
|
|
|
services.influxdb2.enable = cfg.influxdb.enable;
|
2024-02-19 10:46:23 +01:00
|
|
|
|
2024-03-16 17:37:27 +01:00
|
|
|
networking.firewall = mkIf cfg.openFirewall {
|
|
|
|
allowedTCPPorts = [ cfg.settings.web.listen.port ];
|
|
|
|
};
|
2024-02-19 10:46:23 +01:00
|
|
|
|
2024-03-16 17:37:27 +01:00
|
|
|
systemd.services.scrutiny = {
|
|
|
|
description = "Hard Drive S.M.A.R.T Monitoring, Historical Trends & Real World Failure Thresholds";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2024-03-16 17:46:03 +01:00
|
|
|
after = [ "network.target" ] ++ lib.optional cfg.influxdb.enable "influxdb2.service";
|
|
|
|
wants = lib.optional cfg.influxdb.enable "influxdb2.service";
|
2024-03-16 17:37:27 +01:00
|
|
|
environment = {
|
|
|
|
SCRUTINY_VERSION = "1";
|
|
|
|
SCRUTINY_WEB_DATABASE_LOCATION = "/var/lib/scrutiny/scrutiny.db";
|
|
|
|
SCRUTINY_WEB_SRC_FRONTEND_PATH = "${cfg.package}/share/scrutiny";
|
|
|
|
};
|
|
|
|
serviceConfig = {
|
|
|
|
DynamicUser = true;
|
|
|
|
ExecStart = "${getExe cfg.package} start --config ${settingsFormat.generate "scrutiny.yaml" cfg.settings}";
|
|
|
|
Restart = "always";
|
|
|
|
StateDirectory = "scrutiny";
|
|
|
|
StateDirectoryMode = "0750";
|
2024-02-19 10:46:23 +01:00
|
|
|
};
|
2024-03-16 17:37:27 +01:00
|
|
|
};
|
|
|
|
})
|
|
|
|
(mkIf cfg.collector.enable {
|
|
|
|
services.smartd = {
|
|
|
|
enable = true;
|
|
|
|
extraOptions = [
|
|
|
|
"-A /var/log/smartd/"
|
|
|
|
"--interval=600"
|
|
|
|
];
|
|
|
|
};
|
2024-02-19 10:46:23 +01:00
|
|
|
|
2024-03-16 17:37:27 +01:00
|
|
|
systemd = {
|
|
|
|
services.scrutiny-collector = {
|
2024-02-19 10:46:23 +01:00
|
|
|
description = "Scrutiny Collector Service";
|
2024-03-18 16:30:51 +01:00
|
|
|
after = lib.optional cfg.enable "scrutiny.service";
|
|
|
|
wants = lib.optional cfg.enable "scrutiny.service";
|
2024-02-19 10:46:23 +01:00
|
|
|
environment = {
|
|
|
|
COLLECTOR_VERSION = "1";
|
|
|
|
COLLECTOR_API_ENDPOINT = cfg.collector.settings.api.endpoint;
|
|
|
|
};
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
2024-03-05 06:07:08 +01:00
|
|
|
ExecStart = "${getExe cfg.collector.package} run --config ${settingsFormat.generate "scrutiny-collector.yaml" cfg.collector.settings}";
|
2024-02-19 10:46:23 +01:00
|
|
|
};
|
2024-03-13 03:12:04 +01:00
|
|
|
startAt = cfg.collector.schedule;
|
2024-02-19 10:46:23 +01:00
|
|
|
};
|
|
|
|
|
2024-03-16 17:37:27 +01:00
|
|
|
timers.scrutiny-collector.timerConfig.Persistent = true;
|
2024-02-19 10:46:23 +01:00
|
|
|
};
|
2024-03-16 17:37:27 +01:00
|
|
|
})
|
|
|
|
];
|
2024-02-19 10:46:23 +01:00
|
|
|
|
2024-03-05 06:07:08 +01:00
|
|
|
meta.maintainers = [ maintainers.jnsgruk ];
|
2024-02-19 10:46:23 +01:00
|
|
|
}
|