root / plugins / nova / nova_instance_ @ 17f78427
Historique | Voir | Annoter | Télécharger (3,46 ko)
| 1 | 22fbe167 | Mehdi Abaakouk | #!/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 | 33cff547 | Mehdi Abaakouk | #%# capabilities=autoconf suggest |
| 15 | #%# family=auto |
||
| 16 | 22fbe167 | Mehdi Abaakouk | |
| 17 | import sys |
||
| 18 | |||
| 19 | 33cff547 | Mehdi Abaakouk | 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 | fba800ae | Veres Lajos | successful_import = False |
| 27 | 33cff547 | Mehdi Abaakouk | else: |
| 28 | fba800ae | Veres Lajos | successful_import = True |
| 29 | 33cff547 | Mehdi Abaakouk | |
| 30 | 22fbe167 | Mehdi Abaakouk | |
| 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 | 33cff547 | Mehdi Abaakouk | for it in db.instance_type_get_all(ctxt, True): |
| 44 | 22fbe167 | Mehdi Abaakouk | 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 | 33cff547 | Mehdi Abaakouk | 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 | c4e32176 | Mehdi Abaakouk | return "%d gb" % code |
| 70 | 33cff547 | Mehdi Abaakouk | else: |
| 71 | return code |
||
| 72 | 22fbe167 | Mehdi Abaakouk | |
| 73 | def print_config(metric): |
||
| 74 | states = InstanceState.get_states(metric) |
||
| 75 | 33cff547 | Mehdi Abaakouk | print 'graph_title Nova Instance %s' % metric |
| 76 | 22fbe167 | Mehdi Abaakouk | print 'graph_vlabel instances' |
| 77 | print 'graph_args --base 1000 --lower-limit 0' |
||
| 78 | f41b6861 | dipohl | print 'graph_category cloud' |
| 79 | 22fbe167 | Mehdi Abaakouk | print 'graph_scale no' |
| 80 | 33cff547 | Mehdi Abaakouk | print 'graph_info This graph shows the number of instances by %s' % metric |
| 81 | 22fbe167 | Mehdi Abaakouk | for state in states: |
| 82 | c4e32176 | Mehdi Abaakouk | print '%s.label %s' % (state, get_name(metric, state)) |
| 83 | 22fbe167 | Mehdi Abaakouk | 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 | 33cff547 | Mehdi Abaakouk | 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 | 22fbe167 | Mehdi Abaakouk | |
| 105 | if __name__ == '__main__': |
||
| 106 | argv = sys.argv[:] |
||
| 107 | utils.default_flagfile() |
||
| 108 | flags.FLAGS(sys.argv) |
||
| 109 | 33cff547 | Mehdi Abaakouk | metric = argv[0].split('nova_instance_').pop() or 'vm_state'
|
| 110 | 22fbe167 | Mehdi Abaakouk | |
| 111 | if len(argv) > 1: |
||
| 112 | if argv[1] == 'config': |
||
| 113 | print_config(metric) |
||
| 114 | 33cff547 | Mehdi Abaakouk | elif argv[1] == 'suggest': |
| 115 | print_suggest() |
||
| 116 | 22fbe167 | Mehdi Abaakouk | elif argv[1] == 'autoconf': |
| 117 | 17f78427 | Lars Kruse | if not successful_import: |
| 118 | 33cff547 | Mehdi Abaakouk | print 'no (failed import nova module)' |
| 119 | sys.exit(0) |
||
| 120 | else: |
||
| 121 | print 'yes' |
||
| 122 | fba800ae | Veres Lajos | elif successful_import: |
| 123 | 22fbe167 | Mehdi Abaakouk | print_values(metric) |
