Ethernet and Coffee

Very simple Graphite client in bash

The script below is a very simple graphite client using only bash. It periodically reads a number from /proc/sys/kernel/random/entropy_avail and sends it to the Graphite server specified in the SERVER= line.

#!/bin/bash
#
# test_metric.sh - bash script to send some test metric data to
# a graphite server
#

# Define DNS name or ip address of graphite server and TCP port
#SERVER="mygraphite.server.com"
SERVER="192.168.2.2"
PORT="2003"

echo "attempting to open socket to $SERVER:$PORT"
exec 3<>/dev/tcp/$SERVER/$PORT

if [ $? -eq 0 ]; then
    echo "connection to $SERVER:$PORT successful"
else
    echo "connection to $SERVER:$PORT unsuccessful"
    exit
fi


# execute this forever
while [ 1 ]
  do
    # get the current epoch time
    printf -v EPOCH '%(%s)T' -1

    # get a value from entropy_avail
    VALUE=$(<"/proc/sys/kernel/random/entropy_avail")

    # build the graphite metric message with <namespace> <value> <time>
    MESSAGE="test-metric.$HOSTNAME.entropy_avail $VALUE $EPOCH"

    # tell the user what we're doing
    echo "sending metric '$MESSAGE' to $SERVER:$PORT"

    # send the message to the graphite socket
    echo -e -n "$MESSAGE" >&3

    # sleep and do it again
    sleep 5;
  done