Skip to content

collect more apt metrics (Closes: #220) #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions apt_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ def _convert_candidates_to_upgrade_infos(candidates):
changes_dict = collections.defaultdict(lambda: collections.defaultdict(int))

for candidate in candidates:
# The 'now' archive only shows that packages are not installed. We tend
# to filter the candidates on those kinds of conditions before reaching
# here so here we don't want to include this information in order to
# reduce noise in the data.
origins = sorted(
{f"{o.origin}:{o.codename}/{o.archive}" for o in candidate.origins}
{f"{o.origin}:{o.codename}/{o.archive}" for o in candidate.origins
if o.archive != 'now'}
)
changes_dict[",".join(origins)][candidate.architecture] += 1

Expand All @@ -55,7 +60,7 @@ def _convert_candidates_to_upgrade_infos(candidates):

def _write_pending_upgrades(registry, cache):
candidates = {
p.candidate for p in cache if p.is_upgradable
p.candidate for p in cache if p.is_upgradable and not p.phasing_applied
}
upgrade_list = _convert_candidates_to_upgrade_infos(candidates)

Expand All @@ -69,7 +74,11 @@ def _write_pending_upgrades(registry, cache):
def _write_held_upgrades(registry, cache):
held_candidates = {
p.candidate for p in cache
if p.is_upgradable and p._pkg.selected_state == apt_pkg.SELSTATE_HOLD
if (
p.is_upgradable
and p._pkg.selected_state == apt_pkg.SELSTATE_HOLD
and not p.phasing_applied
)
}
upgrade_list = _convert_candidates_to_upgrade_infos(held_candidates)

Expand All @@ -80,13 +89,38 @@ def _write_held_upgrades(registry, cache):
g.labels(change.labels['origin'], change.labels['arch']).set(change.count)


def _write_obsolete_packages(registry, cache):
# This corresponds to the apt filter "?obsolete"
obsoletes = [p for p in cache if p.is_installed and (
p.candidate is None or
not p.candidate.origins or
(len(p.candidate.origins) == 1 and
p.candidate.origins[0].origin in ['', "/var/lib/dpkg/status"])
)]

g = Gauge('apt_packages_obsolete_count', "Apt packages which are obsolete",
registry=registry)
g.set(len(obsoletes))


def _write_autoremove_pending(registry, cache):
autoremovable_packages = {p for p in cache if p.is_auto_removable}
g = Gauge('apt_autoremove_pending', "Apt packages pending autoremoval.",
registry=registry)
g.set(len(autoremovable_packages))


def _write_installed_packages_per_origin(registry, cache):
installed_packages = {p.candidate for p in cache if p.is_installed}
per_origin = _convert_candidates_to_upgrade_infos(installed_packages)

if per_origin:
g = Gauge('apt_packages_per_origin_count', "Number of packages installed per origin.",
['origin', 'arch'], registry=registry)
for o in per_origin:
g.labels(o.labels['origin'], o.labels['arch']).set(o.count)


def _write_cache_timestamps(registry):
g = Gauge('apt_package_cache_timestamp_seconds', "Apt update last run time.", registry=registry)
apt_pkg.init_config()
Expand Down Expand Up @@ -118,7 +152,9 @@ def _main():
registry = CollectorRegistry()
_write_pending_upgrades(registry, cache)
_write_held_upgrades(registry, cache)
_write_obsolete_packages(registry, cache)
_write_autoremove_pending(registry, cache)
_write_installed_packages_per_origin(registry, cache)
_write_cache_timestamps(registry)
_write_reboot_required(registry)
print(generate_latest(registry).decode(), end='')
Expand Down
Loading