root / plugins / virtualization / kvm_cpu @ e5ce7492
Historique | Voir | Annoter | Télécharger (2,9 ko)
| 1 | dda92e9b | Rodolphe Qui?deville | #!/usr/bin/python |
|---|---|---|---|
| 2 | # -*- coding: utf-8 -*- |
||
| 3 | # vim: set fileencoding=utf-8 |
||
| 4 | # |
||
| 5 | # Munin plugin to show CPU used 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 | e78a1590 | loic | percent = len(filter(lambda x: x[0:3] == 'cpu' and x[3] != ' ', open('/proc/stat', 'r').readlines())) * 100
|
| 25 | |||
| 26 | dda92e9b | Rodolphe Qui?deville | base_config = """graph_title KVM Virtual Machine CPU usage |
| 27 | e78a1590 | loic | graph_vlabel %% |
| 28 | dda92e9b | Rodolphe Qui?deville | graph_category KVM |
| 29 | e78a1590 | loic | graph_scale no |
| 30 | graph_period second |
||
| 31 | dda92e9b | Rodolphe Qui?deville | graph_info This graph shows the current CPU used by virtual machines |
| 32 | e78a1590 | loic | graph_args --base 1000 -r --lower-limit 0 --upper-limit %d""" % percent |
| 33 | dda92e9b | Rodolphe Qui?deville | print base_config |
| 34 | draw = "AREA" |
||
| 35 | for vm in vm_names: |
||
| 36 | print "%s_cpu.label %s" % (vm, vm) |
||
| 37 | print "%s_cpu.min 0" % vm |
||
| 38 | e78a1590 | loic | print "%s_cpu.type DERIVE" % vm |
| 39 | dda92e9b | Rodolphe Qui?deville | print "%s_cpu.draw %s" % (vm, draw) |
| 40 | print "%s_cpu.info percent of cpu time used by virtual machine" % vm |
||
| 41 | draw = "STACK" |
||
| 42 | |||
| 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 | |||
| 50 | # suffix part defined in conf |
||
| 51 | suffix = os.getenv('vmsuffix')
|
||
| 52 | if suffix: |
||
| 53 | vm_name = re.sub(suffix,'',vm_name) |
||
| 54 | |||
| 55 | return re.sub(r"[^a-zA-Z0-9_]", "_", vm_name) |
||
| 56 | |||
| 57 | def detect_kvm(): |
||
| 58 | ''' Check if kvm is installed |
||
| 59 | ''' |
||
| 60 | kvm = Popen("which kvm", shell=True, stdout=PIPE)
|
||
| 61 | kvm.communicate() |
||
| 62 | return not bool(kvm.returncode) |
||
| 63 | |||
| 64 | def find_vm_names(pids): |
||
| 65 | '''Find and clean vm names from pids |
||
| 66 | @return a dictionnary of {pids : cleaned vm name}
|
||
| 67 | ''' |
||
| 68 | result = {}
|
||
| 69 | for pid in pids: |
||
| 70 | cmdline = open("/proc/%s/cmdline" % pid, "r")
|
||
| 71 | result[pid] = clean_vm_name(re.sub(r"^.*-name\x00([a-zA-Z0-9.-]*)\x00\-.*$",r"\1", cmdline.readline())) |
||
| 72 | return result |
||
| 73 | |||
| 74 | def list_pids(): |
||
| 75 | ''' Find the pid of kvm processes |
||
| 76 | @return a list of pids from running kvm |
||
| 77 | ''' |
||
| 78 | pid = Popen("pidof kvm", shell=True, stdout=PIPE)
|
||
| 79 | return pid.communicate()[0].split() |
||
| 80 | |||
| 81 | def fetch(vms): |
||
| 82 | ''' Fetch values for a list of pids |
||
| 83 | @param dictionnary {kvm_pid: cleaned vm name}
|
||
| 84 | ''' |
||
| 85 | e78a1590 | loic | for ( pid, name ) in vms.iteritems(): |
| 86 | ( user, system ) = open("/proc/%s/stat" % pid, 'r').readline().split(' ')[13:15]
|
||
| 87 | print '%s_cpu.value %d' % ( name, int(user) + int(system) ) |
||
| 88 | dda92e9b | Rodolphe Qui?deville | |
| 89 | if __name__ == "__main__": |
||
| 90 | if len(sys.argv) > 1: |
||
| 91 | if sys.argv[1] in ['autoconf', 'detect']: |
||
| 92 | if detect_kvm(): |
||
| 93 | print "yes" |
||
| 94 | else: |
||
| 95 | print "no" |
||
| 96 | elif sys.argv[1] == "config": |
||
| 97 | config(find_vm_names(list_pids()).values()) |
||
| 98 | else: |
||
| 99 | fetch(find_vm_names(list_pids())) |
||
| 100 | else: |
||
| 101 | fetch(find_vm_names(list_pids())) |
