root / plugins / other / apt_ubuntu @ 83159620
Historique | Voir | Annoter | Télécharger (4,25 ko)
| 1 | d5c54167 | Stefan Daniel Schwarz | #!/usr/bin/env python |
|---|---|---|---|
| 2 | # -*- encoding: iso-8859-1 -*- |
||
| 3 | # |
||
| 4 | # apt_ubuntu |
||
| 5 | # |
||
| 6 | # Plugin to monitor packages that should be installed on Ubuntu systems. |
||
| 7 | # |
||
| 8 | # Author: Stefan Daniel Schwarz <munin@wolfram.ravenwolf.de> |
||
| 9 | # |
||
| 10 | # v1.0 2008-11-07 - First draft |
||
| 11 | # v1.1 2008-11-08 - critical = #: First # critical, rest warning |
||
| 12 | # v1.2 2008-11-09 - Code cleanup for MuninExchange submission |
||
| 13 | # |
||
| 14 | # Usage: place in /etc/munin/plugins/ (or link it there using ln -s) |
||
| 15 | # |
||
| 16 | # Parameters understood: |
||
| 17 | # |
||
| 18 | # config (required) |
||
| 19 | # autoconf (optional - used by munin-config) |
||
| 20 | # |
||
| 21 | # Magic markers - optional - used by installation scripts and |
||
| 22 | # munin-config: |
||
| 23 | # |
||
| 24 | #%# capabilities=autoconf |
||
| 25 | #%# family=contrib |
||
| 26 | |||
| 27 | ########################################################### |
||
| 28 | category = 'system' # 'upgrades' |
||
| 29 | title = 'Upgradable packages' # 'Upgradeable packages' |
||
| 30 | vlabel = 'Total packages' |
||
| 31 | other = 'other' |
||
| 32 | total = 'total' |
||
| 33 | |||
| 34 | archives = ['security', 'updates', 'proposed', 'backports'] |
||
| 35 | colour = ['ff0000', '22ff22', '0022ff', '00aaaa', 'ff00ff'] |
||
| 36 | origins = ['Ubuntu'] |
||
| 37 | |||
| 38 | critical = 1 |
||
| 39 | ########################################################### |
||
| 40 | |||
| 41 | import os |
||
| 42 | import sys |
||
| 43 | import warnings |
||
| 44 | |||
| 45 | warnings.filterwarnings('ignore', 'apt API not stable yet', FutureWarning)
|
||
| 46 | |||
| 47 | def autoconf(): |
||
| 48 | if os.path.exists('/etc/lsb-release'):
|
||
| 49 | for line in open('/etc/lsb-release'):
|
||
| 50 | if line.strip() == 'DISTRIB_ID=Ubuntu': |
||
| 51 | try: |
||
| 52 | import apt |
||
| 53 | except ImportError: |
||
| 54 | print 'no (python-apt not installed)' |
||
| 55 | sys.exit(1) |
||
| 56 | cache = apt.Cache() |
||
| 57 | if not cache.has_key('update-notifier-common'):
|
||
| 58 | print 'no (update-notifier-common not found)' |
||
| 59 | sys.exit(1) |
||
| 60 | if not cache['update-notifier-common'].isInstalled: |
||
| 61 | print 'no (update-notifier-common not installed)' |
||
| 62 | sys.exit(1) |
||
| 63 | if not os.path.exists('/etc/apt/apt.conf.d/10periodic'):
|
||
| 64 | print 'no (/etc/apt/apt.conf.d/10periodic not found)' |
||
| 65 | sys.exit(1) |
||
| 66 | for line in open('/etc/apt/apt.conf.d/10periodic'):
|
||
| 67 | if line.strip() == 'APT::Periodic::Update-Package-Lists "1";': |
||
| 68 | print 'yes' |
||
| 69 | sys.exit(0) |
||
| 70 | print 'no (APT::Periodic::Update-Package-Lists not "1")' |
||
| 71 | sys.exit(1) |
||
| 72 | print 'no' |
||
| 73 | sys.exit(1) |
||
| 74 | |||
| 75 | def config(): |
||
| 76 | print 'graph_category %s' % (category) |
||
| 77 | print 'graph_title %s' % (title) |
||
| 78 | #print 'graph_total %s' % (total) |
||
| 79 | print 'graph_vlabel %s' % (vlabel) |
||
| 80 | for i, archive in enumerate(archives + [other]): |
||
| 81 | if len(colour) > i: |
||
| 82 | print '%s.colour %s' % (archive, colour[i]) |
||
| 83 | if i < critical: |
||
| 84 | print '%s.critical 0:0' % (archive) |
||
| 85 | if i == 0: |
||
| 86 | print '%s.draw AREA' % (archive) |
||
| 87 | else: |
||
| 88 | print '%s.draw STACK' % (archive) |
||
| 89 | print '%s.label %s' % (archive, archive) |
||
| 90 | if i + 1 > critical: |
||
| 91 | print '%s.warning 0:0' % (archive) |
||
| 92 | print 'total.colour 000000' |
||
| 93 | print 'total.draw LINE1' |
||
| 94 | print 'total.label %s' % (total) |
||
| 95 | sys.exit(0) |
||
| 96 | |||
| 97 | def check_origin(pkg): |
||
| 98 | #print 'Checking: %s (%s)' % (pkg.name, map(str, pkg.candidateOrigin)) |
||
| 99 | if pkg.candidateOrigin: |
||
| 100 | for archive in archives: |
||
| 101 | for origin in pkg.candidateOrigin: |
||
| 102 | #a = origin.archive.rpartition('-')[2]
|
||
| 103 | a = origin.archive.split('-')[origin.archive.count('-')]
|
||
| 104 | if a == archive and origin.origin in origins: |
||
| 105 | return a |
||
| 106 | return other |
||
| 107 | |||
| 108 | if len(sys.argv) > 1: |
||
| 109 | if sys.argv[1] == 'autoconf': |
||
| 110 | autoconf() |
||
| 111 | elif sys.argv[1] == 'config': |
||
| 112 | config() |
||
| 113 | elif sys.argv[1]: |
||
| 114 | print('unknown argument "' + sys.argv[1] + '"')
|
||
| 115 | sys.exit(1) |
||
| 116 | |||
| 117 | try: |
||
| 118 | import apt |
||
| 119 | except ImportError: |
||
| 120 | 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" |
||
| 121 | sys.exit(1) |
||
| 122 | |||
| 123 | pkgs = {}
|
||
| 124 | total = 0 |
||
| 125 | for pkg in apt.Cache(): |
||
| 126 | if pkg.isUpgradable: |
||
| 127 | a = check_origin(pkg) |
||
| 128 | pkgs[a] = pkgs.get(a, 0) + 1 |
||
| 129 | total += 1 |
||
| 130 | |||
| 131 | for archive in archives + [other]: |
||
| 132 | print '%s.value %s' % (archive, pkgs.pop(archive, 0)) |
||
| 133 | |||
| 134 | print 'total.value %s' % (total) |
