25
25
import apt
26
26
import apt_pkg
27
27
import collections
28
+ import logging
28
29
import os
30
+ import sys
29
31
from prometheus_client import CollectorRegistry , Gauge , generate_latest
30
32
31
33
_UpgradeInfo = collections .namedtuple ("_UpgradeInfo" , ["labels" , "count" ])
@@ -58,10 +60,16 @@ def _convert_candidates_to_upgrade_infos(candidates):
58
60
return changes_list
59
61
60
62
61
- def _write_pending_upgrades (registry , cache ):
63
+ def _write_pending_upgrades (registry , cache , logger ):
62
64
candidates = {
63
65
p .candidate for p in cache if p .is_upgradable and not p .phasing_applied
64
66
}
67
+ for candidate in candidates :
68
+ logger .debug (
69
+ "pending upgrade: %s / %s" ,
70
+ candidate .package ,
71
+ candidate .architecture ,
72
+ )
65
73
upgrade_list = _convert_candidates_to_upgrade_infos (candidates )
66
74
67
75
if upgrade_list :
@@ -71,7 +79,7 @@ def _write_pending_upgrades(registry, cache):
71
79
g .labels (change .labels ['origin' ], change .labels ['arch' ]).set (change .count )
72
80
73
81
74
- def _write_held_upgrades (registry , cache ):
82
+ def _write_held_upgrades (registry , cache , logger ):
75
83
held_candidates = {
76
84
p .candidate for p in cache
77
85
if (
@@ -80,6 +88,12 @@ def _write_held_upgrades(registry, cache):
80
88
and not p .phasing_applied
81
89
)
82
90
}
91
+ for candidate in held_candidates :
92
+ logger .debug (
93
+ "held upgrade: %s / %s" ,
94
+ candidate .package ,
95
+ candidate .architecture ,
96
+ )
83
97
upgrade_list = _convert_candidates_to_upgrade_infos (held_candidates )
84
98
85
99
if upgrade_list :
@@ -89,22 +103,37 @@ def _write_held_upgrades(registry, cache):
89
103
g .labels (change .labels ['origin' ], change .labels ['arch' ]).set (change .count )
90
104
91
105
92
- def _write_obsolete_packages (registry , cache ):
106
+ def _write_obsolete_packages (registry , cache , logger ):
93
107
# This corresponds to the apt filter "?obsolete"
94
108
obsoletes = [p for p in cache if p .is_installed and (
95
109
p .candidate is None or
96
110
not p .candidate .origins or
97
111
(len (p .candidate .origins ) == 1 and
98
112
p .candidate .origins [0 ].origin in ['' , "/var/lib/dpkg/status" ])
99
113
)]
114
+ for package in obsoletes :
115
+ if package .candidate is None :
116
+ logger .debug ("obsolete package with no candidate: %s" , package )
117
+ else :
118
+ logger .debug (
119
+ "obsolete package: %s / %s" ,
120
+ package ,
121
+ package .candidate .architecture ,
122
+ )
100
123
101
124
g = Gauge ('apt_packages_obsolete_count' , "Apt packages which are obsolete" ,
102
125
registry = registry )
103
126
g .set (len (obsoletes ))
104
127
105
128
106
- def _write_autoremove_pending (registry , cache ):
107
- autoremovable_packages = {p for p in cache if p .is_auto_removable }
129
+ def _write_autoremove_pending (registry , cache , logger ):
130
+ autoremovable_packages = {p .candidate for p in cache if p .is_auto_removable }
131
+ for candidate in autoremovable_packages :
132
+ logger .debug (
133
+ "autoremovable package: %s / %s" ,
134
+ candidate .package ,
135
+ candidate .architecture ,
136
+ )
108
137
g = Gauge ('apt_autoremove_pending' , "Apt packages pending autoremoval." ,
109
138
registry = registry )
110
139
g .set (len (autoremovable_packages ))
@@ -147,13 +176,18 @@ def _write_reboot_required(registry):
147
176
148
177
149
178
def _main ():
179
+ logging .basicConfig (stream = sys .stderr )
180
+ logger = logging .getLogger (__name__ )
181
+ if os .environ .get ("DEBUG" ):
182
+ logger .setLevel (logging .DEBUG )
183
+
150
184
cache = apt .cache .Cache ()
151
185
152
186
registry = CollectorRegistry ()
153
- _write_pending_upgrades (registry , cache )
154
- _write_held_upgrades (registry , cache )
155
- _write_obsolete_packages (registry , cache )
156
- _write_autoremove_pending (registry , cache )
187
+ _write_pending_upgrades (registry , cache , logger )
188
+ _write_held_upgrades (registry , cache , logger )
189
+ _write_obsolete_packages (registry , cache , logger )
190
+ _write_autoremove_pending (registry , cache , logger )
157
191
_write_installed_packages_per_origin (registry , cache )
158
192
_write_cache_timestamps (registry )
159
193
_write_reboot_required (registry )
0 commit comments