Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / libvirt / kvm_net @ 67cc769c

Historique | Voir | Annoter | Télécharger (3,35 ko)

1 82242a42 Rodolphe Qui?deville
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
# vim: set fileencoding=utf-8
4
#
5
# Munin plugin to show the network I/O per vm
6
#
7 6bc704a1 Igor Borodikhin
# Copyright Igor Borodikhin
8 82242a42 Rodolphe Qui?deville
#
9
# License : GPLv3
10
#
11
#
12
# parsed environment variables:
13
# vmsuffix: part of vm name to be removed
14
#
15
#%# capabilities=autoconf
16
#%# family=contrib
17
18
import re, os, sys
19
from subprocess import Popen, PIPE
20
21 f6080f5f Lars Kruse
22 82242a42 Rodolphe Qui?deville
def config(vm_names):
23 7f98e21f Lars Kruse
    """ Print the plugin's config
24
25 82242a42 Rodolphe Qui?deville
    @param vm_names : a list of "cleaned" vms' name
26 7f98e21f Lars Kruse
    """
27 82242a42 Rodolphe Qui?deville
    base_config = """graph_title KVM Network I/O
28
graph_vlabel Bytes rx(-)/tx(+) per second
29 81db94e2 Gabriele Pohl
graph_category Virtualization
30 82242a42 Rodolphe Qui?deville
graph_info This graph shows the network I/O of the virtual machines
31
graph_args --base 1024
32
    """
33 67cc769c Lars Kruse
    print(base_config)
34 82242a42 Rodolphe Qui?deville
    for vm in vm_names:
35 67cc769c Lars Kruse
        print("%s_in.label %s" % (vm, vm))
36
        print("%s_in.type COUNTER" % vm)
37
        print("%s_in.min 0" % vm)
38
        print("%s_in.draw LINE2" % vm)
39
        print("%s_out.negative %s_in" % (vm, vm))
40
        print("%s_out.label %s" % (vm, vm))
41
        print("%s_out.type COUNTER" % vm)
42
        print("%s_out.min 0" % vm)
43
        print("%s_out.draw LINE2" % vm)
44 82242a42 Rodolphe Qui?deville
45 f6080f5f Lars Kruse
46 82242a42 Rodolphe Qui?deville
def clean_vm_name(vm_name):
47 7f98e21f Lars Kruse
    """ Replace all special chars
48
49 82242a42 Rodolphe Qui?deville
    @param vm_name : a vm's name
50
    @return cleaned vm's name
51 7f98e21f Lars Kruse
    """
52 82242a42 Rodolphe Qui?deville
    # suffix part defined in conf
53 7f98e21f Lars Kruse
    suffix = os.getenv("vmsuffix")
54 82242a42 Rodolphe Qui?deville
    if suffix:
55 7f98e21f Lars Kruse
        vm_name = re.sub(suffix, "", vm_name)
56 82242a42 Rodolphe Qui?deville
    return re.sub(r"[^a-zA-Z0-9_]", "_", vm_name)
57 f6080f5f Lars Kruse
58
59 82242a42 Rodolphe Qui?deville
def fetch(vms):
60 7f98e21f Lars Kruse
    """ Fetch values for a list of pids
61
62 82242a42 Rodolphe Qui?deville
    @param dictionnary {kvm_pid: cleaned vm name}
63 7f98e21f Lars Kruse
    """
64 82242a42 Rodolphe Qui?deville
    res = {}
65
    for pid in vms:
66 6bc704a1 Igor Borodikhin
        tap = get_vm_mac(pid)
67 82242a42 Rodolphe Qui?deville
        try:
68 6bc704a1 Igor Borodikhin
            f = open("/proc/net/dev", "r")
69
            for line in f.readlines():
70
                if tap in line:
71 67cc769c Lars Kruse
                    print("%s_in.value %s" % (vms[pid], line.split()[1]))
72
                    print("%s_out.value %s" % (vms[pid], line.split()[9]))
73 6bc704a1 Igor Borodikhin
                    break
74
        except Exception as inst:
75 67cc769c Lars Kruse
            print(inst)
76 82242a42 Rodolphe Qui?deville
            continue
77
78 f6080f5f Lars Kruse
79 82242a42 Rodolphe Qui?deville
def detect_kvm():
80 7f98e21f Lars Kruse
    """ Check if kvm is installed """
81 82242a42 Rodolphe Qui?deville
    kvm = Popen("which kvm", shell=True, stdout=PIPE)
82
    kvm.communicate()
83
    return not bool(kvm.returncode)
84
85 f6080f5f Lars Kruse
86 82242a42 Rodolphe Qui?deville
def find_vm_names(pids):
87 7f98e21f Lars Kruse
    """Find and clean vm names from pids
88
89 82242a42 Rodolphe Qui?deville
    @return a dictionnary of {pids : cleaned vm name}
90 7f98e21f Lars Kruse
    """
91 82242a42 Rodolphe Qui?deville
    result = {}
92
    for pid in pids:
93
        cmdline = open("/proc/%s/cmdline" % pid, "r")
94 f6080f5f Lars Kruse
        result[pid] = clean_vm_name(re.sub(r"^.*-name\x00([a-zA-Z0-9.-_-]*)\x00\-.*$",r"\1", cmdline.readline()))
95 82242a42 Rodolphe Qui?deville
    return result
96 f6080f5f Lars Kruse
97
98 82242a42 Rodolphe Qui?deville
def get_vm_mac(pid):
99 7f98e21f Lars Kruse
    """Find and clean vm names from pids
100
101 82242a42 Rodolphe Qui?deville
    @return the mac address for a specified pid
102 7f98e21f Lars Kruse
    """
103 82242a42 Rodolphe Qui?deville
    cmdline = open("/proc/%s/cmdline" % pid, "r")
104 6bc704a1 Igor Borodikhin
    line = cmdline.readline()
105
    mac = re.sub(r"^.*ifname=(tap[^,]+),.*$",r"\1", line)
106 82242a42 Rodolphe Qui?deville
    return mac
107
108 f6080f5f Lars Kruse
109 82242a42 Rodolphe Qui?deville
def list_pids():
110 7f98e21f Lars Kruse
    """ Find the pid of kvm processes
111
112 82242a42 Rodolphe Qui?deville
    @return a list of pids from running kvm
113 7f98e21f Lars Kruse
    """
114 621744a6 Jan Egil Vestbø
    pid = Popen("pidof qemu-kvm qemu-system-x86_64 kvm", shell=True, stdout=PIPE)
115 82242a42 Rodolphe Qui?deville
    return pid.communicate()[0].split()
116
117 6bc704a1 Igor Borodikhin
118 82242a42 Rodolphe Qui?deville
if __name__ == "__main__":
119
    if len(sys.argv) > 1:
120 7f98e21f Lars Kruse
        if sys.argv[1] in ["autoconf", "detect"]:
121 82242a42 Rodolphe Qui?deville
            if detect_kvm():
122 67cc769c Lars Kruse
                print("yes")
123 82242a42 Rodolphe Qui?deville
            else:
124 67cc769c Lars Kruse
                print("no")
125 82242a42 Rodolphe Qui?deville
        elif sys.argv[1] == "config":
126
            config(find_vm_names(list_pids()).values())
127
        else:
128
            fetch(find_vm_names(list_pids()))
129
    else:
130
        fetch(find_vm_names(list_pids()))