33import socket
44import sys
55import time
6+ import json
67
78import sqlalchemy
89
9- def get_executor (dsn ):
10- engine = sqlalchemy .create_engine (dsn , isolation_level = 'READ UNCOMMITTED' )
10+ def get_executor (dbUrl ):
11+ engine = sqlalchemy .create_engine (dbUrl , isolation_level = 'READ UNCOMMITTED' )
1112 connection = engine .connect ()
1213 return connection .execute
1314
14- def get_info ():
15- parser = argparse .ArgumentParser (description = 'Send SQL results to Graphite' )
16- parser .add_argument ('--graphite-host' , metavar = 'graphite-host' , type = str , nargs = 1 , default = None , help = 'Host to send metrics to' )
17- parser .add_argument ('--graphite-port' , metavar = 'graphite-port' , type = int , nargs = 1 , default = 2003 , help = 'Graphite port to send metrics to' )
18- parser .add_argument ('--graphite-prefix' , metavar = 'graphite-prefix' , type = str , nargs = 1 , default = ['db' ], help = 'Prefix for metrics' )
19- return parser .parse_args ()
20-
21- def run (graphite_host , graphite_port , graphite_prefix , queries , executor ):
22- data = []
15+ def run (graphite_host , graphite_port , graphite_prefix , executor , ** query ):
16+ resultRow = []
2317 now = time .time ()
2418 sock = _socket_for_host_port (graphite_host , graphite_port )
25- data = map (executor , queries )
26- for result in data :
27- for line in result :
28- metric , value = line [:2 ]
29- metric = '{0}.{1} {2} {3}\n ' .format (graphite_prefix , metric , value , now )
30- print metric ,
31- sock .sendall (metric )
19+ resultRow = executor (query ['sql' ])
20+ for result in resultRow :
21+ metric , value = result [:2 ]
22+ metric = '{0}.{1} {2} {3}\n ' .format (graphite_prefix , metric , value , now )
23+ print metric ,
24+ sock .sendall (metric )
3225 sock .close ()
3326
3427def _socket_for_host_port (host , port ):
@@ -40,20 +33,40 @@ def _socket_for_host_port(host, port):
4033
4134
4235def main ():
43- dsn = os .environ .get ('S2G_DSN' )
44- if dsn is None :
45- print 'You must set your DSN in the environment variable `S2G_DSN`'
36+ graphiteHost = os .environ .get ('SQL_FEED_GRAPHITE_HOST' )
37+ if graphiteHost is None :
38+ graphiteHost = 'localhost'
39+ graphitePort = os .environ .get ('SQL_FEED_GRAPHITE_PORT' )
40+ if graphitePort is None :
41+ graphitePort = 5432
42+ graphitePrefix = os .environ .get ('SQL_FEED_GRAPHITE_PREFIX' )
43+ if graphitePrefix is None :
44+ print 'You must set your graphitePrefix in the environment variable `SQL_FEED_GRAPHITE_PREFIX`'
4645 sys .exit (1 )
47-
48- queries = sys .stdin .readlines ()
49- args = get_info ()
50- run (
51- args .graphite_host [0 ],
52- args .graphite_port ,
53- args .graphite_prefix [0 ],
54- queries ,
55- get_executor (dsn ),
56- )
46+ dbUrl = os .environ .get ('SQL_FEED_GRAPHITE_DB_URL' )
47+ if dbUrl is None :
48+ print 'You must set your DBUrl in the environment variable `SQL_FEED_GRAPHITE_DB_URL`'
49+ sys .exit (1 )
50+ queriesDIR = os .environ .get ('SQL_FEED_GRAPHITE_QUERIES_DIR' )
51+ if queriesDIR is None :
52+ queriesDIR = os .getcwd ()
53+ for filename in os .listdir (queriesDIR ):
54+ if filename .endswith (".json" ):
55+ currentFile = os .path .join (queriesDIR , filename )
56+ print ('Processing file' ,currentFile )
57+ with open (currentFile ) as data_file :
58+ data = json .load (data_file )
59+ print (data ["queries" ])
60+ for query in data ["queries" ]:
61+ print (query ['sql' ])
62+ run (
63+ graphiteHost ,
64+ graphitePort ,
65+ graphitePrefix ,
66+ get_executor (dbUrl ),
67+ ** query
68+ )
69+ continue
5770
5871if __name__ == '__main__' :
5972 main ()
0 commit comments