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

150 lines
3.5 KiB
Plaintext
Raw Normal View History

2018-06-10 17:37:22 +02:00
#!/usr/bin/python3
"""
=head1 NAME
kvm_net - Munin plugin to show the network I/O per VM
=head1 CONFIGURATION
parsed environment variables:
* vmsuffix: part of vm name to be removed
=head1 AUTHOR
Copyright (C) 2012 - Igor Borodikhin
Copyright (C) 2018 - Lars Kruse <devel@sumpfralle.de>
=head1 LICENSE
GPLv3
=head1 MAGIC MARKERS
#%# capabilities=autoconf
#%# family=contrib
=cut
"""
2009-11-13 19:07:27 +01:00
2018-06-10 17:44:22 +02:00
import os
import re
2009-11-13 19:07:27 +01:00
from subprocess import Popen, PIPE
2018-06-10 17:44:22 +02:00
import sys
2009-11-13 19:07:27 +01:00
2018-06-10 17:35:31 +02:00
2009-11-13 19:07:27 +01:00
def config(vm_names):
2018-06-10 17:33:36 +02:00
""" Print the plugin's config
2009-11-13 19:07:27 +01:00
@param vm_names : a list of "cleaned" vms' name
2018-06-10 17:33:36 +02:00
"""
2009-11-13 19:07:27 +01:00
base_config = """graph_title KVM Network I/O
graph_vlabel Bytes rx(-)/tx(+) per second
2014-09-07 14:20:12 +02:00
graph_category Virtualization
2009-11-13 19:07:27 +01:00
graph_info This graph shows the network I/O of the virtual machines
graph_args --base 1024
"""
print(base_config)
2009-11-13 19:07:27 +01:00
for vm in vm_names:
print("%s_in.label %s" % (vm, vm))
print("%s_in.type COUNTER" % vm)
print("%s_in.min 0" % vm)
print("%s_in.draw LINE2" % vm)
print("%s_out.negative %s_in" % (vm, vm))
print("%s_out.label %s" % (vm, vm))
print("%s_out.type COUNTER" % vm)
print("%s_out.min 0" % vm)
print("%s_out.draw LINE2" % vm)
2009-11-13 19:07:27 +01:00
2018-06-10 17:35:31 +02:00
2009-11-13 19:07:27 +01:00
def clean_vm_name(vm_name):
2018-06-10 17:33:36 +02:00
""" Replace all special chars
2009-11-13 19:07:27 +01:00
@param vm_name : a vm's name
@return cleaned vm's name
2018-06-10 17:33:36 +02:00
"""
2009-11-13 19:07:27 +01:00
# suffix part defined in conf
2018-06-10 17:33:36 +02:00
suffix = os.getenv("vmsuffix")
2009-11-13 19:07:27 +01:00
if suffix:
2018-06-10 17:33:36 +02:00
vm_name = re.sub(suffix, "", vm_name)
2009-11-13 19:07:27 +01:00
return re.sub(r"[^a-zA-Z0-9_]", "_", vm_name)
2018-06-10 17:35:31 +02:00
2009-11-13 19:07:27 +01:00
def fetch(vms):
2018-06-10 17:33:36 +02:00
""" Fetch values for a list of pids
2009-11-13 19:07:27 +01:00
@param dictionnary {kvm_pid: cleaned vm name}
2018-06-10 17:33:36 +02:00
"""
2009-11-13 19:07:27 +01:00
for pid in vms:
tap = get_vm_mac(pid)
2009-11-13 19:07:27 +01:00
try:
f = open("/proc/net/dev", "r")
for line in f.readlines():
if tap in line:
print("%s_in.value %s" % (vms[pid], line.split()[1]))
print("%s_out.value %s" % (vms[pid], line.split()[9]))
break
except Exception as inst:
print(inst)
2009-11-13 19:07:27 +01:00
continue
2018-06-10 17:35:31 +02:00
2009-11-13 19:07:27 +01:00
def detect_kvm():
2018-06-10 17:33:36 +02:00
""" Check if kvm is installed """
2018-06-10 21:40:04 +02:00
kvm = Popen(["which", "kvm"], stdout=PIPE)
2009-11-13 19:07:27 +01:00
kvm.communicate()
2018-06-10 21:40:04 +02:00
return kvm.returncode == 0
2009-11-13 19:07:27 +01:00
2018-06-10 17:35:31 +02:00
2009-11-13 19:07:27 +01:00
def find_vm_names(pids):
2018-06-10 17:33:36 +02:00
"""Find and clean vm names from pids
2009-11-13 19:07:27 +01:00
@return a dictionnary of {pids : cleaned vm name}
2018-06-10 17:33:36 +02:00
"""
2018-06-10 17:44:22 +02:00
vm_name_regex = re.compile(r"^.*-name\x00([a-zA-Z0-9.-_-]*)\x00\-.*$")
2009-11-13 19:07:27 +01:00
result = {}
for pid in pids:
cmdline = open("/proc/%s/cmdline" % pid, "r")
2018-06-10 17:44:22 +02:00
result[pid] = clean_vm_name(vm_name_regex.sub(r"\1", cmdline.readline()))
2009-11-13 19:07:27 +01:00
return result
2018-06-10 17:35:31 +02:00
2009-11-13 19:07:27 +01:00
def get_vm_mac(pid):
2018-06-10 17:33:36 +02:00
"""Find and clean vm names from pids
2009-11-13 19:07:27 +01:00
@return the mac address for a specified pid
2018-06-10 17:33:36 +02:00
"""
2009-11-13 19:07:27 +01:00
cmdline = open("/proc/%s/cmdline" % pid, "r")
line = cmdline.readline()
2018-06-10 17:44:22 +02:00
mac = re.sub(r"^.*ifname=(tap[^,]+),.*$", r"\1", line)
2009-11-13 19:07:27 +01:00
return mac
2018-06-10 17:35:31 +02:00
2009-11-13 19:07:27 +01:00
def list_pids():
2018-06-10 17:33:36 +02:00
""" Find the pid of kvm processes
2009-11-13 19:07:27 +01:00
@return a list of pids from running kvm
2018-06-10 17:33:36 +02:00
"""
2018-06-10 21:40:04 +02:00
pid = Popen(["pidof", "qemu-kvm", "qemu-system-x86_64", "kvm"], stdout=PIPE)
2018-06-10 17:37:22 +02:00
return pid.communicate()[0].decode().split()
2009-11-13 19:07:27 +01:00
2009-11-13 19:07:27 +01:00
if __name__ == "__main__":
2018-06-10 21:40:20 +02:00
action = sys.argv[1] if len(sys.argv) > 1 else None
if action == "autoconf":
if detect_kvm():
print("yes")
2009-11-13 19:07:27 +01:00
else:
2018-06-10 21:40:20 +02:00
print("no")
elif action == "config":
vm_data = find_vm_names(list_pids())
config(vm_data.values())
2009-11-13 19:07:27 +01:00
else:
2018-06-10 21:40:20 +02:00
vm_data = find_vm_names(list_pids())
fetch(vm_data)