Example #1
0
def send_metrics(metric_tuples):
    """Sends a list of metric tuples to the pre-configured carbon backend.

    :param metric_tuples: A list of metric tuples in the form
                          [(path, (timestamp, value)), ...]

    """
    host = CONFIG.get("carbon", "host")
    port = CONFIG.getint("carbon", "port")
    return send_metrics_to(metric_tuples, host, port)
Example #2
0
File: names.py Project: yytsui/nav
def raw_metric_query(query):
    """Runs a query for metric information against Graphite's REST API.

    :param query: A search string, e.g. "nav.devices.some-gw_example_org.*"
    :returns: A list of matching metrics, each represented by a dict.

    """
    base = CONFIG.get("graphiteweb", "base")
    url = urljoin(base, "/metrics/find")
    query = urlencode({'query': query})
    url = "%s?%s" % (url, query)

    req = Request(url)
    try:
        response_data = urlopen(req).read().decode('utf-8')
        return json.loads(response_data)
    except URLError as err:
        raise errors.GraphiteUnreachableError(
            "{0} is unreachable".format(base), err)
    except ValueError:
        # response could not be decoded
        return []
    finally:
        try:
            response.close()
        except NameError:
            pass
Example #3
0
def index(request, uri):
    """
    Proxies render requests to graphite-web, as configured in graphite.conf
    """
    if request.method == 'HEAD':
        response = HttpResponse(content_type='application/octet-stream')
        response['X-Where-Am-I'] = request.get_full_path()
        return response

    base = CONFIG.get('graphiteweb', 'base')

    if request.method == 'GET':
        query = _inject_default_arguments(request.GET)
        url = urljoin(base, uri + ('?' + query) if query else '')
        req = urllib2.Request(url)
    elif request.method == 'POST':
        data = _inject_default_arguments(request.POST)
        url = urljoin(base, uri)
        req = urllib2.Request(url, data)
    else:
        return HttpResponseNotAllowed(['GET', 'POST', 'HEAD'])

    LOGGER.debug("proxying request to %r", url)
    response = urllib2.urlopen(req)
    headers = response.info()
    content_type = headers.getheader('Content-Type', 'text/html')
    return HttpResponse(response.read(), content_type=content_type)
Example #4
0
def get_metric_data(target, start="-5min", end="now"):
    """
    Retrieves raw datapoints from a graphite target for a given period of time.

    :param target: A metric path string or a list of multiple metric paths
    :param start: A start time specification that Graphite will accept.
    :param end: An end time specification that Graphite will accept.

    :returns: A raw, response from Graphite. Normally a list of dicts that
              represent the names and datapoints of each matched target,
              like so::

                  [{'target': 'x', 'datapoints': [(value, timestamp), ...]}]

    """
    if not target:
        return []  # no point in wasting time on http requests for no data

    base = CONFIG.get("graphiteweb", "base")
    url = urljoin(base, "/render/")

    # What does Graphite accept of formats? Lets check if the parameters are
    # datetime objects and try to force a format then
    if isinstance(start, datetime):
        start = start.strftime('%H:%M%Y%m%d')
    if isinstance(end, datetime):
        end = end.strftime('%H:%M%Y%m%d')

    query = {
        'target': target,
        'from': start,
        'until': end,
        'format': 'json',
    }
    query = urlencode(query, True)

    _logger.debug("get_metric_data%r", (target, start, end))
    req = Request(url, data=query.encode('utf-8'))
    try:
        response = urlopen(req)
        json_data = json.load(codecs.getreader('utf-8')(response))
        _logger.debug("get_metric_data: returning %d results", len(json_data))
        return json_data
    except HTTPError as err:
        _logger.error("Got a 500 error from graphite-web when fetching %s"
                      "with data %s", err.url, query)
        _logger.error("Graphite output: %s", err.fp.read())
        raise errors.GraphiteUnreachableError(
            "{0} is unreachable".format(base), err)
    except URLError as err:
        raise errors.GraphiteUnreachableError(
            "{0} is unreachable".format(base), err)
    except ValueError:
        # response could not be decoded
        return []
    finally:
        try:
            response.close()
        except NameError:
            pass
Example #5
0
File: views.py Project: Uninett/nav
def index(request, uri):
    """
    Proxies render requests to graphite-web, as configured in graphite.conf
    """
    base = CONFIG.get('graphiteweb', 'base')

    if request.method in ('GET', 'HEAD'):
        query = _inject_default_arguments(request.GET)
        url = urljoin(base, uri + ('?' + query) if query else '')
        req = Request(url)
        data = None
    elif request.method == 'POST':
        data = _inject_default_arguments(request.POST).encode('utf-8')
        url = urljoin(base, uri)
        req = Request(url, data)
    else:
        return HttpResponseNotAllowed(['GET', 'POST', 'HEAD'])

    _logger.debug("proxying request to %r", url)
    try:
        proxy = urlopen(req)
    except HTTPError as error:
        status = error.code
        headers = error.hdrs
        output = error.fp.read()

        _logger.error(
            "%s error on graphite render request: "
            "%r with arguments: %r",
            status,
            url,
            data,
        )

    else:
        status = proxy.getcode()
        headers = proxy.info()
        output = proxy.read()

    content_type = headers.get('Content-Type', 'text/html')

    if request.method == 'HEAD':
        response = HttpResponse(content_type=content_type, status=status)
        response['Content-Length'] = headers.get('Content-Length', '0')
    else:
        response = HttpResponse(output,
                                content_type=content_type,
                                status=status)

    response['X-Where-Am-I'] = request.get_full_path()
    return response
Example #6
0
def _inject_default_arguments(query):
    """
    Injects default arguments to a render request, unless they are already
    explicitly supplied by the client.
    """
    format_ = CONFIG.get('graphiteweb', 'format')
    query = query.copy()

    if 'format' not in query:
        query['format'] = format_
    if 'tz' not in query and settings.TIME_ZONE:
        query['tz'] = settings.TIME_ZONE

    return query.urlencode()
Example #7
0
def get_metric_data(target, start="-5min", end="now"):
    """
    Retrieves raw datapoints from a graphite target for a given period of time.

    :param target: A metric path string or a list of multiple metric paths
    :param start: A start time specification that Graphite will accept.
    :param end: An end time specification that Graphite will accept.

    :returns: A raw, response from Graphite. Normally a list of dicts that
              represent the names and datapoints of each matched target,
              like so::

                  [{'target': 'x', 'datapoints': [(value, timestamp), ...]}]

    """
    base = CONFIG.get("graphiteweb", "base")
    url = urljoin(base, "/render/")

    query = {
        'target': target,
        'from': start,
        'until': end,
        'format': 'json',
    }
    query = urlencode(query, True)

    req = urllib2.Request(url, data=query)
    try:
        response = urllib2.urlopen(req)
        return simplejson.load(response)
    except urllib2.URLError as err:
        raise errors.GraphiteUnreachableError(
            "{0} is unreachable".format(base),err)
    finally:
        try:
            response.close()
        except NameError:
            pass
Example #8
0
def raw_metric_query(query):
    """Runs a query for metric information against Graphite's REST API.

    :param query: A search string, e.g. "nav.devices.some-gw_example_org.*"
    :returns: A list of matching metrics, each represented by a dict.

    """
    base = CONFIG.get("graphiteweb", "base")
    url = urljoin(base, "/metrics/find")
    query = urlencode({'query': query})
    url = "%s?%s" % (url, query)

    req = urllib2.Request(url)
    try:
        response = urllib2.urlopen(req)
        return simplejson.load(response)
    except urllib2.URLError as err:
        raise errors.GraphiteUnreachableError(
            "{0} is unreachable".format(base),err)
    finally:
        try:
            response.close()
        except NameError:
            pass
Example #9
0
def graphite_base(_request):
    """Provide graphite dashboard url in context"""
    return {'graphite_base': CONFIG.get('graphiteweb', 'base')}
def graphite_base(request):
    """Provide graphite dashboard url in context"""
    return {
        'graphite_base': CONFIG.get('graphiteweb', 'base')
    }