-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlog2mail
More file actions
executable file
·92 lines (82 loc) · 2.62 KB
/
log2mail
File metadata and controls
executable file
·92 lines (82 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
#
# Fatal error notification script
#
# Run as a persistent process by syslog-ng. Any log lines that match the
# regex specified in syslog-ng.conf will be fed to this script. The log
# lines will be queued up in a temp file until ${INTERVAL} passes. If any
# log entries exist, they will be mailed to ${RCPT}.
#
# To configure syslog-ng to use this script, first create a destination
# stanza that looks something like this:
#
# destination d_some_goofy_name { program("/path/to/log2mail"); };
#
# Then create a filter that matches the log entries that need to be mailed
# out. Finally create a log stanza that ties the destination and filter
# to a source. "man 5 syslog-ng.conf" for more info.
SENDMAIL=/usr/sbin/sendmail
SENDER=null@null.clockwork.net
RCPT=errors@clockwork.net
SEPARATOR="----------"
HOST=$(hostname)
TIMEOUT=300
INTERVAL=30
NEXT=$((SECONDS + INTERVAL))
QUEUEDIR=$(mktemp -d /tmp/fatal_php_errors.XXXXXX)
# Clean up after ourselves, even if we exit unexpectedly.
trap 'es=${?}; rm -rf ${QUEUEDIR}; exit ${es}' INT TERM EXIT
queue_log_entry () {
SITENAME=${LOGLINE%%:*}
if [[ ! ( -z "${LOGLINE}" || -z "${SITENAME}" ) ]]
then
echo "${LOGLINE}" >> ${QUEUEDIR}/${SITENAME}
echo ${SEPARATOR} >> ${QUEUEDIR}/${SITENAME}
unset LOGLINE
unset SITENAME
fi
}
mail_logs () {
for logfile in ${QUEUEDIR}/*
do
if [[ -f ${logfile} && (( $(stat -c "%s" ${logfile}) > 0 )) ]]
then
{
echo "X-Mailer: maillogs"
echo "Auto-Submitted: auto-generated"
echo "To: Errors <errors@clockwork.net>"
echo "From: PHP Fatal Error Logger <blech@null.clockwork.net>"
echo "Subject: ${HOST}: $(basename ${logfile}): PHP Fatal Error"
echo
cat ${logfile}
} | ${SENDMAIL} -f ${SENDER} ${RCPT}
rm -f ${logfile}
fi
done
}
########################################################
## Main
#
while true
do
START=${SECONDS}
read -t ${TIMEOUT} LOGLINE
EXITCODE=$?
END=${SECONDS}
ELAPSED=$((END - START))
# The "read" command exits with 1 whether it hit the timeout or it
# received EOF. LOGLINE is null in either case. So the only way to
# tell that syslog-ng has closed the other end of the pipe is to
# detect the case where read exited 1 before its timeout elapsed.
if (( EXITCODE != 0 )) && (( ELAPSED < TIMEOUT ))
then
exit
fi
queue_log_entry
if (( SECONDS >= NEXT ))
then
echo "Calling mail_logs()"
mail_logs
NEXT=$((SECONDS + INTERVAL))
fi
done