Skip to content

Instantly share code, notes, and snippets.

@susatadahiro
Created May 29, 2013 01:51
Show Gist options
  • Save susatadahiro/5667447 to your computer and use it in GitHub Desktop.
Save susatadahiro/5667447 to your computer and use it in GitHub Desktop.
munin plugins apt_ubuntu + reboot-required-packages
#!/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:
#
#%# capabilities=autoconf
#%# family=contrib
###########################################################
category = 'apt' # 'upgrades'
title = 'Upgradable packages' # 'Upgradeable packages'
vlabel = 'Total packages'
other = 'other'
total = 'total'
archives = ['security', 'updates', 'proposed', 'backports']
colour = ['ff0000', '22ff22', '0022ff', '00aaaa', 'ff00ff']
origins = ['Ubuntu']
critical = 1
###########################################################
import os
import sys
import warnings
warnings.filterwarnings('ignore', 'apt API not stable yet', FutureWarning)
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:
print 'no (python-apt not installed)'
sys.exit(1)
cache = apt.Cache()
if not cache.has_key('update-notifier-common'):
print 'no (update-notifier-common not found)'
sys.exit(1)
if not cache['update-notifier-common'].isInstalled:
print 'no (update-notifier-common not installed)'
sys.exit(1)
if not os.path.exists('/etc/apt/apt.conf.d/10periodic'):
print 'no (/etc/apt/apt.conf.d/10periodic not found)'
sys.exit(1)
for line in open('/etc/apt/apt.conf.d/10periodic'):
if line.strip() == 'APT::Periodic::Update-Package-Lists "1";':
print 'yes'
sys.exit(0)
print 'no (APT::Periodic::Update-Package-Lists not "1")'
sys.exit(1)
print 'no'
sys.exit(1)
def config():
print 'graph_category %s' % (category)
print 'graph_title %s' % (title)
#print 'graph_total %s' % (total)
print 'graph_vlabel %s' % (vlabel)
for i, archive in enumerate(archives + [other]):
if len(colour) > i:
print '%s.colour %s' % (archive, colour[i])
if i < critical:
print '%s.critical 0:0' % (archive)
if i == 0:
print '%s.draw AREA' % (archive)
else:
print '%s.draw STACK' % (archive)
print '%s.label %s' % (archive, archive)
if i + 1 > critical:
print '%s.warning 0:4' % (archive)
print 'total.colour 000000'
print 'total.draw LINE1'
print 'total.label %s' % (total)
print 'reboot-required-packages.draw LINE1'
print 'reboot-required-packages.label reboot-required.pkgs'
print 'reboot-required-packages.warning 0:0'
sys.exit(0)
def check_origin(pkg):
#print 'Checking: %s (%s)' % (pkg.name, map(str, pkg.candidateOrigin))
if pkg.candidateOrigin:
for archive in archives:
for origin in pkg.candidateOrigin:
#a = origin.archive.rpartition('-')[2]
a = origin.archive.split('-')[origin.archive.count('-')]
if a == archive and origin.origin in origins:
return a
return other
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
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"
sys.exit(1)
pkgs = {}
total = 0
for pkg in apt.Cache():
if pkg.isUpgradable:
a = check_origin(pkg)
pkgs[a] = pkgs.get(a, 0) + 1
total += 1
for archive in archives + [other]:
print '%s.value %s' % (archive, pkgs.pop(archive, 0))
print 'total.value %s' % (total)
if os.path.exists("/var/run/reboot-required.pkgs"):
print 'reboot-required-packages.value %s' % (len(open("/var/run/reboot-required.pkgs").readlines()))
else:
print 'reboot-required-packages.value 0'
@caroeber
Copy link

Also:
python ./apt_ubuntu autoconf
Traceback (most recent call last):
File "./apt_ubuntu", line 114, in
autoconf()
File "./apt_ubuntu", line 60, in autoconf
if not cache['update-notifier-common'].isInstalled:
AttributeError: 'Package' object has no attribute 'isInstalled'

@nextensible
Copy link

nextensible commented Oct 4, 2017

I saw the very same error under Ubuntu 14.04.5 LTS.
However, these errors are fixed in the official version: https://raw.githubusercontent.com/munin-monitoring/contrib/master/plugins/ubuntu/apt_ubuntu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment