logrotate 不是 运行 预旋转脚本
logrotate not running prerotate script
我写了一个脚本来解析我的 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 运行s 就是这样。
我可以看到当我 运行 logrotate -d -f /etc/logrotate.d/nginx
它没有给我发邮件。
我应该从这里去哪里?
警报-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 '( ~ /404/)' /var/log/nginx/access.log | awk '{print }' | sort | uniq -c | sort -rn >> /root/bin/nginx_email.txt
printf "\n\n502 URLS\n\n" >> /root/bin/nginx_email.txt
awk '( ~ /502/)' /var/log/nginx/access.log | awk '{print }' | 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 }' /var/log/nginx/access.log | awk '{print }' | 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)"
我了解到 /root/bin/nginx-parse.sh 脚本向您发送了电子邮件。当它是来自 logrotate 的 运行 时,请确保诸如邮件之类的命令的二进制文件具有完整路径(例如 /usr/bin/mail 而不仅仅是邮件)。你的剧本内容是什么?此外,运行 带有 -v 标志的 logrotate:logrotate -d -v -f /etc/logrotate.d/nginx
我写了一个脚本来解析我的 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 运行s 就是这样。
我可以看到当我 运行 logrotate -d -f /etc/logrotate.d/nginx
它没有给我发邮件。
我应该从这里去哪里?
警报-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 '( ~ /404/)' /var/log/nginx/access.log | awk '{print }' | sort | uniq -c | sort -rn >> /root/bin/nginx_email.txt
printf "\n\n502 URLS\n\n" >> /root/bin/nginx_email.txt
awk '( ~ /502/)' /var/log/nginx/access.log | awk '{print }' | 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 }' /var/log/nginx/access.log | awk '{print }' | 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)"
我了解到 /root/bin/nginx-parse.sh 脚本向您发送了电子邮件。当它是来自 logrotate 的 运行 时,请确保诸如邮件之类的命令的二进制文件具有完整路径(例如 /usr/bin/mail 而不仅仅是邮件)。你的剧本内容是什么?此外,运行 带有 -v 标志的 logrotate:logrotate -d -v -f /etc/logrotate.d/nginx