#!/usr/bin/env python # # Check_MK APT Plugin - Check for upgradeable packages. # # Copyright 2010, Stefan Schlesinger # # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # # Mod damit es mit Debian UND Ubuntu funktioniert 03/2011 Matthias Henze # Mod damit Packages die auf "keep" stehen ignoriert werden 08/2011 Matthias Henze # Mod fuer neue APT-API v0.8.0 02/2014 Matthias Henze # Mod fuer neue APT-API v0.8.8i.2 06/2014 Matthias Henze import sys import os import datetime import warnings warnings.filterwarnings("ignore") from pprint import pprint try: import apt except ImportError: sys.exit class AptCache(apt.Cache): # apt-cache update interval in seconds update_interval = 3600 # file to check against update_last_file = "/var/tmp/checkmk_apt_timestamp" def __init__(self): apt.Cache.__init__(self) self.update_cache() # automatically update the apt cache once a hour def update_cache(self): delta = self.last_cache_update(self.update_last_file) if not delta or delta.seconds > self.update_interval: self.update() self.open(None) # update the timestamp self.update_timestamp(self.update_last_file) def last_cache_update(self, file): # return false, if we don't have the file yet if not os.path.isfile(file): return False now = datetime.datetime.now() mtime = datetime.datetime.fromtimestamp(os.path.getmtime(file)) delta = now - mtime return delta def update_timestamp(self, file): open(file, 'w').close() return True def get_upgradeable(): pkgs_to_upgrade = [] pkgs_to_sec_upgrade = [] cache = AptCache() for pkg in cache: if pkg.is_upgradable: name = pkg.name candidate = pkgcand.source_name if candidate == "Debian-Security": pkgs_to_sec_upgrade.append(name) elif candidate.find("security") > -1: pkgs_to_sec_upgrade.append(name) else: pkgs_to_upgrade.append(name) return [pkgs_to_upgrade, pkgs_to_sec_upgrade] def main(): upgrades, secupgrades = get_upgradeable() # print the beginning of the plugin header print "<<>>" if len(upgrades) > 0: print "upgrades \"%s\"" % ",".join(upgrades) else: print "upgrades" if len(secupgrades) > 0: print "secupgrades \"%s\"" % ",".join(secupgrades) else: print "secupgrades" if __name__ == "__main__": main()