-
Notifications
You must be signed in to change notification settings - Fork 0
/
es.py
201 lines (163 loc) · 6.02 KB
/
es.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
# -*- encoding: utf-8 -*-
from ceilometer import publisher
from ceilometer.openstack.common import log
try:
from oslo.utils import netutils as network_utils
except ImportError:
from ceilometer.openstack.common import network_utils
try:
from ceilometer.openstack.common.gettextutils import _
except ImportError:
from ceilometer.i18n import _
import novaclient
from oslo_config import cfg
from novaclient import client as nova_client
import re
import socket
import time
import os
import urllib2, json
from datetime import datetime
METRIC_KEYS = (
#'current_workload',
#'disk_available_least',
'local_gb',
#'local_gb_used',
'memory_mb',
#'memory_mb_used',
#'running_vms',
'vcpus',
#'vcpus_used',
'disk.allocation',
'memory.resident',
'cpu_util',
)
METRIC_KEYS_SEND = (
#'current_workload',
#'disk_available_least',
'disk',
#'local_gb_used',
'memory',
#'memory_mb_used',
#'running_vms',
'cpu',
#'vcpus_used',
'disk',
'memory',
'cpu',
)
METRIC_VALUE_NORM = (
#'current_workload',
#'disk_available_least',
1024000000,
#'local_gb_used',
1024000,
#'memory_mb_used',
#'running_vms',
1,
#'vcpus_used',
1,
1024000,
1,
)
LOG = log.getLogger(__name__)
class ESPublisher(publisher.PublisherBase):
def __init__(self, parsed_url):
self.host, self.port = network_utils.parse_host_port(parsed_url.netloc,default_port=9200)
self.hostnode = socket.gethostname().split('.')[0]
conf = cfg.CONF.service_credentials
self.conf = conf
tenant = conf.os_tenant_id or conf.os_tenant_name
self.nc = nova_client.Client(
version=2,
username=conf.os_username,
api_key=conf.os_password,
project_id=tenant,
auth_url=conf.os_auth_url,
region_name=conf.os_region_name,
endpoint_type=conf.os_endpoint_type,
service_type=cfg.CONF.service_types.nova,
cacert=conf.os_cacert,
insecure=conf.insecure,
timeout=cfg.CONF.http_timeout,
http_log_debug=cfg.CONF.nova_http_log_debug,
no_cache=True)
def ESPush(self, metric):
LOG.debug("Sending ElasticSearch metric:" + str(metric))
url = 'http://%s:%s/metrics/metric/_bulk' % (self.host, self.port)
LOG.debug(url)
encoded_data = "\n".join(map(json.dumps, metric)) + "\n"
req = urllib2.Request(url,data=encoded_data)
f = urllib2.urlopen(req)
LOG.debug(f.read())
def publish_samples(self, context, samples):
for sample in samples:
#stats_time = int(time.time())
stats_time = datetime.now()
region = self.conf.os_region_name
msg = sample.as_dict()
resource_id = msg['resource_id']
project_id = msg['project_id']
data_type = msg['type']
volume = msg['volume']
metric_name = msg['name']
metadata = msg['resource_metadata']
instance_match = re.match('instance', metric_name)
network_match = re.match('network', metric_name)
disk_match = re.match('disk', metric_name)
hypervisors = self.nc.hypervisors.list()
data0 = []
for hv in hypervisors:
for key, value in hv.to_dict().iteritems():
if key in METRIC_KEYS and hv.hypervisor_hostname == self.hostnode:
i = METRIC_KEYS.index(key)
data0.append({ "index" : { } })
data0.append({"region" : region, "host" : self.hostnode, "project_id": project_id, \
"metric_name": METRIC_KEYS_SEND[i], "volume_total": value*METRIC_VALUE_NORM[i], "@timestamp":str(stats_time)})
#LOG.debug(data0)
self.ESPush(data0)
if disk_match:
ram = metadata['memory_mb']
vcpus = metadata['vcpus']
disk_gb = metadata['disk_gb']
vmid = metadata.get('instance_id')
if network_match:
vmid = metadata.get('instance_id')
vm = vmid
else:
vm = resource_id
"""
if disk_match:
data1 = [
{ "index" : { } },
{ "region" : region, "host" : self.hostnode, "project_id": project_id, "instance_id": vm, "ram": ram, "stats_time":stats_time },
{ "index" : { } },
{ "region" : region, "host" : self.hostnode, "project_id": project_id, "instance_id": vm, "cpu_count": vcpus, "stats_time":stats_time },
{ "index" : { } },
{ "region" : region, "host" : self.hostnode, "project_id": project_id, "instance_id": vm, "disk_space": disk_gb, "stats_time":stats_time },
]
self.ESPush(data1)
"""
if data_type == 'gauge' and instance_match is None:
data2 = []
if metric_name in METRIC_KEYS:
i = METRIC_KEYS.index(metric_name)
data2.append({ "index" : { } },)
data2.append({ "region" : region, "host" : self.hostnode, \
"project_id": project_id, "instance_id": vm, \
"metric_name": METRIC_KEYS_SEND[i], "volume": volume*METRIC_VALUE_NORM[i], \
"@timestamp": str(stats_time) })
self.ESPush(data2)
else:
LOG.debug(_("[-]"))
try:
LOG.debug(_("OK"))
except Exception as e:
LOG.warn(_("Unable to send to ElasticSearch"))
LOG.exception(e)
def publish_events(self, context, events):
"""Send an event message for publishing
:param context: Execution context from the service or RPC call
:param events: events from pipeline after transformation
"""
raise ceilometer.NotImplementedError