Adds alarm engine
[osm/MON.git] / osm_mon / core / settings.py
1 # Copyright 2017 Intel Research and Development Ireland Limited
2 # *************************************************************
3
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Intel Corporation
6
7 # Licensed under the Apache License, Version 2.0 (the "License"); you may
8 # not use this file except in compliance with the License. You may obtain
9 # a copy of the License at
10
11 # http://www.apache.org/licenses/LICENSE-2.0
12
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 # License for the specific language governing permissions and limitations
17 # under the License.
18
19 # For those usages not covered by the Apache License, Version 2.0 please
20 # contact: helena.mcgough@intel.com or adrian.hoban@intel.com
21 ##
22 """Global configuration managed by environment variables."""
23
24 import logging
25 import os
26
27 from collections import namedtuple
28
29 from osm_mon.core.singleton import Singleton
30
31 import six
32
33 __author__ = "Helena McGough"
34
35 log = logging.getLogger(__name__)
36
37
38 class BadConfigError(Exception):
39 """Configuration exception."""
40
41 pass
42
43
44 class CfgParam(namedtuple('CfgParam', ['key', 'default', 'data_type'])):
45 """Configuration parameter definition."""
46
47 def value(self, data):
48 """Convert a string to the parameter type."""
49 try:
50 return self.data_type(data)
51 except (ValueError, TypeError):
52 raise BadConfigError(
53 'Invalid value "%s" for configuration parameter "%s"' % (
54 data, self.key))
55
56
57 @Singleton
58 class Config(object):
59 """Configuration object."""
60
61 _configuration = [
62 CfgParam('BROKER_URI', "localhost:9092", six.text_type),
63 CfgParam('MONGO_URI', "mongo:27017", six.text_type),
64 CfgParam('DATABASE', "sqlite:///mon_sqlite.db", six.text_type),
65 CfgParam('OS_DEFAULT_GRANULARITY', 300, int),
66 CfgParam('REQUEST_TIMEOUT', 10, int),
67 CfgParam('OSMMON_LOG_LEVEL', "INFO", six.text_type),
68 CfgParam('OSMMON_KAFKA_LOG_LEVEL', "WARN", six.text_type),
69 CfgParam('OSMMON_COLLECTOR_INTERVAL', 30, int),
70 CfgParam('OSMMON_EVALUATOR_INTERVAL', 30, int),
71 CfgParam('OSMMON_VCA_HOST', "localhost", six.text_type),
72 CfgParam('OSMMON_VCA_SECRET', "secret", six.text_type),
73 CfgParam('OSMMON_VCA_USER', "admin", six.text_type),
74 CfgParam('OSMMON_DATABASE_COMMONKEY', "changeme", six.text_type),
75 ]
76
77 _config_dict = {cfg.key: cfg for cfg in _configuration}
78 _config_keys = _config_dict.keys()
79
80 def __init__(self):
81 """Set the default values."""
82 for cfg in self._configuration:
83 setattr(self, cfg.key, cfg.default)
84 self.read_environ()
85
86 def read_environ(self):
87 """Check the appropriate environment variables and update defaults."""
88 for key in self._config_keys:
89 try:
90 val = self._config_dict[key].data_type(os.environ[key])
91 setattr(self, key, val)
92 except KeyError as exc:
93 log.debug("Environment variable not present: %s", exc)
94 return