This file is indexed.

/usr/share/check_mk/modules/compresslog.py is in check-mk-server 1.2.8p16-1ubuntu0.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
#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             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-
# tails. 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.

# Helper file for more effectively organizing monitoring log files.
# Rewrites existing logfiles for CMC. You can concatenate several
# logfiles and then compress them. Do *not* compress compressed
# files again.


def do_compress_history(args):
    if not args:
        bail_out("Please specify files to compress.")

    for filename in args:
        try:
            verbose("%s..." % filename)
            compress_history_file(filename, filename + ".compressed")
            verbose("OK\n")
        except Exception, e:
            if opt_debug:
                raise
            bail_out(str(e))


def compress_history_file(input_path, output_path):
    known_services = {}
    machine_state = "START"

    output = file(output_path, "w")
    for line in file(input_path):
        skip_this_line = False
        timestamp = int(line[1:11])
        line_type, host, service = parse_history_line(line)

        vverbose("%s  (%s) %s / %s / %s\n" % (line, machine_state, line_type, host, service))

        if line_type == "RESTART" or line_type == "LOGGING_INITIAL":
            if machine_state != "START":
                machine_state = "AFTER_RESTART"
                services_after_reload = {}
            if line_type == "LOGGING_INITIAL":
                skip_this_line = True

        elif line_type == "CURRENT":
            if machine_state not in ("START", "CURRENT", "AFTER_RESTART"):
                raise Exception("Unexpected line %s (while in state %s)" % (line, machine_state))
            machine_state = "CURRENT"
            known_services.setdefault(host, set([])).add(service)

        elif line_type == "INITIAL":
            if machine_state == "OPERATION":
                pass # happens at CMC. That does not create a log entry on reload
            elif machine_state == "START":
                machine_state = "INITIAL"
                known_services.setdefault(host, set([])).add(service)
                services_after_reload = {}
            elif machine_state not in ("AFTER_RESTART", "INITIAL"):
                raise Exception("Unexpected line %s (while in state %s)" % (line, machine_state))
            else:
                machine_state = "INITIAL"
                services_after_reload.setdefault(host, set([])).add(service)
                if host in known_services and service in known_services[host]:
                    skip_this_line = True

        elif line_type == "OPERATION":
            if machine_state != "START":
                if machine_state == "INITIAL":
                    for host in known_services.keys():
                        if host not in services_after_reload:
                            for service in known_services[host]:
                                log_vanished_object(output, timestamp, host, service)
                            del known_services[host]
                        else:
                            known = known_services[host]
                            after_reload = services_after_reload[host]
                            for service in list(known):
                                if service not in after_reload:
                                    log_vanished_object(output, timestamp, host, service)
                                    known.remove(service)
                machine_state = "OPERATION"
        else:
            pass



        if not skip_this_line:
            output.write(line)


def parse_history_line(line):
    command = get_line_command(line)
    if "INITIAL" in command:
        host, service = get_host_service_from_history_line(command, line)
        return "INITIAL", host, service
    elif "CURRENT" in command:
        host, service = get_host_service_from_history_line(command, line)
        return "CURRENT", host, service
    elif "logging intitial" in command \
        or "logging initial" in command:
        return "LOGGING_INITIAL", None, None
    elif "LOG ROTATION" in command \
        or "LOG VERSION" in command:
        return "RESTART", None, None
    else:
        return "OPERATION", None, None


def get_host_service_from_history_line(command, line):
    arguments = line.split(":")[1].strip().split(";")
    if "HOST" in command:
        return arguments[0], None
    else:
        return arguments[0], arguments[1]


def get_line_command(line):
    if ":" in line:
        return line.split(":")[0].split("]")[1].strip()
    else:
        return line.split("]")[1].strip()


def log_vanished_object(output, timestamp, host, service):
    if service:
        output.write("[%s] VANISHED SERVICE: %s;%s\n" % (timestamp, host, service))
    else:
        output.write("[%s] VANISHED HOST: %s\n" % (timestamp, host))