#!/usr/bin/env python

import re
import argparse
import memcache
from ipaddr import IPv4Address

from maas_common import status_ok, status_err, metric, metric_bool


VERSION_RE = re.compile('STAT version (\d+\.\d+\.\d+)(?![-+0-9\\.])')
VERSION = '1.4.14 (Ubuntu)'
MEMCACHE_METRICS = ['total_items',
                    'get_hits',
                    'get_misses',
                    'total_connections']


def item_stats(host, port):
    """Check the stats for items and connection status."""

    mc = memcache.Client(['%s:%s' % (host, port)])
    stats = mc.get_stats()[0][1]
    if not stats:
        status_err('could not retrieve status from memcached')
    return stats


def main(args):

    bind_ip = str(args.ip)
    port = args.port

    stats = item_stats(bind_ip, port)
    current_version = stats['version']

    if current_version != VERSION:
        status_err('This plugin has only been tested with version %s '
                   'of memcached, and you are using version %s'
                   % (VERSION, current_version))

    if stats is not None:
        status_ok
        metric_bool('memcache_api_local_status', True)
        for m in MEMCACHE_METRICS:
            metric('memcache_%s' % m, 'uint64', stats[m])
    else:
        status_err('memcached is unreachable')


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Check glance API')
    parser.add_argument('ip', type=IPv4Address, help='memcached IP address.')
    parser.add_argument('--port', type=int,
                        default=11211, help='memcached port.')
    args = parser.parse_args()
    main(args)

@@ -489,16 +489,29 @@
 us.%22%22%22%0A%0A
+    try:%0A    
     mc =
@@ -553,16 +553,20 @@
 port)%5D)%0A
+    
     stat
@@ -598,20 +598,25 @@
     
-if not stats
+except IndexError
 :%0A  
@@ -647,41 +647,69 @@
 not 
-retrieve status from memcached')%0A
+connect to memcached on %25s:%25s' %25 (host, port))%0A    else:%0A    
     

handle broken memcache client better