= Bash: Run a Report on Syslog Messages =
**Summary**: How to receive all messages from a syslog server from the last week using a script. \\
**Date**: Around 2022 \\
**Refactor**: 6 April 2025: Checked links and formatting. \\
{{tag>bash redhat linux}}
I've setup my [[redhat65management |management server]] as my syslog server as well. All servers sent their warning messages or higher to this server and I've created this script so I receive all unique messages from the last week, on Monday morning in my email to be included in my weekly check.
#!/bin/bash
# This report show all entries in /var/log/messages for the last week,
# makes them unique and counts the number of messages.
# Then it will mail the report to whoever needs it.
# Script Variables
HOSTNAME_SHORT=`hostname -s`
BASEDIR=`dirname $0`
WHATAMI=`basename $0`
DATE=`date +%Y%m%d`
LASTWEEK=`date --d='last Week' +%V`
TOMAIL=sjoerd @ getshifting.com,it department @ getshifting.com
REPORT=/tmp/$WHATAMI.txt
# /var/log/messages logfile due to rotation
LASTSUNDAY=`date --d='last Sunday' +%Y%m%d`
LOGFILE=/var/log/messages-$LASTSUNDAY
# Start of Script
# Clear logfile and make things look nice
echo "This is the messages report log" > $REPORT
echo "Server: $HOSTNAME_SHORT" >> $REPORT
echo "Week number: $LASTWEEK" >> $REPORT
echo " " >> $REPORT
echo "Report messages": >> $REPORT
# Get all messages from logfile, sqeeze spaces,
# remove the date columns and sort and count the logentries
cat $LOGFILE | tr -s ' ' | cut -d' ' --complement -f1-3 | sort | uniq -c >> $REPORT
# Mail report
echo "See attachment" | mailx -s "Weekly syslog messages Red Hat Environment" -a $REPORT $TOMAIL
# End of Script
exit 0
Then schedule it in cron like this:
# Run messages report every monday at 06:00
0 6 * * 1 root /adminscripts/logmessagesreport