From 6c19dbfe8015094144f6ac301e1431419506365b Mon Sep 17 00:00:00 2001
From: Thomas Schneider <tschneider@asta.rwth-aachen.de>
Date: Wed, 24 Nov 2021 14:49:23 +0100
Subject: [PATCH] node_exporter: Add needrestart integration

---
 node_exporter/defaults/main.yml     | 12 ++++++++
 node_exporter/tasks/main.yml        |  3 ++
 node_exporter/tasks/needrestart.yml | 48 +++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+)
 create mode 100644 node_exporter/tasks/needrestart.yml

diff --git a/node_exporter/defaults/main.yml b/node_exporter/defaults/main.yml
index a8c1c89..b6743df 100644
--- a/node_exporter/defaults/main.yml
+++ b/node_exporter/defaults/main.yml
@@ -1,3 +1,15 @@
 ---
 node_exporter_args: ""
 force_smartmontools_on_vm_guest: false
+node_exporter_needrestart: true
+needrestart2prom_version: "ff8645ab287017d248ab946e09225b1a60fcbba1"
+needrestart2prom_arch: >-
+  {% if ansible_architecture == "x86_64" -%}
+  amd64
+  {%- elif ansible_architecture == "aarch64" -%}
+  arm64
+  {%- elif ansible_architecture[:3] == "arm" -%}
+  {{ ansible_architecture[:5] }}
+  {%- else -%}
+  {{ ansible_architecture }}
+  {%- endif %}
diff --git a/node_exporter/tasks/main.yml b/node_exporter/tasks/main.yml
index e6d83a2..eaffdfa 100644
--- a/node_exporter/tasks/main.yml
+++ b/node_exporter/tasks/main.yml
@@ -59,3 +59,6 @@
     - prometheus
     - prometheus-exporter
     - config
+
+- import_tasks: needrestart.yml
+  when: node_exporter_needrestart
diff --git a/node_exporter/tasks/needrestart.yml b/node_exporter/tasks/needrestart.yml
new file mode 100644
index 0000000..32688cb
--- /dev/null
+++ b/node_exporter/tasks/needrestart.yml
@@ -0,0 +1,48 @@
+---
+
+- name: Install needrestart
+  apt:
+    name: needrestart
+    state: present
+  tags:
+    - prometheus
+    - prometheus-exporter
+
+- name: Install needrestart2prom
+  get_url:
+    url: >-
+      https://git.fsmpi.rwth-aachen.de/api/v4/projects/233/packages/generic/needrestart2prom/{{
+      needrestart2prom_version }}/needrestart2prom-{{ ansible_system|lower }}-{{
+      needrestart2prom_arch }}
+    dest: /usr/local/bin/needrestart2prom
+    force: true
+    owner: root
+    group: root
+    mode: '0755'
+  tags:
+    - prometheus
+    - prometheus-exporter
+
+- name: Configure needrestart2prom cronjob PATH
+  cron:
+    cron_file: needrestart2prom
+    user: root
+    env: true
+    # Yes, this module actually works this way.
+    name: PATH
+    job: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
+  tags:
+    - prometheus
+    - prometheus-exporter
+
+- name: Configure needrestart2prom cronjob
+  cron:
+    cron_file: needrestart2prom
+    user: root
+    name: needrestart2prom
+    job: >-
+      /usr/local/bin/needrestart2prom -outfile
+      /var/lib/prometheus/node-exporter/needrestart.prom
+  tags:
+    - prometheus
+    - prometheus-exporter
-- 
GitLab