| 1 |
# xenstats.py |
| 2 |
# |
| 3 |
# Copyright 2011 Marcos Amorim <[email protected]> |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 2 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program; if not, write to the Free Software |
| 17 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 18 |
# MA 02110-1301, USA. |
| 19 |
|
| 20 |
import libvirt |
| 21 |
import os |
| 22 |
import time |
| 23 |
|
| 24 |
descriptors = list() |
| 25 |
conn = libvirt.openReadOnly("xen:///") |
| 26 |
conn_info = conn.getInfo() |
| 27 |
|
| 28 |
|
| 29 |
def xen_vms(name): |
| 30 |
'''Return number of virtual is running''' |
| 31 |
global conn |
| 32 |
|
| 33 |
vm_count = conn.numOfDomains() |
| 34 |
|
| 35 |
return vm_count |
| 36 |
|
| 37 |
|
| 38 |
def xen_mem(name): |
| 39 |
'''Return node memory ''' |
| 40 |
global conn |
| 41 |
global conn_info |
| 42 |
|
| 43 |
# O xen retorna o valor da memoria em MB, vamos passar por KB |
| 44 |
return conn_info[1] * 1024 |
| 45 |
|
| 46 |
|
| 47 |
def xen_cpu(name): |
| 48 |
'''Return numbers of CPU's''' |
| 49 |
global conn |
| 50 |
global conn_info |
| 51 |
|
| 52 |
return conn_info[2] |
| 53 |
|
| 54 |
|
| 55 |
def xen_mem_use(name): |
| 56 |
'''Return total memory usage''' |
| 57 |
global conn |
| 58 |
|
| 59 |
vm_mem = 0 |
| 60 |
|
| 61 |
for id in conn.listDomainsID(): |
| 62 |
dom = conn.lookupByID(id) |
| 63 |
info = dom.info() |
| 64 |
vm_mem = vm_mem + info[2] |
| 65 |
|
| 66 |
return vm_mem |
| 67 |
|
| 68 |
|
| 69 |
def metric_init(params): |
| 70 |
global descriptors |
| 71 |
|
| 72 |
d1 = {'name': 'xen_vms', |
| 73 |
'call_back': xen_vms, |
| 74 |
'time_max': 20, |
| 75 |
'value_type': 'uint', |
| 76 |
'units': 'Qtd', |
| 77 |
'slope': 'both', |
| 78 |
'format': '%d', |
| 79 |
'description': 'Total number of running vms', |
| 80 |
'groups': 'xen', |
| 81 |
} |
| 82 |
d2 = {'name': 'xen_cpu', |
| 83 |
'call_back': xen_cpu, |
| 84 |
'time_max': 20, |
| 85 |
'value_type': 'uint', |
| 86 |
'units': 'CPUs', |
| 87 |
'slope': 'both', |
| 88 |
'format': '%d', |
| 89 |
'description': 'CPUs', |
| 90 |
'groups': 'xen' |
| 91 |
} |
| 92 |
|
| 93 |
d3 = {'name': 'xen_mem', |
| 94 |
'call_back': xen_mem, |
| 95 |
'time_max': 20, |
| 96 |
'value_type': 'uint', |
| 97 |
'units': 'KB', |
| 98 |
'slope': 'both', |
| 99 |
'format': '%d', |
| 100 |
'description': 'Total memory Xen', |
| 101 |
'groups': 'xen' |
| 102 |
} |
| 103 |
|
| 104 |
d4 = {'name': 'xen_mem_use', |
| 105 |
'call_back': xen_mem_use, |
| 106 |
'time_max': 20, |
| 107 |
'value_type': 'uint', |
| 108 |
'units': 'KB', |
| 109 |
'slope': 'both', |
| 110 |
'format': '%d', |
| 111 |
'description': 'Memory Usage', |
| 112 |
'groups': 'xen' |
| 113 |
} |
| 114 |
|
| 115 |
descriptors = [d1, d2, d3, d4] |
| 116 |
|
| 117 |
return descriptors |
| 118 |
|
| 119 |
|
| 120 |
def metric_cleanup(): |
| 121 |
'''Clean up the metric module.''' |
| 122 |
global conn |
| 123 |
conn.close() |
| 124 |
pass |
| 125 |
|
| 126 |
#This code is for debugging and unit testing |
| 127 |
if __name__ == '__main__': |
| 128 |
metric_init('init') |
| 129 |
for d in descriptors: |
| 130 |
v = d['call_back'](d['name']) |
| 131 |
print 'value for %s is %u' % (d['name'], v) |