This repository has been archived by the owner on Aug 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
XSConsoleMetrics.py
154 lines (126 loc) · 5.58 KB
/
XSConsoleMetrics.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
# Copyright (c) 2008-2009 Citrix Systems Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import XenAPI
import urllib
import xml.dom.minidom
from XSConsoleAuth import *
from XSConsoleBases import *
from XSConsoleLang import *
class HotMetrics:
LIFETIME_SECS = 5 # The lifetime of objects in the cache before they are refetched
SNAPSHOT_SECS = 10 # The number of seconds of metric data to fetch
__instance = None
@classmethod
def Inst(cls):
if cls.__instance is None:
cls.__instance = HotMetrics()
return cls.__instance
def __init__(self):
self.data = {}
self.timestamp = None
self.thisHostUUID = None
def LocalHostMetrics(self):
self.UpdateMetrics()
retVal = {}
hostPrefix = r'AVERAGE:host:'+self.thisHostUUID
cpuRE = re.compile(hostPrefix+r':cpu[0-9]+')
cpuValues = [ float(v) for k, v in self.data.iteritems() if cpuRE.match(k) ]
retVal['numcpus'] = len(cpuValues)
if len(cpuValues) == 0:
retVal['cpuusage'] = None
else:
retVal['cpuusage'] = sum(cpuValues) / len(cpuValues)
try:
retVal['memory_total'] = float(self.data[hostPrefix +':memory_total_kib']) * 1024.0
except Exception, e:
retVal['memory_total'] = None
try:
retVal['memory_free'] = float(self.data[hostPrefix +':memory_free_kib']) * 1024.0
except Exception, e:
retVal['memory_free'] = None
return retVal
def VMMetrics(self, inUUID):
self.UpdateMetrics()
retVal = {}
vmPrefix = r'AVERAGE:vm:' + inUUID
cpuRE = re.compile(vmPrefix+r':cpu[0-9]+')
cpuValues = [ float(v) for k, v in self.data.iteritems() if cpuRE.match(k) ]
retVal['numcpus'] = len(cpuValues)
if len(cpuValues) == 0:
retVal['cpuusage'] = None
else:
retVal['cpuusage'] = sum(cpuValues) / len(cpuValues)
try:
retVal['memory_total'] = float(self.data[vmPrefix +':memory']) # Not scaled
except Exception, e:
retVal['memory_total'] = None
try:
retVal['memory_free'] = float(self.data[vmPrefix +':memory_internal_free']) * 1024.0 # Value is in kiB
except Exception, e:
retVal['memory_free'] = None
return retVal
def UpdateMetrics(self):
timeNow = time.time()
if self.timestamp is None or abs(timeNow - self.timestamp) > self.LIFETIME_SECS:
# Refetch host metrics
self.data = self.FetchData()
self.timestamp = timeNow
def ParseXML(self, inXML):
xmlDoc = xml.dom.minidom.parseString(inXML)
metaNode = xmlDoc.getElementsByTagName('meta')[0]
valuesNode = xmlDoc.getElementsByTagName('data')[0]
meta = Struct()
# Values comments out below are currently not required
# for name in ('start', 'end', 'rows', 'columns'):
# setattr(meta, name, int(metaNode.getElementsByTagName(name)[0].firstChild.nodeValue.strip()))
legendNode = metaNode.getElementsByTagName('legend')[0]
meta.entries = [ str(entry.firstChild.nodeValue.strip()) for entry in legendNode.getElementsByTagName('entry') ]
# Find the most recent row in the values
mostRecentRow = None
mostRecentTime = None
for row in valuesNode.getElementsByTagName('row'):
rowTime = int(row.getElementsByTagName('t')[0].firstChild.nodeValue.strip())
if mostRecentTime is None or mostRecentTime < rowTime:
mostRecentRow = row
mostRecentTime = rowTime
# Decode the row contents
if mostRecentRow is None:
values = []
else:
values = [ str(v.firstChild.nodeValue.strip()) for v in mostRecentRow.getElementsByTagName('v') ]
retVal = {}
for entry, value in zip(meta.entries, values):
retVal[entry] = value
return retVal
def FetchData(self):
retVal = None
session = Auth.Inst().OpenSession()
try:
sessionID = session._session
if self.thisHostUUID is None:
# Make use of this session to get the local host UUID
opaqueRef = session.xenapi.session.get_this_host(sessionID)
self.thisHostUUID = session.xenapi.host.get_uuid(opaqueRef)
httpRequest = 'https://localhost/rrd_updates?session_id=%s&start=%s&host=true' % (sessionID, int(time.time()) - self.SNAPSHOT_SECS)
socket = urllib.URLopener().open(httpRequest)
try:
content = socket.read()
finally:
socket.close()
retVal = self.ParseXML(content)
finally:
if session is not None:
Auth.Inst().CloseSession(session)
return retVal