Skip to content

Commit f3537e8

Browse files
committed
apt_info.py: allow setting different root dir
Signed-off-by: egvimo <[email protected]>
1 parent f5c56e7 commit f3537e8

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

apt_info.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
# Authors: Kyle Fazzari <[email protected]>
2121
# Daniel Swarbrick <[email protected]>
2222

23-
import apt
24-
import apt_pkg
23+
import argparse
2524
import collections
2625
import os
26+
import apt
27+
import apt_pkg
2728
from prometheus_client import CollectorRegistry, Gauge, generate_latest
2829

2930
_UpgradeInfo = collections.namedtuple("_UpgradeInfo", ["labels", "count"])
@@ -38,12 +39,12 @@ def _convert_candidates_to_upgrade_infos(candidates):
3839
)
3940
changes_dict[",".join(origins)][candidate.architecture] += 1
4041

41-
changes_list = list()
42+
changes_list = []
4243
for origin in sorted(changes_dict.keys()):
4344
for arch in sorted(changes_dict[origin].keys()):
4445
changes_list.append(
4546
_UpgradeInfo(
46-
labels=dict(origin=origin, arch=arch),
47+
labels={"origin": origin, "arch": arch},
4748
count=changes_dict[origin][arch],
4849
)
4950
)
@@ -86,38 +87,50 @@ def _write_autoremove_pending(registry, cache):
8687
g.set(len(autoremovable_packages))
8788

8889

89-
def _write_cache_timestamps(registry):
90+
def _write_cache_timestamps(registry, root_dir):
9091
g = Gauge('apt_package_cache_timestamp_seconds', "Apt update last run time.", registry=registry)
9192
apt_pkg.init_config()
9293
if apt_pkg.config.find_b("APT::Periodic::Update-Package-Lists"):
9394
# if we run updates automatically with APT::Periodic, we can
9495
# check this timestamp file
95-
stamp_file = "/var/lib/apt/periodic/update-success-stamp"
96+
stamp_file = os.path.join(root_dir, 'var/lib/apt/periodic/update-success-stamp')
9697
else:
9798
# if not, let's just fallback on the lists directory
98-
stamp_file = '/var/lib/apt/lists'
99+
stamp_file = os.path.join(root_dir, 'var/lib/apt/lists')
99100
try:
100101
g.set(os.stat(stamp_file).st_mtime)
101102
except OSError:
102103
pass
103104

104105

105-
def _write_reboot_required(registry):
106+
def _write_reboot_required(registry, root_dir):
106107
g = Gauge('node_reboot_required', "Node reboot is required for software updates.",
107108
registry=registry)
108-
g.set(int(os.path.isfile('/run/reboot-required')))
109+
g.set(int(os.path.isfile(os.path.join(root_dir, 'run/reboot-required'))))
109110

110111

111-
def _main():
112-
cache = apt.cache.Cache()
113-
112+
def generate_metrics(root_dir: str = '/') -> bytes:
113+
cache = apt.cache.Cache(rootdir=root_dir)
114114
registry = CollectorRegistry()
115+
115116
_write_pending_upgrades(registry, cache)
116117
_write_held_upgrades(registry, cache)
117118
_write_autoremove_pending(registry, cache)
118-
_write_cache_timestamps(registry)
119-
_write_reboot_required(registry)
120-
print(generate_latest(registry).decode(), end='')
119+
_write_cache_timestamps(registry, root_dir)
120+
_write_reboot_required(registry, root_dir)
121+
122+
return generate_latest(registry)
123+
124+
125+
def _main():
126+
parser = argparse.ArgumentParser()
127+
parser.add_argument('-r', '--root-dir', dest='root_dir', type=str, default='/',
128+
help="Set root directory to a different path than /")
129+
args = parser.parse_args()
130+
131+
metrics = generate_metrics(args.root_dir)
132+
133+
print(metrics.decode(), end='')
121134

122135

123136
if __name__ == "__main__":

0 commit comments

Comments
 (0)