/usr/share/pyshared/carbon/amqp_publisher.py is in graphite-carbon 0.9.12-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #!/usr/bin/env python
"""
Copyright 2009 Lucio Torre <lucio.torre@canonical.com>
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.
Will publish metrics over AMQP
"""
import os
import time
from optparse import OptionParser
from twisted.python.failure import Failure
from twisted.internet.defer import deferredGenerator, waitForDeferred
from twisted.internet import reactor, task
from twisted.internet.protocol import ClientCreator
from txamqp.protocol import AMQClient
from txamqp.client import TwistedDelegate
from txamqp.content import Content
import txamqp.spec
@deferredGenerator
def writeMetric(metric_path, value, timestamp, host, port, username, password,
vhost, exchange, spec=None, channel_number=1, ssl=False):
if not spec:
spec = txamqp.spec.load(os.path.normpath(
os.path.join(os.path.dirname(__file__), 'amqp0-8.xml')))
delegate = TwistedDelegate()
connector = ClientCreator(reactor, AMQClient, delegate=delegate,
vhost=vhost, spec=spec)
if ssl:
from twisted.internet.ssl import ClientContextFactory
wfd = waitForDeferred(connector.connectSSL(host, port,
ClientContextFactory()))
yield wfd
conn = wfd.getResult()
else:
wfd = waitForDeferred(connector.connectTCP(host, port))
yield wfd
conn = wfd.getResult()
wfd = waitForDeferred(conn.authenticate(username, password))
yield wfd
wfd = waitForDeferred(conn.channel(channel_number))
yield wfd
channel = wfd.getResult()
wfd = waitForDeferred(channel.channel_open())
yield wfd
wfd = waitForDeferred(channel.exchange_declare(exchange=exchange,
type="topic",
durable=True,
auto_delete=False))
yield wfd
message = Content( "%f %d" % (value, timestamp) )
message["delivery mode"] = 2
channel.basic_publish(exchange=exchange, content=message,
routing_key=metric_path)
wfd = waitForDeferred(channel.channel_close())
yield wfd
def main():
parser = OptionParser(usage="%prog [options] <metric> <value> [timestamp]")
parser.add_option("-t", "--host", dest="host",
help="host name", metavar="HOST", default="localhost")
parser.add_option("-p", "--port", dest="port", type=int,
help="port number", metavar="PORT",
default=5672)
parser.add_option("-u", "--user", dest="username",
help="username", metavar="USERNAME",
default="guest")
parser.add_option("-w", "--password", dest="password",
help="password", metavar="PASSWORD",
default="guest")
parser.add_option("-v", "--vhost", dest="vhost",
help="vhost", metavar="VHOST",
default="/")
parser.add_option("-s", "--ssl", dest="ssl",
help="ssl", metavar="SSL", action="store_true",
default=False)
parser.add_option("-e", "--exchange", dest="exchange",
help="exchange", metavar="EXCHANGE",
default="graphite")
(options, args) = parser.parse_args()
try:
metric_path = args[0]
value = float(args[1])
if len(args) > 2:
timestamp = int(args[2])
else:
timestamp = time.time()
except:
parser.print_usage()
raise SystemExit(1)
d = writeMetric(metric_path, value, timestamp, options.host, options.port,
options.username, options.password, vhost=options.vhost,
exchange=options.exchange, ssl=options.ssl)
d.addErrback(lambda f: f.printTraceback())
d.addBoth(lambda _: reactor.stop())
reactor.run()
if __name__ == "__main__":
main()
|