File: nova_instance_

package info (click to toggle)
munin-plugins-openstack 1.20120627-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 184 kB
  • ctags: 62
  • sloc: python: 535; sh: 63; makefile: 2
file content (143 lines) | stat: -rwxr-xr-x 4,404 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python
#
# Copyright (C) 2012 eNovance <licensing@enovance>
# 
# Authors: Mehdi Abaakouk <sileht@sileht.net>
#          Gael Lambert <gael.lambert@enovance.com>
#          Jesse Andrews <anotherjesse@rackspace.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
# 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
#%# capabilities=autoconf suggest
#%# family=auto

import sys

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:
    succesful_import = False
else:
    succesful_import = True


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 = {}
        for it in db.instance_type_get_all(ctxt, True):
            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

def get_name(metric, code):
    if metric == "power_state":
        return power_state.name(code)
    elif metric in ["root_gb", "ephemeral_gb"]:
        return "%d gb" % code
    else:
        return code

def print_config(metric):
    states = InstanceState.get_states(metric)
    print 'graph_title Nova Instance %s' % metric
    print 'graph_vlabel instances'
    print 'graph_args --base 1000 --lower-limit 0'
    print 'graph_category nova'
    print 'graph_scale no'
    print 'graph_info This graph shows the number of instances by %s' % metric
    for state in states:
        print '%s.label %s' % (state, get_name(metric, state))
        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)

def print_suggest():
    suggest = [ "vm_state",
                "vcpus",
                "task_state",
                "root_gb",
                "ephemeral_gb",
                "power_state",
                "memory_mb",
                "instance_type_id",
                ]
    print "\n".join(suggest)


if __name__ == '__main__':
    argv = sys.argv[:]
    utils.default_flagfile()
    flags.FLAGS(sys.argv)
    metric = argv[0].split('nova_instance_').pop() or 'vm_state'

    if len(argv) > 1:
        if argv[1] == 'config':
            print_config(metric)
        elif argv[1] == 'suggest':
            print_suggest()
        elif argv[1] == 'autoconf':
            if not succesful_import:  
                print 'no (failed import nova module)'
                sys.exit(0)
            else:
                print 'yes'
    elif succesful_import:
        print_values(metric)