Projet

Général

Profil

Révision b7617471

IDb76174718ea3fa02271217a51de5a9b0c23893de
Parent 0866add1
Enfant 367a4b37

Ajouté par Alois il y a environ 4 ans

kvm_io now in python3 (#1254)

  • works on debian bullseye with this modification.
  • removed all codestyle issues, checked with "pycodestyle" on debian bullseye, besides two "line too long"
  • "config" param must not have newlines to avoid a warning in munin-update.log

Voir les différences:

plugins/libvirt/kvm_io
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
# vim: set fileencoding=utf-8
4
#
5
# Munin plugin to show io by vm
6
#
7
# Copyright Maxence Dunnewind, Rodolphe Quiédeville
8
#
9
# License : GPLv3
10
#
11
# parsed environment variables:
12
# vmsuffix: part of vm name to be removed
13
#
14
#%# capabilities=autoconf
15
#%# family=contrib
16

  
17
import re, os, sys
1
#!/usr/bin/env python3
2
"""
3
=encoding utf8
4

  
5
=head1 NAME
6

  
7
kvm_io - show IO usage of VM
8

  
9

  
10
=head1 CONFIGURATION
11

  
12
Parsed environment variables:
13

  
14
 vmsuffix: part of VM name to be removed
15

  
16

  
17
=head1 LICENSE
18

  
19
GPLv3
20

  
21
SPDX-License-Identifier: GPL-3.0-only
22

  
23

  
24
=head1 AUTHORS
25

  
26
Maxence Dunnewind
27

  
28
Rodolphe Quiédeville
29

  
30

  
31
=head1 MAGIC MARKERS
32

  
33
 #%# capabilities=autoconf
34
 #%# family=contrib
35

  
36
=cut
37
"""
38

  
39
import re
40
import os
41
import sys
18 42
from subprocess import Popen, PIPE
19 43

  
44

  
20 45
def config(vm_names):
21 46
    ''' Print the plugin's config
22 47
    @param vm_names : a list of "cleaned" vms' name
......
25 50
graph_vlabel Bytes read(-)/written(+) per second
26 51
graph_category virtualization
27 52
graph_info This graph shows the block device I/O used of virtual machines
28
graph_args --base 1024
29
    """
30
    print base_config
53
graph_args --base 1024"""
54
    print(base_config)
31 55

  
32 56
    for vm in vm_names:
33
        print "%s_read.label %s" % (vm, vm)
34
        print "%s_read.type COUNTER" % vm
35
        print "%s_read.min 0" % vm
36
        print "%s_read.info I/O used by virtual machine %s" % (vm, vm)
37
        print "%s_read.graph no" % vm
38
        print "%s_write.label %s" % (vm, vm)
39
        print "%s_write.type COUNTER" % vm
40
        print "%s_write.min 0" % vm
41
        print "%s_write.negative %s_read" % (vm, vm)
42
        print "%s_write.info I/O used by virtual machine %s" % (vm, vm)
57
        print("%s_read.label %s" % (vm, vm))
58
        print("%s_read.type COUNTER" % vm)
59
        print("%s_read.min 0" % vm)
60
        print("%s_read.info I/O used by virtual machine %s" % (vm, vm))
61
        print("%s_read.graph no" % vm)
62
        print("%s_write.label %s" % (vm, vm))
63
        print("%s_write.type COUNTER" % vm)
64
        print("%s_write.min 0" % vm)
65
        print("%s_write.negative %s_read" % (vm, vm))
66
        print("%s_write.info I/O used by virtual machine %s" % (vm, vm))
67

  
43 68

  
44 69
def clean_vm_name(vm_name):
45 70
    ''' Replace all special chars
......
49 74
    # suffix part defined in conf
50 75
    suffix = os.getenv('vmsuffix')
51 76
    if suffix:
52
        vm_name = re.sub(suffix,'',vm_name)
77
        vm_name = re.sub(suffix, '', vm_name)
53 78
    # proxmox uses kvm with -name parameter
54 79
    parts = vm_name.split('\x00')
55 80
    if (parts[0].endswith('kvm')):
......
59 84
            pass
60 85
    return re.sub(r"[^a-zA-Z0-9_]", "_", vm_name)
61 86

  
87

  
62 88
def fetch(vms):
63 89
    ''' Fetch values for a list of pids
64 90
    @param dictionary {kvm_pid: cleaned vm name}
65 91
    '''
66
    res = {}
67 92
    for pid in vms:
68
        f = open("/proc/%s/io" % pid, "r")
69
        for line in f.readlines():
70
            if "read_bytes" in line:
71
                read = line.split()[1]
72
                print "%s_read.value %s" % (vms[pid], read)
73
            if "write_bytes" in line:
74
                write = line.split()[1]
75
                print "%s_write.value %s" % (vms[pid], write)
76
                break
77
        f.close()
93
        with open("/proc/%s/io" % pid.decode(), "r") as f:
94
            for line in f.readlines():
95
                if "read_bytes" in line:
96
                    read = line.split()[1]
97
                    print("%s_read.value %s" % (vms[pid], read))
98
                if "write_bytes" in line:
99
                    write = line.split()[1]
100
                    print("%s_write.value %s" % (vms[pid], write))
101
                    break
102

  
78 103

  
79 104
def detect_kvm():
80 105
    ''' Check if kvm is installed
......
83 108
    kvm.communicate()
84 109
    return not bool(kvm.returncode)
85 110

  
111

  
86 112
def find_vm_names(pids):
87 113
    '''Find and clean vm names from pids
88 114
    @return a dictionary of {pids : cleaned vm name}
89 115
    '''
90 116
    result = {}
91 117
    for pid in pids:
92
        cmdline = open("/proc/%s/cmdline" % pid, "r")
93
        result[pid] = clean_vm_name(re.sub(r"^.*guest=([a-zA-Z0-9.-_-]*).*$",r"\1", cmdline.readline()))
118
        with open("/proc/%s/cmdline" % pid.decode(), "r") as cmdline:
119
            result[pid] = clean_vm_name(re.sub(r"^.*guest=([a-zA-Z0-9.-_-]*).*$", r"\1", cmdline.readline()))
94 120
    return result
95 121

  
122

  
96 123
def list_pids():
97 124
    ''' Find the pid of kvm processes
98 125
    @return a list of pids from running kvm
......
100 127
    pid = Popen("pidof qemu-kvm qemu-system-x86_64 kvm", shell=True, stdout=PIPE)
101 128
    return pid.communicate()[0].split()
102 129

  
130

  
103 131
if __name__ == "__main__":
104 132
    if len(sys.argv) > 1:
105 133
        if sys.argv[1] in ['autoconf', 'detect']:
106 134
            if detect_kvm():
107
                print "yes"
135
                print("yes")
108 136
            else:
109
                print "no"
137
                print("no (detect_kvm failed - is kvm installed?)")
110 138
        elif sys.argv[1] == "config":
111 139
            config(find_vm_names(list_pids()).values())
112 140
        else:

Formats disponibles : Unified diff