Skip to content
This repository
tag: v3144
Fetching contributors…

Cannot retrieve contributors at this time

executable file 58 lines (50 sloc) 1.506 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#!/usr/bin/env bash

set -e

skip=1

while [ $# -gt 0 ]; do
    case $1 in
        --help)
            echo "plotpipe: plots data from stdin in real-time using gnuplot"
            echo "Usage: <command> | $0 [options] <regex>"
            echo "Options:"
            echo " -s <int> only take one out of <int> values, skip the rest"
            echo ""
            echo "The regexp should match the first group with the value to"
            echo 'plot (for instance, "^Stack usage: \([0-9]\+\)")'
            exit 0;;
        -s)
            shift
skip=$1
            shift;;

        *)
            regex=$1
            shift
esac
done

data=$(mktemp -t plotpipe-XXXXX)
trap "rm -f $data" EXIT

t=$(date +%s%N)

(
    echo 'set xlabel "Time (seconds)"'

    first=2 # wait until we have two points to plot
    i=0
    while read line; do
        if echo "$line" | grep "$regex" >/dev/null; then
            if [ $i -gt 0 ]; then i=$((i - 1))
            else
                i=$((skip - 1))
                echo "$line" | sed "s/$regex/$(date +%s%N) \1/" >>$data
                if [ $first -eq 0 ]; then
                    echo "replot";
                else
                    if [ $first -eq 1 ]; then
                        echo "plot \"$data\" using ((\$1 - $t.)/1e9):2 with lines notitle"
                    fi
                    first=$((first - 1))
                fi
            fi
        else
            echo "$line" >&2
        fi
    done
) | gnuplot >/dev/null 2>&1
Something went wrong with that request. Please try again.