Cleanup all direct config.service_extra_conf calls
[check_mk.git] / cmk_base / dump_host.py
blobe646b4df2c14def7c5c808ce90249ec75bcf2bcb
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 import time
29 import cmk.utils.tty as tty
30 import cmk.utils.render
32 import cmk_base.utils
33 import cmk_base.config as config
34 import cmk_base.core_config as core_config
35 import cmk_base.console as console
36 import cmk_base.data_sources as data_sources
37 import cmk_base.ip_lookup as ip_lookup
38 import cmk_base.check_table as check_table
39 import cmk_base.checking as checking
42 def dump_host(hostname):
43 config_cache = config.get_config_cache()
44 host_config = config_cache.get_host_config(hostname)
46 console.output("\n")
47 if config.is_cluster(hostname):
48 color = tty.bgmagenta
49 add_txt = " (cluster of " + (", ".join(config.nodes_of(hostname))) + ")"
50 else:
51 color = tty.bgblue
52 add_txt = ""
53 console.output(
54 "%s%s%s%-78s %s\n" % (color, tty.bold, tty.white, hostname + add_txt, tty.normal))
56 ipaddress = _ip_address_for_dump_host(hostname)
58 addresses = ""
59 if not config.is_ipv4v6_host(hostname):
60 addresses = ipaddress
61 else:
62 ipv6_primary = config.is_ipv6_primary(hostname)
63 try:
64 if ipv6_primary:
65 secondary = _ip_address_for_dump_host(hostname, 4)
66 else:
67 secondary = _ip_address_for_dump_host(hostname, 6)
68 except:
69 secondary = "X.X.X.X"
71 addresses = "%s, %s" % (ipaddress, secondary)
72 if ipv6_primary:
73 addresses += " (Primary: IPv6)"
74 else:
75 addresses += " (Primary: IPv4)"
77 console.output(tty.yellow + "Addresses: " + tty.normal +
78 (addresses if addresses is not None else "No IP") + "\n")
80 tag_template = tty.bold + "[" + tty.normal + "%s" + tty.bold + "]" + tty.normal
81 tags = [
82 (tag_template % ":".join(t)) for t in sorted(config_cache.tags_of_host(hostname).items())
84 console.output(tty.yellow + "Tags: " + tty.normal + ", ".join(tags) + "\n")
85 if config.is_cluster(hostname):
86 parents_list = config.nodes_of(hostname)
87 else:
88 parents_list = config.parents_of(hostname)
89 if len(parents_list) > 0:
90 console.output(tty.yellow + "Parents: " + tty.normal +
91 ", ".join(parents_list) + "\n")
92 console.output(tty.yellow + "Host groups: " + tty.normal +
93 cmk_base.utils.make_utf8(", ".join(config.hostgroups_of(hostname))) + "\n")
94 console.output(tty.yellow + "Contact groups: " + tty.normal +
95 cmk_base.utils.make_utf8(", ".join(config.contactgroups_of(hostname))) + "\n")
97 agenttypes = []
98 sources = data_sources.DataSources(hostname, ipaddress)
99 for source in sources.get_data_sources():
100 agenttypes.append(source.describe())
102 if host_config.is_ping_host:
103 agenttypes.append('PING only')
105 console.output(tty.yellow + "Agent mode: " + tty.normal)
106 console.output(sources.describe_data_sources() + "\n")
108 console.output(tty.yellow + "Type of agent: " + tty.normal)
109 if len(agenttypes) == 1:
110 console.output(agenttypes[0] + "\n")
111 else:
112 console.output("\n ")
113 console.output("\n ".join(agenttypes) + "\n")
115 console.output(tty.yellow + "Services:" + tty.normal + "\n")
116 check_items = check_table.get_sorted_check_table(hostname)
118 headers = ["checktype", "item", "params", "description", "groups"]
119 colors = [tty.normal, tty.blue, tty.normal, tty.green, tty.normal]
120 if config.service_dependencies != []:
121 headers.append("depends on")
122 colors.append(tty.magenta)
124 tty.print_table(headers, colors, [[
125 checktype,
126 cmk_base.utils.make_utf8(item),
127 _evaluate_params(params),
128 cmk_base.utils.make_utf8(description),
129 cmk_base.utils.make_utf8(",".join(
130 config_cache.service_extra_conf(hostname, description, config.service_groups))),
131 ",".join(deps)
132 ] for checktype, item, params, description, deps in check_items], " ")
135 def _evaluate_params(params):
136 if not isinstance(params, cmk_base.config.TimespecificParamList):
137 return params
139 current_params = checking.determine_check_params(params)
140 return "Timespecific parameters at %s: %r" % (cmk.utils.render.date_and_time(time.time()),
141 current_params)
144 def _ip_address_for_dump_host(hostname, family=None):
145 if config.is_cluster(hostname):
146 try:
147 ipaddress = ip_lookup.lookup_ip_address(hostname, family)
148 except:
149 ipaddress = ""
150 else:
151 try:
152 ipaddress = ip_lookup.lookup_ip_address(hostname, family)
153 except:
154 ipaddress = core_config.fallback_ip_for(hostname, family)
155 return ipaddress