2013-05-23 12:04:07 +01:00
|
|
|
# Global configuration for atop.
|
|
|
|
|
2021-05-14 01:11:05 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2013-05-23 12:04:07 +01:00
|
|
|
|
|
|
|
let cfg = config.programs.atop;
|
|
|
|
|
|
|
|
in
|
|
|
|
{
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
2021-05-14 01:11:05 +01:00
|
|
|
programs.atop = rec {
|
2013-05-23 12:04:07 +01:00
|
|
|
|
2024-04-17 12:37:58 +01:00
|
|
|
enable = lib.mkEnableOption "Atop, a tool for monitoring system resources";
|
2021-05-14 01:11:05 +01:00
|
|
|
|
2024-04-17 12:37:58 +01:00
|
|
|
package = lib.mkPackageOption pkgs "atop" { };
|
2021-05-14 01:11:05 +01:00
|
|
|
|
|
|
|
netatop = {
|
2024-04-17 12:37:58 +01:00
|
|
|
enable = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2021-05-14 01:11:05 +01:00
|
|
|
default = false;
|
2022-07-20 11:32:04 +01:00
|
|
|
description = ''
|
2021-05-14 01:11:05 +01:00
|
|
|
Whether to install and enable the netatop kernel module.
|
2021-05-16 20:43:20 +01:00
|
|
|
Note: this sets the kernel taint flag "O" for loading out-of-tree modules.
|
2021-05-14 01:11:05 +01:00
|
|
|
'';
|
|
|
|
};
|
2024-04-17 12:37:58 +01:00
|
|
|
package = lib.mkOption {
|
|
|
|
type = lib.types.package;
|
2021-05-14 01:11:05 +01:00
|
|
|
default = config.boot.kernelPackages.netatop;
|
2024-04-17 12:37:58 +01:00
|
|
|
defaultText = lib.literalExpression "config.boot.kernelPackages.netatop";
|
2022-07-20 11:32:04 +01:00
|
|
|
description = ''
|
2021-05-14 01:11:05 +01:00
|
|
|
Which package to use for netatop.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-04-17 12:37:58 +01:00
|
|
|
atopgpu.enable = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2021-05-14 01:11:05 +01:00
|
|
|
default = false;
|
2022-07-20 11:32:04 +01:00
|
|
|
description = ''
|
2021-05-14 01:11:05 +01:00
|
|
|
Whether to install and enable the atopgpud daemon to get information about
|
|
|
|
NVIDIA gpus.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-04-17 12:37:58 +01:00
|
|
|
setuidWrapper.enable = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2021-05-16 17:19:27 +01:00
|
|
|
default = false;
|
2022-07-20 11:32:04 +01:00
|
|
|
description = ''
|
2021-05-14 01:11:05 +01:00
|
|
|
Whether to install a setuid wrapper for Atop. This is required to use some of
|
|
|
|
the features as non-root user (e.g.: ipc information, netatop, atopgpu).
|
|
|
|
Atop tries to drop the root privileges shortly after starting.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-04-17 12:37:58 +01:00
|
|
|
atopService.enable = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2021-05-14 01:11:05 +01:00
|
|
|
default = true;
|
2022-07-20 11:32:04 +01:00
|
|
|
description = ''
|
2021-05-14 01:11:05 +01:00
|
|
|
Whether to enable the atop service responsible for storing statistics for
|
|
|
|
long-term analysis.
|
|
|
|
'';
|
|
|
|
};
|
2024-04-17 12:37:58 +01:00
|
|
|
atopRotateTimer.enable = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2021-05-14 01:11:05 +01:00
|
|
|
default = true;
|
2022-07-20 11:32:04 +01:00
|
|
|
description = ''
|
2021-05-14 01:11:05 +01:00
|
|
|
Whether to enable the atop-rotate timer, which restarts the atop service
|
|
|
|
daily to make sure the data files are rotate.
|
|
|
|
'';
|
|
|
|
};
|
2024-04-17 12:37:58 +01:00
|
|
|
atopacctService.enable = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2021-05-14 01:11:05 +01:00
|
|
|
default = true;
|
2022-07-20 11:32:04 +01:00
|
|
|
description = ''
|
2021-05-14 01:11:05 +01:00
|
|
|
Whether to enable the atopacct service which manages process accounting.
|
|
|
|
This allows Atop to gather data about processes that disappeared in between
|
|
|
|
two refresh intervals.
|
|
|
|
'';
|
|
|
|
};
|
2024-04-17 12:37:58 +01:00
|
|
|
settings = lib.mkOption {
|
|
|
|
type = lib.types.attrs;
|
2021-05-14 01:11:05 +01:00
|
|
|
default = { };
|
2013-05-23 12:04:07 +01:00
|
|
|
example = {
|
|
|
|
flags = "a1f";
|
|
|
|
interval = 5;
|
|
|
|
};
|
2022-07-20 11:32:04 +01:00
|
|
|
description = ''
|
|
|
|
Parameters to be written to {file}`/etc/atoprc`.
|
2013-05-23 12:04:07 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-04-17 12:37:58 +01:00
|
|
|
config = lib.mkIf cfg.enable (
|
2021-05-14 01:11:05 +01:00
|
|
|
let
|
|
|
|
atop =
|
|
|
|
if cfg.atopgpu.enable then
|
|
|
|
(cfg.package.override { withAtopgpu = true; })
|
|
|
|
else
|
|
|
|
cfg.package;
|
|
|
|
in
|
|
|
|
{
|
2024-04-17 12:37:58 +01:00
|
|
|
environment.etc = lib.mkIf (cfg.settings != { }) {
|
|
|
|
atoprc.text = lib.concatStrings
|
|
|
|
(lib.mapAttrsToList
|
2021-05-14 01:11:05 +01:00
|
|
|
(n: v: ''
|
2024-04-17 12:37:58 +01:00
|
|
|
${n} ${builtins.toString v}
|
2021-05-14 01:11:05 +01:00
|
|
|
'')
|
|
|
|
cfg.settings);
|
|
|
|
};
|
2021-05-16 21:00:24 +01:00
|
|
|
environment.systemPackages = [ atop (lib.mkIf cfg.netatop.enable cfg.netatop.package) ];
|
2021-05-14 01:11:05 +01:00
|
|
|
boot.extraModulePackages = [ (lib.mkIf cfg.netatop.enable cfg.netatop.package) ];
|
|
|
|
systemd =
|
|
|
|
let
|
2023-06-30 15:11:23 +01:00
|
|
|
mkSystemd = type: name: restartTriggers: {
|
|
|
|
${name} = {
|
2021-05-16 21:00:24 +01:00
|
|
|
inherit restartTriggers;
|
2021-05-14 01:11:05 +01:00
|
|
|
wantedBy = [ (if type == "services" then "multi-user.target" else if type == "timers" then "timers.target" else null) ];
|
|
|
|
};
|
|
|
|
};
|
2024-05-16 12:53:25 +01:00
|
|
|
mkService = mkSystemd "services";
|
|
|
|
mkTimer = mkSystemd "timers";
|
2021-05-14 01:11:05 +01:00
|
|
|
in
|
|
|
|
{
|
2021-05-16 21:00:24 +01:00
|
|
|
packages = [ atop (lib.mkIf cfg.netatop.enable cfg.netatop.package) ];
|
2023-06-30 15:11:23 +01:00
|
|
|
services = lib.mkMerge [
|
|
|
|
(lib.mkIf cfg.atopService.enable (lib.recursiveUpdate
|
|
|
|
(mkService "atop" [ atop ])
|
|
|
|
{
|
|
|
|
# always convert logs to newer version first
|
|
|
|
# XXX might trigger TimeoutStart but restarting atop.service will
|
|
|
|
# convert remainings logs and start eventually
|
|
|
|
atop.preStart = ''
|
|
|
|
set -e -u
|
|
|
|
shopt -s nullglob
|
2023-12-23 14:02:52 +00:00
|
|
|
rm -f "$LOGPATH"/atop_*.new
|
2023-06-30 15:11:23 +01:00
|
|
|
for logfile in "$LOGPATH"/atop_*
|
|
|
|
do
|
|
|
|
${atop}/bin/atopconvert "$logfile" "$logfile".new
|
|
|
|
# only replace old file if version was upgraded to avoid
|
|
|
|
# false positives for atop-rotate.service
|
|
|
|
if ! ${pkgs.diffutils}/bin/cmp -s "$logfile" "$logfile".new
|
|
|
|
then
|
2023-12-23 14:02:52 +00:00
|
|
|
mv -v -f "$logfile".new "$logfile"
|
2023-06-30 15:11:23 +01:00
|
|
|
else
|
2023-12-23 14:02:52 +00:00
|
|
|
rm -f "$logfile".new
|
2023-06-30 15:11:23 +01:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
'';
|
|
|
|
}))
|
|
|
|
(lib.mkIf cfg.atopacctService.enable (mkService "atopacct" [ atop ]))
|
|
|
|
(lib.mkIf cfg.netatop.enable (mkService "netatop" [ cfg.netatop.package ]))
|
|
|
|
(lib.mkIf cfg.atopgpu.enable (mkService "atopgpu" [ atop ]))
|
|
|
|
];
|
|
|
|
timers = lib.mkIf cfg.atopRotateTimer.enable (mkTimer "atop-rotate" [ atop ]);
|
2021-05-14 01:11:05 +01:00
|
|
|
};
|
2021-09-22 11:38:19 +01:00
|
|
|
|
|
|
|
security.wrappers = lib.mkIf cfg.setuidWrapper.enable {
|
2023-06-30 15:11:23 +01:00
|
|
|
atop = {
|
|
|
|
setuid = true;
|
|
|
|
owner = "root";
|
|
|
|
group = "root";
|
|
|
|
source = "${atop}/bin/atop";
|
|
|
|
};
|
2021-09-22 11:38:19 +01:00
|
|
|
};
|
2021-05-14 01:11:05 +01:00
|
|
|
}
|
|
|
|
);
|
2013-05-23 12:04:07 +01:00
|
|
|
}
|