Refactoring: Changed all check parameters starting with an 'e' to new rulespec regist...
[check_mk.git] / cmk_base / compress_history.py
blob17f3843267a187053565a1cce51519500f052dee
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 # Helper file for more effectively organizing monitoring log files.
28 # Rewrites existing logfiles for CMC. You can concatenate several
29 # logfiles and then compress them. Do *not* compress compressed
30 # files again.
32 from cmk.utils.exceptions import MKBailOut
34 import cmk.utils.log
35 import cmk.utils.debug
36 logger = cmk.utils.log.get_logger(__name__)
39 def do_compress_history(args):
40 if not args:
41 raise MKBailOut("Please specify files to compress.")
43 for filename in args:
44 try:
45 logger.verbose("%s...", filename)
46 compress_history_file(filename, filename + ".compressed")
47 except Exception as e:
48 if cmk.utils.debug.enabled():
49 raise
50 raise MKBailOut("%s" % e)
53 def compress_history_file(input_path, output_path):
54 known_services = {}
55 machine_state = "START"
57 output = file(output_path, "w")
58 for line in file(input_path):
59 skip_this_line = False
60 timestamp = int(line[1:11])
61 line_type, host, service = parse_history_line(line)
63 logger.debug("%s (%s) %s / %s / %s", line, machine_state, line_type, host, service)
65 if line_type == "RESTART" or line_type == "LOGGING_INITIAL":
66 if machine_state != "START":
67 machine_state = "AFTER_RESTART"
68 services_after_reload = {}
69 if line_type == "LOGGING_INITIAL":
70 skip_this_line = True
72 elif line_type == "CURRENT":
73 if machine_state not in ("START", "CURRENT", "AFTER_RESTART"):
74 raise Exception("Unexpected line %s (while in state %s)" % (line, machine_state))
75 machine_state = "CURRENT"
76 known_services.setdefault(host, set([])).add(service)
78 elif line_type == "INITIAL":
79 if machine_state == "OPERATION":
80 pass # happens at CMC. That does not create a log entry on reload
81 elif machine_state == "START":
82 machine_state = "INITIAL"
83 known_services.setdefault(host, set([])).add(service)
84 services_after_reload = {}
85 elif machine_state not in ("AFTER_RESTART", "INITIAL"):
86 raise Exception("Unexpected line %s (while in state %s)" % (line, machine_state))
87 else:
88 machine_state = "INITIAL"
89 services_after_reload.setdefault(host, set([])).add(service)
90 if host in known_services and service in known_services[host]:
91 skip_this_line = True
93 elif line_type == "OPERATION":
94 if machine_state != "START":
95 if machine_state == "INITIAL":
96 for host in known_services:
97 if host not in services_after_reload:
98 for service in known_services[host]:
99 log_vanished_object(output, timestamp, host, service)
100 del known_services[host]
101 else:
102 known = known_services[host]
103 after_reload = services_after_reload[host]
104 for service in list(known):
105 if service not in after_reload:
106 log_vanished_object(output, timestamp, host, service)
107 known.remove(service)
108 machine_state = "OPERATION"
109 else:
110 pass
112 if not skip_this_line:
113 output.write(line)
116 def parse_history_line(line):
117 command = get_line_command(line)
118 if "INITIAL" in command:
119 host, service = get_host_service_from_history_line(command, line)
120 return "INITIAL", host, service
121 elif "CURRENT" in command:
122 host, service = get_host_service_from_history_line(command, line)
123 return "CURRENT", host, service
124 elif "logging intitial" in command \
125 or "logging initial" in command:
126 return "LOGGING_INITIAL", None, None
127 elif "LOG ROTATION" in command \
128 or "LOG VERSION" in command:
129 return "RESTART", None, None
130 return "OPERATION", None, None
133 def get_host_service_from_history_line(command, line):
134 arguments = line.split(":")[1].strip().split(";")
135 if "HOST" in command:
136 return arguments[0], None
137 return arguments[0], arguments[1]
140 def get_line_command(line):
141 if ":" in line:
142 return line.split(":")[0].split("]")[1].strip()
143 return line.split("]")[1].strip()
146 def log_vanished_object(output, timestamp, host, service):
147 if service:
148 output.write("[%s] VANISHED SERVICE: %s;%s\n" % (timestamp, host, service))
149 else:
150 output.write("[%s] VANISHED HOST: %s\n" % (timestamp, host))