Created
December 30, 2011 15:51
-
-
Save tsuna/1540383 to your computer and use it in GitHub Desktop.
Improved code for http://n-chandra.blogspot.com/2011/12/time-series-and-opentsdb.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) throws Exception { | |
Socket echoSocket = null; | |
PrintWriter out = null; | |
String host = null; | |
int port = 0; | |
while (true) { | |
try { | |
if (echoSocket == null) { // Only open the connection if necessary. | |
echoSocket = new Socket(host, port); | |
out = new PrintWriter(echoSocket.getOutputStream(), true); | |
} | |
int orderCount = getOrderCount(60);//Orders in last 60 sec | |
out.println("put order.count.1m " + (new Date().getTime() / 1000) + " 100"); | |
out.flush(); | |
} catch (UnknownHostException e) { | |
System.err.println("Don't know about host: " + host); | |
return; | |
} catch (IOException e) { | |
System.err.println("Couldn't get I/O for the connection to: " + host); | |
out.close(); // Only close the connection on errors. | |
echoSocket.close(); | |
out = null | |
echoSocket = null; | |
} | |
System.out.println("Sleeping for 60 s"); | |
Thread.sleep(60000); | |
} | |
} | |
public int getOrderCount(int interval) { | |
......... | |
return orderCount; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Note: it's better to use tcollector, which comes with a MySQL collector: | |
# https://github.com/stumbleupon/tcollector/blob/master/collectors/0/mysql.py | |
set -e | |
while true; do | |
echo >&2 "Querying orders...." | |
mysql -u user -ppassword -Ddatabase --batch -N --execute "select count(orderdate) from ORDERS where orderdate > date_sub(now(), INTERVAL 1 MINUTE)" \ | |
| awk -F"\t" -v now=`date +%s` -v host=`hostname` '{ print "put order.count.1m " now " " $1 " host=" host }' \ | |
|| exit # To handle SIGPIPE properly. | |
sleep 60 | |
done | nc -w 5 $TSD_HOSTNAME $TSD_PORT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is for http://n-chandra.blogspot.com/2011/12/time-series-and-opentsdb.html