This file is indexed.

/usr/share/check_mk/modules/automation.py is in check-mk-server 1.2.2p3-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2013             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# ails.  You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.


class MKAutomationError(Exception):
    def __init__(self, reason):
        self.reason = reason
    def __str__(self):
        return self.reason

def do_automation(cmd, args):
    try:
        if cmd == "get-configuration":
            read_config_files(with_autochecks=False, with_conf_d=False)
            result = automation_get_configuration()
        elif cmd == "get-check-information":
            result = automation_get_check_information()
        elif cmd == "delete-host":
            read_config_files(with_autochecks=False)
            result = automation_delete_host(args)
        else:
            read_config_files()
            if cmd == "try-inventory":
                result = automation_try_inventory(args)
            elif cmd == "inventory":
                result = automation_inventory(args)
            elif cmd == "get-autochecks":
                result = automation_get_autochecks(args)
            elif cmd == "set-autochecks":
                result = automation_set_autochecks(args)
            elif cmd == "reload":
                result = automation_restart("reload")
            elif cmd == "restart":
                result = automation_restart("restart")
            elif cmd == "scan-parents":
                result = automation_scan_parents(args)
            else:
                raise MKAutomationError("Automation command '%s' is not implemented." % cmd)

    except MKAutomationError, e:
        sys.stderr.write("%s\n" % e)
        if opt_debug:
            raise
        output_profile()
        sys.exit(1)

    except Exception, e:
        if opt_debug:
            raise
        else:
            sys.stderr.write("%s\n" % e)
            output_profile()
            sys.exit(2)

    if opt_debug:
        import pprint
        sys.stdout.write(pprint.pformat(result)+"\n")
    else:
        sys.stdout.write("%r\n" % (result,))
    output_profile()
    sys.exit(0)

# Does inventory for *one* host. Possible values for how:
# "new" - find only new services (like -I)
# "remove" - remove exceeding services
# "fixall" - find new, remove exceeding
# "refresh" - drop all services and reinventorize
def automation_inventory(args):
    if len(args) < 2:
        raise MKAutomationError("Need two arguments: [new|remove|fixall|refresh] HOSTNAME")

    how = args[0]
    hostname = args[1]

    count_added = 0
    count_removed = 0
    count_kept = 0
    if how == "refresh":
        count_removed = remove_autochecks_of(hostname) # checktype could be added here
        reread_autochecks()

    # Compute current state of new and existing checks
    table = automation_try_inventory([hostname])
    # Create new list of checks
    new_items = []
    for entry in table:
        state_type, ct, checkgroup, item, paramstring = entry[:5]
        if state_type in [ "legacy", "active", "manual", "ignored" ]:
            continue # this is not an autocheck or ignored and currently not checked

        if state_type == "new":
            if how in [ "new", "fixall", "refresh" ]:
                count_added += 1
                new_items.append((ct, item, paramstring))

        elif state_type == "old":
            # keep currently existing valid services in any case
            new_items.append((ct, item, paramstring))
            count_kept += 1

        elif state_type in [ "obsolete", "vanished" ]:
            # keep item, if we are currently only looking for new services
            # otherwise fix it: remove ignored and non-longer existing services
            if how not in [ "fixall", "remove" ]:
                new_items.append((ct, item, paramstring))
                count_kept += 1
            else:
                count_removed += 1

    automation_write_autochecks_file(hostname, new_items)
    return (count_added, count_removed, count_kept, len(new_items))


def automation_try_inventory(args):
    global opt_use_cachefile, inventory_max_cachefile_age, check_max_cachefile_age
    if args[0] == '--cache':
        opt_use_cachefile = True
        check_max_cachefile_age = 1000000000
        inventory_max_cachefile_age = 1000000000
        args = args[1:]

    hostname = args[0]

    # hostname might be a cluster. In that case we compute the clustered
    # services of that cluster.
    services = []
    if is_cluster(hostname):
        already_added = set([])
        for node in nodes_of(hostname):
            new_services = automation_try_inventory_node(node)
            for entry in new_services:
                if host_of_clustered_service(node, entry[6]) == hostname:
                    # 1: check, 6: Service description
                    if (entry[1], entry[6]) not in already_added:
                        services.append(entry)
                        already_added.add((entry[1], entry[6])) # make it unique

    else:
        new_services = automation_try_inventory_node(hostname)
        for entry in new_services:
            if host_of_clustered_service(hostname, entry[6]) == hostname:
                services.append(entry)

    return services



def automation_try_inventory_node(hostname):
    global opt_use_cachefile, opt_no_tcp, opt_dont_submit

    try:
        ipaddress = lookup_ipaddress(hostname)
    except:
        raise MKAutomationError("Cannot lookup IP address of host %s" % hostname)

    found_services = []

    dual_host = is_snmp_host(hostname) and is_tcp_host(hostname)

    # if we are using cache files, then we restrict us to existing
    # check types. SNMP scan is only done without the --cache option
    snmp_error = None
    if is_snmp_host(hostname):
        try:
            if opt_use_cachefile:
                existing_checks = set([ cn for (cn, item) in get_check_table(hostname) ])
                for cn in inventorable_checktypes("snmp"):
                    if cn in existing_checks:
                        found_services += make_inventory(cn, [hostname], True, True)
            else:
                sys_descr = get_single_oid(hostname, ipaddress, ".1.3.6.1.2.1.1.1.0")
                if sys_descr != None:
                    found_services = do_snmp_scan([hostname], True, True)
                else:
                    raise MKSNMPError("Cannot get system description via SNMP. "
                                      "SNMP agent is not responding. Probably wrong "
                                      "community or wrong SNMP version.")

        except Exception, e:
            if not dual_host:
                raise
            snmp_error = str(e)

    tcp_error = None
    if is_tcp_host(hostname):
        try:
            for cn in inventorable_checktypes("tcp"):
                found_services += make_inventory(cn, [hostname], True, True)
        except Exception, e:
            if not dual_host:
                raise
            tcp_error = str(e)

    # raise MKAutomationError("%s/%s/%s" % (dual_host, snmp_error, tcp_error))
    if dual_host and snmp_error and tcp_error:
        raise MKAutomationError("Error using TCP (%s)\nand SNMP (%s)" %
                (tcp_error, snmp_error))

    found = {}
    for hn, ct, item, paramstring, state_type in found_services:
       found[(ct, item)] = ( state_type, paramstring )

    # Check if already in autochecks (but not found anymore)
    for hn, ct, item, params in autochecks:
        if hn == hostname and (ct, item) not in found:
            found[(ct, item)] = ( 'vanished', repr(params) ) # This is not the real paramstring!

    # Find manual checks
    existing = get_check_table(hostname)
    for (ct, item), (params, descr, deps) in existing.items():
        if (ct, item) not in found:
            found[(ct, item)] = ('manual', repr(params) )

    # Add legacy checks and active checks with artificial type 'legacy'
    legchecks = host_extra_conf(hostname, legacy_checks)
    for cmd, descr, perf in legchecks:
        found[('legacy', descr)] = ( 'legacy', 'None' )

    # Similar for 'active_checks', but here we have parameters
    for acttype, rules in active_checks.items():
        act_info = active_check_info[acttype]
        entries = host_extra_conf(hostname, rules)
        for params in entries:
            descr = act_info["service_description"](params)
            found[(acttype, descr)] = ( 'active', repr(params) )


    # Collect current status information about all existing checks
    table = []
    for (ct, item), (state_type, paramstring) in found.items():
        params = None
        if state_type not in [ 'legacy', 'active' ]:
            # apply check_parameters
            try:
                if type(paramstring) == str:
                    params = eval(paramstring)
                else:
                    params = paramstring
            except:
                raise MKAutomationError("Invalid check parameter string '%s'" % paramstring)


            descr = service_description(ct, item)
            infotype = ct.split('.')[0]
            opt_use_cachefile = True
            opt_no_tcp = True
            opt_dont_submit = True

            try:
                exitcode = None
                perfdata = []
                info = get_host_info(hostname, ipaddress, infotype)
            # Handle cases where agent does not output data
            except MKAgentError, e:
                exitcode = 3
                output = "Error getting data from agent"
                if str(e):
                    output += ": %s" % e
                tcp_error = output

            except MKSNMPError, e:
                exitcode = 3
                output = "Error getting data from agent for %s via SNMP" % infotype
                if str(e):
                    output += ": %s" % e
                snmp_error = output

            except Exception, e:
                exitcode = 3
                output = "Error getting data for %s: %s" % (infotype, e)
                if check_uses_snmp(ct):
                    snmp_error = output
                else:
                    tcp_error = output

            if exitcode == None:
                check_function = check_info[ct]["check_function"]
                if state_type != 'manual':
                    params = compute_check_parameters(hostname, ct, item, params)

                try:
                    result = check_function(item, params, info)
                except MKCounterWrapped, e:
                    result = (None, "WAITING - Counter based check, cannot be done offline")
                except Exception, e:
                    result = (3, "UNKNOWN - invalid output from agent or error in check implementation")
                if len(result) == 2:
                    result = (result[0], result[1], [])
                exitcode, output, perfdata = result
        else:
            descr = item
            exitcode = None
            output = "WAITING - Legacy check, cannot be done offline"
            perfdata = []

        if state_type == "active":
            params = eval(paramstring)

        if state_type in [ "legacy", "active" ]:
            checkgroup = None
        else:
            checkgroup = check_info[ct]["group"]
        table.append((state_type, ct, checkgroup, item, paramstring, params, descr, exitcode, output, perfdata))

    if not table and (tcp_error or snmp_error):
        error = ""
        if snmp_error:
            error = "Error getting data via SNMP: %s" % snmp_error
        if tcp_error:
            if error:
                error += ", "
            error += "Error getting data from Check_MK agent: %s" % tcp_error
        raise MKAutomationError(error)

    return table

# Set the new list of autochecks. This list is specified by a
# table of (checktype, item). No parameters are specified. Those
# are either (1) kept from existing autochecks or (2) computed
# from a new inventory. Note: we must never convert check parameters
# from python source code to actual values.
def automation_set_autochecks(args):
    hostname = args[0]
    new_items = eval(sys.stdin.read())

    do_cleanup_autochecks()
    existing = automation_parse_autochecks_file(hostname)

    # write new autochecks file, but take paramstrings from existing ones
    # for those checks which are kept
    new_autochecks = []
    for ct, item, params, paramstring in existing:
        if (ct, item) in new_items:
            new_autochecks.append((ct, item, paramstring))
            del new_items[(ct, item)]

    for (ct, item), paramstring in new_items.items():
        new_autochecks.append((ct, item, paramstring))

    # write new autochecks file for that host
    automation_write_autochecks_file(hostname, new_autochecks)


def automation_get_autochecks(args):
    hostname = args[0]
    do_cleanup_autochecks()
    return automation_parse_autochecks_file(hostname)

def automation_write_autochecks_file(hostname, table):
    if not os.path.exists(autochecksdir):
        os.makedirs(autochecksdir)
    path = "%s/%s.mk" % (autochecksdir, hostname)
    f = file(path, "w")
    f.write("# Autochecks for host %s, created by Check_MK automation\n[\n" % hostname)
    for ct, item, paramstring in table:
        f.write("  (%r, %r, %r, %s),\n" % (hostname, ct, item, paramstring))
    f.write("]\n")

def automation_parse_autochecks_file(hostname):
    def split_python_tuple(line):
        quote = None
        bracklev = 0
        backslash = False
        for i, c in enumerate(line):
            if backslash:
                backslash = False
                continue
            elif c == '\\':
                backslash = True
            elif c == quote:
                quote = None # end of quoted string
            elif c in [ '"', "'" ]:
                quote = c # begin of quoted string
            elif quote:
                continue
            elif c in [ '(', '{', '[' ]:
                bracklev += 1
            elif c in [ ')', '}', ']' ]:
                bracklev -= 1
            elif bracklev > 0:
                continue
            elif c == ',':
                value = line[0:i]
                rest = line[i+1:]
                return value.strip(), rest
        return line.strip(), None

    path = "%s/%s.mk" % (autochecksdir, hostname)
    if not os.path.exists(path):
        return []
    lineno = 0

    table = []
    for line in file(path):
        lineno += 1
        try:
            line = line.strip()
            if not line.startswith("("):
                continue

            # drop everything after potential '#' (from older versions)
	    i = line.rfind('#')
	    if i > 0: # make sure # is not contained in string
		rest = line[i:]
		if '"' not in rest and "'" not in rest:
		    line = line[:i].strip()

            if line.endswith(","):
                line = line[:-1]
            line = line[1:-1] # drop brackets

            hostnamestring, line = split_python_tuple(line) # should be hostname
            checktypestring, line = split_python_tuple(line)
            itemstring, line = split_python_tuple(line)
            paramstring, line = split_python_tuple(line)
            table.append((eval(checktypestring), eval(itemstring), eval(paramstring), paramstring))
        except:
            if opt_debug:
                raise
            raise MKAutomationError("Invalid line %d in autochecks file %s" % (lineno, path))
    return table

def automation_delete_host(args):
    hostname = args[0]
    for path in [
        "%s/%s"    % (precompiled_hostchecks_dir, hostname),
        "%s/%s.py" % (precompiled_hostchecks_dir, hostname),
        "%s/%s.mk" % (autochecksdir, hostname),
        "%s/%s"    % (logwatch_dir, hostname),
        "%s/%s"    % (counters_directory, hostname),
        "%s/%s"    % (tcp_cache_dir, hostname),
        "%s/%s.*"  % (tcp_cache_dir, hostname)]:
        os.system("rm -rf '%s'" % path)

def automation_restart(job="restart"):
    # make sure, Nagios does not inherit any open
    # filedescriptors. This really happens, e.g. if
    # check_mk is called by WATO via Apache. Nagios inherits
    # the open file where Apache is listening for incoming
    # HTTP connections. Really.
    for fd in range(3, 256):
        try:
            os.close(fd)
        except:
            pass
    # os.closerange(3, 256) --> not available in older Python versions

    class null_file:
        def write(self, stuff):
           pass
        def flush(self):
           pass

    # Deactivate stdout by introducing fake file without filedescriptor
    old_stdout = sys.stdout
    sys.stdout = null_file()

    try:
        backup_path = None
        if not lock_nagios_objects_file():
            raise MKAutomationError("Cannot activate changes. "
                  "Another activation process is currently in progresss")
        if os.path.exists(nagios_objects_file):
            backup_path = nagios_objects_file + ".save"
            os.rename(nagios_objects_file, backup_path)
        else:
            backup_path = None

        try:
	    create_nagios_config(file(nagios_objects_file, "w"))
        except Exception, e:
	    if backup_path:
		os.rename(backup_path, nagios_objects_file)
	    raise MKAutomationError("Error creating configuration: %s" % e)

        if do_check_nagiosconfig():
            if backup_path:
                os.remove(backup_path)
            do_precompile_hostchecks()
            if job == 'restart':
                do_restart_nagios(False)
            elif job == 'reload':
                do_restart_nagios(True)
        else:
            if backup_path:
                os.rename(backup_path, nagios_objects_file)
            else:
                os.remove(nagios_objects_file)
            raise MKAutomationError("Nagios configuration is invalid. Rolling back.")

    except Exception, e:
        if backup_path and os.path.exists(backup_path):
            os.remove(backup_path)
        raise MKAutomationError(str(e))

    sys.stdout = old_stdout

def automation_get_configuration():
    # We read the list of variable names from stdin since
    # that could be too much for the command line
    variable_names = eval(sys.stdin.read())
    result = {}
    for varname in variable_names:
        if varname in globals():
            result[varname] = globals()[varname]
    return result

def automation_get_check_information():
    manuals = all_manuals()
    checks = {}
    for check_type, check in check_info.items():
        manfile = manuals.get(check_type)
        if manfile:
            title = file(manfile).readline().strip().split(":", 1)[1].strip()
        else:
            title = check_type
        checks[check_type] = { "title" : title }
        if check["group"]:
            checks[check_type]["group"] = check["group"]
    return checks

def automation_scan_parents(args):
    settings = {
        "timeout"     : int(args[0]),
        "probes"      : int(args[1]),
        "max_ttl"     : int(args[2]),
        "ping_probes" : int(args[3]),
    }
    hostnames = args[4:]
    traceroute_prog = find_bin_in_path('traceroute')
    if not traceroute_prog:
        raise MKAutomationError("Cannot find binary <tt>traceroute</tt> in search path.")

    try:
        gateways = scan_parents_of(hostnames, silent=True, settings=settings)
        return gateways
    except Exception, e:
        raise MKAutomationError(str(e))