From ff69095ad85cbfcc9f464c2113c46784569f5f7e Mon Sep 17 00:00:00 2001 From: Frank Sachsenheim Date: Tue, 7 May 2024 16:09:49 +0200 Subject: [PATCH 1/4] apt_info.py: use docstring for module documentation (#211) Signed-off-by: Frank Sachsenheim --- apt_info.py | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/apt_info.py b/apt_info.py index 90f2b52..7abb2cf 100755 --- a/apt_info.py +++ b/apt_info.py @@ -1,24 +1,26 @@ #!/usr/bin/env python3 -# -# Description: Expose metrics from apt. This is inspired by and -# intended to be a replacement for the original apt.sh. -# -# This script deliberately does *not* update the apt cache. You need -# something else to run `apt update` regularly for the metrics to be -# up to date. This can be done in numerous ways, but the canonical way -# is to use the normal `APT::Periodic::Update-Package-Lists` -# setting. -# -# This, for example, will enable a nightly job that runs `apt update`: -# -# echo 'APT::Periodic::Update-Package-Lists "1";' > /etc/apt/apt.conf.d/99_auto_apt_update.conf -# -# See /usr/lib/apt/apt.systemd.daily for details. -# -# Dependencies: python3-apt, python3-prometheus-client -# -# Authors: Kyle Fazzari -# Daniel Swarbrick + +""" +Description: Expose metrics from apt. This is inspired by and +intended to be a replacement for the original apt.sh. + +This script deliberately does *not* update the apt cache. You need +something else to run `apt update` regularly for the metrics to be +up to date. This can be done in numerous ways, but the canonical way +is to use the normal `APT::Periodic::Update-Package-Lists` +setting. + +This, for example, will enable a nightly job that runs `apt update`: + + echo 'APT::Periodic::Update-Package-Lists "1";' > /etc/apt/apt.conf.d/99_auto_apt_update.conf + +See /usr/lib/apt/apt.systemd.daily for details. + +Dependencies: python3-apt, python3-prometheus-client + +Authors: Kyle Fazzari + Daniel Swarbrick +""" import apt import apt_pkg From e804e312bda51f2b3117e5fbd389220cc383b063 Mon Sep 17 00:00:00 2001 From: kennethso168 Date: Thu, 9 May 2024 03:49:50 +0800 Subject: [PATCH 2/4] Improve heuristic for the apt update last run time (#208) As suggested by #194 and this fixes it Signed-off-by: kennethso168 --- apt_info.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/apt_info.py b/apt_info.py index 7abb2cf..1945591 100755 --- a/apt_info.py +++ b/apt_info.py @@ -90,13 +90,16 @@ def _write_autoremove_pending(registry, cache): def _write_cache_timestamps(registry): g = Gauge('apt_package_cache_timestamp_seconds', "Apt update last run time.", registry=registry) apt_pkg.init_config() - if apt_pkg.config.find_b("APT::Periodic::Update-Package-Lists"): + if ( + apt_pkg.config.find_b("APT::Periodic::Update-Package-Lists") and + os.path.isfile("/var/lib/apt/periodic/update-success-stamp") + ): # if we run updates automatically with APT::Periodic, we can - # check this timestamp file + # check this timestamp file if it exists stamp_file = "/var/lib/apt/periodic/update-success-stamp" else: - # if not, let's just fallback on the lists directory - stamp_file = '/var/lib/apt/lists' + # if not, let's just fallback on the partial file of the lists directory + stamp_file = '/var/lib/apt/lists/partial' try: g.set(os.stat(stamp_file).st_mtime) except OSError: From 415cfb7c0d9587f05f9236d902bae4867f07027b Mon Sep 17 00:00:00 2001 From: Chris Aumann Date: Wed, 8 May 2024 22:15:09 +0200 Subject: [PATCH 3/4] Use awk instead of sed when parsing du output (#112) This feel a little less "magic", is better readable and maintainable. Might have a slight impact on performance, too. --- directory-size.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/directory-size.sh b/directory-size.sh index 3e03f04..5357901 100755 --- a/directory-size.sh +++ b/directory-size.sh @@ -13,4 +13,4 @@ echo "# HELP node_directory_size_bytes Disk space used by some directories" echo "# TYPE node_directory_size_bytes gauge" du --block-size=1 --summarize "$@" \ - | sed -ne 's/\\/\\\\/;s/"/\\"/g;s/^\([0-9]\+\)\t\(.*\)$/node_directory_size_bytes{directory="\2"} \1/p' + | awk '{ print "node_directory_size_bytes{directory=\"" $2 "\"} " $1 }' From 5ec17196304a0be6080405d23674d3cec5ff67f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8D=BF?= Date: Thu, 9 May 2024 19:24:38 +0800 Subject: [PATCH 4/4] lint: format yum.sh (#212) Signed-off-by: wangqing --- yum.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yum.sh b/yum.sh index f30143f..ce06b97 100755 --- a/yum.sh +++ b/yum.sh @@ -32,17 +32,17 @@ upgrades=$(check_upgrades) echo '# HELP yum_upgrades_pending Yum package pending updates by origin.' echo '# TYPE yum_upgrades_pending gauge' -if [[ -n "${upgrades}" ]] ; then +if [[ -n "${upgrades}" ]]; then echo "${upgrades}" else echo 'yum_upgrades_pending{origin=""} 0' fi # If yum-utils/dnf-utils is not installed then we skip rendering this metric -if [[ -x /bin/needs-restarting ]] ; then +if [[ -x /bin/needs-restarting ]]; then echo '# HELP node_reboot_required Node reboot is required for software updates.' echo '# TYPE node_reboot_required gauge' - if /bin/needs-restarting -r > /dev/null 2>&1 ; then + if /bin/needs-restarting -r >/dev/null 2>&1; then echo 'node_reboot_required 0' else echo 'node_reboot_required 1'