From e266ada5500346ec6e45703ead5592798871deff Mon Sep 17 00:00:00 2001 From: Ben Firshman Date: Wed, 30 Nov 2011 15:31:22 +0000 Subject: [PATCH 1/3] Added apt.py plugin to check for updated apt packages --- apt.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 apt.py diff --git a/apt.py b/apt.py new file mode 100644 index 0000000..d833431 --- /dev/null +++ b/apt.py @@ -0,0 +1,57 @@ +#!/usr/bin/python +# Cloudkick plugin to check for updated apt packages +# Requires python-apt package +# +# Copyright (C) 2010 by Ben Firshman +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: + +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. + +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +import apt + +DIST_UPGRADE = True +SECURITY_STATUS = 'err' +# If you do not want a warning for updates which aren't security updates, +# set this to 'ok' +CHANGED_STATUS = 'warn' + +cache = apt.Cache() +cache.update() +cache.open(None) +# Doesn't actually upgrade unless we call commit +cache.upgrade(DIST_UPGRADE) + +changed = cache.get_changes() +security = 0 + +for package in changed: + for origin in package.candidate.origins: + if 'security' in origin.archive: + security += 1 + +if security > 0: + print 'status %s %s packages with security updates' % (SECURITY_STATUS, security) +elif len(changed) > 0: + print 'status %s %s packages changed status' % (CHANGED_STATUS, len(changed)) +else: + print 'status ok 0 packages changed status' + +print 'metric changed int %s' % len(changed) +print 'metric security int %s' % security + + From e64caf23e076a9266dee3b1d06d7bcf99134ee2e Mon Sep 17 00:00:00 2001 From: Ben Firshman Date: Wed, 30 Nov 2011 15:57:22 +0000 Subject: [PATCH 2/3] Renamed apt.py to check_apt.py so import apt works --- apt.py => check_apt.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename apt.py => check_apt.py (100%) mode change 100644 => 100755 diff --git a/apt.py b/check_apt.py old mode 100644 new mode 100755 similarity index 100% rename from apt.py rename to check_apt.py From 8b3dfbebe6a7f134cd92e7721c7192a5337bcba3 Mon Sep 17 00:00:00 2001 From: Ben Firshman Date: Wed, 21 Dec 2011 15:55:50 +0000 Subject: [PATCH 3/3] Piggy back Ubuntu's update notifier --- check_apt.py => check_apt.sh | 48 ++++++++++++++---------------------- 1 file changed, 18 insertions(+), 30 deletions(-) rename check_apt.py => check_apt.sh (54%) diff --git a/check_apt.py b/check_apt.sh similarity index 54% rename from check_apt.py rename to check_apt.sh index d833431..95fd5c2 100755 --- a/check_apt.py +++ b/check_apt.sh @@ -1,6 +1,5 @@ -#!/usr/bin/python -# Cloudkick plugin to check for updated apt packages -# Requires python-apt package +#!/bin/bash +# Cloudkick plugin to check for updated apt packages on Ubuntu # # Copyright (C) 2010 by Ben Firshman # @@ -22,36 +21,25 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -import apt -DIST_UPGRADE = True -SECURITY_STATUS = 'err' -# If you do not want a warning for updates which aren't security updates, -# set this to 'ok' -CHANGED_STATUS = 'warn' +# Piggy back Ubuntu's cached update notices +UPDATES_AVAILABLE=`/usr/lib/update-notifier/update-motd-updates-available` +UPDATE_COUNT=`echo "$UPDATES_AVAILABLE" | awk 'NR==2 { print $1 }'` +SECURITY_COUNT=`echo "$UPDATES_AVAILABLE" | awk 'NR==3 { print $1 }'` +UPDATES_SINGLE_LINE=`echo "$UPDATES_AVAILABLE" | tr "\n" " "` -cache = apt.Cache() -cache.update() -cache.open(None) -# Doesn't actually upgrade unless we call commit -cache.upgrade(DIST_UPGRADE) +REBOOT_REQUIRED=`/usr/lib/update-notifier/update-motd-reboot-required` -changed = cache.get_changes() -security = 0 +if [ -n "$REBOOT_REQUIRED" ]; then + echo "status err $REBOOT_REQUIRED, $UPDATES_SINGLE_LINE" + exit +fi -for package in changed: - for origin in package.candidate.origins: - if 'security' in origin.archive: - security += 1 - -if security > 0: - print 'status %s %s packages with security updates' % (SECURITY_STATUS, security) -elif len(changed) > 0: - print 'status %s %s packages changed status' % (CHANGED_STATUS, len(changed)) -else: - print 'status ok 0 packages changed status' - -print 'metric changed int %s' % len(changed) -print 'metric security int %s' % security +if [ $SECURITY_COUNT -gt 0 ]; then + STATUS="err" +else + STATUS="ok" +fi +echo "status $STATUS $UPDATES_SINGLE_LINE"