1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-22 02:51:03 +00:00

plugin apt_ubuntu: various improvements

* fix category parsing for plugin gallery
* python3 compatibility
* zero exitcodes for autoconf failures
* PEP8 clean
This commit is contained in:
Lars Kruse 2018-03-28 05:57:02 +02:00
parent 8e3f1d8865
commit 87927d194c

View file

@ -21,11 +21,14 @@
# Magic markers - optional - used by installation scripts and # Magic markers - optional - used by installation scripts and
# munin-config: # munin-config:
# #
#%# capabilities=autoconf # #%# capabilities=autoconf
#%# family=contrib # #%# family=contrib
import os
import sys
import warnings
########################################################### ###########################################################
category = 'security' # 'upgrades'
title = 'Upgradable packages' # 'Upgradeable packages' title = 'Upgradable packages' # 'Upgradeable packages'
vlabel = 'Total packages' vlabel = 'Total packages'
other = 'other' other = 'other'
@ -38,12 +41,9 @@ origins = ['Ubuntu']
critical = 1 critical = 1
########################################################### ###########################################################
import os
import sys
import warnings
warnings.filterwarnings('ignore', 'apt API not stable yet', FutureWarning) warnings.filterwarnings('ignore', 'apt API not stable yet', FutureWarning)
def autoconf(): def autoconf():
if os.path.exists('/etc/lsb-release'): if os.path.exists('/etc/lsb-release'):
for line in open('/etc/lsb-release'): for line in open('/etc/lsb-release'):
@ -51,60 +51,60 @@ def autoconf():
try: try:
import apt import apt
except ImportError: except ImportError:
print 'no (python-apt not installed)' print('no (python-apt not installed)')
sys.exit(1) sys.exit(0)
cache = apt.Cache() cache = apt.Cache()
if not cache.has_key('update-notifier-common'): if 'update-notifier-common' not in cache:
print 'no (update-notifier-common not found)' print('no (update-notifier-common not found)')
sys.exit(1) sys.exit(0)
if not cache['update-notifier-common'].isInstalled: if not cache['update-notifier-common'].isInstalled:
print 'no (update-notifier-common not installed)' print('no (update-notifier-common not installed)')
sys.exit(1) sys.exit(0)
if not os.path.exists('/etc/apt/apt.conf.d/10periodic'): if not os.path.exists('/etc/apt/apt.conf.d/10periodic'):
print 'no (/etc/apt/apt.conf.d/10periodic not found)' print('no (/etc/apt/apt.conf.d/10periodic not found)')
sys.exit(1) sys.exit(1)
for line in open('/etc/apt/apt.conf.d/10periodic'): for line in open('/etc/apt/apt.conf.d/10periodic'):
if line.strip() == 'APT::Periodic::Update-Package-Lists "1";': if line.strip() == 'APT::Periodic::Update-Package-Lists "1";':
print 'yes' print('yes')
sys.exit(0) sys.exit(0)
print 'no (APT::Periodic::Update-Package-Lists not "1")' print('no (APT::Periodic::Update-Package-Lists not "1")')
sys.exit(1) sys.exit(0)
print 'no' print('no (missing /etc/lsb-release file)')
sys.exit(1) sys.exit(0)
def config(): def config():
print 'graph_category security' print('graph_category security')
print 'graph_title %s' % (title) print('graph_title %s' % (title))
#print 'graph_total %s' % (total) print('graph_vlabel %s' % (vlabel))
print 'graph_vlabel %s' % (vlabel)
for i, archive in enumerate(archives + [other]): for i, archive in enumerate(archives + [other]):
if len(colour) > i: if len(colour) > i:
print '%s.colour %s' % (archive, colour[i]) print('%s.colour %s' % (archive, colour[i]))
if i < critical: if i < critical:
print '%s.critical 0:0' % (archive) print('%s.critical 0:0' % (archive))
if i == 0: if i == 0:
print '%s.draw AREA' % (archive) print('%s.draw AREA' % (archive))
else: else:
print '%s.draw STACK' % (archive) print('%s.draw STACK' % (archive))
print '%s.label %s' % (archive, archive) print('%s.label %s' % (archive, archive))
if i + 1 > critical: if i + 1 > critical:
print '%s.warning 0:0' % (archive) print('%s.warning 0:0' % (archive))
print 'total.colour 000000' print('total.colour 000000')
print 'total.draw LINE1' print('total.draw LINE1')
print 'total.label %s' % (total) print('total.label %s' % (total))
sys.exit(0) sys.exit(0)
def check_origin(pkg): def check_origin(pkg):
#print 'Checking: %s (%s)' % (pkg.name, map(str, pkg.candidateOrigin))
if pkg.candidate.origins: if pkg.candidate.origins:
for archive in archives: for archive in archives:
for origin in pkg.candidate.origins: for origin in pkg.candidate.origins:
#a = origin.archive.rpartition('-')[2]
a = origin.archive.split('-')[origin.archive.count('-')] a = origin.archive.split('-')[origin.archive.count('-')]
if a == archive and origin.origin in origins: if (a == archive) and (origin.origin in origins):
return a return a
return other return other
if len(sys.argv) > 1: if len(sys.argv) > 1:
if sys.argv[1] == 'autoconf': if sys.argv[1] == 'autoconf':
autoconf() autoconf()
@ -118,9 +118,11 @@ try:
import apt import apt
import apt_pkg import apt_pkg
except ImportError: except ImportError:
print "The module 'apt' is currently not installed. You can install it by typing:\nsudo apt-get install python-apt\nImportError: No module named apt" print("The module 'apt' is currently not installed. You can install it by typing:\n"
"sudo apt-get install python-apt\nImportError: No module named apt")
sys.exit(1) sys.exit(1)
pkgs = {} pkgs = {}
total = 0 total = 0
for pkg in apt.Cache(): for pkg in apt.Cache():
@ -130,6 +132,6 @@ for pkg in apt.Cache():
total += 1 total += 1
for archive in archives + [other]: for archive in archives + [other]:
print '%s.value %s' % (archive, pkgs.pop(archive, 0)) print('%s.value %s' % (archive, pkgs.pop(archive, 0)))
print 'total.value %s' % (total) print('total.value %s' % (total))