From 8595d565de1d511b2e448ee2cf064a27ec696331 Mon Sep 17 00:00:00 2001 From: Oussama Siyagh Date: Tue, 30 Jan 2024 01:04:50 +0100 Subject: [PATCH 1/3] added unit socket file Signed-off-by: Oussama Siyagh --- roles/node_exporter/defaults/main.yml | 3 ++- roles/node_exporter/tasks/configure.yml | 9 +++++++++ roles/node_exporter/templates/node_exporter.service.j2 | 3 ++- roles/node_exporter/templates/node_exporter.socket.j2 | 8 ++++++++ 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 roles/node_exporter/templates/node_exporter.socket.j2 diff --git a/roles/node_exporter/defaults/main.yml b/roles/node_exporter/defaults/main.yml index 116ed0e3f..ab9c7ac71 100644 --- a/roles/node_exporter/defaults/main.yml +++ b/roles/node_exporter/defaults/main.yml @@ -6,7 +6,8 @@ node_exporter_binary_url: "https://github.com/{{ _node_exporter_repo }}/releases node_exporter_checksums_url: "https://github.com/{{ _node_exporter_repo }}/releases/download/v{{ node_exporter_version }}/sha256sums.txt" node_exporter_skip_install: false -node_exporter_web_listen_address: "0.0.0.0:9100" +node_exporter_web_listen_port: 9100 +node_exporter_web_listen_address: "0.0.0.0:{{ node_exporter_web_listen_port }}" node_exporter_web_telemetry_path: "/metrics" node_exporter_textfile_dir: "/var/lib/node_exporter" diff --git a/roles/node_exporter/tasks/configure.yml b/roles/node_exporter/tasks/configure.yml index db85c1991..a29ced533 100644 --- a/roles/node_exporter/tasks/configure.yml +++ b/roles/node_exporter/tasks/configure.yml @@ -8,6 +8,15 @@ mode: 0644 notify: restart node_exporter +- name: Copy the node_exporter systemd socket file + ansible.builtin.template: + src: node_exporter.socket.j2 + dest: /usr/lib/systemd/system/node_exporter.socket + owner: root + group: root + mode: 0644 + notify: restart node_exporter + - name: Create node_exporter config directory ansible.builtin.file: path: "/etc/node_exporter" diff --git a/roles/node_exporter/templates/node_exporter.service.j2 b/roles/node_exporter/templates/node_exporter.service.j2 index 467cece35..9bfd69cc1 100644 --- a/roles/node_exporter/templates/node_exporter.service.j2 +++ b/roles/node_exporter/templates/node_exporter.service.j2 @@ -3,12 +3,13 @@ [Unit] Description=Prometheus Node Exporter After=network-online.target +Requires=node_exporter.socket [Service] Type=simple User={{ node_exporter_system_user }} Group={{ node_exporter_system_group }} -ExecStart={{ node_exporter_binary_install_dir }}/node_exporter \ +ExecStart={{ node_exporter_binary_install_dir }}/node_exporter --web.systemd-socket \ {% for collector in node_exporter_enabled_collectors -%} {% if not collector is mapping %} '--collector.{{ collector }}' \ diff --git a/roles/node_exporter/templates/node_exporter.socket.j2 b/roles/node_exporter/templates/node_exporter.socket.j2 new file mode 100644 index 000000000..9d334dbb9 --- /dev/null +++ b/roles/node_exporter/templates/node_exporter.socket.j2 @@ -0,0 +1,8 @@ +[Unit] +Description=Node Exporter + +[Socket] +ListenStream={{ node_exporter_web_listen_port }} + +[Install] +WantedBy=socket.target \ No newline at end of file From 9431dd50f2c56b8be23bc036eb954aa71ea27c56 Mon Sep 17 00:00:00 2001 From: Oussama Siyagh Date: Tue, 20 Feb 2024 01:32:13 +0100 Subject: [PATCH 2/3] removed port parameter Signed-off-by: Oussama Siyagh --- roles/node_exporter/defaults/main.yml | 3 +-- roles/node_exporter/templates/node_exporter.socket.j2 | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/roles/node_exporter/defaults/main.yml b/roles/node_exporter/defaults/main.yml index ab9c7ac71..116ed0e3f 100644 --- a/roles/node_exporter/defaults/main.yml +++ b/roles/node_exporter/defaults/main.yml @@ -6,8 +6,7 @@ node_exporter_binary_url: "https://github.com/{{ _node_exporter_repo }}/releases node_exporter_checksums_url: "https://github.com/{{ _node_exporter_repo }}/releases/download/v{{ node_exporter_version }}/sha256sums.txt" node_exporter_skip_install: false -node_exporter_web_listen_port: 9100 -node_exporter_web_listen_address: "0.0.0.0:{{ node_exporter_web_listen_port }}" +node_exporter_web_listen_address: "0.0.0.0:9100" node_exporter_web_telemetry_path: "/metrics" node_exporter_textfile_dir: "/var/lib/node_exporter" diff --git a/roles/node_exporter/templates/node_exporter.socket.j2 b/roles/node_exporter/templates/node_exporter.socket.j2 index 9d334dbb9..6b84a58dc 100644 --- a/roles/node_exporter/templates/node_exporter.socket.j2 +++ b/roles/node_exporter/templates/node_exporter.socket.j2 @@ -2,7 +2,7 @@ Description=Node Exporter [Socket] -ListenStream={{ node_exporter_web_listen_port }} +ListenStream={{ node_exporter_web_listen_address.split(':') | last }} [Install] WantedBy=socket.target \ No newline at end of file From 5b456564a80dd0ba464e6b356514153965902e3c Mon Sep 17 00:00:00 2001 From: Oussama Siyagh Date: Fri, 1 Mar 2024 01:00:08 +0100 Subject: [PATCH 3/3] make systemd socket activation optional Signed-off-by: Oussama Siyagh --- roles/node_exporter/defaults/main.yml | 1 + roles/node_exporter/meta/argument_specs.yml | 4 ++++ roles/node_exporter/tasks/configure.yml | 1 + roles/node_exporter/templates/node_exporter.service.j2 | 7 ++++++- 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/roles/node_exporter/defaults/main.yml b/roles/node_exporter/defaults/main.yml index 116ed0e3f..55cf24dd4 100644 --- a/roles/node_exporter/defaults/main.yml +++ b/roles/node_exporter/defaults/main.yml @@ -30,3 +30,4 @@ node_exporter_disabled_collectors: [] node_exporter_binary_install_dir: "/usr/local/bin" node_exporter_system_group: "node-exp" node_exporter_system_user: "{{ node_exporter_system_group }}" +node_exporter_systemd_socket_activation: false diff --git a/roles/node_exporter/meta/argument_specs.yml b/roles/node_exporter/meta/argument_specs.yml index c7385a70b..62f628272 100644 --- a/roles/node_exporter/meta/argument_specs.yml +++ b/roles/node_exporter/meta/argument_specs.yml @@ -81,3 +81,7 @@ argument_specs: - "I(Advanced)" - "Node exporter user" default: "node-exp" + node_exporter_systemd_socket_activation: + description: + - "Node exporter systemd socket activation task is executed when set to true." + default: false diff --git a/roles/node_exporter/tasks/configure.yml b/roles/node_exporter/tasks/configure.yml index a29ced533..d0598aa9a 100644 --- a/roles/node_exporter/tasks/configure.yml +++ b/roles/node_exporter/tasks/configure.yml @@ -16,6 +16,7 @@ group: root mode: 0644 notify: restart node_exporter + when: node_exporter_systemd_socket_activation - name: Create node_exporter config directory ansible.builtin.file: diff --git a/roles/node_exporter/templates/node_exporter.service.j2 b/roles/node_exporter/templates/node_exporter.service.j2 index 9bfd69cc1..33378f589 100644 --- a/roles/node_exporter/templates/node_exporter.service.j2 +++ b/roles/node_exporter/templates/node_exporter.service.j2 @@ -3,13 +3,18 @@ [Unit] Description=Prometheus Node Exporter After=network-online.target +{% if node_exporter_systemd_socket_activation %} Requires=node_exporter.socket +{% endif -%} [Service] Type=simple User={{ node_exporter_system_user }} Group={{ node_exporter_system_group }} -ExecStart={{ node_exporter_binary_install_dir }}/node_exporter --web.systemd-socket \ +ExecStart={{ node_exporter_binary_install_dir }}/node_exporter \ +{% if node_exporter_systemd_socket_activation %} +--web.systemd-socket \ +{% endif -%} {% for collector in node_exporter_enabled_collectors -%} {% if not collector is mapping %} '--collector.{{ collector }}' \