Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 0 additions & 70 deletions modules/logged-in-users-exporter/default.nix

This file was deleted.

38 changes: 0 additions & 38 deletions modules/logged-in-users-exporter/logged_in_users_exporter.sh

This file was deleted.

100 changes: 100 additions & 0 deletions modules/prometheus/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
config,
lib,
...
}:

let
cfg = config.ocf.prometheus-export;
in
{
options.ocf.prometheus-export = {
enable = lib.mkEnableOption "exporting prometheus data";
textFileDir = lib.mkOption {
type = lib.types.str;
default = "/var/lib/node_exporter/textfile_collector";
};
exporters = lib.mkOption {
default = { };
type = lib.types.attrsOf lib.types.submodule {
enable = lib.mkEnableOption "running this exporter";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
enable = lib.mkEnableOption "running this exporter";

script = lib.mkOption {
type = lib.types.package;
description = "the script to execute, the textfile to write is passed as argv[1]";
};
interval = lib.mkOption {
type = lib.types.str;
description = "how often to run the script, use systemd format (ex: '5s')";
default = "5s";
};
serviceName = lib.mkOption {
type = lib.types.str;
};
Comment on lines +30 to +32

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
serviceName = lib.mkOption {
type = lib.types.str;
};

};
};
};

config = lib.mkMerge [
lib.mkIf
cfg.enable
(
lib.mkMerge (
lib.mapAttrsToList (
name: exporter:
let
serviceName = "prometheus-textfile-exporter-${name}";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
serviceName = "prometheus-textfile-exporter-${name}";
serviceName = "prometheus-textfile-exporter-${utils.escapeSystemdPath name}";

in
{
cfg.exporters."${name}".serviceName = serviceName;
systemd.services."${serviceName}" = {
enable = true;
after = [ "network.target" ];
wants = [ "network.target" ];
wantedBy = [ "multi-user.target" ];

serviceConfig = {
Type = "oneshot";
User = "root";
ExecStart = "${lib.getExe exporter.script} ${cfg.textFileDir}/${name}.prom";
};
};
systemd.timers."${serviceName}" = {
wantedBy = [ "timers.target" ];
timerConfig = {
OnUnitActiveSec = exporter.interval;
AccuracySec = "1ms";
Unit = "${serviceName}.service";
};
};
}
) cfg.exporters
)
// {
systemd.tmpfiles.rules = [
"d ${cfg.textFileDir} 0755 root root -"
];

services.prometheus = {
exporters = {
node = {
enable = true;
port = 9100;
openFirewall = true;
enabledCollectors = [
"systemd"
"textfile"
"ethtool"
"softirqs"
"tcpstat"
"wifi"
];
extraFlags = [
"--collector.textfile.directory=${cfg.textFileDir}"
];
};
};
};
}
)
];
}
46 changes: 46 additions & 0 deletions modules/prometheus/logged-in-users-exporter.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
config,
pkgs,
lib,
...
}:

let
cfg = config.ocf.logged-in-users-exporter;
prom = config.ocf.prometheus-export;
in
{
options.ocf.logged-in-users-exporter.enable = lib.mkEnableOption "logged in users exporter";
config = lib.mkIf cfg.enable {
prom.textFileScripts = lib.singleton pkgs.writeShellScriptBin "logged-in-users-exporter" ''
output_file="${prom.textFileDir}/logged_in_users.prom"

# clear and reset the file, then add anyone who is logged in
> "$output_file"
loginctl list-sessions --no-legend | while read -r session_id uid user seat leader class tty idle since; do
if [[ $class == "user" ]] && [[ $seat == "seat0" ]] && [[ $idle == "no" ]]; then
# we only want to list users who are:
# - online: logged in but not in the foreground
# - active: logged in and in the foreground
# TODO: list logged in users but dont place a penguin if no sessions are active
session_status=$(loginctl show-session "$session_id" -p State --value)
if ! [[ $session_status == "online" || $session_status == "active" ]]; then
continue
fi

# If the locking program supports systemd-logind, LockedHint can checked instead.
# To support lockers that do not support logind, locked_status is only set to
# locked if LockedHint specifically says so.
session_lockedhint=$(loginctl show-session "$session_id" -p LockedHint --value)
if [[ $session_lockedhint == "yes" ]]; then
locked_status="locked"
else
locked_status="unlocked"
fi

# TODO: allow multiple users to be listed as logged in on a computer
echo "node_logged_in_user{name=\"$user\", state=\"$locked_status\"} 1" > "$output_file"
fi
'';
};
}
1 change: 1 addition & 0 deletions profiles/desktop.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
home.tmpfs = true;
home.mountRemote = true;
network.wakeOnLan.enable = true;
prometheus-export.enable = true;
logged-in-users-exporter.enable = true;

zfs.enable = true;
Expand Down
Loading