Read from Nagios perfdata file and send bulk data to Loggly as JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## nagios2loggly.sh | |
## Andy Mayhew <andy@loggly.com> | |
## | |
## Read from Nagios perfdata file and send bulk data. | |
## | |
## Setup as parallel performance data processor. | |
## Create /etc/nagios3/conf.d/nagios2loggly.cfg | |
## define command{ | |
## command_name perfdata-to-loggly | |
## command_line /bin/echo -e "$LASTSERVICECHECK$||$HOSTNAME$||$SERVICEDESC$||$SERVICEOUTPUT$||$SERVICEPERFDATA$||$SERVICESTATE$" >> /var/spool/nagios2loggly/perfdata.log | |
## } | |
## | |
## Edit nagios.cfg: | |
## service_perfdata_command=perfdata-to-loggly | |
## | |
## Create cronjob in /etc/cron.d/nagios2loggly | |
## * * * * * nagios /usr/lib/nagios/loggly/nagios2loggly.sh | |
## | |
token="[YOUR-TOKEN-HERE]" | |
tag="nagios" | |
logfile="/tmp/nagios2loggly.log" | |
DEBUG=1 | |
NAGIOSFILE="/var/spool/nagios2loggly/perfdata.log" | |
TMPFILE="/var/spool/nagios2loggly/perfdata.json" | |
if [ ${DEBUG} -ne 0 ]; then | |
echo `date` "Processing start." >> ${logfile} | |
fi | |
## Move the current data log out of the way so Nagios doesn't write | |
## to it while we are reading. | |
mv ${NAGIOSFILE} ${NAGIOSFILE}.$$ | |
## Primary loop | |
while read line; do | |
dtmp=`echo $line | cut -f1 -d\|` | |
datetime=`date -d @${dtmp} -u +%Y-%m-%dT%H:%M:%SZ` | |
source=`echo $line | cut -f3 -d\|` | |
service=`echo $line | cut -f5 -d\|` | |
value=`echo $line | cut -f9 -d\| | sed -e's/\= /\=/g'` | |
status=`echo $line | cut -f11 -\d\|` | |
## If there is performance data, then add it to the records | |
message="{ \"timestamp\": \"${datetime}\", \"hostname\": \"${source}\", \"service\": \"${service}\", \"status\": \"${status}\"" | |
pdata=() | |
if [ ! -z "${value}" ]; then | |
count=0 | |
for i in $value; do | |
rtmp=`echo $i | cut -f1 -d\;` | |
rname=`echo $rtmp | cut -f1 -d\=` | |
rvalue=`echo $rtmp | cut -f2 -d\= | sed -e's/[^0-9\.]*//g'` | |
rvalue=`printf "%0.2f" ${rvalue}` | |
if [ ! -z "${rvalue}" ]; then | |
if [ -z ${#pdata[@]} ]; then | |
pdata=("\"meter\": \"${rname}\", \"value\": ${rvalue}") | |
else | |
pdata=("${pdata[@]}" "\"meter\": \"${rname}\", \"value\": ${rvalue}") | |
fi | |
count=$((count+1)) | |
fi | |
done | |
if [ ${#pdata[@]} -gt 0 ]; then | |
for (( i = 0; i < ${#pdata[@]}; i++ )); do | |
if [ ${DEBUG} -ne 0 ]; then | |
echo "Writing: ${message}, ${pdata[$i]} }" >> ${logfile} | |
fi | |
echo "${message}, ${pdata[$i]} }" >> ${TMPFILE}.$$ | |
done | |
fi | |
fi | |
done < ${NAGIOSFILE}.$$ | |
## Send the data | |
if [ ${DEBUG} -ne 0 ]; then | |
echo "Sending: ${TMPFILE}.$$" >> ${logfile} | |
curl -s -X POST -T ${TMPFILE}.$$ https://logs-01.loggly.com/bulk/${token}/tag/${tag} >> ${logfile} | |
else | |
curl -s -X POST -T ${TMPFILE}.$$ https://logs-01.loggly.com/bulk/${token}/tag/${tag} >/dev/null | |
fi | |
## clean up | |
if [ ${DEBUG} -ne 0 ]; then | |
echo `date` "Removing: ${NAGIOSFILE}.$$" >> ${logfile} | |
echo `date` "Removing: ${TMPFILE}.$$" >> ${logfile} | |
fi | |
rm -f ${TMPFILE}.$$ | |
rm -f ${NAGIOSFILE}.$$ | |
if [ ${DEBUG} -ne 0 ]; then | |
echo `date` "Processing end." >> ${logfile} | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment