From 21ba810ccb5044e21680ddf083820ae6b8f40fa0 Mon Sep 17 00:00:00 2001 From: Lukas M Date: Mon, 31 Jul 2023 17:30:21 +0200 Subject: [PATCH 1/2] Support host prefix for apt_info.py This will allow to run apt_info.py on the host from the container. ``` docker run --rm -it --entrypoint bash -v "/:/hostfs" ubuntu:22.04 -c 'apt update && apt install -y python3-prometheus-client python3-apt curl && curl https://raw.githubusercontent.com/prometheus-community/node-exporter-textfile-collector-scripts/master/apt_info.py -o /tmp/apt_info.py && python3 /tmp/apt_info.py' ``` Signed-off-by: Lukas M --- apt_info.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/apt_info.py b/apt_info.py index 1945591..a8f625a 100755 --- a/apt_info.py +++ b/apt_info.py @@ -28,6 +28,8 @@ import os from prometheus_client import CollectorRegistry, Gauge, generate_latest +hostPrefix = os.environ.get("APT_HOSTPREFIX","/") + _UpgradeInfo = collections.namedtuple("_UpgradeInfo", ["labels", "count"]) @@ -109,11 +111,11 @@ def _write_cache_timestamps(registry): def _write_reboot_required(registry): g = Gauge('node_reboot_required', "Node reboot is required for software updates.", registry=registry) - g.set(int(os.path.isfile('/run/reboot-required'))) + g.set(int(os.path.isfile(os.path.join(hostPrefix,'/run/reboot-required')))) def _main(): - cache = apt.cache.Cache() + cache = apt.cache.Cache(rootdir=hostPrefix) registry = CollectorRegistry() _write_pending_upgrades(registry, cache) From e92574d5a190dac894a83a4c1d9bc79b10c5daaf Mon Sep 17 00:00:00 2001 From: Lukas M Date: Mon, 31 Jul 2023 18:14:11 +0200 Subject: [PATCH 2/2] Update apt_info.py Signed-off-by: Lukas M --- apt_info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apt_info.py b/apt_info.py index a8f625a..2942555 100755 --- a/apt_info.py +++ b/apt_info.py @@ -111,7 +111,7 @@ def _write_cache_timestamps(registry): def _write_reboot_required(registry): g = Gauge('node_reboot_required', "Node reboot is required for software updates.", registry=registry) - g.set(int(os.path.isfile(os.path.join(hostPrefix,'/run/reboot-required')))) + g.set(int(os.path.isfile(os.path.join(hostPrefix,'run/reboot-required')))) def _main():