我有一个脚本,我写的是解析我的nginx日志并将结果通过电子邮件发送给我,我将其添加到我的nginx logrotate脚本中,但它无效...
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
prerotate
/root/bin/nginx-parse.sh
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
run-parts /etc/logrotate.d/httpd-prerotate; \
fi \
endscript
postrotate
invoke-rc.d nginx rotate >/dev/null 2>&1
endscript
}
该脚本位于prerotate
部分,当我将其称为root用户时,它可以工作,我相信logrotate的运行方式。
我可以看到,当我运行logrotate -d -f /etc/logrotate.d/nginx
时,它不向我发送任何电子邮件。
我应该从哪里离开?
alert-mail.sh.sh:
#!/usr/bin/python
import smtplib
import sys
from email.mime.text import MIMEText
server = smtplib.SMTP('smtp.example.com', 587)
server.starttls()
server.login("me@example.com", "secret")
# Create message
msg = MIMEText(sys.argv[2])
msg['Subject'] = sys.argv[1]
msg['From'] = "me@example.com"
msg['To'] = "me@example.com"
server.sendmail(msg["From"], [msg["To"]], msg.as_string())
server.quit()
nginx-parse.sh
#!/bin/bash
# For parsing of the nginx logfile and emailing to myself
rm /root/bin/nginx_email.txt
printf "NGINX STATUS CODES\n\n" >> /root/bin/nginx_email.txt
cat /var/log/nginx/access.log | cut -d '"' -f3 | cut -d ' ' -f2 | sort | uniq -c | sort -rn >> /root/bin/nginx_email.txt
printf "\n\n404 URLS\n\n" >> /root/bin/nginx_email.txt
awk '($9 ~ /404/)' /var/log/nginx/access.log | awk '{print $7}' | sort | uniq -c | sort -rn >> /root/bin/nginx_email.txt
printf "\n\n502 URLS\n\n" >> /root/bin/nginx_email.txt
awk '($9 ~ /502/)' /var/log/nginx/access.log | awk '{print $7}' | sort | uniq -c | sort -r >> /root/bin/nginx_email.txt
printf "\n\nMOST ACCESSES URLS\n\n" >> /root/bin/nginx_email.txt
awk -F\" '{print $2}' /var/log/nginx/access.log | awk '{print $2}' | sort | uniq -c | sort -rg | head -n 50 >> /root/bin/nginx_email.txt
#Sending the email using the python script I wrote
alert-mail "NGINX PARSING" "$(cat /root/bin/nginx_email.txt)"
答案 0 :(得分:1)
我知道/root/bin/nginx-parse.sh脚本会向您发送电子邮件。从logrotate运行时,请确保邮件等命令的二进制文件具有完整路径(例如/ usr / bin / mail而不仅仅是邮件)。你的脚本有什么内容?另外,使用-v标志运行logrotate:logrotate -d -v -f /etc/logrotate.d/nginx