分类: LINUX
2008-10-07 18:08:15
it is often important for system administrators to get real time
notification of critical events. unfortunately, it isn't immediately
obvious how to do this in the syslog framework. in this article i show
you step-by-step how to do this.
as usual, all code and configurations have been tested on debian etch but should be useful for other *nix flavors with subtle modifications.
logger -p local0.crit "my pants are on fire"
critical
level to a facility local0
.
without changing your script, you can configure syslog perform some or all of these actions:
in our example, we'll take all critical
messages written to the local0
facility and (in addition to logging) send them to the mail recipient, fireman@example.com
.
# mkdir /etc/syslog.pipes
# mknod /etc/syslog.pipes/criticalMessages p
# chmod 600 /etc/syslog.pipes/criticalMessages
critical
messages written to the local0
facility to this pipe. add the following statement to your syslog.conf
file.
local0.crit |/etc/syslog.pipes/criticalMessages
/usr/bin/syslogMailer
:
#!/bin/bash
# syslogMailer: a script to read stdin and turn each line into an alert
# email typically this is used to read a named-pipe written to by syslog
#
# example usage: syslogMailer < /etc/syslog.pipes/criticalMessages
#
alertRecipient="fireman@example.com" # the mail recipient for alerts
TMOUT=1 # don't wait > 1 second for input
# process each line of input and produce an alert email
while read line
do
# remove any repeated messages
echo ${line} | grep "message repeated" > /dev/null 2>&1
if test $? -eq 1
then
# send the alert
echo "${line}" | mailx -s "critical error on syslog" ${alertRecipient}
fi
done
TMOUT=1 # don't wait > 1 second for input
read
.
the script therefore runs to completion after processing one batch of
zero or more messages. this allows you to schedule it in cron to run,
say, every 5 minutes with a statement like:
# m h dom mon dow command
0-59/5 * * * * /usr/bin/syslogMailer < /etc/syslog.pipes/criticalMessages > /dev/null 2>&1
# process each line of input and produce an error message
while :
do
while read line
do
[...]
# send the alert
echo "${line}" | mailx -s "critical error on syslog" ${alertRecipient}
done
done