Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / keystone / keystone_stats @ 017ad1f8

Historique | Voir | Annoter | Télécharger (2,86 ko)

1
#!/usr/bin/env python
2
#
3
# Plugin to monitor status of Keystone
4
#
5
# Needs following minimal configuration in plugin-conf.d/keystone:
6
#   [keystone_*]
7
#   user keystone
8
#
9
# Magic markers
10
#%# capabilities=autoconf
11
#%# family=auto
12

    
13
import sys
14
import traceback
15

    
16
try:
17
    from keystone.common import utils
18
    from keystone import config
19
    from keystone import exception
20
    from keystone import identity
21
except ImportError:
22
    succesful_import = False
23
else:
24
    succesful_import = True
25

    
26
stats = ['users', 'tenants']
27

    
28
def print_config():
29
    global states
30
    print 'graph_title Keystone Stats'
31
    print 'graph_vlabel count'
32
    print 'graph_args --base 1000 --lower-limit 0'
33
    print 'graph_category keystone'
34
    print 'graph_scale no'
35
    print 'graph_info This graph shows stats about keystone: ' + (', ').join(stats)
36
    for field in stats:
37
        print '%s_enabled.label enabled %s' % (field, field)
38
        print '%s_enabled.draw LINE2' % field
39
        print '%s_enabled.info %s enabled' % (field, field)
40
        print '%s_total.label total %s' % (field, field)
41
        print '%s_total.draw LINE2' % field
42
        print '%s_total.info %s total' % (field, field)
43

    
44

    
45
def get_status():
46
    enabled = {}
47
    total = {}
48
    for k in stats:
49
        enabled[k] = 0
50
        total[k] = 0
51

    
52
    identity_api = identity.Manager()
53

    
54
    for user in identity_api.list_users(None):
55
        total['users'] += 1
56
        if user['enabled']:
57
            enabled['users'] += 1
58
    
59
    # Ldap and  pam driver don't support get_all_tenants()
60
    # kvs and sql implement get_tenants() instead of get_all_tenants()
61
    # Whoo: None of backend implements the correct function
62
    tenants = []
63
    for api_func in [ 'get_all_tenants', 'get_tenants']:
64
        try:
65
            tenants = getattr(identity_api, api_func)(None)
66
        except exception.NotImplemented, NotImplementedError:
67
            pass
68

    
69

    
70
    for tenant in tenants:
71
        total['tenants'] += 1
72
        if tenant['enabled']:
73
            enabled['tenants'] += 1
74

    
75
    return {'enabled': enabled, 'total': total}
76

    
77

    
78
def print_values():
79
    stats = get_status()
80
    for state in stats.keys():
81
        for (field, value) in stats[state].iteritems():
82
            print "%s_%s.value %s" % (field, state, value)
83

    
84
def load_conf():
85
    config.CONF(config_files=[utils.find_config('keystone.conf')])
86

    
87
if __name__ == '__main__':
88
    if len(sys.argv) > 1:
89
        if sys.argv[1] == "config":
90
            print_config()
91
        elif sys.argv[1] == "autoconf":
92
            if not succesful_import:  
93
                print 'no (failed import keystone module)'
94
                sys.exit(0)
95
            try:
96
                load_conf()
97
                identity.Manager()
98
            except:
99
                print 'no (failed to connect keystone backend: %s'%traceback.format_exc()
100
                sys.exit(0)
101
            print 'yes'
102

    
103
    elif succesful_import:
104
        load_conf()
105
        print_values()
106