Refactoring: Moved check parameters from unsorted.py to dedicated modules (CMK-1393)
[check_mk.git] / checks / apt
blobcbd4c9dca8c7aedab4f23bfbc58bac08f512eb42
1 #!/usr/bin/python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
10 # | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
11 # +------------------------------------------------------------------+
13 # This file is part of Check_MK.
14 # The official homepage is at http://mathias-kettner.de/check_mk.
16 # check_mk is free software; you can redistribute it and/or modify it
17 # under the terms of the GNU General Public License as published by
18 # the Free Software Foundation in version 2. check_mk is distributed
19 # in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
20 # out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
21 # PARTICULAR PURPOSE. See the GNU General Public License for more de-
22 # tails. You should have received a copy of the GNU General Public
23 # License along with GNU Make; see the file COPYING. If not, write
24 # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 # Boston, MA 02110-1301 USA.
27 # <<<apt:sep(0)>>>
28 # Inst dpkg [1.17.5ubuntu5.3] (1.17.5ubuntu5.4 Ubuntu:14.04/trusty-updates [amd64])
29 # Inst libtasn1-6-dev [3.4-3ubuntu0.1] (3.4-3ubuntu0.2 Ubuntu:14.04/trusty-updates [amd64]) []
30 # Inst libtasn1-6 [3.4-3ubuntu0.1] (3.4-3ubuntu0.2 Ubuntu:14.04/trusty-updates [amd64])
31 # Inst ntpdate [1:4.2.6.p5+dfsg-3ubuntu2.14.04.2] (1:4.2.6.p5+dfsg-3ubuntu2.14.04.3 Ubuntu:14.04/trusty-security [amd64])
32 # Inst udev [204-5ubuntu20.10] (204-5ubuntu20.11 Ubuntu:14.04/trusty-updates [amd64]) []
33 # Inst libudev1 [204-5ubuntu20.10] (204-5ubuntu20.11 Ubuntu:14.04/trusty-updates [amd64])
34 # Inst libpam-systemd [204-5ubuntu20.10] (204-5ubuntu20.11 Ubuntu:14.04/trusty-updates [amd64]) []
35 # Inst systemd-services [204-5ubuntu20.10] (204-5ubuntu20.11 Ubuntu:14.04/trusty-updates [amd64]) []
36 # Inst libsystemd-daemon0 [204-5ubuntu20.10] (204-5ubuntu20.11 Ubuntu:14.04/trusty-updates [amd64])
37 # Inst libsystemd-login0 [204-5ubuntu20.10] (204-5ubuntu20.11 Ubuntu:14.04/trusty-updates [amd64])
38 # Inst libpolkit-gobject-1-0 [0.105-4ubuntu2] (0.105-4ubuntu2.14.04.1 Ubuntu:14.04/trusty-updates [amd64])
39 # Inst libxext-dev [2:1.3.2-1] (2:1.3.2-1ubuntu0.0.14.04.1 Ubuntu:14.04/trusty-security [amd64]) []
41 factory_settings["apt_default_levels"] = {
42 "normal": 1,
43 "security": 2,
47 # Check that the apt section is in valid format of mk_apt plugin and not
48 # from the apt agent plugin which can be found on the Check_MK exchange.
49 def apt_valid_info(info):
50 return info and len(info[0]) == 1 and \
51 (len(info[0][0].split(None, 3)) == 4 or info[0][0] == "No updates pending for installation")
54 def inventory_apt(info):
55 if apt_valid_info(info):
56 yield None, {}
59 def check_apt(_no_item, params, info):
60 if not apt_valid_info(info):
61 return
63 if info[0][0] == "No updates pending for installation":
64 yield 0, "No updates pending for installation"
65 return
67 sec_regex = regex("^[^\\(]*\\(.* (Debian-Security:|Ubuntu:[^/]*/[^-]*-security)")
68 updates = []
69 sec_updates = []
70 for line in info:
71 if not line[0].startswith("Inst"):
72 continue
73 _inst, packet, _version, _hints = line[0].split(None, 3)
74 if sec_regex.match(line[0]):
75 sec_updates.append(packet)
76 else:
77 updates.append(packet)
79 state = 0
80 if updates:
81 state = params["normal"]
82 yield state, "%d normal updates" % len(updates), [("normal_updates", len(updates))]
84 state = 0
85 if sec_updates:
86 state = params["security"]
87 text = " (%s)" % ", ".join(sec_updates)
88 else:
89 text = ""
91 yield state, "%d security updates%s" % (len(sec_updates), text), [("security_updates",
92 len(sec_updates))]
95 check_info['apt'] = {
96 "inventory_function": inventory_apt,
97 "check_function": check_apt,
98 "service_description": "APT Updates",
99 "group": "apt",
100 "has_perfdata": True,
101 "default_levels_variable": "apt_default_levels",