Projet

Général

Profil

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

root / plugins / nova / nova_instance_ @ 37d81d7a

Historique | Voir | Annoter | Télécharger (3,45 ko)

1 37d81d7a Lars Kruse
#!/usr/bin/env python3
2
"""
3 22fbe167 Mehdi Abaakouk
4 37d81d7a Lars Kruse
=head1 NAME
5
6
nova_instance_ - monitor status of Floating IPs in Nova
7
8
9
=head1 CONFIGURATION
10
11
12
To monitor instance states, link instance_states to this file.
13
E.g.
14
15
 ln -s /usr/share/munin/plugins/nova_instance_states /etc/munin/plugins/
16
17
Needs following minimal configuration in plugin-conf.d/nova:
18
19
 [nova_*]
20
 user nova
21
22
23
=head1 AUTHORS
24
25
Copyright 2012 Mehdi Abaakouk <sileht@sileht.net>
26
27
28
=head1 MAGIC MARKERS
29
30
 #%# capabilities=autoconf suggest
31
 #%# family=auto
32
33
=cut
34
"""
35
36
import os
37 22fbe167 Mehdi Abaakouk
import sys
38
39 37d81d7a Lars Kruse
40 33cff547 Mehdi Abaakouk
try:
41
    from nova import context
42
    from nova import db
43
    from nova import flags
44
    from nova import utils
45
    from nova.compute import power_state
46
except ImportError:
47 fba800ae Veres Lajos
    successful_import = False
48 33cff547 Mehdi Abaakouk
else:
49 fba800ae Veres Lajos
    successful_import = True
50 33cff547 Mehdi Abaakouk
51 22fbe167 Mehdi Abaakouk
52 37d81d7a Lars Kruse
class InstanceState:
53 22fbe167 Mehdi Abaakouk
    instance_counts = None
54
    states = None
55
56
    @classmethod
57
    def init(cls, metric):
58
        if cls.states and cls.instance_counts:
59
            return
60
        ctxt = context.get_admin_context()
61
        instances = db.instance_get_all(ctxt)
62
        cls.instance_counts = {}
63
        instance_types = {}
64 33cff547 Mehdi Abaakouk
        for it in db.instance_type_get_all(ctxt, True):
65 22fbe167 Mehdi Abaakouk
            instance_types[it['id']] = it['name']
66
67
        for instance in instances:
68
            i = dict(instance)
69 37d81d7a Lars Kruse
            i['type'] = instance_types.get(instance.instance_type_id, '(unknown')
70
            i['instance_type'] = i['type']
71 22fbe167 Mehdi Abaakouk
            val = cls.instance_counts.get(i[metric], 0)
72
            cls.instance_counts[i[metric]] = val + 1
73
        cls.states = cls.instance_counts.keys()
74
75
    @classmethod
76
    def get_states(cls, metric):
77
        InstanceState.init(metric)
78
        return cls.states
79
80
    @classmethod
81
    def get_instance_counts(cls, metric):
82
        InstanceState.init(metric)
83
        return cls.instance_counts
84
85 37d81d7a Lars Kruse
86 33cff547 Mehdi Abaakouk
def get_name(metric, code):
87
    if metric == "power_state":
88
        return power_state.name(code)
89
    elif metric in ["root_gb", "ephemeral_gb"]:
90 c4e32176 Mehdi Abaakouk
        return "%d gb" % code
91 33cff547 Mehdi Abaakouk
    else:
92
        return code
93 22fbe167 Mehdi Abaakouk
94 37d81d7a Lars Kruse
95 22fbe167 Mehdi Abaakouk
def print_config(metric):
96
    states = InstanceState.get_states(metric)
97 37d81d7a Lars Kruse
    print('graph_title Nova Instance %s' % metric)
98
    print('graph_vlabel instances')
99
    print('graph_args --base 1000 --lower-limit 0')
100
    print('graph_category cloud')
101
    print('graph_scale no')
102
    print('graph_info This graph shows the number of instances by %s' % metric)
103 22fbe167 Mehdi Abaakouk
    for state in states:
104 37d81d7a Lars Kruse
        print('%s.label %s' % (state, get_name(metric, state)))
105
        print('%s.draw LINE2' % state)
106
        print('%s.info %s IPs' % (state, state))
107 22fbe167 Mehdi Abaakouk
108
109
def print_values(metric):
110
    status = InstanceState.get_instance_counts(metric)
111 37d81d7a Lars Kruse
    for (state, value) in status.items():
112
        print('%s.value %s' % (state, value))
113
114 22fbe167 Mehdi Abaakouk
115 33cff547 Mehdi Abaakouk
def print_suggest():
116 37d81d7a Lars Kruse
    suggest = [
117
        "vm_state",
118
        "vcpus",
119
        "task_state",
120
        "root_gb",
121
        "ephemeral_gb",
122
        "power_state",
123
        "memory_mb",
124
        "instance_type_id",
125
    ]
126
    print(os.linesep.join(suggest))
127 33cff547 Mehdi Abaakouk
128 22fbe167 Mehdi Abaakouk
129
if __name__ == '__main__':
130
    argv = sys.argv[:]
131
    utils.default_flagfile()
132
    flags.FLAGS(sys.argv)
133 33cff547 Mehdi Abaakouk
    metric = argv[0].split('nova_instance_').pop() or 'vm_state'
134 22fbe167 Mehdi Abaakouk
135
    if len(argv) > 1:
136
        if argv[1] == 'config':
137
            print_config(metric)
138 33cff547 Mehdi Abaakouk
        elif argv[1] == 'suggest':
139
            print_suggest()
140 22fbe167 Mehdi Abaakouk
        elif argv[1] == 'autoconf':
141 17f78427 Lars Kruse
            if not successful_import:
142 37d81d7a Lars Kruse
                print('no (failed import nova module)')
143 33cff547 Mehdi Abaakouk
            else:
144 37d81d7a Lars Kruse
                print('yes')
145
            sys.exit(0)
146 fba800ae Veres Lajos
    elif successful_import:
147 22fbe167 Mehdi Abaakouk
        print_values(metric)