Révision 7f98e21f
kvm_net: unify quoting
| plugins/libvirt/kvm_net | ||
|---|---|---|
| 19 | 19 |
from subprocess import Popen, PIPE |
| 20 | 20 |
|
| 21 | 21 |
def config(vm_names): |
| 22 |
''' Print the plugin's config |
|
| 22 |
""" Print the plugin's config |
|
| 23 |
|
|
| 23 | 24 |
@param vm_names : a list of "cleaned" vms' name |
| 24 |
'''
|
|
| 25 |
"""
|
|
| 25 | 26 |
base_config = """graph_title KVM Network I/O |
| 26 | 27 |
graph_vlabel Bytes rx(-)/tx(+) per second |
| 27 | 28 |
graph_category Virtualization |
| ... | ... | |
| 41 | 42 |
print "%s_out.draw LINE2" % vm |
| 42 | 43 |
|
| 43 | 44 |
def clean_vm_name(vm_name): |
| 44 |
''' Replace all special chars |
|
| 45 |
""" Replace all special chars |
|
| 46 |
|
|
| 45 | 47 |
@param vm_name : a vm's name |
| 46 | 48 |
@return cleaned vm's name |
| 47 |
'''
|
|
| 49 |
"""
|
|
| 48 | 50 |
# suffix part defined in conf |
| 49 |
suffix = os.getenv('vmsuffix')
|
|
| 51 |
suffix = os.getenv("vmsuffix")
|
|
| 50 | 52 |
if suffix: |
| 51 |
vm_name = re.sub(suffix,'',vm_name)
|
|
| 53 |
vm_name = re.sub(suffix, "", vm_name)
|
|
| 52 | 54 |
|
| 53 | 55 |
return re.sub(r"[^a-zA-Z0-9_]", "_", vm_name) |
| 54 | 56 |
|
| 55 | 57 |
def fetch(vms): |
| 56 |
''' Fetch values for a list of pids |
|
| 58 |
""" Fetch values for a list of pids |
|
| 59 |
|
|
| 57 | 60 |
@param dictionnary {kvm_pid: cleaned vm name}
|
| 58 |
'''
|
|
| 61 |
"""
|
|
| 59 | 62 |
res = {}
|
| 60 | 63 |
for pid in vms: |
| 61 | 64 |
tap = get_vm_mac(pid) |
| ... | ... | |
| 71 | 74 |
continue |
| 72 | 75 |
|
| 73 | 76 |
def detect_kvm(): |
| 74 |
''' Check if kvm is installed |
|
| 75 |
''' |
|
| 77 |
""" Check if kvm is installed """ |
|
| 76 | 78 |
kvm = Popen("which kvm", shell=True, stdout=PIPE)
|
| 77 | 79 |
kvm.communicate() |
| 78 | 80 |
return not bool(kvm.returncode) |
| 79 | 81 |
|
| 80 | 82 |
def find_vm_names(pids): |
| 81 |
'''Find and clean vm names from pids |
|
| 83 |
"""Find and clean vm names from pids |
|
| 84 |
|
|
| 82 | 85 |
@return a dictionnary of {pids : cleaned vm name}
|
| 83 |
'''
|
|
| 86 |
"""
|
|
| 84 | 87 |
result = {}
|
| 85 | 88 |
for pid in pids: |
| 86 | 89 |
cmdline = open("/proc/%s/cmdline" % pid, "r")
|
| ... | ... | |
| 88 | 91 |
return result |
| 89 | 92 |
|
| 90 | 93 |
def get_vm_mac(pid): |
| 91 |
'''Find and clean vm names from pids |
|
| 94 |
"""Find and clean vm names from pids |
|
| 95 |
|
|
| 92 | 96 |
@return the mac address for a specified pid |
| 93 |
'''
|
|
| 97 |
"""
|
|
| 94 | 98 |
cmdline = open("/proc/%s/cmdline" % pid, "r")
|
| 95 | 99 |
line = cmdline.readline() |
| 96 | 100 |
mac = re.sub(r"^.*ifname=(tap[^,]+),.*$",r"\1", line) |
| 97 | 101 |
return mac |
| 98 | 102 |
|
| 99 | 103 |
def list_pids(): |
| 100 |
''' Find the pid of kvm processes |
|
| 104 |
""" Find the pid of kvm processes |
|
| 105 |
|
|
| 101 | 106 |
@return a list of pids from running kvm |
| 102 |
'''
|
|
| 107 |
"""
|
|
| 103 | 108 |
pid = Popen("pidof qemu-kvm qemu-system-x86_64 kvm", shell=True, stdout=PIPE)
|
| 104 | 109 |
return pid.communicate()[0].split() |
| 105 | 110 |
|
| 106 | 111 |
|
| 107 | 112 |
if __name__ == "__main__": |
| 108 | 113 |
if len(sys.argv) > 1: |
| 109 |
if sys.argv[1] in ['autoconf', 'detect']:
|
|
| 114 |
if sys.argv[1] in ["autoconf", "detect"]:
|
|
| 110 | 115 |
if detect_kvm(): |
| 111 | 116 |
print "yes" |
| 112 | 117 |
else: |
Formats disponibles : Unified diff