2012-05-03 15:27:00 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Plugin to monitor used size of a tenant in glance
|
|
|
|
#
|
|
|
|
# To monitor the used size of a tenant in glance do:
|
|
|
|
# E.g.
|
|
|
|
# ln -s /usr/share/munin/plugins/glance_size_ /etc/munin/plugins/glance_size_<tenant_uuid>
|
|
|
|
#
|
|
|
|
# Needs following minimal configuration in plugin-conf.d/glance:
|
|
|
|
# [glance_*]
|
|
|
|
# user glance
|
|
|
|
#
|
2012-05-22 11:20:49 +02:00
|
|
|
# To show tenant name plugin must run as root
|
|
|
|
#
|
2012-05-03 15:27:00 +02:00
|
|
|
# Magic markers
|
2018-03-27 04:30:53 +02:00
|
|
|
# #%# capabilities=autoconf suggest
|
|
|
|
# #%# family=auto
|
2012-05-03 15:27:00 +02:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
|
2018-03-27 04:30:53 +02:00
|
|
|
try:
|
2012-05-22 11:20:49 +02:00
|
|
|
from sqlalchemy.orm import joinedload
|
|
|
|
import sqlalchemy.exc
|
2018-03-27 04:30:53 +02:00
|
|
|
|
2012-05-03 15:27:00 +02:00
|
|
|
from glance.common.cfg import CommonConfigOpts
|
|
|
|
from glance.registry.db import models
|
|
|
|
from glance.registry.db.api import get_session, configure_db
|
2012-05-22 11:20:49 +02:00
|
|
|
|
|
|
|
from keystone.common import utils
|
2018-03-27 04:30:53 +02:00
|
|
|
from keystone import config, exception, identity
|
2012-05-22 11:20:49 +02:00
|
|
|
|
2012-05-03 15:27:00 +02:00
|
|
|
except ImportError:
|
2014-12-05 00:37:42 +01:00
|
|
|
successful_import = False
|
2012-05-03 15:27:00 +02:00
|
|
|
else:
|
2014-12-05 00:37:42 +01:00
|
|
|
successful_import = True
|
2012-05-03 15:27:00 +02:00
|
|
|
|
|
|
|
|
2012-05-22 11:20:49 +02:00
|
|
|
def get_name_from_tenant(tenant):
|
|
|
|
try:
|
2018-03-27 04:30:53 +02:00
|
|
|
config.CONF(config_files=[utils.find_config('keystone.conf')])
|
2012-05-22 11:20:49 +02:00
|
|
|
except:
|
|
|
|
# keystone configuration can not be loaded, use id as name"
|
|
|
|
return tenant
|
|
|
|
|
|
|
|
identity_api = identity.Manager()
|
|
|
|
|
|
|
|
try:
|
|
|
|
tenant_info = identity_api.get_tenant(None, tenant)
|
|
|
|
except sqlalchemy.exc.OperationalError:
|
|
|
|
# keystone database can not be connected, use id as name"
|
|
|
|
return tenant
|
|
|
|
|
2018-03-27 04:30:53 +02:00
|
|
|
if not tenant_info:
|
2012-05-22 11:20:49 +02:00
|
|
|
return tenant
|
|
|
|
else:
|
|
|
|
return tenant_info["name"]
|
|
|
|
|
2018-03-27 04:30:53 +02:00
|
|
|
|
2012-05-03 15:27:00 +02:00
|
|
|
def load_conf():
|
2018-03-27 04:30:53 +02:00
|
|
|
loaded_config = CommonConfigOpts(project="glance", prog="glance-registry")
|
|
|
|
loaded_config()
|
2012-05-03 15:27:00 +02:00
|
|
|
|
|
|
|
# Hide missing logger warning message
|
|
|
|
sys.stderr = open(os.devnull, 'w')
|
2018-03-27 04:30:53 +02:00
|
|
|
configure_db(loaded_config)
|
2012-05-03 15:27:00 +02:00
|
|
|
sys.stderr = sys.__stderr__
|
|
|
|
|
|
|
|
|
|
|
|
def print_config(tenant):
|
2012-05-22 11:20:49 +02:00
|
|
|
if tenant == "Global":
|
2018-03-27 04:30:53 +02:00
|
|
|
print('graph_title Glance used size for all tenants')
|
|
|
|
print('graph_info This graph shows the used size in glance for all tenants')
|
2012-05-22 11:20:49 +02:00
|
|
|
else:
|
2018-03-27 04:30:53 +02:00
|
|
|
print('graph_title Glance used size for tenant %s' % get_name_from_tenant(tenant))
|
|
|
|
print('graph_info This graph shows the used size in glance for tenant %s' % tenant)
|
|
|
|
print('graph_vlabel Bytes')
|
|
|
|
print('graph_args --base 1024 --lower-limit 0')
|
|
|
|
print('graph_category cloud')
|
|
|
|
print('%s.label %s' % (tenant, get_name_from_tenant(tenant)))
|
|
|
|
print('%s.draw LINE2' % tenant)
|
|
|
|
print('%s.info %s MBytes' % (tenant, tenant))
|
|
|
|
|
2012-05-03 15:27:00 +02:00
|
|
|
|
|
|
|
def request(**kwargs):
|
|
|
|
|
|
|
|
session = get_session()
|
|
|
|
try:
|
2018-03-27 04:30:53 +02:00
|
|
|
query = session.query(models.Image).options(joinedload(models.Image.properties)).options(
|
|
|
|
joinedload(models.Image.members))
|
2012-05-03 15:27:00 +02:00
|
|
|
if kwargs:
|
2018-03-27 04:30:53 +02:00
|
|
|
query = query.filter_by(**kwargs)
|
2012-05-03 15:27:00 +02:00
|
|
|
|
|
|
|
images = query.all()
|
|
|
|
|
2018-03-27 04:30:53 +02:00
|
|
|
except exception.NoResultFound:
|
2012-05-03 15:27:00 +02:00
|
|
|
return []
|
|
|
|
return images
|
|
|
|
|
2018-03-27 04:30:53 +02:00
|
|
|
|
2012-05-03 15:27:00 +02:00
|
|
|
def print_suggest():
|
2018-03-27 04:30:53 +02:00
|
|
|
print("Global")
|
|
|
|
print("\n".join(set(image["owner"] for image in request(deleted=False))))
|
|
|
|
|
2012-05-03 15:27:00 +02:00
|
|
|
|
|
|
|
def print_values(tenant):
|
|
|
|
|
2018-03-27 04:30:53 +02:00
|
|
|
if tenant != "Global":
|
2012-05-03 15:27:00 +02:00
|
|
|
images = request(deleted=False, owner=tenant)
|
|
|
|
else:
|
|
|
|
images = request(deleted=False)
|
|
|
|
|
2018-03-27 04:30:53 +02:00
|
|
|
total_size = sum([image["size"] for image in images])
|
|
|
|
print('%s.value %s' % (tenant, total_size))
|
2012-05-03 15:27:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
argv = sys.argv[:]
|
|
|
|
tenant = argv[0].split('glance_size_').pop() or 'Global'
|
|
|
|
|
|
|
|
if len(argv) > 1:
|
|
|
|
if argv[1] == 'config':
|
|
|
|
print_config(tenant)
|
2014-12-05 00:37:42 +01:00
|
|
|
elif argv[1] == 'suggest' and successful_import:
|
2012-05-03 15:27:00 +02:00
|
|
|
load_conf()
|
|
|
|
print_suggest()
|
|
|
|
elif argv[1] == 'autoconf':
|
2018-03-27 04:30:53 +02:00
|
|
|
if not successful_import:
|
|
|
|
print('no (failed import glance and/or sqlachemy module)')
|
2012-05-03 15:27:00 +02:00
|
|
|
sys.exit(0)
|
|
|
|
try:
|
|
|
|
load_conf()
|
|
|
|
get_session()
|
|
|
|
except:
|
2018-03-27 04:30:53 +02:00
|
|
|
print('no (failed to connect glance backend, check user)')
|
2012-05-03 15:27:00 +02:00
|
|
|
sys.exit(0)
|
2018-03-27 04:30:53 +02:00
|
|
|
print('yes')
|
2012-05-03 15:27:00 +02:00
|
|
|
|
2014-12-05 00:37:42 +01:00
|
|
|
elif successful_import:
|
2012-05-03 15:27:00 +02:00
|
|
|
load_conf()
|
|
|
|
print_values(tenant)
|