Refactoring: Moved check parameters from unsorted.py to dedicated modules (CMK-1393)
[check_mk.git] / checks / isc_dhcpd
bloba07ec320b61d28c87cb3113b9c16c386f49e46fd
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 isc_dhcpd_default_levels = (15.0, 5.0)
29 # Example output from agent:
30 # <<<isc_dhcpd>>>
31 # [general]
32 # PID: 3670
33 # [pools]
34 # 10.0.1.1 10.0.1.254
35 # [leases]
36 # 10.0.1.16
37 # 10.0.1.24
38 # 10.0.1.26
39 # 10.0.1.27
40 # 10.0.1.34
41 # 10.0.1.36
42 # 10.0.1.45
43 # 10.0.1.50
44 # 10.0.1.53
45 # 10.0.1.57
48 def parse_isc_dhcpd(info):
49 def ip_to_number(ip):
50 number = 0
51 factor = 1
52 for part in ip.split('.')[::-1]:
53 number += factor * int(part)
54 factor *= 256
55 return number
57 parsed = {
58 "pids": [],
59 "pools": {},
60 "leases": [],
63 mode = None
64 for line in info:
65 if line[0] == '[general]':
66 mode = "general"
67 elif line[0] == '[pools]':
68 mode = "pools"
69 elif line[0] == '[leases]':
70 mode = "leases"
72 elif mode == "general":
73 if line[0] == "PID:":
74 parsed["pids"] = map(int, line[1:])
76 elif mode == "pools":
77 if 'bootp' in line[0]:
78 line = line[1:]
79 start, end = line[0], line[1]
80 item = "%s-%s" % (start, end)
81 parsed["pools"][item] = (ip_to_number(start), ip_to_number(end))
83 elif mode == "leases":
84 parsed["leases"].append(ip_to_number(line[0]))
86 return parsed
89 def inventory_isc_dhcpd(parsed):
90 return [(item, "isc_dhcpd_default_levels") for item in parsed["pools"]]
93 def check_isc_dhcpd(item, params, parsed):
94 if len(parsed["pids"]) == 0:
95 yield 2, "DHCP Daemon not running"
96 elif len(parsed["pids"]) > 1:
97 yield 1, "DHCP Daemon running %d times (PIDs: %s)" % (len(parsed["pids"]), ", ".join(
98 map(str, parsed["pids"])))
100 if item not in parsed["pools"]:
101 return
103 range_from, range_to = parsed["pools"][item]
104 num_leases = range_to - range_from + 1
105 num_used = 0
106 for lease_dec in parsed["leases"]:
107 if lease_dec >= range_from and lease_dec <= range_to:
108 num_used += 1
110 for check_result in check_dhcp_pools_levels(num_leases - num_used, num_used, None, num_leases,
111 params):
112 yield check_result
115 check_info["isc_dhcpd"] = {
116 'parse_function': parse_isc_dhcpd,
117 'inventory_function': inventory_isc_dhcpd,
118 'check_function': check_isc_dhcpd,
119 'service_description': 'DHCP Pool %s',
120 'group': 'win_dhcp_pools',
121 'has_perfdata': True,
122 'includes': ["dhcp_pools.include"],