<commit_before>"""Parses user defined settings from settings.py in top level dir."""
import logging
import os
import ast
import etcd

log = logging.getLogger(__name__)


# Parse user defined settings from settings.py in the top level project dir
settings = {}
try:
    execfile("settings.py", settings)
except IOError:
    log.warning("No settings.py file found.")
except Exception as exc:
    log.error("Error parsing settings py: %r", exc)

ETCD_BACKEND = os.getenv('ETCD_BACKEND') or settings.get('ETCD_BACKEND')

def get_default_gateway_ip():
    with open("/proc/net/route") as fh:
        for line in fh:
            fields = line.strip().split()
            if fields[1] != '00000000' or not int(fields[3], 16) & 2:
                continue

            return socket.inet_ntoa(struct.pack("<L", int(fields[2], 16)))

def etcd_get(client, key, default_value, type='string'):
    try:
        key = client.read('/mist/settings/%s' % key).value
        if type is 'string':
            key = str(key)
        elif type is 'integer':
            key = int(key)
        elif type is 'boolean':
            key = ast.literal_eval(key)
        elif type is 'list':
            key = [key]
    except etcd.EtcdKeyNotFound:
        key = default_value

    return key

if ETCD_BACKEND:
    if ETCD_BACKEND in ["gce", "gke", "GKE", "GCE"]:
        ETCD_URI = "etcd"
    else:
        ETCD_URI = get_default_gateway_ip()
    try:
        client = etcd.Client(ETCD_URI, port=2379)
        machines = client.machines
        ETCD_EXISTS = True
    except:
        ETCD_EXISTS = False
        pass
else:
    ETCD_EXISTS = False

if ETCD_EXISTS:
    CORE_URI = etcd_get(client, 'CORE_URI', "http://localhost:8000")
    GRAPHITE_URI = etcd_get(client, 'GRAPHITE_URI', "http://graphite")
    MONGO_URI = etcd_get(client, 'MONGO_URI', "mongodb:27017")
    MEMCACHED_URI = etcd_get(client, 'MEMCACHED_URI', ["memcached:11211"], type='list')
    SSL_VERIFY = etcd_get(client, 'SSL_VERIFY', False, type='boolean')
    AUTH_FILE_PATH = etcd_get(client, 'AUTH_FILE_PATH', "/opt/mist/collectd.passwd")
else:
    CORE_URI = settings.get("CORE_URI",
                            os.environ.get("CORE_URI", "https://mist.io"))
    GRAPHITE_URI = settings.get("GRAPHITE_URI",
                                os.environ.get("GRAPHITE_URI",
                                               "http://localhost"))
    MONGO_URI = settings.get("MONGO_URI",
                             os.environ.get("MONGO_URI", "localhost:27022"))
    MEMCACHED_URI = settings.get("MEMCACHED_URI", ["localhost:11211"])
    SSL_VERIFY = settings.get("SSL_VERIFY", True)
    AUTH_FILE_PATH = settings.get(
        "AUTH_FILE_PATH",
        os.environ.get("AUTH_FILE_PATH", os.getcwd() + "/conf/collectd.passwd")
    )

# Defines timings of notifications sent to core from mist.alert when a rule
# is triggered. (When untriggered we always send a single notification right
# away.) This is the default reminder list used if one isn't specified from
# core when rule is created/updated.
REMINDER_LIST = settings.get(
    "REMINDER_LIST",
    [
        0,  # notification level 0 - right away
        60 * 10,  # notification level 1 - 10 mins
        60 * 60,  # notification level 2 - 1 hour
        60 * 60 * 24,  # notification level 3 - 1 day
    ]
)


# mist.alert periodically checks if the rules are triggered. This option
# defines mist.alert's period between two consecutive runs (in seconds).
ALERT_PERIOD = settings.get("ALERT_PERIOD", 15)
ALERT_THREADS = settings.get("ALERT_THREADS", 32)


# Graphite's storage interval, needed because derivative metrics always return
# None as their first value and to deal with that we ask for one step
# earlier and then strip measurements that are before the asked 'start'.<commit_msg>Cleanup etcd and make settingsfile path configurable<commit_after>"""Parses user defined settings from settings.py in top level dir."""
import logging
import os
import ast
import etcd

log = logging.getLogger(__name__)


# Parse user defined settings from settings.py in the top level project dir
settings_file = os.getenv('MONITOR_SETTINGS_FILE') or 'settings.py'
settings = {}
try:
    execfile(settings_file, settings)
except IOError:
    log.warning("No %s file found." % settings_file)
except Exception as exc:
    log.error("Error parsing settings py: %r", exc)


CORE_URI = settings.get("CORE_URI",
                        os.environ.get("CORE_URI", "https://mist.io"))
GRAPHITE_URI = settings.get("GRAPHITE_URI",
                            os.environ.get("GRAPHITE_URI",
                                           "http://localhost"))
MONGO_URI = settings.get("MONGO_URI",
                         os.environ.get("MONGO_URI", "localhost:27022"))
MEMCACHED_URI = settings.get("MEMCACHED_URI", ["memcached:11211"])
SSL_VERIFY = settings.get("SSL_VERIFY", True)
AUTH_FILE_PATH = settings.get(
    "AUTH_FILE_PATH",
    os.environ.get("AUTH_FILE_PATH", os.getcwd() + "/conf/collectd.passwd")
)

# Defines timings of notifications sent to core from mist.alert when a rule
# is triggered. (When untriggered we always send a single notification right
# away.) This is the default reminder list used if one isn't specified from
# core when rule is created/updated.
REMINDER_LIST = settings.get(
    "REMINDER_LIST",
    [
        0,  # notification level 0 - right away
        60 * 10,  # notification level 1 - 10 mins
        60 * 60,  # notification level 2 - 1 hour
        60 * 60 * 24,  # notification level 3 - 1 day
    ]
)


# mist.alert periodically checks if the rules are triggered. This option
# defines mist.alert's period between two consecutive runs (in seconds).
ALERT_PERIOD = settings.get("ALERT_PERIOD", 15)
ALERT_THREADS = settings.get("ALERT_THREADS", 32)


# Graphite's storage interval, needed because derivative metrics always return
# None as their first value and to deal with that we ask for one step
# earlier and then strip measurements that are before the asked 'start'.
{'fraction_non_alphanumeric': 0.1081441922563418, 'fraction_numerical': 0.018024032042723633, 'mean_word_length': 2.9820598006644516, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4009010', 'n_tokens_mistral': 1941, 'n_tokens_neox': 1810, 'n_words': 552}