-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathapache-xreport.sh
executable file
·89 lines (75 loc) · 3.01 KB
/
apache-xreport.sh
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
#!/bin/bash
#
# Report Apache errors of the last 24 hours.
#
# VERSION :1.4.2
# DATE :2023-04-23
# AUTHOR :Viktor Szépe <[email protected]>
# URL :https://github.com/szepeviktor/debian-server-tools
# LICENSE :The MIT License (MIT)
# BASH-VERSION :4.2+
# DEPENDS :apt-get install mail-transport-agent apache2 ccze perl dategrep
# LOCATION :/usr/local/sbin/apache-xreport.sh
# CRON-DAILY :/usr/local/sbin/apache-xreport.sh
CCZE_CSS_URL="https://cdn.rawgit.com/szepeviktor/debian-server-tools/master/monitoring/apache-ccze.css"
CCZE_BODY_BG="#fdf6e3"
EMAIL_HEADER="Subject: [admin] HTTP xerrors from $(hostname -f)
From: webserver <root>
MIME-Version: 1.0
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
"
APACHE_CONFIGS="$(find /etc/apache2/sites-enabled/ -type l -name "*.conf")"
Xclude_filter()
{
# AH00128: File does not exist
# #AH00162: server seems busy, (you may need "to increase StartServers, or Min/MaxSpareServers)
# AH02032: Hostname %s provided via SNI and hostname %s provided via HTTP are different
# WAF for WordPress
# Apache access control
# Apache restart messages at 6 AM
# Malformed ??? hostname "5e ed 1d 4c bb 01", "5e ed 51 84 bb 01" via SNI
grep -Ev "\\s(AH00128:|AH02032:\
|w4wp_|bad_request_|no_wp_here_|404_not_found|403_forbidden|File does not exist:\
|client denied by server configuration:)" \
| grep -Evx '\[.* 06:.* [0-9][0-9][0-9][0-9]\] \[\S+:(info|notice)\] \[pid [0-9]+:tid [0-9]+\] (AH00493|AH00830|AH01887|AH01876|AH03090|AH00489|AH00490|AH00094|AH01883|h2_workers):.*' \
}
Color_html()
{
ccze --html --options "cssfile=${CCZE_CSS_URL}" --color "cssbody=${CCZE_BODY_BG}" \
| perl -MMIME::QuotedPrint -p -e '$_=MIME::QuotedPrint::encode_qp($_);'
}
Maybe_sendmail()
{
local STRIPPED_BYTE
read -r -n 1 STRIPPED_BYTE \
&& {
# stdin is not empty
echo "$EMAIL_HEADER"
{ echo -n "$STRIPPED_BYTE"; cat; } | Color_html
} | /usr/sbin/sendmail
}
if [ -z "$APACHE_CONFIGS" ]; then
echo "Apace log files could not be found." 1>&2
exit 10
fi
# APACHE_LOG_DIR is defined here
# shellcheck disable=SC1091
source /etc/apache2/envvars
# For non-existent previous log file
shopt -s nullglob
while read -r CONFIG_FILE; do
ERROR_LOG="$(sed -n -e '/^\s*ErrorLog\s\+\(\S\+\)\s*.*$/I{s//\1/p;q;}' "$CONFIG_FILE")"
SITE_USER="$(sed -n -e '/^\s*Define\s\+SITE_USER\s\+\(\S\+\).*$/I{s//\1/p;q;}' "$CONFIG_FILE")"
# Substitute variables
ERROR_LOG="$(echo "$ERROR_LOG" | sed \
-e "s;\${APACHE_LOG_DIR};${APACHE_LOG_DIR};g" \
-e "s;\${SITE_USER};${SITE_USER};g")"
# Log lines for 1 day from Debian cron.daily
nice dategrep --format '%a %b %d %T(.[0-9]+)? %Y' --multiline \
--start "now truncate 24h add -17h35m" --end "06:25:00" "$ERROR_LOG".[1] "$ERROR_LOG" \
| Xclude_filter \
| sed "s;^;$(basename "$ERROR_LOG" .log): ;"
done <<<"$APACHE_CONFIGS" | Maybe_sendmail
exit 0