Add scripts to create myops-getqueryview:
[myops.git] / web / collect / server / load-graphite.py
1 #!/usr/bin/python
2 """Copyright 2008 Orbitz WorldWide
3
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
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
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."""
15
16 import sys
17 import time
18 import os
19 import platform 
20 import subprocess
21 from socket import socket
22 import time
23 import csv
24
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"]
27         ts = -1
28
29         for format in format_list:
30                 try:
31                         if date_str.find('.') == -1:
32                                 ts = time.mktime(time.strptime(date_str, format))
33                         else:
34                                 ts = time.mktime(time.strptime(date_str[:date_str.find('.')], format))
35                         break
36                 except:
37                         continue
38
39         if ts == -1:
40                 raise Exception("No time format to convert date_str: %s" % date_str)
41
42         return ts
43
44 def connect_to_carbon(server, port):
45         sock = socket()
46         try:
47                 sock.connect( (server,port) )
48         except:
49                 print "Couldn't connect to %(server)s on port %(port)d, is carbon-agent.py running?" % { 'server':server, 'port':port}
50                 sys.exit(1)
51         return sock
52
53 def main():
54
55         from optparse import OptionParser
56         parser = OptionParser()
57         
58         parser.set_defaults(target="",
59                                                 date=None,
60                                                 value=None,
61                                                 datefield=0,
62                                                 valuefield=1,
63                                                 file=None)
64
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", )
71
72         (config, args) = parser.parse_args()
73         if len(sys.argv) == 1:
74                 parser.print_help()
75                 sys.exit(1)
76
77
78         #print "connecting..."
79         CARBON_SERVER = 'HOSTNAME'
80         CARBON_PORT = 2003
81         sock = connect_to_carbon(CARBON_SERVER, CARBON_PORT)
82
83         if config.file:
84                 csvfile = csv.reader(open(config.file, 'r'), delimiter=',', quotechar='|')
85                 first_line=True
86         else:
87                 csvfile = [(config.date, config.value)]
88                 first_line=False
89
90         #print csvfile
91         for line in csvfile:
92                 if first_line:
93                         first_line = False
94                 else:
95                         t = line[int(config.datefield)]
96                         v = line[int(config.valuefield)]
97
98                         #print t
99                         #print v
100                         ts = str_to_ts(t)
101
102                         message = "%s %s %d\n" % (config.target, v,ts)
103                         print "\t" + message[:-1]
104                         sock.sendall(message)
105
106 if __name__ == "__main__":
107         main()