Repository navigation
prometheus: modular exporter module #466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
kafu-densha
wants to merge
1
commit into
main
Choose a base branch
from
modular-prom-exporter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
38 changes: 0 additions & 38 deletions
38
modules/logged-in-users-exporter/logged_in_users_exporter.sh
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"; | ||||||||
| 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| }; | ||||||||
| }; | ||||||||
| }; | ||||||||
|
|
||||||||
| config = lib.mkMerge [ | ||||||||
| lib.mkIf | ||||||||
| cfg.enable | ||||||||
| ( | ||||||||
| lib.mkMerge ( | ||||||||
| lib.mapAttrsToList ( | ||||||||
| name: exporter: | ||||||||
| let | ||||||||
| serviceName = "prometheus-textfile-exporter-${name}"; | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| 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}" | ||||||||
| ]; | ||||||||
| }; | ||||||||
| }; | ||||||||
| }; | ||||||||
| } | ||||||||
| ) | ||||||||
| ]; | ||||||||
| } | ||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ''; | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.