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

127 lines
3.3 KiB
Plaintext
Raw Normal View History

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
#
# 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
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
for vm in vm_names:
print "%s_in.label %s" % (vm, vm)
2010-08-17 16:52:36 +02:00
print "%s_in.type COUNTER" % vm
2009-11-13 19:07:27 +01:00
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)
2010-08-17 16:52:36 +02:00
print "%s_out.type COUNTER" % vm
2009-11-13 19:07:27 +01:00
print "%s_out.min 0" % vm
print "%s_out.draw LINE2" % vm
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)
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:
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:
2017-09-30 17:44:16 +02:00
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
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)
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")
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
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()
mac = re.sub(r"^.*ifname=(tap[^,]+),.*$",r"\1", line)
2009-11-13 19:07:27 +01:00
return mac
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()
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():
print "yes"
else:
print "no"
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()))