Skip to content

Instantly share code, notes, and snippets.

@mhenes
Forked from gm3dmo/gelfsender.sh
Created May 27, 2016 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhenes/742a3a4c212f8f9ed60fc13b6c4502f2 to your computer and use it in GitHub Desktop.
Save mhenes/742a3a4c212f8f9ed60fc13b6c4502f2 to your computer and use it in GitHub Desktop.
Send a log message to a gelf server using the shell.
# This script can be used to raise a graylog2/gelf message
# gzip it and send it to a graylog server using netcat (nc)
hostname='gelftester'
short_message='test message short version'
full_message='longer test message. dont\n worry be happy'
level=1
facility='gelftester'
# gnu date
date=$(date +'%s.%N')
env_name='test'
app_name='testapp'
# Read the message into the variable ${gelf_message}
# see http://graylog2.org/gelf for more info
read -r -d '' gelf_message <<EOF
{
"version": "1.0",
"host": "${hostname}",
"short_message": "${short_message}",
"full_message": "${full_message}",
"timestamp": ${date},
"level": ${level},
"facility": "${facility}",
"_user_id": 42,
"_Environment": "${env_name}",
"_AppName": "${app_name}"
}
EOF
echo Plain
echo -----
echo -n "${gelf_message}"
echo ==============
echo
echo
echo Gzipped and Gunzipped
echo ---------------------
echo -n "${gelf_message}"| gzip -c -f - | gunzip -c -
echo ==============
echo
echo
echo Send
echo ----
echo "${gelf_message}"| gzip -c -f - | nc -w 1 -u grayloghost 12201
echo ==============
echo
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment