2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
10 # | Copyright Mathias Kettner 2012 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.
34 # These variable will be substituted at 'make dist' time
35 __version__
= "1.6.0i1"
40 if opt_check_interval
is not None:
41 check_interval
= "check_interval %d" % opt_check_interval
43 sys
.stdout
.write("""define host {
47 active_checks_enabled 0
48 passive_checks_enabled 1
55 active_checks_enabled 0
56 passive_checks_enabled 1
62 command_name check-livedump
63 command_line echo "WARN - You did an active check, but this check is passive" ; exit 1
71 """ % (check_interval
, check_interval
))
75 for key
, val
in row
.items():
76 if isinstance(val
, unicode):
77 row
[key
] = val
.encode("utf-8")
80 # .-Livedump-------------------------------------------------------------.
82 # | | | (_)_ _____ __| |_ _ _ __ ___ _ __ |
83 # | | | | \ \ / / _ \/ _` | | | | '_ ` _ \| '_ \ |
84 # | | |___| |\ V / __/ (_| | |_| | | | | | | |_) | |
85 # | |_____|_| \_/ \___|\__,_|\__,_|_| |_| |_| .__/ |
87 # +----------------------------------------------------------------------+
88 # | The actual livedump |
89 # '----------------------------------------------------------------------'
96 global g_connection
, opt_socket
98 # TODO: Fix the code and remove the pragma below!
99 if not opt_socket
and not omd_root(): # pylint: disable=used-before-assignment
100 bail_out("Please specify the URL of the livestatus socket.")
102 opt_socket
= "unix:%s/tmp/run/live" % omd_root()
104 g_connection
= livestatus
.SingleSiteConnection(opt_socket
)
107 def livedump_config():
108 if opt_dump_templates
:
111 check_commands
= set([])
113 def prepare_row(row
):
115 check_commands
.add(row
["check_command"])
116 row
["contactsstring"] = (",".join(row
["contacts"])).encode("utf-8")
117 if "contact_groups" in row
:
118 row
["contact_groups"] = (",".join(row
["contact_groups"])).encode("utf-8")
123 "Columns: name alias address groups check_command icon_image " \
124 "max_check_attempts contacts contact_groups\n" + \
126 opt_host_only_headers
127 for row
in g_connection
.query_table_assoc(query
):
129 row
["groupstring"] = ",".join(row
["groups"])
130 sys
.stdout
.write("define host {\n" " use livedump-host\n")
133 sys
.stdout
.write(" host_name %s%s\n" % (opt_prefix
, row
["name"]))
135 sys
.stdout
.write(" host_name %s\n" % row
["name"])
137 sys
.stdout
.write(" alias %(alias)s\n"
138 " address %(address)s\n"
139 " host_groups %(groupstring)s\n"
140 " check_command %(check_command)s\n"
141 " max_check_attempts %(max_check_attempts)d\n" % row
)
144 sys
.stdout
.write(" contacts %s\n" % row
["contactsstring"])
146 sys
.stdout
.write(" contact_groups %s\n" % row
["contact_groups"])
149 if row
.get("icon_image"):
150 sys
.stdout
.write(" icon_image %s\n" % row
["icon_image"])
152 sys
.stdout
.write("}\n\n")
154 # Dump service config
157 "Columns: host_name description groups check_command " \
158 "max_check_attempts contacts\n" + \
161 for row
in g_connection
.query_table_assoc(query
):
164 row
["groupstring"] = "service_groups " + ",".join(row
["groups"])
166 row
["groupstring"] = ""
167 row
["contactsstring"] = (",".join(row
["contacts"])).encode("utf-8")
168 sys
.stdout
.write("define service {\n" " use livedump-service\n")
171 sys
.stdout
.write(" host_name %s%s\n" % (opt_prefix
, row
["host_name"]))
173 sys
.stdout
.write(" host_name %s\n" % row
["host_name"])
175 sys
.stdout
.write(" description %(description)s\n"
177 " check_command check-livedump\n"
178 " contacts %(contactsstring)s\n"
179 " max_check_attempts %(max_check_attempts)d\n"
183 def livedump_state():
188 "Columns: name state plugin_output perf_data latency\n" \
190 + opt_host_only_headers
192 for row
in g_connection
.query_table_assoc(query
):
197 sys
.stdout
.write("host_name=%s%s" % (opt_prefix
, row
["name"]))
199 sys
.stdout
.write("host_name=%s" % row
["name"])
205 latency=%(latency).2f
207 finish_time=%(now).1f
208 return_code=%(state)d
209 output=%(plugin_output)s|%(perf_data)s
215 "Columns: host_name description state plugin_output perf_data latency\n" \
217 + opt_service_headers
219 for row
in g_connection
.query_table_assoc(query
):
224 sys
.stdout
.write("host_name=%s%s" % (opt_prefix
, row
["host_name"]))
226 sys
.stdout
.write("host_name=%s" % row
["host_name"])
229 service_description=%(description)s
233 latency=%(latency).2f
235 finish_time=%(now).1f
236 return_code=%(state)d
237 output=%(plugin_output)s|%(perf_data)s
243 # .-Helpers--------------------------------------------------------------.
245 # | | | | | ___| |_ __ ___ _ __ ___ |
246 # | | |_| |/ _ \ | '_ \ / _ \ '__/ __| |
247 # | | _ | __/ | |_) | __/ | \__ \ |
248 # | |_| |_|\___|_| .__/ \___|_| |___/ |
250 # +----------------------------------------------------------------------+
251 # | Various helper functions |
252 # '----------------------------------------------------------------------'
256 return os
.getenv("OMD_ROOT")
260 return os
.getenv("OMD_SITE")
265 sys
.stderr
.write("%s\n" % x
)
269 sys
.stderr
.write("%s\n" % x
)
274 sys
.stderr
.write("""Usage: %s [OPTION]...
276 -s, --socket S Connect to Livestatus-socket at S
277 -s tcp:10.11.0.55:6557
278 -s unix:/var/run/nagios/rw/live
280 -C, --config Dump configuration (instead of state)
282 -T, --dump-templates Also dump host/service templates
284 -M, --mark-mode Puts the mode (state/configuration dump) in the first
285 line for use with livedump-ssh-recv and similar scripts
287 -p, --prefix P Add a prefix P to hostnames. Use this option to dump live
288 data from multiple sites with duplicated hostnames
290 -O, --host-only-header H Add header H to host queries only (usually Filter: ...)
292 -H, --host-header H Add header H to host queries (usually Filter: ...)
293 This header is also used in service queries
295 -S, --service-header H Add header H to service queries (usually Filter: ...)
297 -i, --interval I Assume this check interval for hosts/services. This is
298 used in nagios config definition "check_interval".
299 Will be used for staleness checks on the server. This
300 option is only used when dumping the config together
301 with templates (-C -T)
303 -G, --include-groups Use contact groups instead of contacts on dumping config
305 --include-host-icon Add host icon_image to config
307 -V, --version Show version and exit
308 -h, --help Show this help
309 -v, --verbose Output debug information on stderr
310 --debug Do not catch Python exceptions
311 """ % os
.path
.split(sys
.argv
[0])[1])
315 sys
.stdout
.write("This is livedump version %s\n" % __version__
)
319 # .-main-----------------------------------------------------------------.
321 # | _ __ ___ __ _(_)_ __ |
322 # | | '_ ` _ \ / _` | | '_ \ |
323 # | | | | | | | (_| | | | | | |
324 # | |_| |_| |_|\__,_|_|_| |_| |
326 # +----------------------------------------------------------------------+
327 # | Main entry point, getopt, etc. |
328 # '----------------------------------------------------------------------'
330 short_options
= 'ChVTMp:s:O:H:S:i:vG'
332 "config", "help", "version", "dump-templates", "mark-mode", "prefix=", "socket=",
333 "host-only-header=", "host-header=", "service-header=", "interval=", "verbose",
334 "include-groups", "include-host-icon", "debug"
340 opt_mark_mode
= False
342 opt_host_headers
= ""
343 opt_host_only_headers
= ""
344 opt_service_headers
= ""
345 opt_dump_templates
= False
347 opt_host_icon
= False
348 opt_check_interval
= None
351 opts
, args
= getopt
.getopt(sys
.argv
[1:], short_options
, long_options
)
352 except getopt
.GetoptError
, err
:
353 sys
.stderr
.write("%s\n\n" % err
)
359 if o
in ['-h', '--help']:
362 elif o
in ['-V', '--version']:
367 elif o
in ['-T', '--dump-templates']:
368 opt_dump_templates
= True
369 elif o
in ['-M', '--mark-mode']:
371 elif o
in ['-p', '--prefix']:
373 elif o
in ['-s', '--socket']:
375 elif o
in ['-O', '--host-only-header']:
376 opt_host_only_headers
+= a
+ "\n"
377 elif o
in ['-H', '--host-header']:
378 opt_host_headers
+= a
+ "\n"
379 elif o
in ['-S', '--service-header']:
380 opt_service_headers
+= a
+ "\n"
381 elif o
in ['-i', '--interval']:
382 opt_check_interval
= int(a
)
383 elif o
in ['-v', '--verbose']:
385 elif o
in ['-G', '--include-groups']:
387 elif o
in ['--include-host-icon']:
396 if o
in ['-C', '--config']:
398 sys
.stdout
.write("config %s\n" % omd_site())
402 sys
.stdout
.write("status\n")