root / plugins / libvirt / kvm_mem @ 8cb47cbf
Historique | Voir | Annoter | Télécharger (3,35 ko)
| 1 |
#!/usr/bin/python |
|---|---|
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# vim: set fileencoding=utf-8 |
| 4 |
# |
| 5 |
# Munin plugin to show amount of memory used by vm |
| 6 |
# |
| 7 |
# Copyright Maxence Dunnewind, Rodolphe Quiédeville, Adrien Pujol |
| 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 Memory usage |
| 25 |
graph_vlabel Bytes |
| 26 |
graph_category virtualization |
| 27 |
graph_info This graph shows the current amount of memory used by virtual machines |
| 28 |
graph_args --base 1024""" |
| 29 |
print base_config |
| 30 |
draw = "AREA" |
| 31 |
for vm in vm_names: |
| 32 |
print "%s_mem.label %s" % (vm, vm) |
| 33 |
print "%s_mem.type GAUGE" % vm |
| 34 |
if draw == 'AREA': |
| 35 |
print "%s_mem.min 0" % vm |
| 36 |
print "%s_mem.draw %s" % (vm, draw) |
| 37 |
print "%s_mem.info memory used by virtual machine %s" % (vm, vm) |
| 38 |
draw = "STACK" |
| 39 |
|
| 40 |
|
| 41 |
def clean_vm_name(vm_name): |
| 42 |
''' Replace all special chars |
| 43 |
@param vm_name : a vm's name |
| 44 |
@return cleaned vm's name |
| 45 |
''' |
| 46 |
# suffix part defined in conf |
| 47 |
suffix = os.getenv('vmsuffix')
|
| 48 |
if suffix: |
| 49 |
vm_name = re.sub(suffix,'',vm_name) |
| 50 |
|
| 51 |
# proxmox uses kvm with -name parameter |
| 52 |
parts = vm_name.split('\x00')
|
| 53 |
if (parts[0].endswith('kvm')):
|
| 54 |
try: |
| 55 |
return parts[parts.index('-name')+1]
|
| 56 |
except ValueError: |
| 57 |
pass |
| 58 |
|
| 59 |
return re.sub(r"[^a-zA-Z0-9_]", "_", vm_name) |
| 60 |
|
| 61 |
def fetch(vms): |
| 62 |
''' Fetch values for a list of pids |
| 63 |
@param dictionary {kvm_pid: cleaned vm name}
|
| 64 |
''' |
| 65 |
res = {}
|
| 66 |
for pid in vms: |
| 67 |
try: |
| 68 |
cmdline = open("/proc/%s/cmdline" % pid, "r")
|
| 69 |
amount = re.sub(r"^.*-m\x00(.*)\x00-smp.*$",r"\1", cmdline.readline()) |
| 70 |
amount = int(amount) * 1024 * 1024 |
| 71 |
print "%s_mem.value %s" % (vms[pid], amount) |
| 72 |
except: |
| 73 |
cmdline = open("/proc/%s/cmdline" % pid, "r")
|
| 74 |
amount = re.sub(r"^.*-m\x00(\d+).*$",r"\1", cmdline.readline()) |
| 75 |
amount = int(amount) * 1024 * 1024 |
| 76 |
print "%s_mem.value %s" % (vms[pid], amount) |
| 77 |
|
| 78 |
def detect_kvm(): |
| 79 |
''' Check if kvm is installed |
| 80 |
''' |
| 81 |
kvm = Popen("which kvm", shell=True, stdout=PIPE)
|
| 82 |
kvm.communicate() |
| 83 |
return not bool(kvm.returncode) |
| 84 |
|
| 85 |
def find_vm_names(pids): |
| 86 |
'''Find and clean vm names from pids |
| 87 |
@return a dictionary of {pids : cleaned vm name}
|
| 88 |
''' |
| 89 |
result = {}
|
| 90 |
for pid in pids: |
| 91 |
cmdline = open("/proc/%s/cmdline" % pid, "r")
|
| 92 |
result[pid] = clean_vm_name(re.sub(r"^.*guest=([a-zA-Z0-9.-_-]*).*$",r"\1", cmdline.readline())) |
| 93 |
return result |
| 94 |
|
| 95 |
def list_pids(): |
| 96 |
''' Find the pid of kvm processes |
| 97 |
@return a list of pids from running kvm |
| 98 |
''' |
| 99 |
pid = Popen("pidof qemu-kvm qemu-system-x86_64 kvm", shell=True, stdout=PIPE)
|
| 100 |
return pid.communicate()[0].split() |
| 101 |
|
| 102 |
if __name__ == "__main__": |
| 103 |
if len(sys.argv) > 1: |
| 104 |
if sys.argv[1] in ['autoconf', 'detect']: |
| 105 |
if detect_kvm(): |
| 106 |
print "yes" |
| 107 |
else: |
| 108 |
print "no" |
| 109 |
elif sys.argv[1] == "config": |
| 110 |
config(find_vm_names(list_pids()).values()) |
| 111 |
else: |
| 112 |
fetch(find_vm_names(list_pids())) |
| 113 |
else: |
| 114 |
fetch(find_vm_names(list_pids())) |
