mirror of
https://github.com/munin-monitoring/contrib.git
synced 2018-11-08 00:59:34 +01:00
Merge pull request #83 from iborodikhin/master
New plugin for KVM network stats.
This commit is contained in:
commit
3535ad102c
@ -4,12 +4,10 @@
|
||||
#
|
||||
# Munin plugin to show the network I/O per vm
|
||||
#
|
||||
# Copyright Maxence Dunnewind, Rodolphe Quiédeville
|
||||
# Copyright Igor Borodikhin
|
||||
#
|
||||
# License : GPLv3
|
||||
#
|
||||
# need to be run with root privilege to execute brctl
|
||||
#
|
||||
#
|
||||
# parsed environment variables:
|
||||
# vmsuffix: part of vm name to be removed
|
||||
@ -58,22 +56,18 @@ def fetch(vms):
|
||||
''' Fetch values for a list of pids
|
||||
@param dictionnary {kvm_pid: cleaned vm name}
|
||||
'''
|
||||
macs = find_vms_tap()
|
||||
res = {}
|
||||
for pid in vms:
|
||||
mac = get_vm_mac(pid)
|
||||
tap = get_vm_mac(pid)
|
||||
try:
|
||||
tap = "tap%s" % macs[mac]
|
||||
f = open("/proc/net/dev", "r")
|
||||
for line in f.readlines():
|
||||
if tap in line:
|
||||
line = line.split(':')[1]
|
||||
print "%s_in.value %s" % (vms[pid], line.split()[0])
|
||||
print "%s_out.value %s" % (vms[pid], line.split()[8])
|
||||
break
|
||||
else:
|
||||
f.close()
|
||||
except:
|
||||
f = open("/proc/net/dev", "r")
|
||||
for line in f.readlines():
|
||||
if tap in line:
|
||||
print "%s_in.value %s" % (vms[pid], re.sub(r"%s:"%tap, "", line.split()[0]))
|
||||
print "%s_out.value %s" % (vms[pid], line.split()[8])
|
||||
break
|
||||
except Exception as inst:
|
||||
print inst
|
||||
continue
|
||||
|
||||
def detect_kvm():
|
||||
@ -98,7 +92,8 @@ def get_vm_mac(pid):
|
||||
@return the mac address for a specified pid
|
||||
'''
|
||||
cmdline = open("/proc/%s/cmdline" % pid, "r")
|
||||
mac = re.sub(r"^.*macaddr=(..:..:..:..:..:..).*$",r"\1", cmdline.readline())
|
||||
line = cmdline.readline()
|
||||
mac = re.sub(r"^.*ifname=(tap[^,]+),.*$",r"\1", line)
|
||||
return mac
|
||||
|
||||
def list_pids():
|
||||
@ -112,16 +107,21 @@ def find_vms_tap():
|
||||
''' Check if kvm is installed
|
||||
@return a list of pids from running kvm
|
||||
'''
|
||||
result = {}
|
||||
kvm = Popen("brctl showmacs br0 | grep no", shell=True, stdout=PIPE)
|
||||
result = []
|
||||
tap = ""
|
||||
mac = ""
|
||||
kvm = Popen("ip a | grep -A 1 tap | awk '{print $2}' | grep -v '^$'", shell=True, stdout=PIPE)
|
||||
res = kvm.communicate()[0].split('\n')
|
||||
for line in res:
|
||||
try:
|
||||
tap = str(int(line.split()[0]) - 1)
|
||||
mac = line.split()[1]
|
||||
result[mac] = tap
|
||||
except:
|
||||
if len(line) > 0:
|
||||
if re.match(r"^tap.*", line):
|
||||
tap = re.sub(r"(tap[^:]+):", r"\1", line)
|
||||
else:
|
||||
result.append(tap)
|
||||
except Exception as inst:
|
||||
continue
|
||||
|
||||
return result
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Loading…
Reference in New Issue
Block a user