Today is: 8 January, 2012
Check todays hot topics

Statistics gathering in one line and 30 seconds

At work we had some crazy error happen in bulk and without warning. We have a number of clusters with several different classes of machine in each, and I was told to gather statistics now now now. The resulting one-liner is what happened (with a little bit of scrubbing to protect the innocent).

#!/bin/bash
for cluster in {foo,bar,baz}{1,2}; do echo -n "${cluster}: "; ERRCOUNT=$(ssh user@host.${cluster} "grep ${errorcode} /path/to/log/file | wc -l "); NOERRCOUNT=$(ssh user@host.${cluster} "grep -v ${errorcode} /path/to/log/file | wc -l "); PERCENT=$(echo "(${ERRCOUNT} / (${ERRCOUNT} + ${NOERRCOUNT})) * 100" | bc -l); echo "${ERRCOUNT} / ${NOERRCOUNT} / ${PERCENT:0:5}%"; done 

We have since cleaned it up with column headings (cluster name, machine name, etc) and printf statements in favor of echo and bash strings, and now it's a rather well-used internal application.

With line-breaks:

for cluster in {foo,bar,baz}{1,2}; do echo -n "${cluster}: ";
ERRCOUNT=$(ssh user@host.${cluster} "grep ${errorcode} /path/to/log/file | wc -l ");
NOERRCOUNT=$(ssh user@host.${cluster} "grep -v ${errorcode} /path/to/log/file | wc -l ");
PERCENT=$(echo "(${ERRCOUNT} / (${ERRCOUNT} + ${NOERRCOUNT})) * 100" | bc -l); 
echo "${ERRCOUNT} / ${NOERRCOUNT} / ${PERCENT:0:5}%"; done