Module: check_mk
Branch: master
Commit: c0a0207a3e094dcf4988cf81f72c15a1dcbcde84
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=c0a0207a3e094d…
Author: Andreas Boesl <ab(a)mathias-kettner.de>
Date: Mon Dec 4 09:45:36 2017 +0100
5247 cmk --list-hosts now supports the additional options --all-sites, --include-offline
Change-Id: I930728c37367d6b19d370e054b1feca435cfb926
---
.werks/5247 | 10 ++++++++++
cmk_base/config.py | 9 +++++++++
cmk_base/modes/check_mk.py | 48 ++++++++++++++++++++++++++++++++++------------
3 files changed, 55 insertions(+), 12 deletions(-)
diff --git a/.werks/5247 b/.werks/5247
new file mode 100644
index 0000000..f874226
--- /dev/null
+++ b/.werks/5247
@@ -0,0 +1,10 @@
+Title: cmk --list-hosts now supports the additional options --all-sites,
--include-offline
+Level: 1
+Component: checks
+Compatible: compat
+Edition: cre
+Version: 1.5.0i2
+Date: 1512377109
+Class: feature
+
+
diff --git a/cmk_base/config.py b/cmk_base/config.py
index 14a4307..a6bd5f3 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -528,6 +528,15 @@ def all_offline_hosts():
if not rulesets.in_binary_hostlist(hostname, only_hosts) ]
+
+def all_configured_offline_hosts():
+ hostlist = all_configured_realhosts().union(all_configured_clusters())
+
+ return set([ hostname for hostname in hostlist
+ if not rulesets.in_binary_hostlist(hostname, only_hosts) ])
+
+
+
#.
# .--Hosts---------------------------------------------------------------.
# | _ _ _ |
diff --git a/cmk_base/modes/check_mk.py b/cmk_base/modes/check_mk.py
index 5a455b2..186708e 100644
--- a/cmk_base/modes/check_mk.py
+++ b/cmk_base/modes/check_mk.py
@@ -161,6 +161,7 @@ modes.register_general_option(Option(
#.
+#.
# .--list-hosts----------------------------------------------------------.
# | _ _ _ _ _ |
# | | (_)___| |_ | |__ ___ ___| |_ ___ |
@@ -170,25 +171,38 @@ modes.register_general_option(Option(
# | |
# '----------------------------------------------------------------------'
-def mode_list_hosts(args):
- hosts = _list_all_hosts(args)
+def mode_list_hosts(options, args):
+ hosts = _list_all_hosts(args, options)
console.output("\n".join(hosts))
if hosts:
console.output("\n")
# TODO: Does not care about internal group "check_mk"
-def _list_all_hosts(hostgroups):
- hostlist = []
+def _list_all_hosts(hostgroups, options):
+
+ hostnames = set()
+
+ if options.get("all-sites"):
+ hostnames.update(config.all_configured_hosts()) # Return all hosts, including
offline
+ if not "include-offline" in options:
+ hostnames -= config.all_configured_offline_hosts()
+ else:
+ hostnames.update(config.all_active_hosts())
+ if "include-offline" in options:
+ hostnames.update(config.all_offline_hosts())
+
+
+ if not hostgroups:
+ return sorted(hostnames)
- for hn in config.all_active_hosts():
- if not hostgroups:
- hostlist.append(hn)
- else:
- for hg in config.hostgroups_of(hn):
- if hg in hostgroups:
- hostlist.append(hn)
- break
+
+ hostlist = []
+ for hn in hostnames:
+ for hg in config.hostgroups_of(hn):
+ if hg in hostgroups:
+ hostlist.append(hn)
+ break
return sorted(hostlist)
@@ -205,6 +219,16 @@ modes.register(Mode(
"Called without argument lists all hosts. You may "
"specify one or more host groups to restrict the output to hosts "
"that are in at least one of those groups.",
+ ],
+ sub_options=[
+ Option(
+ long_option="all-sites",
+ short_help="Include hosts of foreign sites",
+ ),
+ Option(
+ long_option="include-offline",
+ short_help="Include offline hosts",
+ ),
]
))