root / plugins / libvirt / kvm_io @ bde90ba9
Historique | Voir | Annoter | Télécharger (3,4 ko)
| 1 | a7139bca | Lars Kruse | #!/usr/bin/env python |
|---|---|---|---|
| 2 | e69661b7 | Rodolphe Qui?deville | # -*- 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 | 33e95e6f | Lars Kruse | graph_category virtualization |
| 27 | e69661b7 | Rodolphe Qui?deville | 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 | e09d89cd | Rodolphe Qui?deville | print "%s_read.type COUNTER" % vm |
| 35 | e69661b7 | Rodolphe Qui?deville | print "%s_read.min 0" % vm |
| 36 | print "%s_read.info I/O used by virtual machine %s" % (vm, vm) |
||
| 37 | 00f8fce4 | Bianco Veigel | print "%s_read.graph no" % vm |
| 38 | e69661b7 | Rodolphe Qui?deville | print "%s_write.label %s" % (vm, vm) |
| 39 | e09d89cd | Rodolphe Qui?deville | print "%s_write.type COUNTER" % vm |
| 40 | 17f78427 | Lars Kruse | print "%s_write.min 0" % vm |
| 41 | e69661b7 | Rodolphe Qui?deville | 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 | 225a9156 | Bianco Veigel | # proxmox uses kvm with -name parameter |
| 54 | 113008b0 | Bianco Veigel | 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 | e69661b7 | Rodolphe Qui?deville | return re.sub(r"[^a-zA-Z0-9_]", "_", vm_name) |
| 61 | 17f78427 | Lars Kruse | |
| 62 | e69661b7 | Rodolphe Qui?deville | def fetch(vms): |
| 63 | ''' Fetch values for a list of pids |
||
| 64 | 8713eb37 | Lars Kruse | @param dictionary {kvm_pid: cleaned vm name}
|
| 65 | e69661b7 | Rodolphe Qui?deville | ''' |
| 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 | 17f78427 | Lars Kruse | print "%s_read.value %s" % (vms[pid], read) |
| 73 | e69661b7 | Rodolphe Qui?deville | if "write_bytes" in line: |
| 74 | write = line.split()[1] |
||
| 75 | 17f78427 | Lars Kruse | print "%s_write.value %s" % (vms[pid], write) |
| 76 | break |
||
| 77 | e69661b7 | Rodolphe Qui?deville | 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 | 8713eb37 | Lars Kruse | @return a dictionary of {pids : cleaned vm name}
|
| 89 | e69661b7 | Rodolphe Qui?deville | ''' |
| 90 | result = {}
|
||
| 91 | for pid in pids: |
||
| 92 | cmdline = open("/proc/%s/cmdline" % pid, "r")
|
||
| 93 | c4a08bfa | Benoît S | result[pid] = clean_vm_name(re.sub(r"^.*guest=([a-zA-Z0-9.-_-]*).*$",r"\1", cmdline.readline())) |
| 94 | e69661b7 | Rodolphe Qui?deville | return result |
| 95 | 17f78427 | Lars Kruse | |
| 96 | e69661b7 | Rodolphe Qui?deville | def list_pids(): |
| 97 | ''' Find the pid of kvm processes |
||
| 98 | @return a list of pids from running kvm |
||
| 99 | ''' |
||
| 100 | 621744a6 | Jan Egil Vestbø | pid = Popen("pidof qemu-kvm qemu-system-x86_64 kvm", shell=True, stdout=PIPE)
|
| 101 | e69661b7 | Rodolphe Qui?deville | return pid.communicate()[0].split() |
| 102 | 17f78427 | Lars Kruse | |
| 103 | e69661b7 | Rodolphe Qui?deville | 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())) |
