-
Notifications
You must be signed in to change notification settings - Fork 12
/
libvirt_utils.py
executable file
·85 lines (66 loc) · 2.38 KB
/
libvirt_utils.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
"""
*******************
*Copyright 2017, MapleLabs, All Rights Reserved.
*
********************
"""
import libvirt
import collectd
from libvirt_constants import *
def get_vm_state(state):
"""
:param state:
:return: String, VM status
"""
if state == libvirt.VIR_DOMAIN_NOSTATE:
return "noState"
elif state == libvirt.VIR_DOMAIN_RUNNING:
return "Running"
elif state == libvirt.VIR_DOMAIN_BLOCKED:
return "Blocked"
elif state == libvirt.VIR_DOMAIN_PAUSED:
return "Paused"
elif state == libvirt.VIR_DOMAIN_SHUTDOWN:
return "Shutdown"
elif state == libvirt.VIR_DOMAIN_SHUTOFF:
return "Shutoff"
elif state == libvirt.VIR_DOMAIN_CRASHED:
return "Crashed"
elif state == libvirt.VIR_DOMAIN_PMSUSPENDED:
return "Suspended"
else:
return "Unknown"
def get_cpu_percentage(prev_data, cur_data):
cpu_util = NAN
if not prev_data:
return cpu_util
# IF any
if CPU_TIME not in prev_data or CPU_TIME not in cur_data:
collectd.error("%s not present in previous data" % (CPU_TIME))
return cpu_util
# If VM restarted then, the cpu time changes
if prev_data[CPU_TIME] > cur_data[CPU_TIME]:
collectd.error(
"Current CPU time is smaller than the previous CPU time.")
return cpu_util
if TIMESTAMP not in prev_data or TIMESTAMP not in cur_data:
collectd.error("Timestamp is not present in the collected stats")
return cpu_util
if cur_data[CPU_TIME] is not NAN and prev_data[CPU_TIME] is not NAN:
cpu_time_diff = cur_data[CPU_TIME] - prev_data[CPU_TIME]
nr_cores = int(cur_data[NO_OF_VCPU])
time_diff = cur_data[TIMESTAMP] - prev_data[TIMESTAMP]
cpu_util = 100.0 * cpu_time_diff / (time_diff * nr_cores * 1000000000)
return cpu_util
def establish_connection(url):
conn = libvirt.open(url)
if not conn:
collectd.error('Failed to open connection %s' % url)
return None, FAILURE
collectd.info(
"Plugin successfully created connection with hypervisor")
libvirt_version = conn.getLibVersion()
if libvirt_version >= LIBVIRT_VERSION:
lib_ver = ".".join([i for i in LIBVIRT_VERSION[:3]])
collectd.warning("Libvirtd version should be greater than or equal to " + lib_ver + ", things may not work as expected")
return conn, None