Révision bde90ba9
Plugin kvm_cpu: migrate to Python3, format documentation
| plugins/libvirt/kvm_cpu | ||
|---|---|---|
| 1 |
#!/usr/bin/env 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 |
|
| 1 |
#!/usr/bin/env python3 |
|
| 2 |
""" |
|
| 3 |
=encoding utf8 |
|
| 4 |
|
|
| 5 |
=head1 NAME |
|
| 6 |
|
|
| 7 |
kvm_cpu - show CPU usage of VM |
|
| 8 |
|
|
| 9 |
|
|
| 10 |
=head1 CONFIGURATION |
|
| 11 |
|
|
| 12 |
Parsed environment variables: |
|
| 13 |
|
|
| 14 |
vmsuffix: part of VM name to be removed |
|
| 15 |
|
|
| 16 |
|
|
| 17 |
=head1 LICENSE |
|
| 18 |
|
|
| 19 |
GPLv3 |
|
| 20 |
|
|
| 21 |
SPDX-License-Identifier: GPL-3.0-only |
|
| 22 |
|
|
| 23 |
|
|
| 24 |
=head1 AUTHORS |
|
| 25 |
|
|
| 26 |
Maxence Dunnewind |
|
| 27 |
|
|
| 28 |
Rodolphe Quiédeville |
|
| 29 |
|
|
| 30 |
|
|
| 31 |
=head1 MAGIC MARKERS |
|
| 32 |
|
|
| 33 |
#%# capabilities=autoconf |
|
| 34 |
#%# family=contrib |
|
| 35 |
|
|
| 36 |
=cut |
|
| 37 |
""" |
|
| 38 |
|
|
| 39 |
import os |
|
| 40 |
import re |
|
| 41 |
import sys |
|
| 18 | 42 |
from subprocess import Popen, PIPE |
| 19 | 43 |
|
| 44 |
|
|
| 20 | 45 |
def config(vm_names): |
| 21 | 46 |
''' Print the plugin's config |
| 22 | 47 |
@param vm_names : a list of "cleaned" vms' name |
| 23 | 48 |
''' |
| 24 |
percent = len(filter(lambda x: x[0:3] == 'cpu' and x[3] != ' ', open('/proc/stat', 'r').readlines())) * 100
|
|
| 49 |
percent = 100 * len( |
|
| 50 |
filter(lambda x: x[0:3] == 'cpu' and x[3] != ' ', open('/proc/stat', 'r').readlines()))
|
|
| 25 | 51 |
|
| 26 | 52 |
base_config = """graph_title KVM Virtual Machine CPU usage |
| 27 | 53 |
graph_vlabel %% |
| ... | ... | |
| 30 | 56 |
graph_period second |
| 31 | 57 |
graph_info This graph shows the current CPU used by virtual machines |
| 32 | 58 |
graph_args --base 1000 -r --lower-limit 0 --upper-limit %d""" % percent |
| 33 |
print base_config |
|
| 34 |
draw = "AREA" |
|
| 59 |
print(base_config) |
|
| 35 | 60 |
for vm in vm_names: |
| 36 |
print "%s_cpu.label %s" % (vm, vm) |
|
| 37 |
print "%s_cpu.min 0" % vm |
|
| 38 |
print "%s_cpu.type DERIVE" % vm |
|
| 39 |
print "%s_cpu.draw %s" % (vm, draw) |
|
| 40 |
print "%s_cpu.info percent of cpu time used by virtual machine" % vm |
|
| 41 |
draw = "STACK" |
|
| 61 |
print("%s_cpu.label %s" % (vm, vm))
|
|
| 62 |
print("%s_cpu.min 0" % vm)
|
|
| 63 |
print("%s_cpu.type DERIVE" % vm)
|
|
| 64 |
print("%s_cpu.draw AREASTACK" % vm)
|
|
| 65 |
print("%s_cpu.info percent of cpu time used by virtual machine" % vm)
|
|
| 42 | 66 |
|
| 43 | 67 |
|
| 44 | 68 |
def clean_vm_name(vm_name): |
| ... | ... | |
| 46 | 70 |
@param vm_name : a vm's name |
| 47 | 71 |
@return cleaned vm's name |
| 48 | 72 |
''' |
| 49 |
|
|
| 50 | 73 |
# suffix part defined in conf |
| 51 | 74 |
suffix = os.getenv('vmsuffix')
|
| 52 | 75 |
if suffix: |
| 53 |
vm_name = re.sub(suffix,'',vm_name) |
|
| 54 |
|
|
| 76 |
vm_name = re.sub(suffix, '', vm_name) |
|
| 55 | 77 |
# proxmox uses kvm with -name parameter |
| 56 | 78 |
parts = vm_name.split('\x00')
|
| 57 |
if (parts[0].endswith('kvm')):
|
|
| 79 |
if parts[0].endswith('kvm'):
|
|
| 58 | 80 |
try: |
| 59 |
return parts[parts.index('-name')+1]
|
|
| 81 |
return parts[parts.index('-name') + 1]
|
|
| 60 | 82 |
except ValueError: |
| 61 | 83 |
pass |
| 62 |
|
|
| 63 | 84 |
return re.sub(r"[^a-zA-Z0-9_]", "_", vm_name) |
| 64 | 85 |
|
| 86 |
|
|
| 65 | 87 |
def detect_kvm(): |
| 66 |
''' Check if kvm is installed |
|
| 67 |
''' |
|
| 88 |
''' Check if kvm is installed ''' |
|
| 68 | 89 |
kvm = Popen("which kvm", shell=True, stdout=PIPE)
|
| 69 | 90 |
kvm.communicate() |
| 70 | 91 |
return not bool(kvm.returncode) |
| 71 | 92 |
|
| 93 |
|
|
| 72 | 94 |
def find_vm_names(pids): |
| 73 | 95 |
'''Find and clean vm names from pids |
| 74 | 96 |
@return a dictionary of {pids : cleaned vm name}
|
| ... | ... | |
| 76 | 98 |
result = {}
|
| 77 | 99 |
for pid in pids: |
| 78 | 100 |
cmdline = open("/proc/%s/cmdline" % pid, "r")
|
| 79 |
result[pid] = clean_vm_name(re.sub(r"^.*guest=([a-zA-Z0-9.-_-]*).*$",r"\1", cmdline.readline())) |
|
| 101 |
result[pid] = clean_vm_name( |
|
| 102 |
re.sub(r"^.*guest=([a-zA-Z0-9.-_-]*).*$", r"\1", cmdline.readline())) |
|
| 80 | 103 |
return result |
| 81 | 104 |
|
| 105 |
|
|
| 82 | 106 |
def list_pids(): |
| 83 | 107 |
''' Find the pid of kvm processes |
| 84 | 108 |
@return a list of pids from running kvm |
| ... | ... | |
| 86 | 110 |
pid = Popen("pidof qemu-kvm qemu-system-x86_64 kvm", shell=True, stdout=PIPE)
|
| 87 | 111 |
return pid.communicate()[0].split() |
| 88 | 112 |
|
| 113 |
|
|
| 89 | 114 |
def fetch(vms): |
| 90 | 115 |
''' Fetch values for a list of pids |
| 91 | 116 |
@param dictionary {kvm_pid: cleaned vm name}
|
| 92 | 117 |
''' |
| 93 |
for ( pid, name ) in vms.iteritems(): |
|
| 94 |
( user, system ) = open("/proc/%s/stat" % pid, 'r').readline().split(' ')[13:15]
|
|
| 95 |
print '%s_cpu.value %d' % ( name, int(user) + int(system) ) |
|
| 118 |
for pid, name in vms.items(): |
|
| 119 |
user, system = open("/proc/%s/stat" % pid, 'r').readline().split(' ')[13:15]
|
|
| 120 |
print('%s_cpu.value %d' % (name, int(user) + int(system)))
|
|
| 121 |
|
|
| 96 | 122 |
|
| 97 | 123 |
if __name__ == "__main__": |
| 98 | 124 |
if len(sys.argv) > 1: |
| 99 | 125 |
if sys.argv[1] in ['autoconf', 'detect']: |
| 100 | 126 |
if detect_kvm(): |
| 101 |
print "yes"
|
|
| 127 |
print("yes")
|
|
| 102 | 128 |
else: |
| 103 |
print "no"
|
|
| 129 |
print("no")
|
|
| 104 | 130 |
elif sys.argv[1] == "config": |
| 105 | 131 |
config(find_vm_names(list_pids()).values()) |
| 106 | 132 |
else: |
| 107 | 133 |
fetch(find_vm_names(list_pids())) |
| 108 | 134 |
else: |
| 109 | 135 |
fetch(find_vm_names(list_pids())) |
| 110 |
|
|
| t/test-exception-wrapper.expected-failures | ||
|---|---|---|
| 170 | 170 |
plugins/jvm/jstat__gccount |
| 171 | 171 |
plugins/jvm/jstat__gctime |
| 172 | 172 |
plugins/jvm/jstat__heap |
| 173 |
plugins/libvirt/kvm_cpu |
|
| 174 | 173 |
plugins/libvirt/kvm_io |
| 175 | 174 |
plugins/libvirt/kvm_mem |
| 176 | 175 |
plugins/libvirt/kvm_net |
Formats disponibles : Unified diff