2 """Copyright 2008 Orbitz WorldWide
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License."""
21 from socket import socket
25 def str_to_ts(date_str):
26 format_list = ["%m/%d/%Y %H:%M:%S", "%m/%d/%Y", "%Y/%m/%d %H:%M", "%Y/%b/%d %H:%M", "%Y-%m-%dT%H"]
29 for format in format_list:
31 if date_str.find('.') == -1:
32 ts = time.mktime(time.strptime(date_str, format))
34 ts = time.mktime(time.strptime(date_str[:date_str.find('.')], format))
40 raise Exception("No time format to convert date_str: %s" % date_str)
44 def connect_to_carbon(server, port):
47 sock.connect( (server,port) )
49 print "Couldn't connect to %(server)s on port %(port)d, is carbon-agent.py running?" % { 'server':server, 'port':port}
55 from optparse import OptionParser
56 parser = OptionParser()
58 parser.set_defaults(target="",
65 parser.add_option("", "--target", dest="target", help="")
66 parser.add_option("", "--date", dest="date", help="")
67 parser.add_option("", "--value", dest="value", help="")
68 parser.add_option("", "--datefield", dest="datefield", help="")
69 parser.add_option("", "--valuefield", dest="valuefield", help="")
70 parser.add_option("", "--file", dest="file", )
72 (config, args) = parser.parse_args()
73 if len(sys.argv) == 1:
78 #print "connecting..."
79 CARBON_SERVER = 'HOSTNAME'
81 sock = connect_to_carbon(CARBON_SERVER, CARBON_PORT)
84 csvfile = csv.reader(open(config.file, 'r'), delimiter=',', quotechar='|')
87 csvfile = [(config.date, config.value)]
95 t = line[int(config.datefield)]
96 v = line[int(config.valuefield)]
102 message = "%s %s %d\n" % (config.target, v,ts)
103 print "\t" + message[:-1]
104 sock.sendall(message)
106 if __name__ == "__main__":