#!/usr/bin/env python # -*- encoding: utf-8 -*- # # Keystone monitoring script for Nagios # # Copyright © 2012 eNovance # # Author: Florian Lambert # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU 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 General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # import sys import argparse from glance import client as glance_client from glance.common import exception from glance.common import utils from glance import version #### CONFIG #### #numbers of result per fetch (get_images) default_limit=100 ################ STATE_OK = 0 STATE_WARNING = 1 STATE_CRITICAL = 2 STATE_UNKNOWN = 3 parser = argparse.ArgumentParser(description='Check an OpenStack glance server.') parser.add_argument('--host', metavar='host', type=str, required=True, help='Glance host') parser.add_argument('--auth_url', metavar='URL', type=str, required=True, help='Keystone URL') parser.add_argument('--username', metavar='username', type=str, required=True, help='username to use for authentication') parser.add_argument('--password', metavar='password', type=str, required=True, help='password to use for authentication') parser.add_argument('--tenant', metavar='tenant', type=str, required=True, help='tenant name to use for authentication') parser.add_argument('--req_count', metavar='numberImages', type=str, required=False, help='minimum number of images in glance') parser.add_argument('--req_images', metavar='imagesName', type=str, nargs='+', required=False, help='name of images who must be available') parser.add_argument('--region_name', metavar='region_name', type=str, help='Region to select for authentication') args = parser.parse_args() try: c = glance_client.get_client(host=args.host, username=args.username, password=args.password, tenant=args.tenant, auth_url=args.auth_url, region=args.region_name) except Exception as e: print str(e) sys.exit(STATE_CRITICAL) if args.req_images : required_images = args.req_images listimage = ', '.join(required_images) if args.req_count : required_count = int(args.req_count) valid_image = 0 count = 0 def iterimage(c, limit=default_limit, marker=None): if marker: images = c.get_images(**{ "limit": limit, "marker": marker }) else: images = c.get_images(**{ "limit": limit }) for image in images: if image["disk_format"] == "ari" or image["disk_format"] == "aki" : continue yield image["name"] if len(images) == limit: for i in iterimage(c, limit, images[-1]['id']): yield i return if args.req_images or args.req_count: for image in iterimage(c): if args.req_count: count = count + 1 if count == required_count and not args.req_images: break if args.req_images: try: required_images.remove(image) if len(required_images) == 0: valid_image = 1 args.req_images = 1 if not args.req_count: break except : pass if args.req_images and args.req_count and valid_image == 1 and count == required_count: break if args.req_count and count < required_count: print "Failed - not enough images %d/%d" % (count,required_count) sys.exit(STATE_CRITICAL) if args.req_images and valid_image == 0: print "Failed - '%s' image not found" % (required_images) sys.exit(STATE_WARNING) if (args.req_images and args.req_count): print "OK - %s images found and more than %d images" % (listimage, required_count) elif args.req_images: print "OK - %s images found" % (listimage) elif args.req_count: print "OK - more than %d images found" % (required_count) else : print "OK - Connection glance established" sys.exit(STATE_OK)