2
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2018-11-08 00:59:34 +01:00
contrib-munin/plugins/kvm/kvm_cpu

102 lines
2.9 KiB
Text
Raw Normal View History

2009-11-13 19:06:52 +01:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vim: set fileencoding=utf-8
#
# Munin plugin to show CPU used by vm
#
# Copyright Maxence Dunnewind, Rodolphe Quiédeville
#
# 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
'''
2010-08-18 11:46:25 +02:00
percent = len(filter(lambda x: x[0:3] == 'cpu' and x[3] != ' ', open('/proc/stat', 'r').readlines())) * 100
2009-11-13 19:06:52 +01:00
base_config = """graph_title KVM Virtual Machine CPU usage
2010-08-18 11:46:25 +02:00
graph_vlabel %%
2009-11-13 19:06:52 +01:00
graph_category KVM
2010-08-18 11:46:25 +02:00
graph_scale no
graph_period second
2009-11-13 19:06:52 +01:00
graph_info This graph shows the current CPU used by virtual machines
2010-08-18 11:46:25 +02:00
graph_args --base 1000 -r --lower-limit 0 --upper-limit %d""" % percent
2009-11-13 19:06:52 +01:00
print base_config
draw = "AREA"
for vm in vm_names:
print "%s_cpu.label %s" % (vm, vm)
print "%s_cpu.min 0" % vm
2010-08-18 11:46:25 +02:00
print "%s_cpu.type DERIVE" % vm
2009-11-13 19:06:52 +01:00
print "%s_cpu.draw %s" % (vm, draw)
print "%s_cpu.info percent of cpu time used by virtual machine" % 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 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")
result[pid] = clean_vm_name(re.sub(r"^.*-name\x00([a-zA-Z0-9.-]*)\x00\-.*$",r"\1", cmdline.readline()))
return result
def list_pids():
''' Find the pid of kvm processes
@return a list of pids from running kvm
'''
pid = Popen("pidof kvm", shell=True, stdout=PIPE)
return pid.communicate()[0].split()
def fetch(vms):
''' Fetch values for a list of pids
@param dictionnary {kvm_pid: cleaned vm name}
'''
2010-08-18 11:46:25 +02:00
for ( pid, name ) in vms.iteritems():
( user, system ) = open("/proc/%s/stat" % pid, 'r').readline().split(' ')[13:15]
print '%s_cpu.value %d' % ( name, int(user) + int(system) )
2009-11-13 19:06:52 +01:00
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()))