2009-11-13 19:07:27 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: set fileencoding=utf-8
|
|
|
|
#
|
|
|
|
# Munin plugin to show the network I/O per vm
|
|
|
|
#
|
2012-04-10 08:40:37 +02:00
|
|
|
# Copyright Igor Borodikhin
|
2009-11-13 19:07:27 +01:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
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
|
|
|
|
"""
|
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
|
|
|
"""
|
2009-11-13 19:07:27 +01:00
|
|
|
res = {}
|
|
|
|
for pid in vms:
|
2012-04-10 08:40:37 +02:00
|
|
|
tap = get_vm_mac(pid)
|
2009-11-13 19:07:27 +01:00
|
|
|
try:
|
2012-04-10 08:40:37 +02:00
|
|
|
f = open("/proc/net/dev", "r")
|
|
|
|
for line in f.readlines():
|
|
|
|
if tap in line:
|
2018-06-10 17:36:11 +02:00
|
|
|
print("%s_in.value %s" % (vms[pid], line.split()[1]))
|
|
|
|
print("%s_out.value %s" % (vms[pid], line.split()[9]))
|
2012-04-10 08:40:37 +02:00
|
|
|
break
|
|
|
|
except Exception as inst:
|
2018-06-10 17:36:11 +02:00
|
|
|
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 """
|
2009-11-13 19:07:27 +01:00
|
|
|
kvm = Popen("which kvm", shell=True, stdout=PIPE)
|
|
|
|
kvm.communicate()
|
|
|
|
return not bool(kvm.returncode)
|
|
|
|
|
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:
|
|
|
|
cmdline = open("/proc/%s/cmdline" % pid, "r")
|
2018-06-10 17:35:31 +02:00
|
|
|
result[pid] = clean_vm_name(re.sub(r"^.*-name\x00([a-zA-Z0-9.-_-]*)\x00\-.*$",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")
|
2012-04-10 08:40:37 +02:00
|
|
|
line = cmdline.readline()
|
|
|
|
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
|
|
|
"""
|
2017-09-30 17:42:37 +02:00
|
|
|
pid = Popen("pidof qemu-kvm qemu-system-x86_64 kvm", shell=True, stdout=PIPE)
|
2009-11-13 19:07:27 +01:00
|
|
|
return pid.communicate()[0].split()
|
|
|
|
|
2012-04-10 08:40:37 +02:00
|
|
|
|
2009-11-13 19:07:27 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
if len(sys.argv) > 1:
|
2018-06-10 17:33:36 +02:00
|
|
|
if sys.argv[1] in ["autoconf", "detect"]:
|
2009-11-13 19:07:27 +01:00
|
|
|
if detect_kvm():
|
2018-06-10 17:36:11 +02:00
|
|
|
print("yes")
|
2009-11-13 19:07:27 +01:00
|
|
|
else:
|
2018-06-10 17:36:11 +02:00
|
|
|
print("no")
|
2009-11-13 19:07:27 +01:00
|
|
|
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()))
|