Licenses: Updated the list of licenses and added a PDF containing all license texts
[check_mk.git] / cmk_base / discovered_labels.py
blob135f2390ea9607f2dca4fe8feec9bd5d3a59986b
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 import abc
28 import collections
29 from typing import Dict, Text # pylint: disable=unused-import
30 from pathlib2 import Path # pylint: disable=unused-import
32 import cmk.utils.paths
33 import cmk.utils.store
36 class ABCDiscoveredLabelsStore(object):
37 """Managing persistance of discovered labels"""
38 __metaclass__ = abc.ABCMeta
40 @abc.abstractproperty
41 def file_path(self):
42 # type () -> Path
43 raise NotImplementedError()
45 def load(self):
46 # type: () -> Dict
47 return cmk.utils.store.load_data_from_file(str(self.file_path), default={})
49 def save(self, labels):
50 # type: (Dict) -> None
51 if not labels:
52 if self.file_path.exists():
53 self.file_path.unlink()
54 return
56 self.file_path.parent.mkdir(parents=True, exist_ok=True) # pylint: disable=no-member
57 cmk.utils.store.save_data_to_file(str(self.file_path), labels)
60 class DiscoveredHostLabelsStore(ABCDiscoveredLabelsStore):
61 def __init__(self, hostname):
62 # type: (str) -> None
63 super(DiscoveredHostLabelsStore, self).__init__()
64 self._hostname = hostname
66 @property
67 def file_path(self):
68 # type () -> Path
69 return (cmk.utils.paths.discovered_host_labels_dir / self._hostname).with_suffix(".mk")
72 class DiscoveredServiceLabelsStore(ABCDiscoveredLabelsStore):
73 def __init__(self, hostname, service_desc):
74 # type: (str, Text) -> None
75 super(DiscoveredServiceLabelsStore, self).__init__()
76 self._hostname = hostname
77 self._service_desc = service_desc
79 @property
80 def file_path(self):
81 # type () -> Path
82 return (cmk.utils.paths.discovered_service_labels_dir / self._hostname /
83 self._service_desc).with_suffix(".mk")
86 class ABCDiscoveredLabels(collections.MutableMapping, object):
87 __metaclass__ = abc.ABCMeta
89 def __init__(self, **kwargs):
90 super(ABCDiscoveredLabels, self).__init__()
91 self._labels = kwargs
93 def is_empty(self):
94 return not self._labels
96 def __getitem__(self, key):
97 return self._labels[key]
99 def __setitem__(self, key, value):
100 self._labels[key] = value
102 def __delitem__(self, key):
103 del self._labels[key]
105 def __iter__(self):
106 return iter(self._labels)
108 def __len__(self):
109 return len(self._labels)
111 def to_dict(self):
112 return self._labels
115 class DiscoveredHostLabels(ABCDiscoveredLabels):
116 """Encapsulates the discovered labels of a single host during runtime"""
118 def __init__(self, inventory_tree, **kwargs):
119 super(DiscoveredHostLabels, self).__init__(**kwargs)
120 self._inventory_tree = inventory_tree
122 # TODO: Once we redesign the hw/sw inventory plugin API check if we can move it to the
123 # inventory API.
124 def add_label(self, key, value, plugin_name):
125 """Add a label to the collection of discovered labels and inventory tree
127 Add it to the inventory tree for debugging purposes
129 self[key] = value
130 labels = self._inventory_tree.get_list("software.applications.check_mk.host_labels:")
131 labels.append({
132 "label": (key, value),
133 "inventory_plugin_name": plugin_name,
137 class DiscoveredServiceLabels(ABCDiscoveredLabels):
138 """Encapsulates the discovered labels of a single service during runtime"""
139 pass