2009-11-13 19:05:10 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: set fileencoding=utf-8
|
|
|
|
#
|
|
|
|
# Munin plugin to show amount of memory used by vm
|
|
|
|
#
|
2010-10-14 17:50:35 +02:00
|
|
|
# Copyright Maxence Dunnewind, Rodolphe Quiédeville, Adrien Pujol
|
2009-11-13 19:05:10 +01:00
|
|
|
#
|
|
|
|
# License : GPLv3
|
|
|
|
#
|
|
|
|
# parsed environment variables:
|
|
|
|
# vmsuffix: part of vm name to be removed
|
|
|
|
#
|
|
|
|
#%# capabilities=autoconf
|
|
|
|
#%# family=contrib
|
|
|
|
|
|
|
|
import re, os, sys
|
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
|
|
|
|
def config(vm_names):
|
|
|
|
''' Print the plugin's config
|
|
|
|
@param vm_names : a list of "cleaned" vms' name
|
|
|
|
'''
|
|
|
|
base_config = """graph_title KVM Virtual Machine Memory usage
|
|
|
|
graph_vlabel Bytes
|
2018-08-01 23:56:56 +02:00
|
|
|
graph_category virtualization
|
2009-11-13 19:05:10 +01:00
|
|
|
graph_info This graph shows the current amount of memory used by virtual machines
|
|
|
|
graph_args --base 1024
|
|
|
|
"""
|
|
|
|
print base_config
|
|
|
|
draw = "AREA"
|
|
|
|
for vm in vm_names:
|
|
|
|
print "%s_mem.label %s" % (vm, vm)
|
|
|
|
print "%s_mem.type GAUGE" % vm
|
|
|
|
if draw == 'AREA':
|
|
|
|
print "%s_mem.min 0" % vm
|
|
|
|
print "%s_mem.draw %s" % (vm, draw)
|
|
|
|
print "%s_mem.info memory used by virtual machine %s" % (vm, vm)
|
|
|
|
draw = "STACK"
|
|
|
|
|
|
|
|
|
|
|
|
def clean_vm_name(vm_name):
|
|
|
|
''' Replace all special chars
|
|
|
|
@param vm_name : a vm's name
|
|
|
|
@return cleaned vm's name
|
|
|
|
'''
|
|
|
|
# suffix part defined in conf
|
|
|
|
suffix = os.getenv('vmsuffix')
|
|
|
|
if suffix:
|
|
|
|
vm_name = re.sub(suffix,'',vm_name)
|
|
|
|
|
|
|
|
return re.sub(r"[^a-zA-Z0-9_]", "_", vm_name)
|
|
|
|
|
|
|
|
def fetch(vms):
|
|
|
|
''' Fetch values for a list of pids
|
|
|
|
@param dictionnary {kvm_pid: cleaned vm name}
|
|
|
|
'''
|
|
|
|
res = {}
|
|
|
|
for pid in vms:
|
2010-10-14 17:50:35 +02:00
|
|
|
try:
|
|
|
|
cmdline = open("/proc/%s/cmdline" % pid, "r")
|
|
|
|
amount = re.sub(r"^.*-m\x00(.*)\x00-smp.*$",r"\1", cmdline.readline())
|
2014-12-05 00:37:42 +01:00
|
|
|
amount = int(amount) * 1024 * 1024
|
|
|
|
print "%s_mem.value %s" % (vms[pid], amount)
|
2010-10-14 17:50:35 +02:00
|
|
|
except:
|
|
|
|
cmdline = open("/proc/%s/cmdline" % pid, "r")
|
|
|
|
amount = re.sub(r"^.*-m\x00(\d+).*$",r"\1", cmdline.readline())
|
2014-12-05 00:37:42 +01:00
|
|
|
amount = int(amount) * 1024 * 1024
|
|
|
|
print "%s_mem.value %s" % (vms[pid], amount)
|
2009-11-13 19:05:10 +01:00
|
|
|
|
|
|
|
def detect_kvm():
|
|
|
|
''' Check if kvm is installed
|
|
|
|
'''
|
|
|
|
kvm = Popen("which kvm", shell=True, stdout=PIPE)
|
|
|
|
kvm.communicate()
|
|
|
|
return not bool(kvm.returncode)
|
|
|
|
|
|
|
|
def find_vm_names(pids):
|
|
|
|
'''Find and clean vm names from pids
|
|
|
|
@return a dictionnary of {pids : cleaned vm name}
|
|
|
|
'''
|
|
|
|
result = {}
|
|
|
|
for pid in pids:
|
|
|
|
cmdline = open("/proc/%s/cmdline" % pid, "r")
|
2015-05-21 15:40:11 +02:00
|
|
|
result[pid] = clean_vm_name(re.sub(r"^.*-name\x00([a-zA-Z0-9.-_-]*)\x00\-.*$",r"\1", cmdline.readline()))
|
2009-11-13 19:05:10 +01:00
|
|
|
return result
|
|
|
|
|
|
|
|
def list_pids():
|
|
|
|
''' Find the pid of kvm processes
|
|
|
|
@return a list of pids from running kvm
|
|
|
|
'''
|
2017-09-30 17:42:37 +02:00
|
|
|
pid = Popen("pidof qemu-kvm qemu-system-x86_64 kvm", shell=True, stdout=PIPE)
|
2009-11-13 19:05:10 +01:00
|
|
|
return pid.communicate()[0].split()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
if sys.argv[1] in ['autoconf', 'detect']:
|
|
|
|
if detect_kvm():
|
|
|
|
print "yes"
|
|
|
|
else:
|
|
|
|
print "no"
|
|
|
|
elif sys.argv[1] == "config":
|
|
|
|
config(find_vm_names(list_pids()).values())
|
|
|
|
else:
|
|
|
|
fetch(find_vm_names(list_pids()))
|
|
|
|
else:
|
|
|
|
fetch(find_vm_names(list_pids()))
|