/usr/lib/python3/dist-packages/gnocchi/storage/file.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 | # -*- encoding: utf-8 -*-
#
# Copyright © 2014 Objectif Libre
# Copyright © 2015 Red Hat
#
# 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 errno
import os
import shutil
import tempfile
from oslo_config import cfg
from gnocchi import storage
from gnocchi import utils
OPTS = [
cfg.StrOpt('file_basepath',
default='/var/lib/gnocchi',
help='Path used to store gnocchi data files.'),
]
class FileStorage(storage.StorageDriver):
WRITE_FULL = True
def __init__(self, conf, coord=None):
super(FileStorage, self).__init__(conf, coord)
self.basepath = conf.file_basepath
self.basepath_tmp = os.path.join(self.basepath, 'tmp')
def upgrade(self):
utils.ensure_paths([self.basepath_tmp])
def __str__(self):
return "%s: %s" % (self.__class__.__name__, str(self.basepath))
def _atomic_file_store(self, dest, data):
tmpfile = tempfile.NamedTemporaryFile(
prefix='gnocchi', dir=self.basepath_tmp,
delete=False)
tmpfile.write(data)
tmpfile.close()
os.rename(tmpfile.name, dest)
def _build_metric_dir(self, metric):
return os.path.join(self.basepath, str(metric.id))
def _build_unaggregated_timeserie_path(self, metric, version=3):
return os.path.join(
self._build_metric_dir(metric),
'none' + ("_v%s" % version if version else ""))
def _build_metric_path(self, metric, aggregation):
return os.path.join(self._build_metric_dir(metric),
"agg_" + aggregation)
def _build_metric_path_for_split(self, metric, aggregation,
key, version=3):
path = os.path.join(
self._build_metric_path(metric, aggregation),
str(key)
+ "_"
+ str(utils.timespan_total_seconds(key.sampling)))
return path + '_v%s' % version if version else path
def _create_metric(self, metric):
path = self._build_metric_dir(metric)
try:
os.mkdir(path, 0o750)
except OSError as e:
if e.errno == errno.EEXIST:
raise storage.MetricAlreadyExists(metric)
raise
for agg in metric.archive_policy.aggregation_methods:
try:
os.mkdir(self._build_metric_path(metric, agg), 0o750)
except OSError as e:
if e.errno != errno.EEXIST:
raise
def _store_unaggregated_timeserie(self, metric, data, version=3):
dest = self._build_unaggregated_timeserie_path(metric, version)
with open(dest, "wb") as f:
f.write(data)
def _get_unaggregated_timeserie(self, metric, version=3):
path = self._build_unaggregated_timeserie_path(metric, version)
try:
with open(path, 'rb') as f:
return f.read()
except IOError as e:
if e.errno == errno.ENOENT:
raise storage.MetricDoesNotExist(metric)
raise
def _list_split_keys(self, metric, aggregation, granularity, version=3):
try:
files = os.listdir(self._build_metric_path(metric, aggregation))
except OSError as e:
if e.errno == errno.ENOENT:
raise storage.MetricDoesNotExist(metric)
raise
keys = set()
granularity = str(utils.timespan_total_seconds(granularity))
for f in files:
meta = f.split("_")
if meta[1] == granularity and self._version_check(f, version):
keys.add(meta[0])
return keys
def _delete_metric_measures(self, metric, key, aggregation, version=3):
os.unlink(self._build_metric_path_for_split(
metric, aggregation, key, version))
def _store_metric_measures(self, metric, key, aggregation,
data, offset=None, version=3):
self._atomic_file_store(
self._build_metric_path_for_split(
metric, aggregation, key, version),
data)
def _delete_metric(self, metric):
path = self._build_metric_dir(metric)
try:
shutil.rmtree(path)
except OSError as e:
if e.errno != errno.ENOENT:
# NOTE(jd) Maybe the metric has never been created (no
# measures)
raise
def _get_measures_unbatched(self, metric, key, aggregation, version=3):
path = self._build_metric_path_for_split(
metric, aggregation, key, version)
try:
with open(path, 'rb') as aggregation_file:
return aggregation_file.read()
except IOError as e:
if e.errno == errno.ENOENT:
if os.path.exists(self._build_metric_dir(metric)):
raise storage.AggregationDoesNotExist(
metric, aggregation, key.sampling)
raise storage.MetricDoesNotExist(metric)
raise
|