forked from gcoop-libre/freeipa-sssd-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipa-sss-sum
executable file
·220 lines (157 loc) · 4.06 KB
/
ipa-sss-sum
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/bin/bash
# This script comes with ABSOLUTELY NO WARRANTY, use at own risk
# Copyright (C) 2021 Osiris Alejandro Gomez <osiris@gcoop.coop>
#
# 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\` Plot summarized SynLog attribute stats
### Usage
\`\`\`bash
$BIN [LOG_ATTRIBUTE] [IPA_SSS_SYN_LOG]
\`\`\`
### Description
Plot log attribute stats.
### Example
\`\`\`bash
$BIN userAccountControl
\`\`\`
EOF
exit 0
}
[[ "$1" =~ ^[-]+(h|help) ]] && usage
function defaults()
{
LOG='ipa-sss-syn.log'
}
function params()
{
[[ -z "$1" ]] && die "EMPTY ATTRIBUTE"
ATTR="$1"
[[ -z "$2" ]] || LOG="$2"
[[ -e "$LOG" ]] || die "NOT FOUND LOG FILE $LOG"
TOTAL="$(grep -c " $ATTR=" "$LOG")"
[[ "$TOTAL" -eq 0 ]] && die "NOT FOUND ATTRIBUTE $ATTR IN $LOG"
}
function validate()
{
[[ -z "$DIR_DAT" ]] && cfg 'DIR_DAT'
[[ -z "$DIR_PLT" ]] && cfg 'DIR_PLT'
[[ -z "$DIR_PNG" ]] && cfg 'DIR_PNG'
N="$(basename "$LOG" .log)"
DAT="$DIR_DAT/$N-$ATTR.dat"
PLT="$DIR_PLT/$N-$ATTR.plt"
PNG="$DIR_PNG/$N-$ATTR.png"
[[ -d "$DIR_DAT" ]] || die "NOT FOUND DIR $DIR_DAT"
[[ -d "$DIR_PLT" ]] || die "NOT FOUND DIR $DIR_PLT"
[[ -d "$DIR_PNG" ]] || die "NOT FOUND DIR $DIR_PNG"
TMP="$(mktemp)"
COLS="$(mktemp)"
TOTS="$(mktemp)"
DAYS="$(mktemp)"
true > "$DAT"
}
print_header()
{
grep -Eo " $ATTR=[0-9-]+" "$LOG" \
| grep -v '^$' \
| cut -d '=' -f2 \
| sort -u \
| sort -n > "$COLS"
printf "%-10s %11s " 'date' 'total'
while read -r COL
do
printf "%11s " "$COL"
done < "$COLS"
printf "\\n"
}
sum_date()
{
grep -E "^$1" "$LOG" \
| grep -v '^$' \
| grep -Eo " $ATTR=[0-9-]+" \
| sort \
| cut -d '=' -f2 \
| uniq -c \
| sort -k 2 > "$TOTS"
S="$(awk 'BEGIN {S=0} {S+=$1} END {print S}' "$TOTS")"
printf "%-10s %11s " "$1" "$S"
while read -r COL
do
T="$(awk "(\$2 == \"$COL\") {print \$1}" "$TOTS" | tr -d '\n')"
[[ -z "$T" ]] && T=0
P="$(echo "scale=2;$T/$S*100" | bc)"
printf "%11s " "$P"
#printf "%6s " "$T"
done < "$COLS"
printf "\\n"
}
function sum2dat()
{
print_header | tee -a "$DAT"
awk '{print $1}' "$LOG" | sort -u | grep -v '^$' > "$DAYS"
while read -r D
do
sum_date "$D"
done < "$DAYS" | tee -a "$DAT"
}
function dat2plt()
{
TCOLS="$(wc -l "$COLS" | awk '{print $1}')"
PLOT=$(cat << EOF
load '$IPA_PLT'
set title "$ATTR\n$LOG" font '$PLT_TITLE_FONT' tc lt $PLT_TITLE_LT noenhanced
set xlabel "days" font '$PLT_X_FONT' tc lt 5
set ylabel "total (%)" font '$PLT_Y_FONT' tc lt 5
set xtics font "$PLT_X_TICS_FONT" tc ls 1 nomirror rotate by 90 right
set mxtics
set ytics tc ls 1
set tics tc ls 1
set mytics
set border 1 front ls 1
set tics nomirror out
set lmargin 10
set bmargin 5
set rmargin 15
set tmargin 5
set style line 102 lc rgb '#c0c0c0' lt 0 lw 1
set grid back ls 102
set key outside font '$PLT_KEY_FONT' textcolor rgb '#$PLT_KEY_COLOR'
set style data histograms
set style histogram rowstacked
set boxwidth 1 relative
set style fill solid 1.0 border -1
plot for [i=3:($TCOLS+2):1] '$DAT' u i:xticlabels(1) t columnheader
EOF
)
echo "$PLOT" > "$PLT" && gnuplot "$PLT" > "$PNG" && stderror "$PNG"
rm -f "$TMP"
rm -f "$COLS"
rm -f "$TOTS"
rm -f "$DAYS"
}
function main()
{
config
defaults
params "$@"
validate
sum2dat
dat2plt
}
main "$@"