nixpkgs/nixos/modules/services/monitoring/scrutiny.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

221 lines
7.2 KiB
Nix
Raw Normal View History

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;
inherit (lib.modules) mkIf mkMerge;
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 = {
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
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;
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 {
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;
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";
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";
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";
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";
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";
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;
description = "The port of the InfluxDB instance.";
2024-02-19 10:46:23 +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;
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;
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;
description = "InfluxDB bucket in which to store data.";
2024-02-19 10:46:23 +01:00
};
};
};
collector = {
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";
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 {
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;
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;
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}"'';
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";
description = "Log level for Scrutiny collector.";
2024-02-19 10:46:23 +01:00
};
};
};
};
};
};
config = mkMerge [
(mkIf cfg.enable {
services.influxdb2.enable = cfg.influxdb.enable;
2024-02-19 10:46:23 +01:00
networking.firewall = mkIf cfg.openFirewall {
allowedTCPPorts = [ cfg.settings.web.listen.port ];
};
2024-02-19 10:46:23 +01:00
systemd.services.scrutiny = {
description = "Hard Drive S.M.A.R.T Monitoring, Historical Trends & Real World Failure Thresholds";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ] ++ lib.optional cfg.influxdb.enable "influxdb2.service";
wants = lib.optional cfg.influxdb.enable "influxdb2.service";
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
};
};
})
(mkIf cfg.collector.enable {
services.smartd = {
enable = true;
extraOptions = [
"-A /var/log/smartd/"
"--interval=600"
];
};
2024-02-19 10:46:23 +01:00
systemd = {
services.scrutiny-collector = {
2024-02-19 10:46:23 +01:00
description = "Scrutiny Collector Service";
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
};
startAt = cfg.collector.schedule;
2024-02-19 10:46:23 +01:00
};
timers.scrutiny-collector.timerConfig.Persistent = true;
2024-02-19 10:46:23 +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
}