Created
November 13, 2019 21:06
-
-
Save gnyman/a4d7ad7c13113dd9c3fa74442e42c17c to your computer and use it in GitHub Desktop.
A test script to accompany the mini_logcheck.sh
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/sh | |
# Copyright (C) 2011 Glen Pitt-Pladdy | |
# Copyright (C) 2019 Gabriel Nyman | |
# | |
# This program is free software; you can redistribute it and/or | |
# modify it under the terms of the GNU General Public License | |
# as published by the Free Software Foundation; either version 2 | |
# of the License, or (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
# | |
# | |
# See: http://www.pitt-pladdy.com/blog/_20110501-112534_0100_Log_checker_mini_logcheck_for_OpenWrt/ | |
# mail configuration | |
# general paths | |
LOG=/var/log/messages | |
RULES=/etc/logcheck.ignore.d | |
# temporary files | |
TMPTAIL=/tmp/tmplog_`hexdump -n 8 -e \"%x\" /dev/urandom` | |
TMPRULES=/tmp/tmprules_`hexdump -n 8 -e \"%x\" /dev/urandom` | |
TMPMESSAGE=/tmp/tmpmessage_`hexdump -n 8 -e \"%x\" /dev/urandom` | |
# read in existing position if available | |
position=1 | |
# get the unread part of the file | |
touch $TMPTAIL | |
chmod og-rwx $TMPTAIL | |
if [ -f $LOG.0 -a `ls -i $LOG | cut -d' ' -f1` -ne $inode ]; then | |
# looks like logs got rotated (inode changed) | |
TMPOLD=/tmp/tmplog_`hexdump -n 8 -e \"%x\" /dev/urandom` | |
touch $TMPOLD | |
chmod og-rwx $TMPOLD | |
echo "------- log rotation : old log" >$TMPOLD | |
# re-start with old file | |
tail -n +$position $LOG.0 >>$TMPOLD | |
# then do the old file | |
position=0 | |
tail -n +$position $LOG >$TMPTAIL | |
newposition=$(($position+`wc -l <$TMPTAIL`)) | |
# put them together | |
echo "------- log rotation : new log" >>$TMPOLD | |
cat $TMPTAIL >>$TMPOLD | |
mv $TMPOLD $TMPTAIL | |
else | |
tail -n +$position $LOG >$TMPTAIL | |
newposition=$(($position+`wc -l <$TMPTAIL`)) | |
fi | |
# get the rules together | |
touch $TMPRULES | |
chmod og-rwx $TMPRULES | |
grep -hv -e ^# $RULES/* -e ^$ >$TMPRULES | |
echo "<>>>>>TMPRULES" | |
cat $TMPRULES | |
# generate message | |
touch $TMPMESSAGE | |
chmod og-rwx $TMPMESSAGE | |
echo "Logs from $LOG filtered by $RULES/*:" >>$TMPMESSAGE | |
echo >>$TMPMESSAGE | |
linesbefore=`wc -l <$TMPMESSAGE` | |
if [ `wc -l <$TMPRULES` -gt 0 ]; then | |
# grep stalls with empty rules file | |
grep -Ef $TMPRULES $TMPTAIL >>$TMPMESSAGE | |
else | |
# no rules so take everything | |
cat $TMPTAIL >>$TMPMESSAGE | |
fi | |
newlines=$((`wc -l <$TMPMESSAGE`-$linesbefore)) | |
# did we find something | |
if [ $newlines -gt 0 ]; then | |
echo $? | |
# send the message | |
cat $TMPMESSAGE | |
fi | |
# tidy up | |
rm $TMPTAIL | |
rm $TMPRULES | |
rm $TMPMESSAGE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment