2012-05-02 08:34:55 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Plugin to monitor status of Floating IPs in Nova
|
|
|
|
#
|
|
|
|
# To monitor instance states, link instance_states to this file.
|
|
|
|
# E.g.
|
|
|
|
# ln -s /usr/share/munin/plugins/nova_instance_states /etc/munin/plugins/
|
|
|
|
#
|
|
|
|
# Needs following minimal configuration in plugin-conf.d/nova:
|
|
|
|
# [nova_*]
|
|
|
|
# user nova
|
|
|
|
#
|
|
|
|
# Magic markers
|
2012-05-03 16:48:19 +02:00
|
|
|
#%# capabilities=autoconf suggest
|
|
|
|
#%# family=auto
|
2012-05-02 08:34:55 +02:00
|
|
|
|
|
|
|
import sys
|
|
|
|
|
2012-05-03 16:48:19 +02:00
|
|
|
try:
|
|
|
|
from nova import context
|
|
|
|
from nova import db
|
|
|
|
from nova import flags
|
|
|
|
from nova import utils
|
|
|
|
from nova.compute import power_state
|
|
|
|
except ImportError:
|
2014-12-05 00:37:42 +01:00
|
|
|
successful_import = False
|
2012-05-03 16:48:19 +02:00
|
|
|
else:
|
2014-12-05 00:37:42 +01:00
|
|
|
successful_import = True
|
2012-05-03 16:48:19 +02:00
|
|
|
|
2012-05-02 08:34:55 +02:00
|
|
|
|
|
|
|
class InstanceState(object):
|
|
|
|
instance_counts = None
|
|
|
|
states = None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def init(cls, metric):
|
|
|
|
if cls.states and cls.instance_counts:
|
|
|
|
return
|
|
|
|
ctxt = context.get_admin_context()
|
|
|
|
instances = db.instance_get_all(ctxt)
|
|
|
|
cls.instance_counts = {}
|
|
|
|
instance_types = {}
|
2012-05-03 16:48:19 +02:00
|
|
|
for it in db.instance_type_get_all(ctxt, True):
|
2012-05-02 08:34:55 +02:00
|
|
|
instance_types[it['id']] = it['name']
|
|
|
|
|
|
|
|
for instance in instances:
|
|
|
|
i = dict(instance)
|
|
|
|
i['instance_type'] = i['type'] = instance_types.get(
|
|
|
|
instance.instance_type_id,
|
|
|
|
'(unknown')
|
|
|
|
val = cls.instance_counts.get(i[metric], 0)
|
|
|
|
cls.instance_counts[i[metric]] = val + 1
|
|
|
|
cls.states = cls.instance_counts.keys()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_states(cls, metric):
|
|
|
|
InstanceState.init(metric)
|
|
|
|
return cls.states
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_instance_counts(cls, metric):
|
|
|
|
InstanceState.init(metric)
|
|
|
|
return cls.instance_counts
|
|
|
|
|
2012-05-03 16:48:19 +02:00
|
|
|
def get_name(metric, code):
|
|
|
|
if metric == "power_state":
|
|
|
|
return power_state.name(code)
|
|
|
|
elif metric in ["root_gb", "ephemeral_gb"]:
|
2012-05-03 21:27:26 +02:00
|
|
|
return "%d gb" % code
|
2012-05-03 16:48:19 +02:00
|
|
|
else:
|
|
|
|
return code
|
2012-05-02 08:34:55 +02:00
|
|
|
|
|
|
|
def print_config(metric):
|
|
|
|
states = InstanceState.get_states(metric)
|
2012-05-03 16:48:19 +02:00
|
|
|
print 'graph_title Nova Instance %s' % metric
|
2012-05-02 08:34:55 +02:00
|
|
|
print 'graph_vlabel instances'
|
|
|
|
print 'graph_args --base 1000 --lower-limit 0'
|
|
|
|
print 'graph_category nova'
|
|
|
|
print 'graph_scale no'
|
2012-05-03 16:48:19 +02:00
|
|
|
print 'graph_info This graph shows the number of instances by %s' % metric
|
2012-05-02 08:34:55 +02:00
|
|
|
for state in states:
|
2012-05-03 21:27:26 +02:00
|
|
|
print '%s.label %s' % (state, get_name(metric, state))
|
2012-05-02 08:34:55 +02:00
|
|
|
print '%s.draw LINE2' % state
|
|
|
|
print '%s.info %s IPs' % (state, state)
|
|
|
|
|
|
|
|
|
|
|
|
def print_values(metric):
|
|
|
|
status = InstanceState.get_instance_counts(metric)
|
|
|
|
for (state, value) in status.iteritems():
|
|
|
|
print '%s.value %s' % (state, value)
|
|
|
|
|
2012-05-03 16:48:19 +02:00
|
|
|
def print_suggest():
|
|
|
|
suggest = [ "vm_state",
|
|
|
|
"vcpus",
|
|
|
|
"task_state",
|
|
|
|
"root_gb",
|
|
|
|
"ephemeral_gb",
|
|
|
|
"power_state",
|
|
|
|
"memory_mb",
|
|
|
|
"instance_type_id",
|
|
|
|
]
|
|
|
|
print "\n".join(suggest)
|
|
|
|
|
2012-05-02 08:34:55 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
argv = sys.argv[:]
|
|
|
|
utils.default_flagfile()
|
|
|
|
flags.FLAGS(sys.argv)
|
2012-05-03 16:48:19 +02:00
|
|
|
metric = argv[0].split('nova_instance_').pop() or 'vm_state'
|
2012-05-02 08:34:55 +02:00
|
|
|
|
|
|
|
if len(argv) > 1:
|
|
|
|
if argv[1] == 'config':
|
|
|
|
print_config(metric)
|
2012-05-03 16:48:19 +02:00
|
|
|
elif argv[1] == 'suggest':
|
|
|
|
print_suggest()
|
2012-05-02 08:34:55 +02:00
|
|
|
elif argv[1] == 'autoconf':
|
2014-12-05 00:37:42 +01:00
|
|
|
if not successful_import:
|
2012-05-03 16:48:19 +02:00
|
|
|
print 'no (failed import nova module)'
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
print 'yes'
|
2014-12-05 00:37:42 +01:00
|
|
|
elif successful_import:
|
2012-05-02 08:34:55 +02:00
|
|
|
print_values(metric)
|