Skip to content

Instantly share code, notes, and snippets.

@sudar
Last active March 8, 2020 17:57
Show Gist options
  • Save sudar/a4cf0ff150d1fd324aea to your computer and use it in GitHub Desktop.
Save sudar/a4cf0ff150d1fd324aea to your computer and use it in GitHub Desktop.
Automatically send unique errors (with count) from Apache error log as email. Details at http://sudarmuthu.com/blog/automatically-send-unique-errors-with-count-from-apache-error-log-as-email/
#!/usr/bin/env python
import sys
import mandrill
MANDRILL_API_KEY = "api-key"
EMAIL_FROM = "[email protected]"
EMAIL_TO = "[email protected]"
EMAIL_SUBJECT = "Error Log"
def send_mail(content):
"""Send email using Mandrill API"""
mandrill_client = mandrill.Mandrill(MANDRILL_API_KEY)
message = {
'auto_html': True,
'auto_text': None,
'to': [{'email': EMAIL_TO, 'type': 'to'}],
'from_email': EMAIL_FROM,
'subject': EMAIL_SUBJECT,
'html': content,
'track_clicks': None,
'tags': ['error-log']
}
result = mandrill_client.messages.send(message=message, async=False)
if __name__ == '__main__':
lines = []
for line in sys.stdin:
lines.append(line.strip())
content = "<br>".join(filter(None, lines))
if len(content) > 0:
send_mail(content)
/path/to/log/*.log {
daily
missingok
rotate 10000
compress
delaycompress
notifempty
dateext
create 660 root www-data
sharedscripts
postrotate
if [ -f "`. /etc/apache2/envvars ; echo ${APACHE_PID_FILE:-/var/run/apache2.pid}`" ]; then
/etc/init.d/apache2 reload > /dev/null
fi
endscript
prerotate
awk -F'[\[\]]+' '$4 == "error"{print $7}' /path/to/error.log | sort | uniq -c | sor -nr | /path/to/apache-error-mailer.py
endscript
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment