2008-11-10 00:00:00 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- encoding: iso-8859-1 -*-
|
|
|
|
#
|
|
|
|
# apt_ubuntu
|
|
|
|
#
|
|
|
|
# Plugin to monitor packages that should be installed on Ubuntu systems.
|
|
|
|
#
|
|
|
|
# Author: Stefan Daniel Schwarz <munin@wolfram.ravenwolf.de>
|
|
|
|
#
|
|
|
|
# v1.0 2008-11-07 - First draft
|
|
|
|
# v1.1 2008-11-08 - critical = #: First # critical, rest warning
|
|
|
|
# v1.2 2008-11-09 - Code cleanup for MuninExchange submission
|
|
|
|
#
|
|
|
|
# Usage: place in /etc/munin/plugins/ (or link it there using ln -s)
|
|
|
|
#
|
|
|
|
# Parameters understood:
|
|
|
|
#
|
|
|
|
# config (required)
|
|
|
|
# autoconf (optional - used by munin-config)
|
|
|
|
#
|
|
|
|
# Magic markers - optional - used by installation scripts and
|
|
|
|
# munin-config:
|
|
|
|
#
|
2018-03-28 05:57:02 +02:00
|
|
|
# #%# capabilities=autoconf
|
|
|
|
# #%# family=contrib
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import warnings
|
2008-11-10 00:00:00 +01:00
|
|
|
|
|
|
|
###########################################################
|
2018-03-28 05:57:02 +02:00
|
|
|
title = 'Upgradable packages' # 'Upgradeable packages'
|
2008-11-10 00:00:00 +01:00
|
|
|
vlabel = 'Total packages'
|
|
|
|
other = 'other'
|
|
|
|
total = 'total'
|
|
|
|
|
|
|
|
archives = ['security', 'updates', 'proposed', 'backports']
|
|
|
|
colour = ['ff0000', '22ff22', '0022ff', '00aaaa', 'ff00ff']
|
|
|
|
origins = ['Ubuntu']
|
|
|
|
|
|
|
|
critical = 1
|
|
|
|
###########################################################
|
|
|
|
|
|
|
|
warnings.filterwarnings('ignore', 'apt API not stable yet', FutureWarning)
|
|
|
|
|
2018-03-28 05:57:02 +02:00
|
|
|
|
2008-11-10 00:00:00 +01:00
|
|
|
def autoconf():
|
|
|
|
if os.path.exists('/etc/lsb-release'):
|
|
|
|
for line in open('/etc/lsb-release'):
|
|
|
|
if line.strip() == 'DISTRIB_ID=Ubuntu':
|
|
|
|
try:
|
|
|
|
import apt
|
|
|
|
except ImportError:
|
2018-03-28 05:57:02 +02:00
|
|
|
print('no (python-apt not installed)')
|
|
|
|
sys.exit(0)
|
2008-11-10 00:00:00 +01:00
|
|
|
cache = apt.Cache()
|
2018-03-28 05:57:02 +02:00
|
|
|
if 'update-notifier-common' not in cache:
|
|
|
|
print('no (update-notifier-common not found)')
|
|
|
|
sys.exit(0)
|
2008-11-10 00:00:00 +01:00
|
|
|
if not cache['update-notifier-common'].isInstalled:
|
2018-03-28 05:57:02 +02:00
|
|
|
print('no (update-notifier-common not installed)')
|
|
|
|
sys.exit(0)
|
2008-11-10 00:00:00 +01:00
|
|
|
if not os.path.exists('/etc/apt/apt.conf.d/10periodic'):
|
2018-03-28 05:57:02 +02:00
|
|
|
print('no (/etc/apt/apt.conf.d/10periodic not found)')
|
2008-11-10 00:00:00 +01:00
|
|
|
sys.exit(1)
|
|
|
|
for line in open('/etc/apt/apt.conf.d/10periodic'):
|
|
|
|
if line.strip() == 'APT::Periodic::Update-Package-Lists "1";':
|
2018-03-28 05:57:02 +02:00
|
|
|
print('yes')
|
2008-11-10 00:00:00 +01:00
|
|
|
sys.exit(0)
|
2018-03-28 05:57:02 +02:00
|
|
|
print('no (APT::Periodic::Update-Package-Lists not "1")')
|
|
|
|
sys.exit(0)
|
|
|
|
print('no (missing /etc/lsb-release file)')
|
|
|
|
sys.exit(0)
|
|
|
|
|
2008-11-10 00:00:00 +01:00
|
|
|
|
|
|
|
def config():
|
2018-03-28 05:57:02 +02:00
|
|
|
print('graph_category security')
|
|
|
|
print('graph_title %s' % (title))
|
|
|
|
print('graph_vlabel %s' % (vlabel))
|
2008-11-10 00:00:00 +01:00
|
|
|
for i, archive in enumerate(archives + [other]):
|
|
|
|
if len(colour) > i:
|
2018-03-28 05:57:02 +02:00
|
|
|
print('%s.colour %s' % (archive, colour[i]))
|
2008-11-10 00:00:00 +01:00
|
|
|
if i < critical:
|
2018-03-28 05:57:02 +02:00
|
|
|
print('%s.critical 0:0' % (archive))
|
2008-11-10 00:00:00 +01:00
|
|
|
if i == 0:
|
2018-03-28 05:57:02 +02:00
|
|
|
print('%s.draw AREA' % (archive))
|
2008-11-10 00:00:00 +01:00
|
|
|
else:
|
2018-03-28 05:57:02 +02:00
|
|
|
print('%s.draw STACK' % (archive))
|
|
|
|
print('%s.label %s' % (archive, archive))
|
2008-11-10 00:00:00 +01:00
|
|
|
if i + 1 > critical:
|
2018-03-28 05:57:02 +02:00
|
|
|
print('%s.warning 0:0' % (archive))
|
|
|
|
print('total.colour 000000')
|
|
|
|
print('total.draw LINE1')
|
|
|
|
print('total.label %s' % (total))
|
2008-11-10 00:00:00 +01:00
|
|
|
sys.exit(0)
|
|
|
|
|
2018-03-28 05:57:02 +02:00
|
|
|
|
2008-11-10 00:00:00 +01:00
|
|
|
def check_origin(pkg):
|
2014-05-23 18:34:17 +02:00
|
|
|
if pkg.candidate.origins:
|
2008-11-10 00:00:00 +01:00
|
|
|
for archive in archives:
|
2014-05-23 18:34:17 +02:00
|
|
|
for origin in pkg.candidate.origins:
|
2008-11-10 00:00:00 +01:00
|
|
|
a = origin.archive.split('-')[origin.archive.count('-')]
|
2018-03-28 05:57:02 +02:00
|
|
|
if (a == archive) and (origin.origin in origins):
|
2008-11-10 00:00:00 +01:00
|
|
|
return a
|
|
|
|
return other
|
|
|
|
|
2018-03-28 05:57:02 +02:00
|
|
|
|
2008-11-10 00:00:00 +01:00
|
|
|
if len(sys.argv) > 1:
|
|
|
|
if sys.argv[1] == 'autoconf':
|
|
|
|
autoconf()
|
|
|
|
elif sys.argv[1] == 'config':
|
|
|
|
config()
|
|
|
|
elif sys.argv[1]:
|
|
|
|
print('unknown argument "' + sys.argv[1] + '"')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
try:
|
|
|
|
import apt
|
2018-02-21 16:06:04 +01:00
|
|
|
import apt_pkg
|
2008-11-10 00:00:00 +01:00
|
|
|
except ImportError:
|
2018-03-28 05:57:02 +02:00
|
|
|
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")
|
2008-11-10 00:00:00 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
2018-03-28 05:57:02 +02:00
|
|
|
|
2008-11-10 00:00:00 +01:00
|
|
|
pkgs = {}
|
|
|
|
total = 0
|
|
|
|
for pkg in apt.Cache():
|
2018-02-21 16:06:04 +01:00
|
|
|
if (pkg.is_upgradable) and (pkg._pkg.selected_state != apt_pkg.SELSTATE_HOLD):
|
2008-11-10 00:00:00 +01:00
|
|
|
a = check_origin(pkg)
|
|
|
|
pkgs[a] = pkgs.get(a, 0) + 1
|
|
|
|
total += 1
|
|
|
|
|
|
|
|
for archive in archives + [other]:
|
2018-03-28 05:57:02 +02:00
|
|
|
print('%s.value %s' % (archive, pkgs.pop(archive, 0)))
|
2008-11-10 00:00:00 +01:00
|
|
|
|
2018-03-28 05:57:02 +02:00
|
|
|
print('total.value %s' % (total))
|