2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
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.
26 """This is an unsorted collection of functions which are needed in
27 Check_MK modules and/or cmk_base modules code."""
35 from cmk
.utils
.exceptions
import MKGeneralException
, MKTerminate
37 # TODO: Clean up the call sites
38 from cmk
.utils
import ( # pylint: disable=unused-import
39 make_utf8
, quote_shell_string
,
42 # TODO: Try to find a better place for them.
45 # Aggegates several monitoring states to the worst state
46 def worst_service_state(*states
):
52 # Works with Check_MK version (without tailing .cee and/or .demo)
53 def is_daily_build_version(v
):
54 return len(v
) == 10 or '-' in v
57 # Works with Check_MK version (without tailing .cee and/or .demo)
58 def branch_of_daily_build(v
):
61 return v
.split('-')[0]
64 def total_size(o
, handlers
=None):
65 """ Returns the approximate memory footprint an object and all of its contents.
67 Automatically finds the contents of the following builtin containers and
68 their subclasses: tuple, list, dict, set and frozenset.
69 To search other containers, add handlers to iterate over their contents:
71 handlers = {SomeContainerClass: iter,
72 OtherContainerClass: OtherContainerClass.get_elements}
78 dict_handler
= lambda d
: itertools
.chain
.from_iterable(d
.items())
86 all_handlers
.update(handlers
) # user handlers take precedence
87 seen
= set() # track which object id's have already been seen
88 default_size
= sys
.getsizeof(0) # estimate sizeof object without __sizeof__
91 if id(o
) in seen
: # do not double count the same object
94 s
= sys
.getsizeof(o
, default_size
)
96 for typ
, handler
in all_handlers
.items():
97 if isinstance(o
, typ
):
98 s
+= sum(map(sizeof
, handler(o
)))
105 def cachefile_age(path
):
107 return time
.time() - os
.stat(path
)[8]
108 except Exception as e
:
109 raise MKGeneralException("Cannot determine age of cache file %s: %s" \
114 # .--Ctrl-C--------------------------------------------------------------.
116 # | / ___| |_ _ __| | / ___| |
117 # | | | | __| '__| |_____| | |
118 # | | |___| |_| | | |_____| |___ |
119 # | \____|\__|_| |_| \____| |
121 # +----------------------------------------------------------------------+
122 # | Handling of Ctrl-C |
123 # '----------------------------------------------------------------------'
126 # register SIGINT handler for consistent CTRL+C handling
127 def _handle_keepalive_interrupt(signum
, frame
):
131 def register_sigint_handler():
132 signal
.signal(signal
.SIGINT
, _handle_keepalive_interrupt
)