#!/usr/bin/python

# Copyright 2008, Peter Poeml <poeml@suse.de>, Novell/SUSE
# License: GPL

import sys, subprocess

# Standard Nagios return codes
OK       = 0
WARNING  = 1
CRITICAL = 2
UNKNOWN  = 3


#status = subprocess.call('yum check-update &>/dev/null', shell=True)

try:
    status = open('/var/run/yum-check-update-status').read().strip()
except:
    print 'Error reading /var/run/yum-check-update-status'
    sys.exit(UNKNOWN)

try:
    status = int(status)
except:
    print 'Status in /var/run/yum-check-update-status is not a number'
    sys.exit(UNKNOWN)

try:
    pkgs = []
    lines = open('/var/run/yum-check-update').read()
    lines = lines.split('\n Package ')[1]
    lines = lines.split('\nTransaction Summary')[0]
    lines = lines.splitlines()
    lines = [i for i in lines if i.startswith(' ') and not i.startswith('  ')]
    lines = [i.split()[0] for i in lines]
    #print '\n'.join(lines)
    pkgs = ','.join(lines)
    
except:
    pkgs = '(could not parse package list)'


if status == 0:
    print 'System completely up to date.'
    sys.exit(OK)
elif status == 100:
    print 'There are updates:', pkgs
    sys.exit(WARNING)
else:
    sys.exit(UNKNOWN)

# vim: ai ts=4 sw=4 smarttab expandtab smarttab
