root / plugins / libvirt / kvm_io @ 367a4b37
Historique | Voir | Annoter | Télécharger (3,58 ko)
| 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 |
| 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 |
base_config = """graph_title KVM Virtual Machine IO usage |
| 50 |
graph_vlabel Bytes read(-)/written(+) per second |
| 51 |
graph_category virtualization |
| 52 |
graph_info This graph shows the block device I/O used of virtual machines |
| 53 |
graph_args --base 1024""" |
| 54 |
print(base_config) |
| 55 |
|
| 56 |
for vm in vm_names: |
| 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 |
|
| 68 |
|
| 69 |
def clean_vm_name(vm_name): |
| 70 |
''' Replace all special chars |
| 71 |
@param vm_name : a vm's name |
| 72 |
@return cleaned vm's name |
| 73 |
''' |
| 74 |
# suffix part defined in conf |
| 75 |
suffix = os.getenv('vmsuffix')
|
| 76 |
if suffix: |
| 77 |
vm_name = re.sub(suffix, '', vm_name) |
| 78 |
# proxmox uses kvm with -name parameter |
| 79 |
parts = vm_name.split('\x00')
|
| 80 |
if (parts[0].endswith('kvm')):
|
| 81 |
try: |
| 82 |
return parts[parts.index('-name')+1]
|
| 83 |
except ValueError: |
| 84 |
pass |
| 85 |
return re.sub(r"[^a-zA-Z0-9_]", "_", vm_name) |
| 86 |
|
| 87 |
|
| 88 |
def fetch(vms): |
| 89 |
''' Fetch values for a list of pids |
| 90 |
@param dictionary {kvm_pid: cleaned vm name}
|
| 91 |
''' |
| 92 |
for pid in vms: |
| 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 |
|
| 103 |
|
| 104 |
def detect_kvm(): |
| 105 |
''' Check if kvm is installed |
| 106 |
''' |
| 107 |
kvm = Popen("which kvm", shell=True, stdout=PIPE)
|
| 108 |
kvm.communicate() |
| 109 |
return not bool(kvm.returncode) |
| 110 |
|
| 111 |
|
| 112 |
def find_vm_names(pids): |
| 113 |
'''Find and clean vm names from pids |
| 114 |
@return a dictionary of {pids : cleaned vm name}
|
| 115 |
''' |
| 116 |
result = {}
|
| 117 |
for pid in pids: |
| 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())) |
| 120 |
return result |
| 121 |
|
| 122 |
|
| 123 |
def list_pids(): |
| 124 |
''' Find the pid of kvm processes |
| 125 |
@return a list of pids from running kvm |
| 126 |
''' |
| 127 |
pid = Popen("pidof qemu-kvm qemu-system-x86_64 kvm", shell=True, stdout=PIPE)
|
| 128 |
return pid.communicate()[0].split() |
| 129 |
|
| 130 |
|
| 131 |
if __name__ == "__main__": |
| 132 |
if len(sys.argv) > 1: |
| 133 |
if sys.argv[1] in ['autoconf', 'detect']: |
| 134 |
if detect_kvm(): |
| 135 |
print("yes")
|
| 136 |
else: |
| 137 |
print("no (detect_kvm failed - is kvm installed?)")
|
| 138 |
elif sys.argv[1] == "config": |
| 139 |
config(find_vm_names(list_pids()).values()) |
| 140 |
else: |
| 141 |
fetch(find_vm_names(list_pids())) |
| 142 |
else: |
| 143 |
fetch(find_vm_names(list_pids())) |
