-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloudstack.py
253 lines (206 loc) · 8.46 KB
/
cloudstack.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
# collectd-cloudstack - cloudstack.py
#
# Author : Antoine Coetsier @ exoscale
# Description : This is a collectd python module to gather stats from cloudstack
# inspired by collectd-haproxy from Michael Leinartas - https://github.com/mleinart/collectd-haproxy
import collectd
import urllib2
import urllib
import json
import hmac
import base64
import hashlib
import re
class BaseClient(object):
def __init__(self, api, apikey, secret):
self.api = api
self.apikey = apikey
self.secret = secret
def request(self, command, args):
args['apikey'] = self.apikey
args['command'] = command
args['response'] = 'json'
params=[]
keys = sorted(args.keys())
for k in keys:
params.append(k + '=' + urllib.quote_plus(args[k]).replace("+", "%20"))
query = '&'.join(params)
signature = base64.b64encode(hmac.new(
self.secret,
msg=query.lower(),
digestmod=hashlib.sha1
).digest())
query += '&signature=' + urllib.quote_plus(signature)
response = urllib2.urlopen(self.api + '?' + query)
decoded = json.loads(response.read())
propertyResponse = command.lower() + 'response'
if not propertyResponse in decoded:
if 'errorresponse' in decoded:
raise RuntimeError("ERROR: " + decoded['errorresponse']['errortext'])
else:
raise RuntimeError("ERROR: Unable to parse the response")
response = decoded[propertyResponse]
result = re.compile(r"^list(\w+)s").match(command.lower())
if not result is None:
type = result.group(1)
if type in response:
return response[type]
else:
# sometimes, the 's' is kept, as in :
# { "listasyncjobsresponse" : { "asyncjobs" : [ ... ] } }
type += 's'
if type in response:
return response[type]
return response
class Client(BaseClient):
def listHosts(self, args={}):
return self.request('listHosts', args)
def listCapabilities(self, args={}):
return self.request('listCapabilities', args)
def listCapacity(self, args={}):
return self.request('listCapacity', args)
def listSystemVms(self, args={}):
return self.request('listSystemVms', args)
def listZones(self, args={}):
return self.request('listZones', args)
NAME = 'cloudstack'
DEFAULT_API = 'http://localhost:8096/client/api'
DEFAULT_AUTH = False
DEFAULT_APIKEY = ''
DEFAULT_SECRET = ''
VERBOSE_LOGGING = False
METRIC_TYPES = {
'memoryused': ('h_memory_used', 'memory'),
'memorytotal': ('h_memory_total', 'memory'),
'memoryallocated': ('h_memory_allocated', 'memory'),
'activeviewersessions': ('console_active_sessions', 'current'),
'hostscount': ('hosts_count', 'current'),
'zonescount': ('zones_count', 'current'),
'zonepublicipallocated': ('z_public_ip_allocated', 'current'),
'zonepubliciptotal': ('z_public_ip_total', 'current'),
'disksizeallocated': ('h_disk_allocated', 'bytes'),
'disksizetotal': ('h_disk_total', 'bytes')
}
METRIC_DELIM = '.'
hypervisors = []
def get_stats():
stats = dict()
logger('verb', "get_stats calls API %s KEY %s SECRET %s" % (API_MONITORS, APIKEY_MONITORS, SECRET_MONITORS))
cloudstack = Client(API_MONITORS, APIKEY_MONITORS, SECRET_MONITORS)
try:
hypervisors = cloudstack.listHosts({
'type': 'Routing'
})
except:
logger('warn', "status err Unable to connect to CloudStack URL at %s for Hosts" % API_MONITORS)
for h in hypervisors:
metricnameMemUsed = METRIC_DELIM.join([ h['name'].lower(), h['podname'].lower(), re.sub(r"\s+", '-', h['zonename'].lower()), 'memoryused' ])
metricnameMemTotal = METRIC_DELIM.join([ h['name'].lower(), h['podname'].lower(), re.sub(r"\s+", '-', h['zonename'].lower()), 'memorytotal' ])
metricnameMemAlloc = METRIC_DELIM.join([ h['name'].lower(), h['podname'].lower(), re.sub(r"\s+", '-', h['zonename'].lower()), 'memoryallocated' ])
metricnameDiskAlloc = METRIC_DELIM.join([ h['name'].lower(), h['podname'].lower(), re.sub(r"\s+", '-', h['zonename'].lower()), 'disksizeallocated' ])
metricnameDiskTotal = METRIC_DELIM.join([ h['name'].lower(), h['podname'].lower(), re.sub(r"\s+", '-', h['zonename'].lower()), 'disksizetotal' ])
try:
stats[metricnameMemUsed] = h['memoryused']
stats[metricnameMemTotal] = h['memorytotal']
stats[metricnameMemAlloc] = h['memoryallocated']
if h['islocalstorageactive'] == "True":
stats[metricnameDiskAlloc] = h['disksizeallocated']
stats[metricnameDiskTotal] = h['disksizetotal']
logger('verb', "readings : %s memory used %s " % (h['name'], h['memoryused']))
except (TypeError, ValueError), e:
pass
# collect number of active console sessions
try:
systemvms = cloudstack.listSystemVms({
'systemvmtype': 'consoleproxy'
})
except:
logger('warn', "status err Unable to connect to CloudStack URL at %s for SystemVms" % API_MONITORS)
for systemvm in systemvms:
metricnameSessions = METRIC_DELIM.join([ 'activeviewersessions', systemvm['zonename'].lower(), systemvm['name'].lower(), 'activeviewersessions' ])
if 'activeviewersessions' in systemvm:
stats[metricnameSessions] = systemvm['activeviewersessions']
# collect number of zones and available public ips
try:
zones = cloudstack.listZones({
'showcapacities': 'true'
})
except:
logger('warn', "status err Unable to connect to CloudStack URL at %s for SystemVms" % API_MONITORS)
for zone in zones:
metricnameIpAllocated = METRIC_DELIM.join([ 'zonepublicipallocated', zone['name'].lower(), 'zonepublicipallocated' ])
metricnameIpTotal = METRIC_DELIM.join([ 'zonepubliciptotal', zone['name'].lower(), 'zonepubliciptotal' ])
for capacity in zone['capacity']:
if capacity['type'] == '4':
stats[metricnameIpTotal] = capacity['capacitytotal']
stats[metricnameIpAllocated] = capacity['capacityused']
metricnameZonesCount = METRIC_DELIM.join([ 'zonescount', 'count' ])
stats[metricnameZonesCount] = len(zones)
try:
capacities = cloudstack.listCapacity({
})
except:
logger('warn', "status err Unable to connect to CloudStack URL at %s for Capacity" % API_MONITORS)
return stats
# callback configuration for module
def configure_callback(conf):
global API_MONITORS, APIKEY_MONITORS, SECRET_MONITORS, AUTH_MONITORS, VERBOSE_LOGGING
API_MONITORS = ''
APIKEY_MONITORS = ''
SECRET_MONITORS = ''
AUTH_MONITORS = DEFAULT_AUTH
VERBOSE_LOGGING = False
for node in conf.children:
if node.key == "Api":
API_MONITORS = node.values[0]
elif node.key == "ApiKey":
APIKEY_MONITORS = node.values[0]
elif node.key == "Secret":
SECRET_MONITORS = node.values[0]
elif node.key == "Auth":
AUTH_MONITORS = node.values[0]
elif node.key == "Verbose":
VERBOSE_LOGGING = bool(node.values[0])
else:
logger('warn', 'Unknown config key: %s' % node.key)
if not API_MONITORS:
API_MONITORS += DEFAULT_API
def read_callback():
logger('verb', "beginning read_callback")
info = get_stats()
if not info:
logger('warn', "%s: No data received" % NAME)
return
for key,value in info.items():
key_prefix = ''
key_root = key
logger('verb', "read_callback key %s" % (key))
logger('verb', "read_callback value %s" % (value))
if not value in METRIC_TYPES:
try:
key_prefix, key_root = key.rsplit(METRIC_DELIM,1)
except ValueError, e:
pass
if not key_root in METRIC_TYPES:
continue
key_root, val_type = METRIC_TYPES[key_root]
key_name = METRIC_DELIM.join([key_prefix, key_root])
logger('verb', "key_name %s" % (key_name))
val = collectd.Values(plugin=NAME, type=val_type)
val.type_instance = key_name
val.values = [ value ]
val.dispatch()
# logging function
def logger(t, msg):
if t == 'err':
collectd.error('%s: %s' % (NAME, msg))
elif t == 'warn':
collectd.warning('%s: %s' % (NAME, msg))
elif t == 'verb':
if VERBOSE_LOGGING:
collectd.info('%s: %s' % (NAME, msg))
else:
collectd.notice('%s: %s' % (NAME, msg))
# main
collectd.register_config(configure_callback)
collectd.register_read(read_callback)