Skip to content

Instantly share code, notes, and snippets.

@tsuna
Created December 30, 2011 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsuna/1540383 to your computer and use it in GitHub Desktop.
Save tsuna/1540383 to your computer and use it in GitHub Desktop.
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;
}
#!/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
@tsuna
Copy link
Author

tsuna commented Dec 30, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment