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
|
#!/usr/bin/env python
#
# Copyright (C) 2012 eNovance <licensing@enovance>
#
# Authors: Mehdi Abaakouk <sileht@sileht.net>
# Gael Lambert <gael.lambert@enovance.com>
# Jesse Andrews <anotherjesse@rackspace.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
# 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
#
# To show tenant name plugin must run as root
#
# Magic markers
#%# capabilities=autoconf suggest
#%# family=auto
import sys
import os
try:
from sqlalchemy.orm import joinedload
import sqlalchemy.exc
from glance.common.cfg import CommonConfigOpts
from glance.registry.db import models
from glance.registry.db.api import get_session, configure_db
from keystone.common import utils
from keystone import config
from keystone import exception
from keystone import identity
except ImportError:
succesful_import = False
else:
succesful_import = True
def get_name_from_tenant(tenant):
try:
KEYSTONE_CONF = config.CONF(config_files=[utils.find_config('keystone.conf')])
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
if not tenant_info:
return tenant
else:
return tenant_info["name"]
def load_conf():
CONF = CommonConfigOpts(project="glance", prog="glance-registry")
CONF()
# Hide missing logger warning message
sys.stderr = open(os.devnull, 'w')
configure_db(CONF)
sys.stderr = sys.__stderr__
def print_config(tenant):
if tenant == "Global":
print 'graph_title Glance used size for all tenants'
print 'graph_info This graph shows the used size in glance for all tenants'
else:
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 glance'
print '%s.label %s' % (tenant, get_name_from_tenant(tenant))
print '%s.draw LINE2' % tenant
print '%s.info %s MBytes' % (tenant, tenant)
def request(**kwargs):
session = get_session()
try:
query = session.query(models.Image).\
options(joinedload(models.Image.properties)).\
options(joinedload(models.Image.members))
if kwargs:
query = query.filter_by(**kwargs)
images = query.all()
except exc.NoResultFound:
return []
return images
def print_suggest():
print "Global"
print "\n".join(set( image["owner"] for image in request(deleted=False) ))
def print_values(tenant):
if tenant != "Global" :
images = request(deleted=False, owner=tenant)
else:
images = request(deleted=False)
total_size = sum([ image["size"] for image in images ])
print '%s.value %s' % (tenant, total_size)
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)
elif argv[1] == 'suggest' and succesful_import:
load_conf()
print_suggest()
elif argv[1] == 'autoconf':
if not succesful_import:
print 'no (failed import glance and/or sqlachemy module)'
sys.exit(0)
try:
load_conf()
get_session()
except:
print 'no (failed to connect glance backend, check user)'
sys.exit(0)
print 'yes'
elif succesful_import:
load_conf()
print_values(tenant)
|