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
|
##############################################################################
# Copyright (c) 2015 EMC and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
import calendar
import logging
import socket
import time
from storperf.db.graphite_db import GraphiteDB
class CarbonMetricTransmitter():
carbon_servers = [('127.0.0.1', 2003),
('storperf-graphite', 2003)]
def __init__(self):
self.logger = logging.getLogger(__name__)
self.graphite_db = GraphiteDB()
self.commit_markers = {}
def transmit_metrics(self, metrics, commit_marker):
timestamp = str(calendar.timegm(time.gmtime()))
self.commit_markers[commit_marker] = int(timestamp)
carbon_socket = None
for host, port in self.carbon_servers:
try:
carbon_socket = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
carbon_socket.connect((host, port))
for key, value in metrics.items():
try:
float(value)
message = "%s %s %s\n" \
% (key, value, timestamp)
self.logger.debug("Metric: " + message.strip())
carbon_socket.send(message)
except ValueError:
self.logger.debug("Ignoring non numeric metric %s %s"
% (key, value))
message = "%s.commit-marker %s %s\n" \
% (commit_marker, timestamp, timestamp)
carbon_socket.send(message)
self.logger.debug("Marker %s" % message.strip())
self.logger.info("Sent metrics to %s:%s with timestamp %s"
% (host, port, timestamp))
except Exception, e:
self.logger.error("While notifying carbon %s:%s %s"
% (host, port, e))
if carbon_socket is not None:
carbon_socket.close()
def confirm_commit(self, commit_marker):
marker_timestamp = self.commit_markers[commit_marker]
request = "%s.commit-marker&from=%s" \
% (commit_marker, marker_timestamp - 60)
marker_data = self.graphite_db.fetch_item(request)
self.logger.debug("Marker data %s" % marker_data)
fetched_timestamps = self.parse_timestamp(marker_data)
return marker_timestamp in fetched_timestamps
def parse_timestamp(self, marker_data):
timestamps = []
if (type(marker_data) is list and
len(marker_data) > 0):
datapoints = marker_data[0]['datapoints']
for datapoint in datapoints:
try:
timestamps.append(int(datapoint[0]))
except Exception:
pass
return timestamps
|