X-Git-Url: http://git.onelab.eu/?p=myops.git;a=blobdiff_plain;f=web%2Fcollect%2Fserver%2Fload-graphite.py;fp=web%2Fcollect%2Fserver%2Fload-graphite.py;h=ef6231fcb3457f2aeeaf92d27e229c4874c41b3c;hp=0000000000000000000000000000000000000000;hb=85070b3d456667f238051af1a2f1f2a0c12300ab;hpb=607f0e13927eb18075c375fa9ba5527da4fcbb44 diff --git a/web/collect/server/load-graphite.py b/web/collect/server/load-graphite.py new file mode 100755 index 0000000..ef6231f --- /dev/null +++ b/web/collect/server/load-graphite.py @@ -0,0 +1,107 @@ +#!/usr/bin/python +"""Copyright 2008 Orbitz WorldWide + +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 time +import os +import platform +import subprocess +from socket import socket +import time +import csv + +def str_to_ts(date_str): + 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"] + ts = -1 + + for format in format_list: + try: + if date_str.find('.') == -1: + ts = time.mktime(time.strptime(date_str, format)) + else: + ts = time.mktime(time.strptime(date_str[:date_str.find('.')], format)) + break + except: + continue + + if ts == -1: + raise Exception("No time format to convert date_str: %s" % date_str) + + return ts + +def connect_to_carbon(server, port): + sock = socket() + try: + sock.connect( (server,port) ) + except: + print "Couldn't connect to %(server)s on port %(port)d, is carbon-agent.py running?" % { 'server':server, 'port':port} + sys.exit(1) + return sock + +def main(): + + from optparse import OptionParser + parser = OptionParser() + + parser.set_defaults(target="", + date=None, + value=None, + datefield=0, + valuefield=1, + file=None) + + parser.add_option("", "--target", dest="target", help="") + parser.add_option("", "--date", dest="date", help="") + parser.add_option("", "--value", dest="value", help="") + parser.add_option("", "--datefield", dest="datefield", help="") + parser.add_option("", "--valuefield", dest="valuefield", help="") + parser.add_option("", "--file", dest="file", ) + + (config, args) = parser.parse_args() + if len(sys.argv) == 1: + parser.print_help() + sys.exit(1) + + + #print "connecting..." + CARBON_SERVER = 'HOSTNAME' + CARBON_PORT = 2003 + sock = connect_to_carbon(CARBON_SERVER, CARBON_PORT) + + if config.file: + csvfile = csv.reader(open(config.file, 'r'), delimiter=',', quotechar='|') + first_line=True + else: + csvfile = [(config.date, config.value)] + first_line=False + + #print csvfile + for line in csvfile: + if first_line: + first_line = False + else: + t = line[int(config.datefield)] + v = line[int(config.valuefield)] + + #print t + #print v + ts = str_to_ts(t) + + message = "%s %s %d\n" % (config.target, v,ts) + print "\t" + message[:-1] + sock.sendall(message) + +if __name__ == "__main__": + main()