Projet

Général

Profil

Révision 37d81d7a

ID37d81d7a6b4f287b57932b244ec3cd251c0262b0
Parent d168d49e
Enfant d8c30923

Ajouté par Lars Kruse il y a environ 5 ans

Plugin nova_instance_: migrate to Python3, format documentation

Voir les différences:

plugins/nova/nova_instance_
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
1
#!/usr/bin/env python3
2
"""
16 3

  
4
=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
17 37
import sys
18 38

  
39

  
19 40
try:
20 41
    from nova import context
21 42
    from nova import db
......
28 49
    successful_import = True
29 50

  
30 51

  
31
class InstanceState(object):
52
class InstanceState:
32 53
    instance_counts = None
33 54
    states = None
34 55

  
......
45 66

  
46 67
        for instance in instances:
47 68
            i = dict(instance)
48
            i['instance_type'] = i['type'] = instance_types.get(
49
                                                 instance.instance_type_id,
50
                                                '(unknown')
69
            i['type'] = instance_types.get(instance.instance_type_id, '(unknown')
70
            i['instance_type'] = i['type']
51 71
            val = cls.instance_counts.get(i[metric], 0)
52 72
            cls.instance_counts[i[metric]] = val + 1
53 73
        cls.states = cls.instance_counts.keys()
......
62 82
        InstanceState.init(metric)
63 83
        return cls.instance_counts
64 84

  
85

  
65 86
def get_name(metric, code):
66 87
    if metric == "power_state":
67 88
        return power_state.name(code)
......
70 91
    else:
71 92
        return code
72 93

  
94

  
73 95
def print_config(metric):
74 96
    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
97
    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)
81 103
    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)
104
        print('%s.label %s' % (state, get_name(metric, state)))
105
        print('%s.draw LINE2' % state)
106
        print('%s.info %s IPs' % (state, state))
85 107

  
86 108

  
87 109
def print_values(metric):
88 110
    status = InstanceState.get_instance_counts(metric)
89
    for (state, value) in status.iteritems():
90
        print '%s.value %s' % (state, value)
111
    for (state, value) in status.items():
112
        print('%s.value %s' % (state, value))
113

  
91 114

  
92 115
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)
116
    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))
103 127

  
104 128

  
105 129
if __name__ == '__main__':
......
115 139
            print_suggest()
116 140
        elif argv[1] == 'autoconf':
117 141
            if not successful_import:
118
                print 'no (failed import nova module)'
119
                sys.exit(0)
142
                print('no (failed import nova module)')
120 143
            else:
121
                print 'yes'
144
                print('yes')
145
            sys.exit(0)
122 146
    elif successful_import:
123 147
        print_values(metric)

Formats disponibles : Unified diff