This file is indexed.

/usr/lib/python3/dist-packages/gnocchi/rest/influxdb.py is in python3-gnocchi 4.2.0-0ubuntu5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# -*- encoding: utf-8 -*-
#
# Copyright © 2017 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.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 collections
import time

from gnocchi import incoming
from gnocchi import indexer
from gnocchi.rest import api
from gnocchi import utils

import daiquiri
import numpy
import pbr.version
import pecan
from pecan import rest
import pyparsing
import six
import tenacity
try:
    import uwsgi
except ImportError:
    uwsgi = None


LOG = daiquiri.getLogger(__name__)


boolean = "False|True|false|true|FALSE|TRUE|F|T|f|t"
boolean = pyparsing.Regex(boolean).setParseAction(
    lambda t: t[0].lower()[0] == "t")

quoted_string = pyparsing.QuotedString('"', escChar="\\")
unquoted_string = pyparsing.OneOrMore(
    pyparsing.CharsNotIn(" ,=\\") +
    pyparsing.Optional(
        pyparsing.OneOrMore(
            (pyparsing.Literal("\\ ") |
             pyparsing.Literal("\\,") |
             pyparsing.Literal("\\=") |
             pyparsing.Literal("\\")).setParseAction(
                 lambda s, loc, tok: tok[0][-1])))).setParseAction(
                     lambda s, loc, tok: "".join(list(tok)))
measurement = tag_key = tag_value = field_key = quoted_string | unquoted_string
number = r"[+-]?\d+(:?\.\d*)?(:?[eE][+-]?\d+)?"
number = pyparsing.Regex(number).setParseAction(
    lambda s, loc, tok: float(tok[0]))
integer = (
    pyparsing.Word(pyparsing.nums).setParseAction(
        lambda s, loc, tok: int(tok[0])) +
    pyparsing.Suppress("i")
    )
field_value = integer | number | quoted_string
timestamp = pyparsing.Word(pyparsing.nums).setParseAction(
    lambda s, loc, tok: numpy.datetime64(int(tok[0]), 'ns'))

line_protocol = (
    measurement +
    # Tags
    pyparsing.Optional(pyparsing.Suppress(",") +
                       pyparsing.delimitedList(
                           pyparsing.OneOrMore(
                               pyparsing.Group(
                                   tag_key +
                                   pyparsing.Suppress("=") +
                                   tag_value), ",")).setParseAction(
                                       lambda s, loc, tok: dict(list(tok))),
                       default={}) +
    pyparsing.Suppress(" ") +
    # Fields
    pyparsing.delimitedList(
        pyparsing.OneOrMore(
            pyparsing.Group(field_key +
                            pyparsing.Suppress("=") +
                            field_value), ",")).setParseAction(
                                lambda s, loc, tok: dict(list(tok))) +
    # Timestamp
    pyparsing.Optional(pyparsing.Suppress(" ") + timestamp, default=None)
).leaveWhitespace()


query_parser = (
    pyparsing.Suppress(pyparsing.CaselessLiteral("create")) +
    pyparsing.Suppress(pyparsing.CaselessLiteral("database")) +
    pyparsing.Suppress(pyparsing.White()) +
    (pyparsing.QuotedString('"', escChar="\\") |
     pyparsing.Word(pyparsing.alphas + "_",
                    pyparsing.alphanums + "_")) +
    pyparsing.Suppress(
        pyparsing.Optional(pyparsing.Optional(pyparsing.White()) +
                           pyparsing.Optional(pyparsing.Literal(";"))))
)


class InfluxDBController(rest.RestController):
    _custom_actions = {
        'ping': ['HEAD', 'GET'],
        'query': ['POST'],
        'write': ['POST'],
    }

    DEFAULT_TAG_RESOURCE_ID = "host"

    @pecan.expose()
    def ping(self):
        pecan.response.headers['X-Influxdb-Version'] = (
            "Gnocchi " + pbr.version.VersionInfo('gnocchi').version_string()
        )

    @pecan.expose('json')
    def post_query(self, q=None):
        if q is not None:
            try:
                query = query_parser.parseString(q)
            except pyparsing.ParseException:
                api.abort(501, {"cause": "Not implemented error",
                                "detail": "q",
                                "reason": "Query not implemented"})
            resource_type = query[0]
            api.enforce("create resource type", {"name": resource_type})
            schema = pecan.request.indexer.get_resource_type_schema()
            rt = schema.resource_type_from_dict(resource_type, {}, 'creating')
            try:
                pecan.request.indexer.create_resource_type(rt)
            except indexer.ResourceTypeAlreadyExists:
                pass
            pecan.response.status = 204

    @staticmethod
    def _write_get_lines():
        encoding = pecan.request.headers.get('Transfer-Encoding', "").lower()
        if encoding == "chunked":
            # TODO(sileht): Support reading chunk without uwsgi when
            # pecan.request.environ['wsgi.input_terminated'] is set.
            # https://github.com/unbit/uwsgi/issues/1428
            if uwsgi is None:
                api.abort(
                    501, {"cause": "Not implemented error",
                          "reason": "This server is not running with uwsgi"})
            return encoding, uwsgi.chunked_read()
        return None, pecan.request.body

    @pecan.expose('json')
    def post_write(self, db="influxdb"):

        creator = pecan.request.auth_helper.get_current_user(pecan.request)
        tag_to_rid = pecan.request.headers.get(
            "X-Gnocchi-InfluxDB-Tag-Resource-ID",
            self.DEFAULT_TAG_RESOURCE_ID)

        while True:
            encoding, chunk = self._write_get_lines()

            # If chunk is empty then this is over.
            if not chunk:
                break

            # Compute now on a per-chunk basis
            now = numpy.datetime64(int(time.time() * 10e8), 'ns')

            # resources = { resource_id: {
            #     metric_name: [ incoming.Measure(t, v), …], …
            #   }, …
            # }
            resources = collections.defaultdict(
                lambda: collections.defaultdict(list))
            for line_number, line in enumerate(chunk.split(b"\n")):
                # Ignore empty lines
                if not line:
                    continue

                try:
                    measurement, tags, fields, timestamp = (
                        line_protocol.parseString(line.decode())
                    )
                except (UnicodeDecodeError, SyntaxError,
                        pyparsing.ParseException):
                    api.abort(400, {
                        "cause": "Value error",
                        "detail": "line",
                        "reason": "Unable to parse line %d" % (
                            line_number + 1),
                    })

                if timestamp is None:
                    timestamp = now

                try:
                    resource_id = tags.pop(tag_to_rid)
                except KeyError:
                    api.abort(400, {
                        "cause": "Value error",
                        "detail": "key",
                        "reason": "Unable to find key `%s' in tags" % (
                            tag_to_rid),
                    })

                tags_str = (("@" if tags else "") +
                            ",".join(("%s=%s" % (k, tags[k]))
                                     for k in sorted(tags)))

                for field_name, field_value in six.iteritems(fields):
                    if isinstance(field_value, str):
                        # We do not support field value that are not numerical
                        continue

                    # Metric name is the:
                    # <measurement>.<field_key>@<tag_key>=<tag_value>,…
                    # with tag ordered
                    # Replace "/" with "_" because Gnocchi does not support /
                    # in metric names
                    metric_name = (
                        measurement + "." + field_name + tags_str
                    ).replace("/", "_")

                    resources[resource_id][metric_name].append(
                        incoming.Measure(timestamp, field_value))

            measures_to_batch = {}
            for resource_name, metrics_and_measures in six.iteritems(
                    resources):
                resource_name = resource_name
                resource_id = utils.ResourceUUID(
                    resource_name, creator=creator)
                LOG.debug("Getting metrics from resource `%s'", resource_name)
                timeout = pecan.request.conf.api.operation_timeout
                metrics = (
                    api.get_or_create_resource_and_metrics.retry_with(
                        stop=tenacity.stop_after_delay(timeout))(
                            creator, resource_id, resource_name,
                            metrics_and_measures.keys(),
                            {}, db)
                )

                for metric in metrics:
                    api.enforce("post measures", metric)

                measures_to_batch.update(
                    dict((metric.id, metrics_and_measures[metric.name])
                         for metric in metrics
                         if metric.name in metrics_and_measures))

            LOG.debug("Add measures batch for %d metrics",
                      len(measures_to_batch))
            pecan.request.incoming.add_measures_batch(measures_to_batch)
            pecan.response.status = 204

            if encoding != "chunked":
                return