diff --git a/roles/prometheus_node/files/apt.sh b/roles/prometheus_node/files/apt.sh new file mode 100644 index 0000000..b61e72f --- /dev/null +++ b/roles/prometheus_node/files/apt.sh @@ -0,0 +1,32 @@ +#!/bin/bash +# +# Description: Expose metrics from apt updates. +# +# Author: Ben Kochie + +upgrades="$(/usr/bin/apt-get --just-print dist-upgrade \ + | /usr/bin/awk -F'[()]' \ + '/^Inst/ { sub("^[^ ]+ ", "", $2); gsub(" ","",$2); + sub("\\[", " ", $2); sub("\\]", "", $2); print $2 }' \ + | /usr/bin/sort \ + | /usr/bin/uniq -c \ + | awk '{ gsub(/\\\\/, "\\\\", $2); gsub(/\"/, "\\\"", $2); + gsub(/\[/, "", $3); gsub(/\]/, "", $3); + print "apt_upgrades_pending{origin=\"" $2 "\",arch=\"" $NF "\"} " $1}' +)" + +echo '# HELP apt_upgrades_pending Apt package pending updates by origin.' +echo '# TYPE apt_upgrades_pending gauge' +if [[ -n "${upgrades}" ]] ; then + echo "${upgrades}" +else + echo 'apt_upgrades_pending{origin="",arch=""} 0' +fi + +echo '# HELP node_reboot_required Node reboot is required for software updates.' +echo '# TYPE node_reboot_required gauge' +if [[ -f '/run/reboot-required' ]] ; then + echo 'node_reboot_required 1' +else + echo 'node_reboot_required 0' +fi diff --git a/roles/prometheus_node/files/prometheus-node-exporter-apt.service b/roles/prometheus_node/files/prometheus-node-exporter-apt.service new file mode 100644 index 0000000..63c901d --- /dev/null +++ b/roles/prometheus_node/files/prometheus-node-exporter-apt.service @@ -0,0 +1,7 @@ +[Unit] +Description=Collect apt metrics for prometheus-node-exporter + +[Service] +Type=oneshot +Environment=TMPDIR=/var/lib/prometheus/node-exporter +ExecStart=/bin/bash -c "/usr/share/prometheus-node-exporter/apt.sh | sponge /var/lib/prometheus/node-exporter/apt.prom" diff --git a/roles/prometheus_node/files/prometheus-node-exporter-apt.timer b/roles/prometheus_node/files/prometheus-node-exporter-apt.timer new file mode 100644 index 0000000..6ed4b45 --- /dev/null +++ b/roles/prometheus_node/files/prometheus-node-exporter-apt.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Run apt metrics collection every 15 minutes + +[Timer] +OnBootSec=0 +OnUnitActiveSec=15min + +[Install] +WantedBy=timers.target diff --git a/roles/prometheus_node/tasks/main.yml b/roles/prometheus_node/tasks/main.yml index 8a82947..b1bf76f 100644 --- a/roles/prometheus_node/tasks/main.yml +++ b/roles/prometheus_node/tasks/main.yml @@ -37,3 +37,61 @@ line: | ARGS="--web.listen-address={{ ansible_hostname }}.adm.auro.re:9100" notify: Restart prometheus-node-exporter + +- name: Add monitoring for apt on bullseye + block: + - name: Install moreutils # we need the sponge command + apt: + name: + - moreutils + state: latest + update_cache: true + register: apt_result + retries: 3 + until: apt_result is succeeded + + - name: Ensure /usr/share/prometheus-node-exporter exist + file: + path: /usr/share/prometheus-node-exporter/ + state: directory + group: root + owner: root + mode: u=rwx,g=rx,o=rx + + - name: Add the script + copy: + src: apt.sh + dest: /usr/share/prometheus-node-exporter/apt.sh + group: root + owner: root + mode: u=rwx,g=rx,o=rx + + - name: Add the timer + copy: + src: prometheus-node-exporter-apt.timer + dest: /lib/systemd/system/prometheus-node-exporter-apt.timer + group: root + owner: root + mode: u=rw,g=r,o=r + + - name: Add the service + copy: + src: prometheus-node-exporter-apt.service + dest: /lib/systemd/system/prometheus-node-exporter-apt.service + group: root + owner: root + mode: u=rw,g=r,o=r + + - name: Enable the timer + systemd: + name: prometheus-node-exporter-apt.timer + state: started + enabled: true + + - name: Enable the service + systemd: + name: prometheus-node-exporter-apt.service + state: started + enabled: true + + when: ansible_facts['lsb']['codename'] == 'bullseye'