Allow piping data directly to the script with '-' #1

Merged
merged 3 commits into from Dec 20, 2012

Conversation

Projects
None yet
2 participants
Owner

danielbeardsley commented Dec 8, 2012

i.e. if some-command generates data like:

thingA 4325
thingB 243

Then you can send that directly to graphite with

some-command | ./pipe-to-graphite.sh -

@xiongchiamiov xiongchiamiov commented on the diff Dec 11, 2012

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
@xiongchiamiov

xiongchiamiov Dec 11, 2012

Contributor

We don't need to explicitly read from stdin?

@danielbeardsley

danielbeardsley Dec 11, 2012

Owner

Test it out, no it's not needed. Make a file less.sh with only four chars
in it: "less" then do "cat somefile | sh less.sh"
On Dec 11, 2012 12:52 PM, "James Pearson" notifications@github.com wrote:

In 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

We don't need to explicitly read from stdin?


Reply to this email directly or view it on GitHubhttps://github.com/iFixit/pipe-to-graphite/pull/1/files#r2384516.

@xiongchiamiov

xiongchiamiov Dec 11, 2012

Contributor

I trust you. :) Merge it.

@danielbeardsley danielbeardsley added a commit that referenced this pull request Dec 20, 2012

@danielbeardsley danielbeardsley Merge allow-piping (#1) into master
Allow piping data directly to the script with '-'

i.e. if `some-command` generates data like:

    thingA 4325
    thingB 243

Then you can send that directly to graphite with

    some-command | ./pipe-to-graphite.sh -
52baf70

danielbeardsley merged commit aa2dbc8 into master Dec 20, 2012

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