-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger
executable file
·103 lines (63 loc) · 1.62 KB
/
logger
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/bash
# config
REMOTEPATH=/var/log/httpd/access.log
LOGCOPY=/tmp/access.log
PAYMENTSLOG=/tmp/payments.log
LASTSENT=~/.lastsent
MAIL_TO=yourmail@example.com
MAIL_FROM=youremail@example.com
function utcCleanup() {
# the current.log rotates at 7pm - also force rotation of ~/.lastsent
HOUR=$(date -u +%H)
if [ "$HOUR" == '00' ];
then
echo > "$LASTSENT"
# reset the database for prtg
/usr/local/bin/prtg-toggle --clear
fi
}
function obtain() {
utcCleanup
# only obtain post & payment lines
cp "$REMOTEPATH" "$LOGCOPY"
}
function payment() {
# make sure log has content, otherwise we're wasting time
if [[ ! -s "$LOGCOPY" ]];
then
echo "Log is 0 bytes"
exit 0
fi
# extract payment posts
grep -P '^(?=.*POST )(?=.*payment)' "$LOGCOPY" > "$PAYMENTSLOG"
if [[ ! -s "$PAYMENTSLOG" ]];
then
echo "Payments log is 0 bytes"
exit 0
else
sendMail
fi
}
function admins() {
# we don't care about admins sending forms, so delete any line entries so we don't get alerts about it
sed -i '/^127.0.0.1/ d' "$PAYMENTSLOG"
}
function sendMail() {
# cleanup, first
admins
# dirty "bugfix" - copy the prev payments log and if they match, don't send anything
PAYSZ=$(ls -l $PAYMENTSLOG | awk '{ print $5 }')
SENTSZ=$(ls -l $LASTSENT | awk '{ print $5 }')
# compare the sizes
if [[ "$PAYSZ" -gt "$SENTSZ" ]];
then
# send an email with the log contents
mailx -r "$MAIL_TO" -s "Payments Log Extraction" -v "$MAIL_FROM" < "$PAYMENTSLOG"
cp "$PAYMENTSLOG" "$LASTSENT"
else
echo "Nothing new to tell you about!"
fi
exit 0
}
obtain
payment