Projet

Général

Profil

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

root / plugins / libvirt / kvm_cpu @ b6912e76

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

1
#!/usr/bin/env python3
2
"""
3
=encoding utf8
4

    
5
=head1 NAME
6

    
7
kvm_cpu - show CPU 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 os
40
import re
41
import sys
42
from subprocess import Popen, PIPE
43

    
44

    
45
def config(vm_names):
46
    ''' Print the plugin's config
47
    @param vm_names : a list of "cleaned" vms' name
48
    '''
49
    percent = 100 * len(
50
        list(
51
            filter(
52
                lambda x: x[0:3] == 'cpu' and x[3] != ' ', open('/proc/stat', 'r').readlines())))
53

    
54
    base_config = """graph_title KVM Virtual Machine CPU usage
55
graph_vlabel %%
56
graph_category virtualization
57
graph_scale no
58
graph_period second
59
graph_info This graph shows the current CPU used by virtual machines
60
graph_args --base 1000 -r --lower-limit 0 --upper-limit %d""" % percent
61
    print(base_config)
62
    for vm in vm_names:
63
        print("%s_cpu.label %s" % (vm, vm))
64
        print("%s_cpu.min 0" % vm)
65
        print("%s_cpu.type DERIVE" % vm)
66
        print("%s_cpu.draw AREASTACK" % vm)
67
        print("%s_cpu.info percent of cpu time used by virtual machine" % vm)
68

    
69

    
70
def clean_vm_name(vm_name):
71
    ''' Replace all special chars
72
    @param vm_name : a vm's name
73
    @return cleaned vm's name
74
    '''
75
    # suffix part defined in conf
76
    suffix = os.getenv('vmsuffix')
77
    if suffix:
78
        vm_name = re.sub(suffix, '', vm_name)
79
    # proxmox uses kvm with -name parameter
80
    parts = vm_name.split('\x00')
81
    if parts[0].endswith('kvm'):
82
        try:
83
            return parts[parts.index('-name') + 1]
84
        except ValueError:
85
            pass
86
    return re.sub(r"[^a-zA-Z0-9_]", "_", vm_name)
87

    
88

    
89
def detect_kvm():
90
    ''' Check if kvm is installed '''
91
    kvm = Popen("which kvm", shell=True, stdout=PIPE)
92
    kvm.communicate()
93
    return not bool(kvm.returncode)
94

    
95

    
96
def find_vm_names(pids):
97
    '''Find and clean vm names from pids
98
    @return a dictionary of {pids : cleaned vm name}
99
    '''
100
    result = {}
101
    for pid in pids:
102
        cmdline = open("/proc/%s/cmdline" % pid, "r")
103
        result[pid] = clean_vm_name(
104
            re.sub(r"^.*guest=([a-zA-Z0-9.-_-]*).*$", r"\1", cmdline.readline()))
105
    return result
106

    
107

    
108
def list_pids():
109
    ''' Find the pid of kvm processes
110
    @return a list of pids from running kvm
111
    '''
112
    pid = Popen("pidof qemu-kvm qemu-system-x86_64 kvm", shell=True, stdout=PIPE)
113
    return pid.communicate()[0].decode().split()
114

    
115

    
116
def fetch(vms):
117
    ''' Fetch values for a list of pids
118
    @param dictionary {kvm_pid: cleaned vm name}
119
    '''
120
    for pid, name in vms.items():
121
        user, system = open("/proc/%s/stat" % pid, 'r').readline().split(' ')[13:15]
122
        print('%s_cpu.value %d' % (name, int(user) + int(system)))
123

    
124

    
125
if __name__ == "__main__":
126
    if len(sys.argv) > 1:
127
        if sys.argv[1] in ['autoconf', 'detect']:
128
            if detect_kvm():
129
                print("yes")
130
            else:
131
                print("no")
132
        elif sys.argv[1] == "config":
133
            config(find_vm_names(list_pids()).values())
134
        else:
135
            fetch(find_vm_names(list_pids()))
136
    else:
137
        fetch(find_vm_names(list_pids()))