Being notified quickly of errors triggered on a server allow for a quick resolution of problems on a production site. This guide set up a e-mail notification of PHP errors.
This howto is tested on:
- Debian 10.0 Buster
Requirements
This howto requires:
Settings
Set the e-mail address receiving the notifications:
targetEmail="root@localhost"
Set the PHP messages log file path:
logPath='/var/log/php.log'
Installation
Detect if sudo is available (“command” is used if not):
cmdProxy='command'
command type -f 'sudo' &>'/dev/null' && cmdProxy='sudo'
Install Simple Event Correlator:
${cmdProxy} apt-get install 'sec' 'bsd-mailx'
Créez le dossier pour les fichiers de configuration:
${cmdProxy} mkdir --parent \
"/etc/sec.d" \
"/etc/default/sec.d"
Configuration
Set the PHP messages detection pattern:
pattern='^.+: PHP .+$'
Set the files watched by the daemon:
inputFiles="${logPath}"
If PHP messages are not logged by Syslog, but by Apache 2 instead, use:
pattern='^\[.+\] \[error\] \[client .+\] PHP .+$'
inputFiles='/var/log/apache2/*error.log'
or by Lighttpd:
pattern='^.+: \(mod_fastcgi\.c\..+\) .+$'
inputFiles='/var/log/lighttpd/*error.log'
Set the configuration file name:
confFile='/etc/sec.d/php-errors.conf'
Create the software configuration file:
${cmdProxy} tee "${confFile}" \
<<< "# Capture error lines and store them in php-errors
type=Single
ptype=RegExp
pattern=${pattern}
desc=PHP error or warning
action=add php-errors \$0
# Report errors every minute if php-errors is set
type=Calendar
time=* * * * *
desc=Mail PHP errors
context=php-errors
action=report php-errors /usr/bin/mail -s \"PHP errors\" ${targetEmail}; delete php-errors;
"
Create the daemon options file:
${cmdProxy} tee '/etc/default/sec.d/php-errors' \
<<< "# sec options to send email notifications on PHP errors.
RUN_DAEMON=\"yes\"
DAEMON_ARGS=\"--conf='${confFile}' --input=${inputFiles} --pid='/var/run/sec.pid' --detach --syslog='daemon'"
Start the daemon at boot:
${cmdProxy} systemctl enable sec
Restart the daemon:
${cmdProxy} systemctl restart sec
If needed, remove deprecated and notice messages from PHP logs to only send notifications on error and warning messages:
${cmdProxy} php-tools --fix 'error_reporting' 'E_ALL & ~E_DEPRECATED & ~E_NOTICE' \
&& ${cmdProxy} php-tools --reload
Thanks
- Thanks to S.E.C., the Simple Event Correlator (en) developers.
- Thanks to the authors of Simple Event Correlator F.A.Q (en).
- Thanks to Garret from powdahound.com (en) for Emails alerts for PHP errors via SEC (en).
- Thanks to GuiguiAbloc (fr) for Interception des erreurs applicatives dans Nagios avec SEC et Prelude-lml (fr).
- Thanks to Wiki Monitoring-fr (fr) for SEC (fr).
0 Comments