You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.9 KiB
63 lines
1.9 KiB
import json
|
|
import logging
|
|
import splunk
|
|
import splunk.rest as rest
|
|
from splunk.util import normalizeBoolean
|
|
|
|
logger = logging.getLogger('splunk.WebFeatures')
|
|
|
|
class WebFeaturesSingleton(object):
|
|
|
|
__initialized = False
|
|
__features = []
|
|
|
|
def __init__(self):
|
|
if not WebFeaturesSingleton.__initialized:
|
|
self.__fetch_feature_settings()
|
|
WebFeaturesSingleton.__initialized = True
|
|
|
|
|
|
def __fetch_feature_settings(self):
|
|
uri = 'web-features?output_mode=json'
|
|
features = []
|
|
|
|
try:
|
|
resp, cont = rest.simpleRequest(uri)
|
|
features_json = json.loads(cont)
|
|
|
|
features = features_json['entry']
|
|
# Note: ints 0,1 will not be converted into booleans
|
|
for feat in features:
|
|
feat['content'] = normalizeBoolean(feat['content'], includeIntegers=False)
|
|
|
|
WebFeaturesSingleton.__features = features
|
|
|
|
except splunk.ResourceNotFound:
|
|
logger.exception('Resource not found: %s' % uri)
|
|
except Exception as e:
|
|
logger.exception(e)
|
|
|
|
"""
|
|
feature <str> - maps to stanza in web-feature.conf
|
|
setting <str> - setting name for the given feature
|
|
|
|
Looks for a boolean settting in web-features.conf and returns it
|
|
|
|
@returns <bool>
|
|
"""
|
|
def get_bool_setting(self, feature, setting):
|
|
try:
|
|
feature_content = [obj for obj in WebFeaturesSingleton.__features if obj['name'] == feature][0]['content']
|
|
|
|
if type(feature_content[setting]) is not bool:
|
|
raise TypeError
|
|
|
|
return feature_content[setting]
|
|
|
|
except IndexError:
|
|
logger.exception('The setting "%s" does not exsit for feature "%s" in web-features.conf' % (setting, feature))
|
|
except TypeError:
|
|
logger.exception('The setting "%s" for feature "%s" is not bool type' % (setting, feature))
|
|
except Exception as e:
|
|
logger.exception(e)
|