forked from graphite-project/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarbon-client.py
executable file
·136 lines (110 loc) · 4.23 KB
/
carbon-client.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
#!/usr/bin/env python
"""Copyright 2009 Chris Davis
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License."""
import sys
import imp
from os.path import dirname, join, abspath, exists
from optparse import OptionParser
# Figure out where we're installed
BIN_DIR = dirname(abspath(__file__))
ROOT_DIR = dirname(BIN_DIR)
CONF_DIR = join(ROOT_DIR, 'conf')
default_relayrules = join(CONF_DIR, 'relay-rules.conf')
# Make sure that carbon's 'lib' dir is in the $PYTHONPATH if we're running from
# source.
LIB_DIR = join(ROOT_DIR, 'lib')
sys.path.insert(0, LIB_DIR)
try:
from twisted.internet import epollreactor
epollreactor.install()
except ImportError:
pass
from twisted.internet import stdio, reactor, defer
from twisted.protocols.basic import LineReceiver
from carbon.routers import ConsistentHashingRouter, RelayRulesRouter
from carbon.client import CarbonClientManager
from carbon import log, events
option_parser = OptionParser(usage="%prog [options] <host:port:instance> <host:port:instance> ...")
option_parser.add_option('--debug', action='store_true', help="Log debug info to stdout")
option_parser.add_option('--keyfunc', help="Use a custom key function (path/to/module.py:myFunc)")
option_parser.add_option('--replication', type='int', default=1, help='Replication factor')
option_parser.add_option('--routing', default='consistent-hashing',
help='Routing method: "consistent-hashing" (default) or "relay"')
option_parser.add_option('--relayrules', default=default_relayrules,
help='relay-rules.conf file to use for relay routing')
options, args = option_parser.parse_args()
if not args:
print 'At least one host:port destination required\n'
option_parser.print_usage()
raise SystemExit(1)
if options.routing not in ('consistent-hashing', 'relay'):
print "Invalid --routing value, must be one of:"
print " consistent-hashing"
print " relay"
raise SystemExit(1)
destinations = []
for arg in args:
parts = arg.split(':', 2)
host = parts[0]
port = int(parts[1])
if len(parts) > 2:
instance = parts[2]
else:
instance = None
destinations.append( (host, port, instance) )
if options.debug:
log.logToStdout()
log.setDebugEnabled(True)
defer.setDebugging(True)
if options.routing == 'consistent-hashing':
router = ConsistentHashingRouter(options.replication)
elif options.routing == 'relay':
if exists(options.relayrules):
router = RelayRulesRouter(options.relayrules)
else:
print "relay rules file %s does not exist" % options.relayrules
raise SystemExit(1)
client_manager = CarbonClientManager(router)
reactor.callWhenRunning(client_manager.startService)
if options.keyfunc:
router.setKeyFunctionFromModule(options.keyfunc)
firstConnectAttempts = [client_manager.startClient(dest) for dest in destinations]
firstConnectsAttempted = defer.DeferredList(firstConnectAttempts)
class StdinMetricsReader(LineReceiver):
delimiter = '\n'
def lineReceived(self, line):
#log.msg("[DEBUG] lineReceived(): %s" % line)
try:
(metric, value, timestamp) = line.split()
datapoint = (float(timestamp), float(value))
assert datapoint[1] == datapoint[1] # filter out NaNs
client_manager.sendDatapoint(metric, datapoint)
except ValueError:
log.err(None, 'Dropping invalid line: %s' % line)
def connectionLost(self, reason):
log.msg('stdin disconnected')
def startShutdown(results):
log.msg("startShutdown(%s)" % str(results))
allStopped = client_manager.stopAllClients()
allStopped.addCallback(shutdown)
firstConnectsAttempted.addCallback(startShutdown)
stdio.StandardIO( StdinMetricsReader() )
exitCode = 0
def shutdown(results):
global exitCode
for success, result in results:
if not success:
exitCode = 1
break
if reactor.running:
reactor.stop()
reactor.run()
raise SystemExit(exitCode)