root / plugins / glance / glance_status @ 17f78427
Historique | Voir | Annoter | Télécharger (2,72 ko)
| 1 |
#!/usr/bin/env python |
|---|---|
| 2 |
# |
| 3 |
# Plugin to monitor used size of a tenant in glance |
| 4 |
# |
| 5 |
# To monitor the used size of a tenant in glance do: |
| 6 |
# E.g. |
| 7 |
# ln -s /usr/share/munin/plugins/glance_size_ /etc/munin/plugins/glance_size_<tenant_uuid> |
| 8 |
# |
| 9 |
# Needs following minimal configuration in plugin-conf.d/glance: |
| 10 |
# [glance_*] |
| 11 |
# user glance |
| 12 |
# |
| 13 |
# Magic markers |
| 14 |
#%# capabilities=autoconf |
| 15 |
#%# family=auto |
| 16 |
|
| 17 |
|
| 18 |
import sys |
| 19 |
import os |
| 20 |
|
| 21 |
try: |
| 22 |
from sqlalchemy.orm import exc, joinedload |
| 23 |
|
| 24 |
from glance.common.cfg import CommonConfigOpts |
| 25 |
from glance.registry.db import models |
| 26 |
from glance.registry.db.api import get_session, configure_db |
| 27 |
except ImportError: |
| 28 |
successful_import = False |
| 29 |
else: |
| 30 |
successful_import = True |
| 31 |
|
| 32 |
def load_conf(): |
| 33 |
CONF = CommonConfigOpts(project="glance", prog="glance-registry") |
| 34 |
CONF() |
| 35 |
|
| 36 |
# Hide missing logger warning message |
| 37 |
sys.stderr = open(os.devnull, 'w') |
| 38 |
configure_db(CONF) |
| 39 |
sys.stderr = sys.__stderr__ |
| 40 |
|
| 41 |
possible_status = [ 'queued', 'saving', 'active', 'killed', 'deleted', 'pending_delete' ] |
| 42 |
|
| 43 |
def print_config(): |
| 44 |
print 'graph_title Glance images status' |
| 45 |
print 'graph_vlabel images' |
| 46 |
print 'graph_args --lower-limit 0' |
| 47 |
print 'graph_category cloud' |
| 48 |
print 'graph_scale no' |
| 49 |
print 'graph_info This graph show number of images by status' |
| 50 |
|
| 51 |
for status in possible_status: |
| 52 |
print '%s.label %s' % (status, status) |
| 53 |
print '%s.draw LINE2' % status |
| 54 |
print '%s.info %s image(s)' % (status, status) |
| 55 |
|
| 56 |
def request(**kwargs): |
| 57 |
|
| 58 |
session = get_session() |
| 59 |
try: |
| 60 |
query = session.query(models.Image).\ |
| 61 |
options(joinedload(models.Image.properties)).\ |
| 62 |
options(joinedload(models.Image.members)) |
| 63 |
if kwargs: |
| 64 |
query = query.filter_by(**kwargs) |
| 65 |
|
| 66 |
images = query.all() |
| 67 |
|
| 68 |
except exc.NoResultFound: |
| 69 |
return [] |
| 70 |
return images |
| 71 |
|
| 72 |
def print_values(): |
| 73 |
images = request() |
| 74 |
|
| 75 |
n_image_by_status = {}
|
| 76 |
for image in images: |
| 77 |
n_image_by_status[image["status"]] = n_image_by_status.get(image["status"], 0) + 1 |
| 78 |
|
| 79 |
for status in possible_status: |
| 80 |
print '%s.value %s' % (status, n_image_by_status.get(status, 0)) |
| 81 |
|
| 82 |
if __name__ == '__main__': |
| 83 |
argv = sys.argv[:] |
| 84 |
|
| 85 |
if len(argv) > 1: |
| 86 |
if argv[1] == 'config': |
| 87 |
print_config() |
| 88 |
elif argv[1] == 'autoconf': |
| 89 |
if not successful_import: |
| 90 |
print 'no (failed import glance and/or sqlachemy module)' |
| 91 |
sys.exit(0) |
| 92 |
try: |
| 93 |
load_conf() |
| 94 |
get_session() |
| 95 |
except: |
| 96 |
print 'no (failed to connect glance backend, check user)' |
| 97 |
sys.exit(0) |
| 98 |
print 'yes' |
| 99 |
|
| 100 |
elif successful_import: |
| 101 |
load_conf() |
| 102 |
print_values() |
| 103 |
|
