1
# Copyright 2017 Canonical Limited.
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
17
from charmhelpers.core.hookenv import log
19
stats_intervals = ['stats_day', 'stats_five_minute',
20
'stats_hour', 'stats_total']
29
def __init__(self, cachepath):
30
self.cachepath = cachepath
33
def fromdevice(cls, devname):
34
return cls('{}/block/{}/bcache'.format(SYSFS, devname))
39
def get_stats(self, interval):
42
intervaldir = 'stats_{}'.format(interval)
43
path = "{}/{}".format(self.cachepath, intervaldir)
45
for elem in os.listdir(path):
46
out[elem] = open('{}/{}'.format(path, elem)).read().strip()
51
"""Return all cache sets
53
cachesetroot = "{}/fs/bcache".format(SYSFS)
55
dirs = os.listdir(cachesetroot)
57
log("No bcache fs found")
59
cacheset = set([Bcache('{}/{}'.format(cachesetroot, d)) for d in dirs if not d.startswith('register')])
63
def get_stats_action(cachespec, interval):
64
"""Action for getting bcache statistics
66
if cachespec == 'global':
67
caches = get_bcache_fs()
69
caches = [Bcache.fromdevice(cachespec)]
70
res = dict((c.cachepath, c.get_stats(interval)) for c in caches)
71
return json.dumps(res, indent=4, separators=(',', ': '))