/
config.py
362 lines (289 loc) · 10.7 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# Authors:
# rafael@postgresql.org.es / http://www.postgresql.org.es/
#
# Copyright (c) 2014-2016 USIT-University of Oslo
#
# This file is part of Zabbix-CLI
# https://github.com/rafaelma/zabbix-cli
#
# Zabbix-CLI is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Zabbix-CLI is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Zabbix-CLI. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import collections
import logging
import os
import sys
try:
# PY3
import configparser
except ImportError:
import ConfigParser as configparser
# Config file basename
CONFIG_FILENAME = 'zabbix-cli.conf'
CONFIG_FIXED_NAME = 'zabbix-cli.fixed.conf'
# Config file locations
CONFIG_DEFAULT_DIR = '/usr/share/zabbix-cli'
CONFIG_SYSTEM_DIR = '/etc/zabbix-cli'
CONFIG_USER_DIR = os.path.expanduser('~/.zabbix-cli')
# Any item will overwrite values from the previous
CONFIG_PRIORITY = tuple((
os.path.join(d, f) for d, f in (
(CONFIG_DEFAULT_DIR, CONFIG_FILENAME),
(CONFIG_SYSTEM_DIR, CONFIG_FILENAME),
(CONFIG_USER_DIR, CONFIG_FILENAME),
(CONFIG_SYSTEM_DIR, CONFIG_FIXED_NAME),
(CONFIG_DEFAULT_DIR, CONFIG_FIXED_NAME),
)))
# Where custom configs should be put into the order
CONFIG_CUSTOM_GOES_AFTER = os.path.join(CONFIG_USER_DIR, CONFIG_FILENAME)
logger = logging.getLogger(__name__)
def get_priority(filename=None):
""" Get and ordered list of config file locations. """
priority = list(CONFIG_PRIORITY)
if filename:
if CONFIG_CUSTOM_GOES_AFTER in priority:
priority.insert(priority.index(CONFIG_CUSTOM_GOES_AFTER) + 1,
filename)
else:
priority.append(filename)
return priority
def find_config(filename=None):
""" Find all available configuration files.
:param filename: An optional user supplied file to throw into the mix
"""
for filename in get_priority(filename):
if os.path.isfile(filename):
logger.debug('found config %r', filename)
yield filename
class OptionDescriptor(object):
""" Descriptor to access ConfigParser settings as attributes. """
# TODO: Add serialization, so that 'ON', 'OFF' -> boolean, etc...
def __init__(self, section, option,
default=None, required=False, disable=False):
self.section = section
self.option = option
self.default = default
self.required = required
self.disable = disable
def __get__(self, obj, cls=None):
if not obj:
return self
return obj.get(self.section, self.option)
def __set__(self, obj, value):
return obj.set(self.section, self.option, value)
def __delete__(self, obj):
return obj.set(self.section, self.option, self.default)
def __repr__(self):
return ('<{cls.__name__} {obj.section}.{obj.option}'
' default={obj.default!r}'
' required={obj.required!r}'
' disable={obj.disable!r}'
'>').format(cls=type(self), obj=self)
class OptionRegister(collections.Mapping):
""" A registry of ConfigParser sections, options and default values. """
def __init__(self):
self._settings = collections.OrderedDict()
def __len__(self):
return len(self._settings)
def __iter__(self):
return iter(self._settings)
def __getitem__(self, option_tuple):
return self._settings[option_tuple]
def add(self, section, option, *args, **kwargs):
self._settings[(section, option)] = OptionDescriptor(section, option,
*args, **kwargs)
return self._settings[(section, option)]
@property
def sections(self):
def _get():
seen = set()
for section, option in self:
if section not in seen:
seen.add(section)
yield section
return tuple(_get())
def initialize(self, obj):
seen = set()
for section, option in self:
if section not in seen:
seen.add(section)
obj.add_section(section)
obj.set(section, option, self[(section, option)].default)
class Configuration(configparser.RawConfigParser, object):
""" A custom ConfigParser object with zabbix-cli settings. """
_registry = OptionRegister()
zabbix_api_url = _registry.add(
'zabbix_api', 'zabbix_api_url',
required=True)
cert_verify = _registry.add(
'zabbix_api', 'cert_verify',
default='ON')
system_id = _registry.add(
'zabbix_config', 'system_id',
default='zabbix-ID')
default_hostgroup = _registry.add(
'zabbix_config', 'default_hostgroup',
default='All-hosts')
default_admin_usergroup = _registry.add(
'zabbix_config', 'default_admin_usergroup',
default='All-root')
default_create_user_usergroup = _registry.add(
'zabbix_config', 'default_create_user_usergroup',
default='All-users')
default_notification_users_usergroup = _registry.add(
'zabbix_config', 'default_notification_users_usergroup',
default='All-notification-users')
default_directory_exports = _registry.add(
'zabbix_config', 'default_directory_exports',
default=os.path.expanduser('~/zabbix_exports'))
default_export_format = _registry.add(
'zabbix_config', 'default_export_format',
default='XML',
# We deactivate this until https://support.zabbix.com/browse/ZBX-10607
# gets fixed. We use XML as the export format.
disable=True)
include_timestamp_export_filename = _registry.add(
'zabbix_config', 'include_timestamp_export_filename',
default='ON'
)
use_colors = _registry.add(
'zabbix_config', 'use_colors',
default='ON')
use_auth_token_file = _registry.add(
'zabbix_config', 'use_auth_token_file',
default='OFF')
use_paging = _registry.add(
'zabbix_config', 'use_paging',
default='OFF')
logging = _registry.add(
'logging', 'logging',
default='OFF')
log_level = _registry.add(
'logging', 'log_level',
default='ERROR')
log_file = _registry.add(
'logging', 'log_file',
default='/var/log/zabbix-cli/zabbix-cli.log')
def __init__(self):
super(Configuration, self).__init__()
self._registry.initialize(self)
self.loaded_files = []
def set(self, section, option, value):
descriptor = self._registry[(section, option)]
if descriptor.disable and value != descriptor.default:
logger.warning('setting %s.%s is disabled, setting to %r',
section, option, descriptor.default)
value = descriptor.default
return super(Configuration, self).set(section, option, value)
def read(self, filenames):
files_read = super(Configuration, self).read(filenames)
for filename in files_read:
if filename in self.loaded_files:
self.loaded_files.remove(filename)
self.loaded_files.append(filename)
return files_read
def readfp(self, fp, filename=None):
try:
add_filename = filename or fp.name
except AttributeError:
pass
retval = super(Configuration, self).readfp(fp, filename=filename)
if add_filename:
self.loaded_files.append(add_filename)
return retval # should be None
def iter_descriptors(self):
return iter(self._registry.values())
def iter_required(self):
for descriptor in self.iter_descriptors():
if descriptor.required:
yield descriptor
def iter_missing(self):
for descriptor in self.iter_required():
value = self.get(descriptor.section, descriptor.option)
if value == descriptor.default:
yield descriptor
def get_config(filename=None):
config = Configuration()
for filename in find_config(filename):
logger.debug('loading config %r', filename)
config.read(filename)
return config
def validate_config(config):
missing = ['{0.section}.{0.option}'.format(d)
for d in config.iter_missing()]
if missing:
raise ValueError("Missing settings: " + ', '.join(missing))
#
# python -m zabbix_cli.config
#
def main(inargs=None):
import argparse
class Actions(object):
""" subparser to function map. """
def __init__(self):
self.funcmap = dict()
def __getitem__(self, key):
return self.funcmap[key]
def __call__(self, subparser):
def wrapper(func):
key = subparser.prog.split(' ')[-1]
self.funcmap[key] = func
return func
return wrapper
parser = argparse.ArgumentParser(description='write default config')
commands = parser.add_subparsers(title='commands', dest='command')
actions = Actions()
#
# defaults [filename]
#
defaults_cmd = commands.add_parser('defaults')
defaults_cmd.add_argument(
'output',
type=argparse.FileType('w'),
nargs='?',
default='-',
metavar='FILE',
help='Write an example config to %(metavar)s (default: stdout)')
@actions(defaults_cmd)
def write_default_config(args):
config = Configuration()
config.write(args.output)
args.output.flush()
if args.output not in (sys.stdout, sys.stderr):
args.output.close()
#
# show
#
show_cmd = commands.add_parser('show')
show_cmd.add_argument(
'-c', '--config',
default=None,
metavar='FILE',
help='Use config from %(metavar)s')
show_cmd.add_argument(
'-v', '--validate',
action='store_true',
default=False,
help='validate config')
@actions(show_cmd)
def show_config(args):
config = get_config(args.config)
if args.validate:
validate_config(config)
config.write(sys.stdout)
sys.stdout.flush()
args = parser.parse_args()
actions[args.command](args)
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
main()