====== APT Check ====== Benötigt: **python-apt** =====Client===== #!/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() =====Server===== #!/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 # def inventory_apt_upgrades(checkname, info): if len(info) >= 1: return [(None, "", None)] return [] def check_apt_upgrades(item, params, info): level = 0 # 0:OK, 1:WARNING 2:CRITICAL, 3: UNKNOWN updates = [] for line in info: # if we don't have any upgrades listed, the line list # will only contain one element, eg. "upgrades\n" if len(line) < 2: continue # there are upgrades availiable, evaluate the importency. #secupgrades return CRITICAL, normal upgrades only WARNING. type = line[0] pkgs = line[1].strip("\"").split(",") if len(pkgs) > 0: if level < 2 and type == "secupgrades": level = 2 if level < 1 and type == "upgrades": level = 1 updates.extend(pkgs) # Construct a the status message. if level == 0: msg = "OK - All packages are up to date." elif level == 1: msg = "%s packages available for upgrade: %s" % (len(updates), ",".join(updates)) elif level == 2: msg = "%s packages available for upgrade: %s" % (len(updates), ",".join(updates)) return (level, msg) # declare the check to Check_MK. Dict format: # ( check_function, "service name", perf_data_provided?(0:1), inventory_function) check_info['apt.upgrades'] = \ (check_apt_upgrades, "apt.upgrades", 0, inventory_apt_upgrades)