Licenses: Updated the list of licenses and added a PDF containing all license texts
[check_mk.git] / cmk_base / utils.py
blob28581597cee4c0b85a3363bcca5f4801427d8698
1 #!/usr/bin/env 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.
26 """This is an unsorted collection of functions which are needed in
27 Check_MK modules and/or cmk_base modules code."""
29 import itertools
30 import os
31 import signal
32 import sys
33 import time
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):
47 if 2 in states:
48 return 2
49 return max(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):
59 if len(v) == 10:
60 return "master"
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}
74 """
75 if handlers is None:
76 handlers = {}
78 dict_handler = lambda d: itertools.chain.from_iterable(d.items())
79 all_handlers = {
80 tuple: iter,
81 list: iter,
82 dict: dict_handler,
83 set: iter,
84 frozenset: iter,
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__
90 def sizeof(o):
91 if id(o) in seen: # do not double count the same object
92 return 0
93 seen.add(id(o))
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)))
99 break
100 return s
102 return sizeof(o)
105 def cachefile_age(path):
106 try:
107 return time.time() - os.stat(path)[8]
108 except Exception as e:
109 raise MKGeneralException("Cannot determine age of cache file %s: %s" \
110 % (path, e))
114 # .--Ctrl-C--------------------------------------------------------------.
115 # | ____ _ _ ____ |
116 # | / ___| |_ _ __| | / ___| |
117 # | | | | __| '__| |_____| | |
118 # | | |___| |_| | | |_____| |___ |
119 # | \____|\__|_| |_| \____| |
120 # | |
121 # +----------------------------------------------------------------------+
122 # | Handling of Ctrl-C |
123 # '----------------------------------------------------------------------'
126 # register SIGINT handler for consistent CTRL+C handling
127 def _handle_keepalive_interrupt(signum, frame):
128 raise MKTerminate()
131 def register_sigint_handler():
132 signal.signal(signal.SIGINT, _handle_keepalive_interrupt)