Skip to content

Commit

Permalink
feat: update apt_info.py from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
egvimo committed Oct 21, 2023
1 parent ab26051 commit abfab6c
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions apt_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
# 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 <[email protected]>
Expand All @@ -12,7 +24,6 @@

import argparse
import collections
import contextlib
import os
import apt
from prometheus_client import CollectorRegistry, Gauge, generate_latest
Expand Down Expand Up @@ -85,24 +96,13 @@ def _write_reboot_required(registry, root_dir):

def generate_metrics(root_dir: str = '/') -> bytes:
cache = apt.cache.Cache(rootdir=root_dir)

# First of all, attempt to update the index. If we don't have permission
# to do so (or it fails for some reason), it's not the end of the world,
# we'll operate on the old index.
with contextlib.suppress(apt.cache.LockFailedException, apt.cache.FetchFailedException):
cache.update()

cache.open()
cache.upgrade(True)

registry = CollectorRegistry()

_write_pending_upgrades(registry, cache)
_write_held_upgrades(registry, cache)
_write_autoremove_pending(registry, cache)
_write_reboot_required(registry, root_dir)

cache.close()

return generate_latest(registry)


Expand Down

0 comments on commit abfab6c

Please sign in to comment.