-
Notifications
You must be signed in to change notification settings - Fork 12
/
libvirt_interface.py
executable file
·266 lines (234 loc) · 9.2 KB
/
libvirt_interface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""
*******************
*Copyright 2017, MapleLabs, All Rights Reserved.
*
********************
"""
#!/usr/bin/python
#
# Collectd libvirt interface / network python plugin
#
#
import datetime
import re
import time
from copy import deepcopy
from xml.etree import ElementTree
from subprocess import (PIPE, Popen)
import collectd
from utils import *
import libvirt
from libvirt_utils import *
from libvirt_constants import *
PLUGIN_NAME = "libvirt_interface"
class LibvirtInterface:
"""
LibvirtInterface collects libvirt related interface data from libvirt
"""
def __init__(self, interval=5):
"""
:param:interval: integer collection interval
"""
self.interval = interval
url = LIBVIRT_URL
self.error = None
self.prev_iface_data = {}
self.prev_iface_agg = {}
self.conn = None
self.url = LIBVIRT_URL
self.first_time = True
def read_config(self, cfg):
"""
read_config method fetch configuration parameter from
collectd.conf
:param cfg: Configuration object from collectd.conf
:return: None
"""
for child in cfg.children:
if child.key == 'interval':
self.interval = int(child.values[0])
collectd.info("Collector interval : %d sec" % self.interval)
def establish_connection(self):
try:
self.conn, self.error = establish_connection(self.url)
except libvirt.libvirtError as e:
collectd.error('Failed to open connection %s, reason: %s' % (self.url, e.get_error_message()))
self.error= FAILURE
def read(self):
"""
read() method collects the data from libvirt and dispatch it
"""
self.establish_connection()
if self.error == FAILURE:
collectd.error("Empty stats dispatch, failed to open connection.")
return
for domain in self.conn.listAllDomains(0):
if not domain.isActive():
collectd.warning("Failed to collectd interface "
"stats for VM %s, VM is not running!" % domain.name())
continue
tree = ElementTree.fromstring(domain.XMLDesc())
interfaces = [i.get("dev")
for i in tree.findall('devices/interface/target')]
total_rx_pkts = 0
total_tx_pkts = 0
total_rx_drops = 0
total_tx_drops = 0
total_rx_bytes = 0
total_tx_bytes = 0
for iface in interfaces:
if iface:
collectd.info(
"Collecting stats for '%s' interface of VM: %s" %
(iface, domain.name()))
nic_data = self.collectd_nic_stats(domain, iface)
self.prev_iface_data[nic_data[IFACE_NAME]] = deepcopy(
nic_data)
dispatch(nic_data)
collectd.info("Data for interface: %s of VM: %s is "
"dispatched" % (iface, domain.name()))
total_rx_pkts = total_rx_pkts + nic_data[RX_PKTS]
total_tx_pkts = total_tx_pkts + nic_data[TX_PKTS]
total_rx_drops = total_rx_drops + nic_data[RX_DROPS]
total_tx_drops = total_tx_drops + nic_data[TX_DROPS]
total_rx_bytes = total_rx_bytes + nic_data[RX_BYTES]
total_tx_bytes = total_tx_bytes + nic_data[TX_BYTES]
interface = {}
interface[TYPE] = AGGREGATE
interface[RX_PKTS] = total_rx_pkts
interface[TX_PKTS] = total_tx_pkts
interface[RX_DROPS] = total_rx_drops
interface[TX_DROPS] = total_tx_drops
interface[RX_BYTES] = total_rx_bytes
interface[TX_BYTES] = total_tx_bytes
self.add_aggregate(domain, interface)
self.prev_iface_agg[domain.name()] = interface
dispatch(interface)
if not self.error:
self.conn.close()
def add_aggregate(self, domain, iface):
iface[PLUGINTYPE] = IFACE_PLUGIN
iface[PLUGIN] = LIBVIRT
iface[UTC] = str(datetime.datetime.utcnow())
iface[INTERVAL] = self.interval
iface[PLUGIN_INS] = str(domain.name() + "-iface-aggregate")
iface[VMNAME] = domain.name()
state, reason = domain.state()
iface[VM_STATE] = get_vm_state(state)
iface[UTC] = str(datetime.datetime.utcnow())
iface[TIMESTAMP] = int(round(time.time() * 1000))
rx_rate = get_rate(
RX_BYTES, iface, self.prev_iface_agg.get(domain.name(), {}))
tx_rate = get_rate(
TX_BYTES, iface, self.prev_iface_agg.get(domain.name(), {}))
if rx_rate != NAN:
iface[AGG_RX_RATE] = round(float(rx_rate) / (1024 * 1024), 2)
if tx_rate != NAN:
iface[AGG_TX_RATE] = round(float(tx_rate) / (1024 * 1024), 2)
collectd.info(
"Collectd stats for nic: '%s' is collected of VM: %s" %
(iface, domain.name()))
def collectd_nic_stats(self, domain, iface):
"""
Collect interface stats
:param domain: Object represents a domain or VM
:param iface: String naming an interface
:return: Dictionary with nic stats
"""
data = {}
data[PLUGIN] = LIBVIRT
data[PLUGINTYPE] = IFACE_PLUGIN
data[UTC] = str(datetime.datetime.utcnow())
data[INTERVAL] = self.interval
data[PLUGIN_INS] = str(domain.name() + "-" + iface)
data[VMNAME] = domain.name()
state, reason = domain.state()
data[VM_STATE] = get_vm_state(state)
data[IFACE_NAME] = iface
try:
stats = domain.interfaceStats(iface)
data[RX_BYTES] = int(stats[0])
data[RX_PKTS] = int(stats[1])
data[RX_ERR] = int(stats[2])
data[RX_DROPS] = int(stats[3])
data[TX_BYTES] = int(stats[4])
data[TX_PKTS] = int(stats[5])
data[TX_ERR] = int(stats[6])
data[TX_DROPS] = int(stats[7])
data[UTC] = str(datetime.datetime.utcnow())
data[TIMESTAMP] = int(round(time.time() * 1000))
rx_rate = get_rate(
RX_BYTES, data, self.prev_iface_data.get(
data[IFACE_NAME], {}))
tx_rate = get_rate(
TX_BYTES, data, self.prev_iface_data.get(
data[IFACE_NAME], {}))
if rx_rate != NAN:
data[RX_RATE] = round(float(rx_rate) / (1024 * 1024), 2)
if tx_rate != NAN:
data[TX_RATE] = round(float(tx_rate) / (1024 * 1024), 2)
collectd.info(
"Collectd stats for nic: '%s' is collected of VM: %s" %
(iface, domain.name()))
except libvirt.libvirtError as e:
collectd.info(
"Nic stats ignored for VM: %s, reason: %s" %
(domain.name(), e.get_error_message()))
tag, trunks = self.get_vlan_details(iface)
data[VLAN_TAG] = tag
data[VLAN_TRUNKS] = trunks
return data
def execute_cli(self, cli):
try:
p = Popen(cli, stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = p.communicate()
except Exception as e:
return None, e.message
return output, err
def get_vlan_details(self, iface):
"""
Collect vlan details from ovs-vsctl show output
for the specified interface
:param iface: String , contain interface name
:return: Two String value, tag and trunk details
"""
cli = ["ovs-vsctl", "show"]
tag = None
trunks = None
output, err = self.execute_cli(cli)
if output is None:
collectd.warning("Unable to get VLAN details %s." % (err))
return tag, trunks
entries = re.split(r"Port", output)
for entry in entries:
if iface in entry:
try:
tag = re.findall(r'tag:.*', entry)[0][5:]
except IndexError:
pass
try:
trunks = re.findall(r'trunks:.*', entry)[0][8:].strip("[]")
except IndexError:
pass
return tag, trunks
# Ignoring first three characters of the interface name
# to make it work with OVS, if the it is an OpenStack setup
elif iface[3:] in entry:
try:
tag = re.findall(r'tag:.*', entry)[0][5:]
except IndexError:
pass
try:
trunks = re.findall(r'trunks:.*', entry)[0][8:].strip("[]")
except IndexError:
pass
return tag, trunks
return tag, trunks
def read_temp(self):
collectd.unregister_read(self.read_temp)
collectd.register_read(self.read, interval=int(self.interval))
collectd.info("Registering '%s' ... " % PLUGIN_NAME)
virt = LibvirtInterface()
collectd.register_config(virt.read_config)
collectd.register_read(virt.read_temp)
collectd.info("Registered '%s' plugin successfully :)" % PLUGIN_NAME)