2018-06-10 17:37:22 +02:00
|
|
|
#!/usr/bin/python3
|
2018-06-10 17:40:40 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
kvm_net - Munin plugin to show the network I/O per VM
|
|
|
|
|
|
|
|
|
2018-06-10 21:53:39 +02:00
|
|
|
=head1 APPLICABLE SYSTEMS
|
|
|
|
|
|
|
|
Virtualization server with VMs based on KVM may be able to track the network
|
|
|
|
traffic of their VMs, if the KVM processes are started in a specific way.
|
|
|
|
|
|
|
|
Probably proxmox-based virtualization hosts fit into this category.
|
|
|
|
|
|
|
|
You can easily check if your KVM processes are started in the expected way, by
|
|
|
|
running the following command:
|
|
|
|
|
|
|
|
ps -ef | grep "netdev.*ifname="
|
|
|
|
|
|
|
|
The plugin can be used, if the above command outputs one line for every
|
|
|
|
currently running VM.
|
|
|
|
|
|
|
|
In all other cases you need to use other munin plugins instead, e.g. "libvirt".
|
|
|
|
|
|
|
|
|
2018-06-10 17:40:40 +02:00
|
|
|
=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
|
|
|
|
2018-06-10 21:48:08 +02:00
|
|
|
VM_NAME_REGEX = re.compile("^.*\x00-{arg_name}\x00(.+)\x00.*$")
|
2018-06-10 21:53:39 +02:00
|
|
|
KVM_INTERFACE_NAME_REGEX = re.compile("(?:^|,)ifname=([^,]+)(?:,|$)")
|
2018-06-10 21:48:08 +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
|
|
|
|
"""
|
2018-06-10 17:36:11 +02:00
|
|
|
print(base_config)
|
2009-11-13 19:07:27 +01:00
|
|
|
for vm in vm_names:
|
2018-06-10 17:36:11 +02:00
|
|
|
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
|
|
|
"""
|
2018-06-10 21:53:39 +02:00
|
|
|
for pid, vm_data in vms.items():
|
|
|
|
vm_interface_names = get_vm_network_interface_names(pid)
|
|
|
|
sum_incoming = 0
|
|
|
|
sum_outgoing = 0
|
|
|
|
interface_found = False
|
|
|
|
with open("/proc/net/dev", "r") as net_file:
|
|
|
|
for line in net_file.readlines():
|
|
|
|
tokens = line.split()
|
|
|
|
current_interface_name = tokens[0].rstrip(":").strip()
|
|
|
|
if current_interface_name in vm_interface_names:
|
|
|
|
sum_incoming += int(tokens[1])
|
|
|
|
sum_outgoing += int(tokens[9])
|
|
|
|
interface_found = True
|
|
|
|
if not interface_found:
|
|
|
|
# we want to distinguish "no traffic" from "not found"
|
|
|
|
sum_incoming = "U"
|
|
|
|
sum_outgoing = "U"
|
|
|
|
print("%s_in.value %s" % (vm_data, sum_incoming))
|
|
|
|
print("%s_out.value %s" % (vm_data, sum_outgoing))
|
|
|
|
|
|
|
|
|
|
|
|
def get_vm_network_interface_names(pid):
|
|
|
|
""" return the MAC addresses configured for network interfacs of a PID """
|
|
|
|
result = set()
|
|
|
|
for netdev_description in _get_kvm_process_arguments(pid, "netdev"):
|
|
|
|
match = KVM_INTERFACE_NAME_REGEX.search(netdev_description)
|
|
|
|
if match:
|
|
|
|
result.add(match.groups()[0])
|
|
|
|
return result
|
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 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
|
|
|
"""
|
2009-11-13 19:07:27 +01:00
|
|
|
result = {}
|
|
|
|
for pid in pids:
|
2018-06-10 21:48:08 +02:00
|
|
|
name = None
|
|
|
|
name_arg_values = _get_kvm_process_arguments(pid, "name")
|
|
|
|
if name_arg_values:
|
|
|
|
name_arg_value = name_arg_values[0]
|
|
|
|
if "," in name_arg_value:
|
|
|
|
# the modern parameter format may look like this:
|
|
|
|
# guest=foo,debug-threads=on
|
|
|
|
for index, token in enumerate(name_arg_value.split(",")):
|
|
|
|
if (index == 0) and ("=" not in token):
|
|
|
|
# the first item may the plain name
|
|
|
|
name = value
|
|
|
|
elif "=" in token:
|
|
|
|
key, value = token.split("=", 1)
|
|
|
|
if key == "guest":
|
|
|
|
name = value
|
|
|
|
else:
|
|
|
|
# unknown format (no "mapping")
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
name = name_arg_value
|
|
|
|
if name is None:
|
|
|
|
print("Failed to parse VM name from commandline of process: {}"
|
|
|
|
.format(name_arg_values), file=sys.stderr)
|
|
|
|
else:
|
|
|
|
result[pid] = clean_vm_name(name)
|
2009-11-13 19:07:27 +01:00
|
|
|
return result
|
2018-06-10 17:35:31 +02:00
|
|
|
|
|
|
|
|
2018-06-10 21:48:08 +02:00
|
|
|
def _get_kvm_process_arguments(pid, arg_name):
|
|
|
|
""" parse all value with the given name from the process identified by PID
|
|
|
|
|
|
|
|
The result is a list of tokens, that follow this argument name. The result
|
|
|
|
is empty in case of problems.
|
|
|
|
"""
|
|
|
|
# the "cmdline" (e.g. /proc/self/cmdline) is a null-separated token list
|
|
|
|
try:
|
|
|
|
with open("/proc/%s/cmdline" % pid, "r") as cmdline_file:
|
|
|
|
cmdline = cmdline_file.read()
|
|
|
|
except IOError:
|
|
|
|
# the process seems to have died meanwhile
|
|
|
|
return []
|
|
|
|
is_value = False
|
|
|
|
result = []
|
|
|
|
for arg_token in cmdline.split("\0"):
|
|
|
|
if is_value:
|
|
|
|
# the previous token was our argument name
|
|
|
|
result.append(arg_token)
|
|
|
|
is_value = False
|
|
|
|
elif arg_token == "-{}".format(arg_name):
|
|
|
|
# this is our argument name - we want to store the next value
|
|
|
|
is_value = True
|
|
|
|
else:
|
|
|
|
# any other irrelevant value
|
|
|
|
pass
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2012-04-10 08:40:37 +02: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)
|