Projet

Général

Profil

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

root / plugins / keystone / keystone_stats @ 0d055149

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

1
#!/usr/bin/env python3
2

    
3
"""
4
=head1 NAME
5

    
6

    
7
keystone_stats - monitor status of Keystone
8

    
9
=head1 CONFIGURATION
10

    
11

    
12
Needs following minimal configuration in plugin-conf.d/keystone:
13

    
14
  [keystone_*]
15
  user keystone
16

    
17
=head1 AUTHORS
18

    
19
Copyright 2012 - Mehdi Abaakouk <sileht@sileht.net>
20

    
21

    
22
=head1 MAGIC MARKERS
23

    
24
 #%# capabilities=autoconf
25
 #%# family=auto
26

    
27
=cut
28
"""
29

    
30
import sys
31
import traceback
32

    
33
try:
34
    from keystone.common import utils
35
    from keystone import config
36
    from keystone import exception
37
    from keystone import identity
38
except ImportError:
39
    successful_import = False
40
else:
41
    successful_import = True
42

    
43

    
44
stats = ['users', 'tenants']
45

    
46

    
47
def print_config():
48
    print('graph_title Keystone Stats')
49
    print('graph_vlabel count')
50
    print('graph_args --base 1000 --lower-limit 0')
51
    print('graph_category auth')
52
    print('graph_scale no')
53
    print('graph_info This graph shows stats about keystone: ' + (', ').join(stats))
54
    for field in stats:
55
        print('%s_enabled.label enabled %s' % (field, field))
56
        print('%s_enabled.draw LINE2' % field)
57
        print('%s_enabled.info %s enabled' % (field, field))
58
        print('%s_total.label total %s' % (field, field))
59
        print('%s_total.draw LINE2' % field)
60
        print('%s_total.info %s total' % (field, field))
61

    
62

    
63
def get_status():
64
    enabled = {}
65
    total = {}
66
    for k in stats:
67
        enabled[k] = 0
68
        total[k] = 0
69

    
70
    identity_api = identity.Manager()
71

    
72
    for user in identity_api.list_users(None):
73
        total['users'] += 1
74
        if user['enabled']:
75
            enabled['users'] += 1
76

    
77
    # Ldap and  pam driver don't support get_all_tenants()
78
    # kvs and sql implement get_tenants() instead of get_all_tenants()
79
    # Whoo: None of backend implements the correct function
80
    tenants = []
81
    for api_func in ['get_all_tenants', 'get_tenants']:
82
        try:
83
            tenants = getattr(identity_api, api_func)(None)
84
        except (exception.NotImplemented, NotImplementedError):
85
            pass
86

    
87
    for tenant in tenants:
88
        total['tenants'] += 1
89
        if tenant['enabled']:
90
            enabled['tenants'] += 1
91

    
92
    return {'enabled': enabled, 'total': total}
93

    
94

    
95
def print_values():
96
    stats = get_status()
97
    for state in stats.keys():
98
        for (field, value) in stats[state].items():
99
            print("%s_%s.value %s" % (field, state, value))
100

    
101

    
102
def load_conf():
103
    config.CONF(config_files=[utils.find_config('keystone.conf')])
104

    
105

    
106
if __name__ == '__main__':
107
    if len(sys.argv) > 1:
108
        if sys.argv[1] == "config":
109
            print_config()
110
        elif sys.argv[1] == "autoconf":
111
            if not successful_import:
112
                print('no (failed import keystone module)')
113
                sys.exit(0)
114
            try:
115
                load_conf()
116
                identity.Manager()
117
            except Exception:
118
                print('no (failed to connect keystone backend: %s' % traceback.format_exc())
119
                sys.exit(0)
120
            print('yes')
121
    elif successful_import:
122
        load_conf()
123
        print_values()