mirror of
https://github.com/munin-monitoring/contrib.git
synced 2025-07-21 18:41:03 +00:00
kvm_net: unify quoting
This commit is contained in:
parent
e87d3a6b3c
commit
7f98e21f2b
1 changed files with 22 additions and 17 deletions
|
@ -19,9 +19,10 @@ import re, os, sys
|
||||||
from subprocess import Popen, PIPE
|
from subprocess import Popen, PIPE
|
||||||
|
|
||||||
def config(vm_names):
|
def config(vm_names):
|
||||||
''' Print the plugin's config
|
""" Print the plugin's config
|
||||||
|
|
||||||
@param vm_names : a list of "cleaned" vms' name
|
@param vm_names : a list of "cleaned" vms' name
|
||||||
'''
|
"""
|
||||||
base_config = """graph_title KVM Network I/O
|
base_config = """graph_title KVM Network I/O
|
||||||
graph_vlabel Bytes rx(-)/tx(+) per second
|
graph_vlabel Bytes rx(-)/tx(+) per second
|
||||||
graph_category Virtualization
|
graph_category Virtualization
|
||||||
|
@ -41,21 +42,23 @@ graph_args --base 1024
|
||||||
print "%s_out.draw LINE2" % vm
|
print "%s_out.draw LINE2" % vm
|
||||||
|
|
||||||
def clean_vm_name(vm_name):
|
def clean_vm_name(vm_name):
|
||||||
''' Replace all special chars
|
""" Replace all special chars
|
||||||
|
|
||||||
@param vm_name : a vm's name
|
@param vm_name : a vm's name
|
||||||
@return cleaned vm's name
|
@return cleaned vm's name
|
||||||
'''
|
"""
|
||||||
# suffix part defined in conf
|
# suffix part defined in conf
|
||||||
suffix = os.getenv('vmsuffix')
|
suffix = os.getenv("vmsuffix")
|
||||||
if suffix:
|
if suffix:
|
||||||
vm_name = re.sub(suffix,'',vm_name)
|
vm_name = re.sub(suffix, "", vm_name)
|
||||||
|
|
||||||
return re.sub(r"[^a-zA-Z0-9_]", "_", vm_name)
|
return re.sub(r"[^a-zA-Z0-9_]", "_", vm_name)
|
||||||
|
|
||||||
def fetch(vms):
|
def fetch(vms):
|
||||||
''' Fetch values for a list of pids
|
""" Fetch values for a list of pids
|
||||||
|
|
||||||
@param dictionnary {kvm_pid: cleaned vm name}
|
@param dictionnary {kvm_pid: cleaned vm name}
|
||||||
'''
|
"""
|
||||||
res = {}
|
res = {}
|
||||||
for pid in vms:
|
for pid in vms:
|
||||||
tap = get_vm_mac(pid)
|
tap = get_vm_mac(pid)
|
||||||
|
@ -71,16 +74,16 @@ def fetch(vms):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
def detect_kvm():
|
def detect_kvm():
|
||||||
''' Check if kvm is installed
|
""" Check if kvm is installed """
|
||||||
'''
|
|
||||||
kvm = Popen("which kvm", shell=True, stdout=PIPE)
|
kvm = Popen("which kvm", shell=True, stdout=PIPE)
|
||||||
kvm.communicate()
|
kvm.communicate()
|
||||||
return not bool(kvm.returncode)
|
return not bool(kvm.returncode)
|
||||||
|
|
||||||
def find_vm_names(pids):
|
def find_vm_names(pids):
|
||||||
'''Find and clean vm names from pids
|
"""Find and clean vm names from pids
|
||||||
|
|
||||||
@return a dictionnary of {pids : cleaned vm name}
|
@return a dictionnary of {pids : cleaned vm name}
|
||||||
'''
|
"""
|
||||||
result = {}
|
result = {}
|
||||||
for pid in pids:
|
for pid in pids:
|
||||||
cmdline = open("/proc/%s/cmdline" % pid, "r")
|
cmdline = open("/proc/%s/cmdline" % pid, "r")
|
||||||
|
@ -88,25 +91,27 @@ def find_vm_names(pids):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def get_vm_mac(pid):
|
def get_vm_mac(pid):
|
||||||
'''Find and clean vm names from pids
|
"""Find and clean vm names from pids
|
||||||
|
|
||||||
@return the mac address for a specified pid
|
@return the mac address for a specified pid
|
||||||
'''
|
"""
|
||||||
cmdline = open("/proc/%s/cmdline" % pid, "r")
|
cmdline = open("/proc/%s/cmdline" % pid, "r")
|
||||||
line = cmdline.readline()
|
line = cmdline.readline()
|
||||||
mac = re.sub(r"^.*ifname=(tap[^,]+),.*$",r"\1", line)
|
mac = re.sub(r"^.*ifname=(tap[^,]+),.*$",r"\1", line)
|
||||||
return mac
|
return mac
|
||||||
|
|
||||||
def list_pids():
|
def list_pids():
|
||||||
''' Find the pid of kvm processes
|
""" Find the pid of kvm processes
|
||||||
|
|
||||||
@return a list of pids from running kvm
|
@return a list of pids from running kvm
|
||||||
'''
|
"""
|
||||||
pid = Popen("pidof qemu-kvm qemu-system-x86_64 kvm", shell=True, stdout=PIPE)
|
pid = Popen("pidof qemu-kvm qemu-system-x86_64 kvm", shell=True, stdout=PIPE)
|
||||||
return pid.communicate()[0].split()
|
return pid.communicate()[0].split()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
if sys.argv[1] in ['autoconf', 'detect']:
|
if sys.argv[1] in ["autoconf", "detect"]:
|
||||||
if detect_kvm():
|
if detect_kvm():
|
||||||
print "yes"
|
print "yes"
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue