aboutsummaryrefslogtreecommitdiff
path: root/libvirt-ifstat
diff options
context:
space:
mode:
Diffstat (limited to 'libvirt-ifstat')
-rw-r--r--libvirt-ifstat64
1 files changed, 35 insertions, 29 deletions
diff --git a/libvirt-ifstat b/libvirt-ifstat
index 4fd2f93..1561c6a 100644
--- a/libvirt-ifstat
+++ b/libvirt-ifstat
@@ -1,8 +1,8 @@
-#!/usr/bin/python
+#!/usr/bin/python3
# vim: set fileencoding=utf-8 :
#
# Munin plugin to show the network traffic of libvirt managed virtual machines
-#
+#
# Copyright 2008 Guido Guenther <agx@sigxcpu.org>
#
# License GPLv2
@@ -12,20 +12,23 @@
#%# capabilities=autoconf
#%# family=contrib
+from __future__ import print_function
import re, sys, os
import libvirt
import libxml2
+
def canon(name):
return re.sub(r"[^a-zA-Z0-9_]", "_", name)
+
def print_config(uri):
"""print the plugin config, determine the domains"""
- print """graph_title Virtual Domain Network I/O
+ print("""graph_title Virtual Domain Network I/O
graph_vlabel Bytes rx (-)/ tx (+) per ${graph_period}
graph_category Virtual Machines
-graph_info This graph shows the network I/O of the virtual machines"""
+graph_info This graph shows the network I/O of the virtual machines""")
conn = libvirt.openReadOnly(uri)
ids = conn.listDomainsID()
@@ -33,28 +36,29 @@ graph_info This graph shows the network I/O of the virtual machines"""
try:
dom = conn.lookupByID(id)
name = dom.name()
- except libvirt.libvirtError, err:
- print >>sys.stderr, "Id: %s: %s" % (id, err)
+ except libvirt.libvirtError as err:
+ print("Id: %s: %s" % (id, err), file=sys.stderr)
continue
if name == "Domain-0":
continue
- print "%s_rx.label %s" % (canon(name), name)
- print "%s_rx.type DERIVE" % canon(name)
- print "%s_rx.min 0" % canon(name)
- print "%s_rx.graph no" % canon(name)
- print "%s_rx.draw LINE1" % canon(name)
- print "%s_tx.label %s" % (canon(name), name)
- print "%s_tx.type DERIVE" % canon(name)
- print "%s_tx.min 0" % canon(name)
- print "%s_tx.negative %s_rx" % (canon(name), canon(name))
- print "%s_tx.draw LINE1" % canon(name)
+ print("%s_rx.label %s" % (canon(name), name))
+ print("%s_rx.type DERIVE" % canon(name))
+ print("%s_rx.min 0" % canon(name))
+ print("%s_rx.graph no" % canon(name))
+ print("%s_rx.draw LINE1" % canon(name))
+ print("%s_tx.label %s" % (canon(name), name))
+ print("%s_tx.type DERIVE" % canon(name))
+ print("%s_tx.min 0" % canon(name))
+ print("%s_tx.negative %s_rx" % (canon(name), canon(name)))
+ print("%s_tx.draw LINE1" % canon(name))
+
def get_ifaces(dom):
xml = dom.XMLDesc(0)
doc = None
try:
doc = libxml2.parseDoc(xml)
- except:
+ except Exception:
return []
ctx = doc.xpathNewContext()
ifaces = []
@@ -65,16 +69,17 @@ def get_ifaces(dom):
for child in node.children:
if child.name == "target":
devdst = child.prop("dev")
- if devdst == None:
+ if devdst is None:
continue
ifaces.append(devdst)
finally:
- if ctx != None:
+ if ctx is not None:
ctx.xpathFreeContext()
- if doc != None:
+ if doc is not None:
doc.freeDoc()
return ifaces
-
+
+
def fetch_values(uri):
conn = libvirt.openReadOnly(uri)
ids = conn.listDomainsID()
@@ -84,8 +89,8 @@ def fetch_values(uri):
try:
dom = conn.lookupByID(id)
name = dom.name()
- except libvirt.libvirtError, err:
- print >>sys.stderr, "Id: %s: %s" % (id, err)
+ except libvirt.libvirtError as err:
+ print("Id: %s: %s" % (id, err), file=sys.stderr)
continue
if name == "Domain-0":
continue
@@ -96,21 +101,21 @@ def fetch_values(uri):
rd += stats[0]
wr += stats[4]
except TypeError:
- print >>sys.stderr, "Cannot get ifstats for '%s' on '%s'" % (iface, name)
- print "%s_rx.value %d" % (canon(name), rd)
- print "%s_tx.value %d" % (canon(name), wr)
+ print("Cannot get ifstats for '%s' on '%s'" % (iface, name), file=sys.stderr)
+ print("%s_rx.value %d" % (canon(name), rd))
+ print("%s_tx.value %d" % (canon(name), wr))
def main(sys):
uri = os.getenv("uri", "qemu:///system")
if len(sys) > 1:
- if sys[1] in [ 'autoconf', 'detect' ]:
+ if sys[1] in ['autoconf', 'detect']:
if libvirt.openReadOnly(uri):
- print "yes"
+ print("yes")
return 0
else:
- print "no"
+ print("no")
return 1
elif sys[1] == 'config':
print_config(uri)
@@ -118,6 +123,7 @@ def main(sys):
fetch_values(uri)
return 0
+
if __name__ == "__main__":
sys.exit(main(sys.argv))