root / plugins / virtualization / kvm_mem @ e5ce7492
Historique | Voir | Annoter | Télécharger (3,12 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 KVM |
| 27 |
graph_info This graph shows the current amount of memory used by virtual machines |
| 28 |
graph_args --base 1024 |
| 29 |
""" |
| 30 |
print base_config |
| 31 |
draw = "AREA" |
| 32 |
for vm in vm_names: |
| 33 |
print "%s_mem.label %s" % (vm, vm) |
| 34 |
print "%s_mem.type GAUGE" % vm |
| 35 |
if draw == 'AREA': |
| 36 |
print "%s_mem.min 0" % vm |
| 37 |
print "%s_mem.draw %s" % (vm, draw) |
| 38 |
print "%s_mem.info memory used by virtual machine %s" % (vm, vm) |
| 39 |
draw = "STACK" |
| 40 |
|
| 41 |
|
| 42 |
def clean_vm_name(vm_name): |
| 43 |
''' Replace all special chars |
| 44 |
@param vm_name : a vm's name |
| 45 |
@return cleaned vm's name |
| 46 |
''' |
| 47 |
# suffix part defined in conf |
| 48 |
suffix = os.getenv('vmsuffix')
|
| 49 |
if suffix: |
| 50 |
vm_name = re.sub(suffix,'',vm_name) |
| 51 |
|
| 52 |
return re.sub(r"[^a-zA-Z0-9_]", "_", vm_name) |
| 53 |
|
| 54 |
def fetch(vms): |
| 55 |
''' Fetch values for a list of pids |
| 56 |
@param dictionnary {kvm_pid: cleaned vm name}
|
| 57 |
''' |
| 58 |
res = {}
|
| 59 |
for pid in vms: |
| 60 |
try: |
| 61 |
cmdline = open("/proc/%s/cmdline" % pid, "r")
|
| 62 |
amount = re.sub(r"^.*-m\x00(.*)\x00-smp.*$",r"\1", cmdline.readline()) |
| 63 |
ammount = int(amount) * 1024 * 1024 |
| 64 |
print "%s_mem.value %s" % (vms[pid], ammount) |
| 65 |
except: |
| 66 |
cmdline = open("/proc/%s/cmdline" % pid, "r")
|
| 67 |
amount = re.sub(r"^.*-m\x00(\d+).*$",r"\1", cmdline.readline()) |
| 68 |
ammount = int(amount) * 1024 * 1024 |
| 69 |
print "%s_mem.value %s" % (vms[pid], ammount) |
| 70 |
|
| 71 |
def detect_kvm(): |
| 72 |
''' Check if kvm is installed |
| 73 |
''' |
| 74 |
kvm = Popen("which kvm", shell=True, stdout=PIPE)
|
| 75 |
kvm.communicate() |
| 76 |
return not bool(kvm.returncode) |
| 77 |
|
| 78 |
def find_vm_names(pids): |
| 79 |
'''Find and clean vm names from pids |
| 80 |
@return a dictionnary of {pids : cleaned vm name}
|
| 81 |
''' |
| 82 |
result = {}
|
| 83 |
for pid in pids: |
| 84 |
cmdline = open("/proc/%s/cmdline" % pid, "r")
|
| 85 |
result[pid] = clean_vm_name(re.sub(r"^.*-name\x00([a-zA-Z0-9.-]*)\x00\-.*$",r"\1", cmdline.readline())) |
| 86 |
return result |
| 87 |
|
| 88 |
def list_pids(): |
| 89 |
''' Find the pid of kvm processes |
| 90 |
@return a list of pids from running kvm |
| 91 |
''' |
| 92 |
pid = Popen("pidof kvm", shell=True, stdout=PIPE)
|
| 93 |
return pid.communicate()[0].split() |
| 94 |
|
| 95 |
if __name__ == "__main__": |
| 96 |
if len(sys.argv) > 1: |
| 97 |
if sys.argv[1] in ['autoconf', 'detect']: |
| 98 |
if detect_kvm(): |
| 99 |
print "yes" |
| 100 |
else: |
| 101 |
print "no" |
| 102 |
elif sys.argv[1] == "config": |
| 103 |
config(find_vm_names(list_pids()).values()) |
| 104 |
else: |
| 105 |
fetch(find_vm_names(list_pids())) |
| 106 |
else: |
| 107 |
fetch(find_vm_names(list_pids())) |
