forked from gcoop-libre/freeipa-sssd-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipa-sss-day
executable file
·129 lines (87 loc) · 2.77 KB
/
ipa-sss-day
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
# This script comes with ABSOLUTELY NO WARRANTY, use at own risk
# Copyright (C) 2021 Osiris Alejandro Gomez <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# shellcheck disable=SC1090
# shellcheck disable=SC1091
DIR_BIN="$(dirname "$0")" && source "$DIR_BIN/ipa-src-cfg"
function usage()
{
cat << EOF
## \`$BIN\` Split SynLog by date
### Usage
\`\`\`bash
$BIN [IPA_SSS_SYN_LOG] [YYYY-MM-DD]
\`\`\`
### Description
Split \`ipa-sss-syn.log\` by date.
### Examples
Example split all days:
\`\`\`bash
$BIN ipa-sss-syn.log
3866 2021-11-24-ipa-sss-syn.log
4060 2021-11-25-ipa-sss-syn.log
4041 2021-11-26-ipa-sss-syn.log
3865 2021-11-27-ipa-sss-syn.log
3865 2021-11-28-ipa-sss-syn.log
4424 2021-11-29-ipa-sss-syn.log
4088 2021-11-30-ipa-sss-syn.log
\`\`\`
Example split specific day:
\`\`\`bash
$BIN ipa-sss-syn.log 2022-01-01
3861 2022-01-01-ipa-sss-syn.log
\`\`\`
Example split specific day between hour range:
\`\`\`bash
XSTART=10:00 XEND=18:00 $BIN ipa-sss-syn.log 2022-01-01
2861 2022-01-01-ipa-sss-syn.log
\`\`\`
EOF
exit 0
}
[[ "$1" =~ ^[-]+(h|help) ]] && usage
[[ -n "$LOG" ]] || LOG='/var/log/ipa-sss-syn.log'
[[ -z "$1" ]] || LOG="$1"
[[ -n "$DIR_BAK" ]] || DIR_BAK="$PWD/bak"
[[ -e "$LOG" ]] || die "NOT FOUND LOG FILE $LOG"
[[ -n "$DATE" ]] || DATE="$2"
DATE_LST="$(mktemp --suffix .log)"
NAME="$(basename "$LOG" .log)"
if [[ -z "$DATE" ]]
then
awk '{print $1}' "$LOG" | sort -u > "$DATE_LST"
else
echo "$DATE" > "$DATE_LST"
fi
[[ -d "$DIR_BAK" ]] || mkdir -p "$DIR_BAK"
[[ -n "$XSTART" ]] || XSTART='07:00'
[[ -n "$XEND" ]] || XEND='19:00'
while read -r DATE _
do
DATE_LOG="$DATE-$NAME.log"
DATE_BAK="$DIR_BAK/$DATE-$NAME-$(date +%s).gz"
ESTART="$(date -d "$DATE $XSTART" '+%s')"
EEND="$(date -d "$DATE $XEND" '+%s')"
[[ -e "$DATE_LOG" ]] && gzip -9 -c "$DATE_LOG" > "$DATE_BAK"
grep -E "^$DATE " "$LOG" | while read -r D TIME OTHERS
do
ETIME="$(date -d "$D $TIME" '+%s')"
[[ "$ETIME" -lt "$ESTART" ]] && continue
[[ "$ETIME" -gt "$EEND" ]] && continue
echo "$D $TIME $OTHERS"
done > "$DATE_LOG" && wc -l "$DATE_LOG"
done < "$DATE_LST"
rm -f "$DATE_LST"