root / plugins / libvirt / kvm_io @ a7139bca
Historique | Voir | Annoter | Télécharger (3,4 ko)
| 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 |
| 18 |
from subprocess import Popen, PIPE |
| 19 |
|
| 20 |
def config(vm_names): |
| 21 |
''' Print the plugin's config |
| 22 |
@param vm_names : a list of "cleaned" vms' name |
| 23 |
''' |
| 24 |
base_config = """graph_title KVM Virtual Machine IO usage |
| 25 |
graph_vlabel Bytes read(-)/written(+) per second |
| 26 |
graph_category virtualization |
| 27 |
graph_info This graph shows the block device I/O used of virtual machines |
| 28 |
graph_args --base 1024 |
| 29 |
""" |
| 30 |
print base_config |
| 31 |
|
| 32 |
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) |
| 43 |
|
| 44 |
def clean_vm_name(vm_name): |
| 45 |
''' Replace all special chars |
| 46 |
@param vm_name : a vm's name |
| 47 |
@return cleaned vm's name |
| 48 |
''' |
| 49 |
# suffix part defined in conf |
| 50 |
suffix = os.getenv('vmsuffix')
|
| 51 |
if suffix: |
| 52 |
vm_name = re.sub(suffix,'',vm_name) |
| 53 |
# proxmox uses kvm with -name parameter |
| 54 |
parts = vm_name.split('\x00')
|
| 55 |
if (parts[0].endswith('kvm')):
|
| 56 |
try: |
| 57 |
return parts[parts.index('-name')+1]
|
| 58 |
except ValueError: |
| 59 |
pass |
| 60 |
return re.sub(r"[^a-zA-Z0-9_]", "_", vm_name) |
| 61 |
|
| 62 |
def fetch(vms): |
| 63 |
''' Fetch values for a list of pids |
| 64 |
@param dictionary {kvm_pid: cleaned vm name}
|
| 65 |
''' |
| 66 |
res = {}
|
| 67 |
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() |
| 78 |
|
| 79 |
def detect_kvm(): |
| 80 |
''' Check if kvm is installed |
| 81 |
''' |
| 82 |
kvm = Popen("which kvm", shell=True, stdout=PIPE)
|
| 83 |
kvm.communicate() |
| 84 |
return not bool(kvm.returncode) |
| 85 |
|
| 86 |
def find_vm_names(pids): |
| 87 |
'''Find and clean vm names from pids |
| 88 |
@return a dictionary of {pids : cleaned vm name}
|
| 89 |
''' |
| 90 |
result = {}
|
| 91 |
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())) |
| 94 |
return result |
| 95 |
|
| 96 |
def list_pids(): |
| 97 |
''' Find the pid of kvm processes |
| 98 |
@return a list of pids from running kvm |
| 99 |
''' |
| 100 |
pid = Popen("pidof qemu-kvm qemu-system-x86_64 kvm", shell=True, stdout=PIPE)
|
| 101 |
return pid.communicate()[0].split() |
| 102 |
|
| 103 |
if __name__ == "__main__": |
| 104 |
if len(sys.argv) > 1: |
| 105 |
if sys.argv[1] in ['autoconf', 'detect']: |
| 106 |
if detect_kvm(): |
| 107 |
print "yes" |
| 108 |
else: |
| 109 |
print "no" |
| 110 |
elif sys.argv[1] == "config": |
| 111 |
config(find_vm_names(list_pids()).values()) |
| 112 |
else: |
| 113 |
fetch(find_vm_names(list_pids())) |
| 114 |
else: |
| 115 |
fetch(find_vm_names(list_pids())) |
