1#!/usr/local/bin/python3.8
2"""
3=head1 NAME
4
5nova_services - report nova service status
6
7
8=head1 CONFIGURATION
9
10Needs following minimal configuration in plugin-conf.d/nova:
11
12 [nova_*]
13 user nova
14
15
16=head1 AUTHORS
17
18Copyright 2012 Mehdi Abaakouk <sileht@sileht.net>
19
20
21=head1 MAGIC MARKERS
22
23 #%# capabilities=autoconf
24 #%# family=auto
25
26=cut
27"""
28
29import sys
30
31try:
32    from nova import context, db, flags, utils
33except ImportError:
34    successful_import = False
35else:
36    successful_import = True
37
38
39services = [
40    'nova-compute',
41    'nova-volume',
42    'nova-scheduler',
43    'nova-vncproxy',
44    'nova-network',
45    'nova-cert',
46    'nova-console',
47    'nova-consoleauth',
48]
49
50
51def print_config():
52    print('graph_title Nova Services')
53    print('graph_vlabel qty')
54    print('graph_args --base 1000 --lower-limit 0')
55    print('graph_category cloud')
56    print('graph_scale no')
57    print('graph_info Nova services - alive and active')
58    for service in services:
59        print('%s_alive.label %s alive' % (service, service))
60        print('%s_alive.draw LINE2' % service)
61        print('%s_alive.info seen in last 30 seconds' % service)
62        print('%s_active.label %s active' % (service, service))
63        print('%s_active.draw LINE2' % service)
64        print('%s_active.info alive and enabled' % service)
65
66
67def get_status():
68    global services
69    alive = {}
70    active = {}
71    for k in services:
72        alive[k] = 0
73        active[k] = 0
74    ctxt = context.get_admin_context()
75    now = utils.utcnow()
76    services = db.service_get_all(ctxt)
77    for svc in services:
78        delta = now - (svc['updated_at'] or svc['created_at'])
79        if (delta.seconds <= 30):
80            alive[svc['binary']] += 1
81            if not svc['disabled']:
82                active[svc['binary']] += 1
83    return {'alive': alive, 'active': active}
84
85
86def print_values():
87    status = get_status()
88    for (state, value) in status['alive'].items():
89        print("%s_alive.value %s" % (state, value))
90    for (state, value) in status['active'].items():
91        print("%s_active.value %s" % (state, value))
92
93
94if __name__ == '__main__':
95    if len(sys.argv) > 1:
96        if sys.argv[1] == "config":
97            print_config()
98        elif sys.argv[1] == "autoconf":
99            if not successful_import:
100                print('no (failed import nova module]')
101            else:
102                print('yes')
103    elif successful_import:
104        utils.default_flagfile()
105        flags.FLAGS(sys.argv)
106        print_values()
107