Skip to content
Browse files

Allow piping data directly to the script with '-'

  • Loading branch information...
1 parent 14ac1a2 commit 937d4c9894558a032915d1a5a34645b6ef3c1108 @danielbeardsley danielbeardsley committed Dec 7, 2012
Showing with 18 additions and 2 deletions.
  1. +6 −1 README.md
  2. +12 −1 pipe-to-graphite.sh
View
7 README.md
@@ -39,7 +39,12 @@ Your script (lets say `~/glork-stats.sh`) would look something like this:
To get that regularly reported to Graphite, run this command
- pipe-to-graphite.sh ~/glork-stats.sh >> /var/log/glork-stats.log
+ ./pipe-to-graphite.sh ~/glork-stats.sh >> /var/log/glork-stats.log
+
+Or to report it to graphite at any time, if you want to use cron or something.
+The '-' argument indicates input should be read form stdin
+
+ ~/glork-stats.sh | ./pipe-to-graphite.sh -
### Logging
Output from each run is prepended with a timestamp and echoed to stdout
View
13 pipe-to-graphite.sh
@@ -19,9 +19,20 @@ if [ -f graphite.conf ]; then
source graphite.conf
fi;
+# '-' indicates we should read from stdin
+if [ "$1" = "-" ]; then
+ # Get a timestamp for sending to graphite
+ ts=`date +%s`
+
+ # Pipe the output through sed, using a regex to
+ # append a $ts timestamp to the end of each line,
+ # and then to the correct server and port using netcat
+ sed -e '/ [0-9]\+$/!d' -e "s/\$/ $ts/" |
+ nc -w 1 $GRAPHITE_SERVER $GRAPHITE_PORT
+
# Normal usage just passes the command as the only parameter
# This checks if we're on a recursive call.
-if [ "$1" != "report-to-graphite" ]; then
+elif [ "$1" != "report-to-graphite" ]; then
command="$1"
echo -n "Running '$command' as a test.. " >&2

0 comments on commit 937d4c9

Please sign in to comment.
Something went wrong with that request. Please try again.