示例#1
0
def main(period=None):
    """Constantly check services availability.

    This runs infinite availability check with given period.
    :param period: period in seconds
    """
    if not config.get_config().get("regions"):
        LOG.error("No regions configured. Quitting.")
        return 1

    period = period or config.get_config().get("period", 60)

    if SERVICE_CONN_TIMEOUT + SERVICE_READ_TIMEOUT > period:
        LOG.error("Period can not be lesser than timeout, "
                  "otherwise threads could crowd round.")
        return 1

    backend = config.get_config().get("backend")
    if backend["type"] != "elastic":
        LOG.error("Unexpected backend: %(type)s" % backend)
        return 1

    if not storage.get_elasticsearch(check_availability=True):
        LOG.error("Failed to set up Elasticsearch")
        return 1

    LOG.info("Start watching with period %s seconds" % period)
    schedule.every(period).seconds.do(watch_services)

    while True:
        time.sleep(1)
        schedule.run_pending()
示例#2
0
    def test_get_config(self, mock_validate, mock_load, mock_open, mock_get):
        mock_get.return_value = "foo_path"
        mock_load.return_value = {"foo": 42, "bar": "spam"}
        mock_open.return_value = "foo_stream"

        cfg = config.get_config()

        self.assertEqual({"foo": 42, "bar": "spam"}, cfg)
        mock_get.assert_called_once_with("AVAILABILITY_CONF",
                                         "/etc/availability/config.json")
        mock_open.assert_called_once_with("foo_path")
        mock_load.assert_called_once_with("foo_stream")
        mock_validate.assert_called_once_with({
            "foo": 42,
            "bar": "spam"
        }, config.CONF_SCHEMA)
示例#3
0
def get_elasticsearch(check_availability=False):
    """Return Elasticsearch instance.

    :param check_availability: check if nodes are available
    :returns: Elasticsearch or None on failure
    :rtype: elasticsearch.Elasticsearch
    """
    nodes = config.get_config()["backend"]["connection"]
    try:
        es = elasticsearch.Elasticsearch(nodes)
        if check_availability:
                es.info()
    except Exception as e:
        LOG.warning(
            "Failed to query Elasticsearch nodes %s: %s"
            % (nodes, str(e)))
        raise
    return es
示例#4
0
def watch_services():
    """Query services of all regions and save results."""
    for region in config.get_config().get("regions"):
        for service in region.get("services"):
            LOG.info("Checking service '%(name)s' availability on %(url)s" %
                     service)
            data = {
                "url": service["url"],
                "name": service["name"],
                "region": region["name"],
                "timestamp": dt.datetime.now().isoformat()
            }

            thr = threading.Thread(target=check_availability,
                                   args=(data, results_queue))
            thr.start()

    results_saver_thr = threading.Thread(target=save_availability,
                                         args=(results_queue, ))
    results_saver_thr.start()
示例#5
0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import json
import logging

import elasticsearch

from availability import config

LOG = logging.getLogger("storage")
LOG.setLevel(config.get_config().get("logging", {}).get("level", "INFO"))
NUMBER_OF_SHARDS = 2


def get_elasticsearch(check_availability=False):
    """Return Elasticsearch instance.

    :param check_availability: check if nodes are available
    :returns: Elasticsearch or None on failure
    :rtype: elasticsearch.Elasticsearch
    """
    nodes = config.get_config()["backend"]["connection"]
    try:
        es = elasticsearch.Elasticsearch(nodes)
        if check_availability:
                es.info()
示例#6
0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import flask
from flask_helpers import routing

from availability.api.v1 import api
from availability.api.v1 import regions
from availability import config

app = flask.Flask(__name__, static_folder=None)
app.config.update(config.get_config()["flask"])


@app.errorhandler(404)
def not_found(error):
    return flask.jsonify({"error": "Not Found"}), 404


for bp in [api, regions]:
    for url_prefix, blueprint in bp.get_blueprints():
        app.register_blueprint(blueprint, url_prefix="/api/v1%s" % url_prefix)

app = routing.add_routing_map(app, html_uri=None, json_uri="/")


def main():
示例#7
0
import json
import logging
import sys
import threading
import time
import uuid

from elasticsearch import exceptions as es_exceptions
import queue
import requests
import schedule

from availability import config
from availability import storage

SERVICE_CONN_TIMEOUT = config.get_config().get("connection_timeout", 1)
SERVICE_READ_TIMEOUT = config.get_config().get("read_timeout", 10)

LOG = logging.getLogger("watcher")
LOG.setLevel(config.get_config().get("logging", {}).get("level", "INFO"))


def check_availability(data, results_queue):
    """Check if service is available and put result to Queue.

    :param data: dict that represents service query data
    :param results_queue: Queue for results
    :rtype: None
    """
    try:
        requests.get(data["url"],
示例#8
0
 def test_get_config_cached(self):
     self.assertEqual(42, config.get_config())
示例#9
0
 def test_get_config_open_error(self, mock_validate, mock_load, mock_open,
                                mock_get):
     mock_open.side_effect = IOError
     cfg = config.get_config()
     self.assertEqual(config.DEFAULT_CONF, cfg)