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.
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
43 raise NotImplementedError()
47 return cmk
.utils
.store
.load_data_from_file(str(self
.file_path
), default
={})
49 def save(self
, labels
):
50 # type: (Dict) -> None
52 if self
.file_path
.exists():
53 self
.file_path
.unlink()
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
):
63 super(DiscoveredHostLabelsStore
, self
).__init
__()
64 self
._hostname
= hostname
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
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
__()
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
]
106 return iter(self
._labels
)
109 return len(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
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
130 labels
= self
._inventory
_tree
.get_list("software.applications.check_mk.host_labels:")
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"""