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